From dd6dbd898e0540ce294483f0dd0d1bdab686fe27 Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Sat, 8 Feb 2020 13:50:50 +0100 Subject: [PATCH] Added flac-support --- music-sync.sh | 64 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 46 insertions(+), 18 deletions(-) diff --git a/music-sync.sh b/music-sync.sh index 7283d25..730d2d6 100755 --- a/music-sync.sh +++ b/music-sync.sh @@ -11,6 +11,7 @@ bitrate=192 help=false temp="/tmp/converted" convertart=false +noflac=false coverartsize=200 CheckDeps() { @@ -39,6 +40,17 @@ CheckDeps() { fi VerboseOutput 0 "\`lame --version\` succeeded" + # Check ffmpeg + if [[ $noflac == true && ! $(ffmpeg -h 2>/dev/null) ]]; then + VerboseOutput 5 "\`ffmpeg -h\` failed" + VerboseOutput 5 "Sorry, It seems that ffmpeg is not installed on your system" + VerboseOutput 5 "Please install ffmpeg from your repositories and make sure it is available in your \$PATH" + VerboseOutput 5 "If you don't have any FLAC-files use --no-flac" + ExecTime + exit 1 + fi + VerboseOutput 0 "\`ffmpeg -h\` succeeded" + # Check EyeD3 if [[ $convertart == true && ! $(eyeD3 --version 2>/dev/null) ]]; then VerboseOutput 5 "\`eyeD3 --version\` failed" @@ -67,8 +79,8 @@ CheckDeps() { GetOptions() { # https://stackoverflow.com/a/29754866 - OPTIONS=s:d:t:c::a::v::h - LONGOPTS=source:,dest:,temp:,convert::,resize-art::,verbose::,help + OPTIONS=s:d:t:c::a::v::hF + LONGOPTS=source:,dest:,temp:,convert::,resize-art::,verbose::,help,no-flac # -use ! and PIPESTATUS to get exit code with errexit set # -temporarily store output to be able to check for errors @@ -129,6 +141,12 @@ GetOptions() { VerboseOutput 1 "Converted bitrate is ${bitrate}" shift ;; + -F|--no-flac) + VerboseOutput 0 "--no-flac given" + noflac=true + VerboseOutput 1 "FLAC conversion is disabled" + shift + ;; -a|--resize-art) VerboseOutput 0 "--resize-art given" convertart=true @@ -269,9 +287,11 @@ CreateFileList() { ConvertFiles() { curline=0 percentage=0 - while read -r line + IFS=" +"; + for line in $(cat "/tmp/music-sync-filelist") do - + VerboseOutput 0 "${source}/$line" if [[ ! -f "${source}/$line" ]]; then VerboseOutput 5 "Source-file ${source}/$line Unreachable" ExecTime @@ -289,34 +309,42 @@ ConvertFiles() { VerboseOutput 0 "Creating folder $temp/${line%/*}" mkdir -p "$temp/${line%/*}"; fi; - if [[ ! -f "$temp/$line" || "${source}/$line" -nt "$temp/$line" ]]; then - VerboseOutput 0 "Converting MP3-file $temp/${line%/*}" - lame -b ${bitrate} $source/$line $temp/$line 1>/dev/null 2>/dev/null + mp3line=$(echo "$line" | sed -e 's/.flac/.mp3/') + if [[ ! -f "$temp/$mp3line" || "${source}/$mp3line" -nt "$temp/$mp3line" ]]; then + if [[ $line == *".flac" ]]; then + VerboseOutput 0 "Converting FLAC-file $line to mp3" + ffmpeg -y -i "$source/$line" -acodec libmp3lame -map_metadata 0 -id3v2_version 3 -b:a ${bitrate}k "$temp/${mp3line}" 1>/dev/null 2>/dev/null + else + VerboseOutput 0 "Converting MP3-file $line" + lame -b ${bitrate} "$source/$line" "$temp/$mp3line" 1>/dev/null 2>/dev/null + fi if [[ $convertart == true ]]; then - VerboseOutput 0 "Creating folder $temp/$line-images/" - mkdir -p "$temp/$line-images/" + VerboseOutput 0 "Creating folder $temp/${mp3line}-images/" + mkdir -p "$temp/${mp3line}-images/" VerboseOutput 0 "Extracted albumart" - eyeD3 --write-images "$temp/$line-images/" "$temp/$line" 1>/dev/null 2>/dev/null + eyeD3 --write-images "$temp/${mp3line}-images/" "$temp/${mp3line}" 1>/dev/null 2>/dev/null VerboseOutput 0 "Converting albumart" - convert "$temp/$line-images/FRONT_COVER.*" -resize ${coverartsize}x${coverartsize} "$temp/$line-images/FRONT_COVER.jpg" 1>/dev/null 2>/dev/null - eyeD3 --remove-all-images "$temp/$line" 1>/dev/null 2>/dev/null + convert "$temp/${mp3line}-images/FRONT_COVER.*" -resize ${coverartsize}x${coverartsize} "$temp/${mp3line}-images/FRONT_COVER.jpg" 1>/dev/null 2>/dev/null + eyeD3 --remove-all-images "$temp/${mp3line}" 1>/dev/null 2>/dev/null VerboseOutput 0 "Embedding albumart" - eyeD3 --add-image "$temp/$line-images/FRONT_COVER.jpg:FRONT_COVER" "$temp/$line" 1>/dev/null 2>/dev/null - VerboseOutput 1 "Converted cover art: $line" + eyeD3 --add-image "$temp/${mp3line}-images/FRONT_COVER.jpg:FRONT_COVER" "$temp/${mp3line}" 1>/dev/null 2>/dev/null + VerboseOutput 1 "Converted cover art: ${mp3line}" fi VerboseOutput 2 "Converted: $line" else VerboseOutput 3 "$line already converted" fi; - done < "/tmp/music-sync-filelist" + done VerboseOutput 2 "Done converting files" } CopyFiles() { curline=0 percentage=0 - while read -r line + IFS=" +"; + for line in $(cat "/tmp/music-sync-filelist") do if [[ ! -d "$dest" ]]; then VerboseOutput 5 "Destination unreachable" @@ -343,11 +371,11 @@ CopyFiles() { mkdir -p "$dest/${destline%/*}"; fi; - cp -f $temp/$line $dest/$destline 1>/dev/null 2>/dev/null + cp -f $temp/$line $dest/${destline/.flac/.mp3} 1>/dev/null 2>/dev/null VerboseOutput 2 "Copied: $line" - done < "/tmp/music-sync-filelist" + done VerboseOutput 2 "Done copying files" }