From b7da698d004ffbcd323cc47257a1e5c66049a79f Mon Sep 17 00:00:00 2001 From: The Yellow Architect Date: Sat, 18 Nov 2023 18:33:39 +0200 Subject: [PATCH 1/3] Added lfrc hotkey which converts videos (webm,mkv,mp4) to mp3 via ffmpeg. Ignores non-videos and videos which have no audio layer (ffprobe ftw). Moves them to the music folder, and notifies the user. --- .config/lf/lfrc | 66 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/.config/lf/lfrc b/.config/lf/lfrc index e783d728..e58e47a2 100644 --- a/.config/lf/lfrc +++ b/.config/lf/lfrc @@ -130,6 +130,69 @@ cmd bulkrename ${{ lf -remote "send $id unselect" }} + +cmd encodetomp3 ${{ + clear; + set -f; + + is_valid_filetype=0; + + #Variables for notify-send + converted_filenames=""; + converted_files_count=0; + videos_without_audio_streams=""; + videos_without_audio_streams_count=0; + + for pickedFilepath in $fx; do + case $pickedFilepath in + *.mp4) + is_valid_filetype=1 ;; + *.webm) + is_valid_filetype=1 ;; + *.mkv) + is_valid_filetype=1 ;; + esac + + if [[ is_valid_filetype -eq 0 ]]; then + echo 'Skipping ${pickedFilepath}' && continue 1 ; + fi; + + parsed_MP3=$(echo "$pickedFilepath" | sed 's/\(.mp4\|.webm\|.mkv\)/.mp3/' | sed 's|.*\/||'); + parsed_MP3="~/Music/${parsed_MP3}"; + + #Using ffprobe because videos without audiostream result in exit code 1 which stops this entire loop of many files + #Remove (alongside its 2 variables) if you don't record videos without audio (which are admittedly rare) + if [[ $(ffprobe -loglevel error -show_entries stream=codec_type -of csv=p=0 "$pickedFilepath") != *"audio"* ]]; then + ((videos_without_audio_streams_count=videos_without_audio_streams_count+1)); + videos_without_audio_streams="$videos_without_audio_streams"$'\n'"$pickedFilepath"; + continue 1; + fi + + ffmpeg -i "$pickedFilepath" "$parsed_MP3"; + + ((converted_files_count=converted_files_count+1)); + converted_filenames="$converted_filenames"$'\n'"$pickedFilepath"; + + if [[ $# -eq 1 ]]; then + rm -f -- $pickedFilepath; + fi + done + + #Notify the results to the user + if [[ $converted_files_count -gt 0 ]]; then + converted_filenames=$(echo "$converted_filenames" | sed 's|.*\/||'); + notify-send "Converted MP3 Files($converted_files_count): $converted_filenames"; + fi; + + if [[ $videos_without_audio_streams_count -gt 0 ]]; then + videos_without_audio_streams=$(echo "$videos_without_audio_streams" | sed 's|.*\/||'); + notify-send "Videos without audio stream($videos_without_audio_streams_count): $videos_without_audio_streams"; + fi; + + #Uncomment the below line if you want to automatically unselect the original converted video files + #lf -remote "send $id unselect"; +}} + # Bindings map $lf -remote "send $id select \"$(fzf)\"" map J $lf -remote "send $id cd $(sed -e 's/\s*#.*//' -e '/^$/d' -e 's/^\S*\s*//' ${XDG_CONFIG_HOME:-$HOME/.config}/shell/bm-dirs | fzf)" @@ -164,5 +227,8 @@ map W $setsid -f $TERMINAL >/dev/null 2>&1 map Y $printf "%s" "$fx" | xclip -selection clipboard +map b encodetomp3 +map encodetomp3 delete_after_encoding + # Source Bookmarks source "~/.config/lf/shortcutrc" From 6ec5ae9779d03ad24ac02890ac9661f546d8db50 Mon Sep 17 00:00:00 2001 From: The Yellow Architect Date: Sun, 19 Nov 2023 09:43:10 +0200 Subject: [PATCH 2/3] Cleaned up the if conditions in order to shave codelines. Notify-send is now split between header and body (so title gets automatically bold and is cleanly seperated from the list) --- .config/lf/lfrc | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/.config/lf/lfrc b/.config/lf/lfrc index e58e47a2..b43c5771 100644 --- a/.config/lf/lfrc +++ b/.config/lf/lfrc @@ -130,12 +130,11 @@ cmd bulkrename ${{ lf -remote "send $id unselect" }} - cmd encodetomp3 ${{ clear; set -f; - is_valid_filetype=0; + music_folder="${MUSIC}"; #Variables for notify-send converted_filenames=""; @@ -145,20 +144,14 @@ cmd encodetomp3 ${{ for pickedFilepath in $fx; do case $pickedFilepath in - *.mp4) - is_valid_filetype=1 ;; - *.webm) - is_valid_filetype=1 ;; - *.mkv) - is_valid_filetype=1 ;; + *.mp4 | *.webm | *.mkv) + ;; + *) + echo 'Skipping ${pickedFilepath}' && continue 1;; esac - if [[ is_valid_filetype -eq 0 ]]; then - echo 'Skipping ${pickedFilepath}' && continue 1 ; - fi; - parsed_MP3=$(echo "$pickedFilepath" | sed 's/\(.mp4\|.webm\|.mkv\)/.mp3/' | sed 's|.*\/||'); - parsed_MP3="~/Music/${parsed_MP3}"; + parsed_MP3="${music_folder}/${parsed_MP3}"; #Using ffprobe because videos without audiostream result in exit code 1 which stops this entire loop of many files #Remove (alongside its 2 variables) if you don't record videos without audio (which are admittedly rare) @@ -178,15 +171,15 @@ cmd encodetomp3 ${{ fi done - #Notify the results to the user + #Notify the user of the results if [[ $converted_files_count -gt 0 ]]; then converted_filenames=$(echo "$converted_filenames" | sed 's|.*\/||'); - notify-send "Converted MP3 Files($converted_files_count): $converted_filenames"; + notify-send "Converted MP3 Files($converted_files_count):" "$converted_filenames"; fi; if [[ $videos_without_audio_streams_count -gt 0 ]]; then videos_without_audio_streams=$(echo "$videos_without_audio_streams" | sed 's|.*\/||'); - notify-send "Videos without audio stream($videos_without_audio_streams_count): $videos_without_audio_streams"; + notify-send "Videos without audio stream($videos_without_audio_streams_count):" "$videos_without_audio_streams"; fi; #Uncomment the below line if you want to automatically unselect the original converted video files From 1d1c9dd1a8877ecb424f3a3c19b9024f7e66fb1b Mon Sep 17 00:00:00 2001 From: The Yellow Architect Date: Sun, 19 Nov 2023 09:45:30 +0200 Subject: [PATCH 3/3] Reverted the change to Music variable so its like the original, and also reverted 1 comment --- .config/lf/lfrc | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.config/lf/lfrc b/.config/lf/lfrc index b43c5771..a2ab486f 100644 --- a/.config/lf/lfrc +++ b/.config/lf/lfrc @@ -134,8 +134,6 @@ cmd encodetomp3 ${{ clear; set -f; - music_folder="${MUSIC}"; - #Variables for notify-send converted_filenames=""; converted_files_count=0; @@ -151,7 +149,7 @@ cmd encodetomp3 ${{ esac parsed_MP3=$(echo "$pickedFilepath" | sed 's/\(.mp4\|.webm\|.mkv\)/.mp3/' | sed 's|.*\/||'); - parsed_MP3="${music_folder}/${parsed_MP3}"; + parsed_MP3="~/Music/${parsed_MP3}"; #Using ffprobe because videos without audiostream result in exit code 1 which stops this entire loop of many files #Remove (alongside its 2 variables) if you don't record videos without audio (which are admittedly rare) @@ -171,7 +169,7 @@ cmd encodetomp3 ${{ fi done - #Notify the user of the results + #Notify the results to the user if [[ $converted_files_count -gt 0 ]]; then converted_filenames=$(echo "$converted_filenames" | sed 's|.*\/||'); notify-send "Converted MP3 Files($converted_files_count):" "$converted_filenames";