From 9b8a12fc92dfaca74043b28b9528cbe3f70c0c49 Mon Sep 17 00:00:00 2001 From: The Yellow Architect Date: Sat, 18 Nov 2023 18:51:37 +0200 Subject: [PATCH 1/4] Added lfrc hotkey which compresses videos (webm,mkv,mp4) via ffmpeg Basically it is the following command but with proper checks and for multiple files: ffmpeg -i input.video -vcodec libx265 -crf output.mp4 --- .config/lf/lfrc | 61 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/.config/lf/lfrc b/.config/lf/lfrc index e783d728..375badd2 100644 --- a/.config/lf/lfrc +++ b/.config/lf/lfrc @@ -85,6 +85,65 @@ cmd delete ${{ [ $ans = "y" ] && rm -rf -- $fx }} + +cmd compressvideo ${{ + clear; + set -f; + + is_mp4_already=0; + converted_filenames=""; #notify-send variable + converted_files_count=0; #notify-send variable + compressionRatio="31"; #30 is impossible to notice difference + + echo "Compression Ratio? (default: 31, maximum: 50)"; + read compressionRatio; + + #If not a number (e.g. empty), give default 31 value + if ! [[ $compressionRatio =~ '^[0-9]+$' ]]; then + compressionRatio="31" + fi + + for pickedFilepath in $fx; do + is_valid_filetype=0; + + #could instead use ffprobe but would get more complicated as the filetype suffix becomes unknown + case $pickedFilepath in + *.mp4) + is_valid_filetype=1 && is_mp4_already=1;; + *.webm) + is_valid_filetype=1 && is_mp4_already=0;; + *.mkv) + is_valid_filetype=1 && is_mp4_already=0;; + esac + + if [[ $is_valid_filetype -eq 0 ]]; then + continue 1 ; + fi; + + if [[ $is_mp4_already -eq 1 ]]; then + tempFilepath=$(echo "$pickedFilepath" | sed 's|.mp4|(CONVERTING).mp4|'); + mv -f "$pickedFilepath" "$tempFilepath"; + + ffmpeg -i "$tempFilepath" -vcodec libx265 -crf "$compressionRatio" "$pickedFilepath"; + rm -f -- "$tempFilepath"; + else + newFilepath=$(echo "$pickedFilepath" | sed 's/\(.webm\|.mkv\)/.mp4/'); + ffmpeg -i "$pickedFilepath" -vcodec libx265 -crf "$compressionRatio" "$newFilepath"; + rm -f -- "$pickedFilepath"; + fi + + ((converted_files_count=converted_files_count+1)); + converted_filenames="$converted_filenames"$'\n'"$pickedFilepath"; + + done + + #Notify the user of the results + if [[ $converted_files_count -gt 0 ]]; then + converted_filenames=$(echo "$converted_filenames" | sed 's|.*\/||'); + notify-send "Compressed Videos($converted_files_count): $converted_filenames"; + fi; +}} + cmd moveto ${{ clear; tput cup $(($(tput lines)/3)); tput bold set -f @@ -164,5 +223,7 @@ map W $setsid -f $TERMINAL >/dev/null 2>&1 map Y $printf "%s" "$fx" | xclip -selection clipboard +map compressvideo + # Source Bookmarks source "~/.config/lf/shortcutrc" From eb388a398d93877ac1f5ce945d32f85ffa958854 Mon Sep 17 00:00:00 2001 From: The Yellow Architect Date: Sun, 19 Nov 2023 09:38:27 +0200 Subject: [PATCH 2/4] Cleaned up the if conditions in order to shave codelines. Notify-send now is split between header and body (so title gets automatically bold and is cleanly seperated from the list). Removed the check for compressionRate because of a bizzare lf bug. --- .config/lf/lfrc | 52 +++++++++++++++++++------------------------------ 1 file changed, 20 insertions(+), 32 deletions(-) diff --git a/.config/lf/lfrc b/.config/lf/lfrc index 375badd2..104f79f9 100644 --- a/.config/lf/lfrc +++ b/.config/lf/lfrc @@ -85,53 +85,41 @@ cmd delete ${{ [ $ans = "y" ] && rm -rf -- $fx }} - cmd compressvideo ${{ clear; set -f; - is_mp4_already=0; converted_filenames=""; #notify-send variable converted_files_count=0; #notify-send variable - compressionRatio="31"; #30 is impossible to notice difference - echo "Compression Ratio? (default: 31, maximum: 50)"; - read compressionRatio; + echo "Compression Rate? (default: 31, maximum: 50)"; + read compressionRate; #If not a number (e.g. empty), give default 31 value - if ! [[ $compressionRatio =~ '^[0-9]+$' ]]; then - compressionRatio="31" - fi + #if ! [[ $cr =~ "^[0-5][0-9]$" ]]; then + #echo "IS NOT A NUMBER" && sleep 1; + #compressionRate="31"; + #fi for pickedFilepath in $fx; do - is_valid_filetype=0; - #could instead use ffprobe but would get more complicated as the filetype suffix becomes unknown case $pickedFilepath in *.mp4) - is_valid_filetype=1 && is_mp4_already=1;; - *.webm) - is_valid_filetype=1 && is_mp4_already=0;; - *.mkv) - is_valid_filetype=1 && is_mp4_already=0;; + tempFilepath=$(echo "$pickedFilepath" | sed 's|.mp4|(CONVERTING).mp4|'); + mv -f "$pickedFilepath" "$tempFilepath"; + + ffmpeg -i "$tempFilepath" -vcodec libx265 -crf "$compressionRate" "$pickedFilepath"; + rm -f -- "$tempFilepath"; + ;; + *.webm | *.mkv) + newFilepath=$(echo "$pickedFilepath" | sed 's/\(.webm\|.mkv\)/.mp4/'); + ffmpeg -i "$pickedFilepath" -vcodec libx265 -crf "$compressionRate" "$newFilepath"; + rm -f -- "$pickedFilepath"; + ;; + *) + continue 1;; esac - if [[ $is_valid_filetype -eq 0 ]]; then - continue 1 ; - fi; - - if [[ $is_mp4_already -eq 1 ]]; then - tempFilepath=$(echo "$pickedFilepath" | sed 's|.mp4|(CONVERTING).mp4|'); - mv -f "$pickedFilepath" "$tempFilepath"; - - ffmpeg -i "$tempFilepath" -vcodec libx265 -crf "$compressionRatio" "$pickedFilepath"; - rm -f -- "$tempFilepath"; - else - newFilepath=$(echo "$pickedFilepath" | sed 's/\(.webm\|.mkv\)/.mp4/'); - ffmpeg -i "$pickedFilepath" -vcodec libx265 -crf "$compressionRatio" "$newFilepath"; - rm -f -- "$pickedFilepath"; - fi - ((converted_files_count=converted_files_count+1)); converted_filenames="$converted_filenames"$'\n'"$pickedFilepath"; @@ -140,7 +128,7 @@ cmd compressvideo ${{ #Notify the user of the results if [[ $converted_files_count -gt 0 ]]; then converted_filenames=$(echo "$converted_filenames" | sed 's|.*\/||'); - notify-send "Compressed Videos($converted_files_count): $converted_filenames"; + notify-send "Compressed Videos($converted_files_count):" "$converted_filenames"; fi; }} From 8af73a99373105c49a0d8375558e7ff167b68b33 Mon Sep 17 00:00:00 2001 From: The Yellow Architect Date: Sun, 19 Nov 2023 15:47:49 +0200 Subject: [PATCH 3/4] Previously, the input condition for whether the input is a valid digit was commented out. Now it's fixed, just had to remove quotations from regex. --- .config/lf/lfrc | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/.config/lf/lfrc b/.config/lf/lfrc index 104f79f9..842f870c 100644 --- a/.config/lf/lfrc +++ b/.config/lf/lfrc @@ -96,10 +96,9 @@ cmd compressvideo ${{ read compressionRate; #If not a number (e.g. empty), give default 31 value - #if ! [[ $cr =~ "^[0-5][0-9]$" ]]; then - #echo "IS NOT A NUMBER" && sleep 1; - #compressionRate="31"; - #fi + if ! [[ $compressionRate =~ ^[0-5][0-9]$ ]]; then + compressionRate="31"; + fi for pickedFilepath in $fx; do #could instead use ffprobe but would get more complicated as the filetype suffix becomes unknown From b2de469c0ed8f5554f66b51506fc153762a2b09c Mon Sep 17 00:00:00 2001 From: The Yellow Architect Date: Sun, 19 Nov 2023 15:55:29 +0200 Subject: [PATCH 4/4] On the input prompt of compression ratio (0 to 51) it automatically enters on entering the 2nd character. No need to press 2 digits then press enter. Now you write 2 digits and the enter is pressed automatically. --- .config/lf/lfrc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.config/lf/lfrc b/.config/lf/lfrc index 842f870c..792ff6c7 100644 --- a/.config/lf/lfrc +++ b/.config/lf/lfrc @@ -93,7 +93,7 @@ cmd compressvideo ${{ converted_files_count=0; #notify-send variable echo "Compression Rate? (default: 31, maximum: 50)"; - read compressionRate; + read -N 2 compressionRate; #If not a number (e.g. empty), give default 31 value if ! [[ $compressionRate =~ ^[0-5][0-9]$ ]]; then