From 0d6f75726ce2fac4bd2d7021ec49a62b810b3933 Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Fri, 30 Nov 2018 14:57:08 +0100 Subject: [PATCH 01/25] Comment-fix --- music-sync.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/music-sync.sh b/music-sync.sh index ff117a8..deb3806 100755 --- a/music-sync.sh +++ b/music-sync.sh @@ -101,7 +101,7 @@ VerboseOutput() { CreateFileList() { # ${1} /mnt/hdd/Example-Artist/Example-Album # ${2} /mnt/mtp/Example-Artist/Example-Album - # ${3} Example-Artist/Example-Album/ + # ${3} Example-Artist/Example-Album IFS="" sourcepath="${1}/*" for file in $sourcepath; do From 62a820de5c4303a7d9c95bf3d0cc32e98ad9c1a8 Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Fri, 30 Nov 2018 15:45:02 +0100 Subject: [PATCH 02/25] continue-on-stop --- music-sync.sh | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/music-sync.sh b/music-sync.sh index deb3806..7a76b07 100755 --- a/music-sync.sh +++ b/music-sync.sh @@ -126,8 +126,13 @@ ConvertFiles() { if [[ "/tmp/converted/$line" = */* ]]; then mkdir -p "/tmp/converted/${line%/*}"; fi; - lame -b 192 $source/$line /tmp/converted/$line 1>/dev/null 2>/dev/null - VerboseOutput "Info" "Converted $line" + if [[ ! -f "/tmp/converted/$line" || "${source}/$file" -nt "/tmp/converted/$line" ]]; then + lame -b 192 $source/$line /tmp/converted/$line 1>/dev/null 2>/dev/null + VerboseOutput "Info" "Converted $line" + else + VerboseOutput "Warning" "$line already converted" + fi; + done < "/tmp/music-sync-filelist" } @@ -155,6 +160,9 @@ if [[ $help == true ]]; then Usage exit fi +if [[ -f /tmp/music-sync-filelist ]]; then + rm /tmp/music-sync-filelist +fi CreateFileList $source $dest "" if [[ ! -f /tmp/music-sync-filelist ]]; then VerboseOutput "Info" "Nothing to do!" From 24b3371b5ad4f8a94b2f8f0d2f1a62fc293d0c94 Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Fri, 30 Nov 2018 16:12:50 +0100 Subject: [PATCH 03/25] Added variable verbosity level --- README.MD | 12 ++++++++- music-sync.sh | 69 +++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 62 insertions(+), 19 deletions(-) diff --git a/README.MD b/README.MD index 83ec5f5..c16ff0d 100644 --- a/README.MD +++ b/README.MD @@ -14,8 +14,18 @@ Syncronises music from one folder to another. Options: -s, --source The source folder of the music -d, --dest The destionation folder of the music - -v, --verbose Enable verbose output + -v, --verbose <0-6> Set log level -h, --help Display this help text + +Log levels: + 0 | Verbose + 1 | Debug + 2 | Info (Default) + 3 | Warning + 4 | Error + 5 | Fatal + 6 | No logging + ``` ## Licence diff --git a/music-sync.sh b/music-sync.sh index 7a76b07..c7f4403 100755 --- a/music-sync.sh +++ b/music-sync.sh @@ -4,7 +4,7 @@ set -o errexit -o pipefail -o noclobber -o nounset source="-" dest="-" -verbose=true +verbose=2 help=false GetOptions() { @@ -12,7 +12,7 @@ GetOptions() { # https://stackoverflow.com/a/29754866 ! getopt --test > /dev/null if [[ ${PIPESTATUS[0]} -ne 4 ]]; then - VerboseOutput "Fatal" "\`getopt --test\` failed" + VerboseOutput 5 "\`getopt --test\` failed" echo "Sorry, It seems that your shell is not supported" echo "If you're using mac or another unix-like system, please install GNU getopt" exit 1 @@ -22,8 +22,8 @@ GetOptions() { source=${*: -2:1} dest=${*: -1:1} - OPTIONS=s:d:vh - LONGOPTS=source:,dest:,verbose,help + OPTIONS=s:d:v::h + LONGOPTS=source:,dest:,verbose::,help # -use ! and PIPESTATUS to get exit code with errexit set # -temporarily store output to be able to check for errors @@ -40,12 +40,15 @@ GetOptions() { # read getopt’s output this way to handle the quoting right: eval set -- "$PARSED" - verbose=false # now enjoy the options in order and nicely split until we see -- while true; do case "$1" in -v|--verbose) - verbose=true + verbose=2 + if [[ $2 != "" ]]; then + verbose=${2} + shift + fi shift ;; -h|--help) @@ -87,14 +90,44 @@ Usage() { echo "Options:" echo " -s, --source The source folder of the music" echo " -d, --dest The destionation folder of the music" - echo " -v, --verbose Enable verbose output" + echo " -v, --verbose <0-6> Set log level" echo " -h, --help Display this help text" + echo "Log levels:" + echo " 0 | Verbose" + echo " 1 | Debug" + echo " 2 | Info (Default)" + echo " 3 | Warning" + echo " 4 | Error" + echo " 5 | Fatal" + echo " 6 | No logging" echo "" } VerboseOutput() { - if [[ "$verbose" = true ]]; then - echo "[$1] $2" >&2 + level="" + if [[ $verbose -le $1 ]]; then + case "$1" in + 0) + level="Verbose" + ;; + 1) + level=" Debug " + ;; + 2) + level=" Info " + ;; + 3) + level="Warning" + ;; + 4) + level=" Error " + ;; + + 5) + level=" Fatal " + ;; + esac + echo "[$level] $2" >&2 fi } @@ -109,11 +142,11 @@ CreateFileList() { if [[ -d "${1}/$relfile" ]]; then newdir="${3}/$relfile" newdir=${newdir#"/"} - VerboseOutput "Info" "Entering $newdir" + VerboseOutput 1 "Entering $newdir" CreateFileList "${1}/$relfile" "${2}/$relfile" "$newdir" elif [[ ! -f "${2}/$relfile" || "${1}/$relfile" -nt "${2}/$relfile" ]]; then echo ${3}/$relfile >> /tmp/music-sync-filelist - VerboseOutput "Info" "Added: ${3}/${relfile}" + VerboseOutput 2 "Added: ${3}/${relfile}" fi done } @@ -122,15 +155,15 @@ ConvertFiles() { mkdir -p /tmp/converted while read -r line do - VerboseOutput "Info" "Converting $line" + VerboseOutput 1 "Converting: $line" if [[ "/tmp/converted/$line" = */* ]]; then mkdir -p "/tmp/converted/${line%/*}"; fi; if [[ ! -f "/tmp/converted/$line" || "${source}/$file" -nt "/tmp/converted/$line" ]]; then lame -b 192 $source/$line /tmp/converted/$line 1>/dev/null 2>/dev/null - VerboseOutput "Info" "Converted $line" + VerboseOutput 2 "Converted: $line" else - VerboseOutput "Warning" "$line already converted" + VerboseOutput 3 "$line already converted" fi; done < "/tmp/music-sync-filelist" @@ -139,20 +172,20 @@ ConvertFiles() { CopyFiles() { while read -r line do - VerboseOutput "Info" "Copy $line" + VerboseOutput 1 "Copying: $line" if [[ "$dest/$line" = */* ]]; then mkdir -p "$dest/${line%/*}"; fi; cp -f $source/$line $dest/$line 1>/dev/null 2>/dev/null done < "/tmp/music-sync-filelist" - VerboseOutput "Info" "Copied $line" + VerboseOutput 2 "Copied: $line" } CleanUp() { - VerboseOutput "Info" "Cleaning Up" + VerboseOutput 1 "Cleaning Up" rm "/tmp/music-sync-filelist" rm -rf "/tmp/converted" - VerboseOutput "Info" "Done" + VerboseOutput 1 "Done" } GetOptions $@ From 13a420a5b10d43c3835e40eb2e1e75ac3756a588 Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Fri, 30 Nov 2018 16:26:58 +0100 Subject: [PATCH 04/25] Outputting script execution time --- music-sync.sh | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/music-sync.sh b/music-sync.sh index c7f4403..b3ed53d 100755 --- a/music-sync.sh +++ b/music-sync.sh @@ -1,5 +1,6 @@ #!/bin/bash +begin=$(date +"%s") # saner programming env: these switches turn some bugs into errors set -o errexit -o pipefail -o noclobber -o nounset source="-" @@ -203,4 +204,8 @@ if [[ ! -f /tmp/music-sync-filelist ]]; then fi ConvertFiles CopyFiles -CleanUp \ No newline at end of file +CleanUp + +termin=$(date +"%s") +difftimelps=$(($termin-$begin)) +VerboseOutput 1 "$(($difftimelps / 60)) minutes and $(($difftimelps % 60)) seconds elapsed for Script Execution." \ No newline at end of file From b872adb7fe15a5e94083537f937dbbf48183db43 Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Fri, 30 Nov 2018 16:56:15 +0100 Subject: [PATCH 05/25] Added colours to output level --- music-sync.sh | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/music-sync.sh b/music-sync.sh index b3ed53d..d85b116 100755 --- a/music-sync.sh +++ b/music-sync.sh @@ -14,8 +14,8 @@ GetOptions() { ! getopt --test > /dev/null if [[ ${PIPESTATUS[0]} -ne 4 ]]; then VerboseOutput 5 "\`getopt --test\` failed" - echo "Sorry, It seems that your shell is not supported" - echo "If you're using mac or another unix-like system, please install GNU getopt" + VerboseOutput 5 "Sorry, It seems that your shell is not supported" + VerboseOutput 5 "If you're using MacOS or another unix-like system, please install GNU getopt" exit 1 fi @@ -69,7 +69,7 @@ GetOptions() { break ;; *) - echo "Programming error" + VerboseOutput 5 "Programming error" return 3 ;; esac @@ -109,26 +109,26 @@ VerboseOutput() { if [[ $verbose -le $1 ]]; then case "$1" in 0) - level="Verbose" + level="\033[1;36mVerbose\033[0m" ;; 1) - level=" Debug " + level="\033[1;34m Debug \033[0m" ;; 2) - level=" Info " + level="\033[1;37m Info \033[0m" ;; 3) - level="Warning" + level="\033[1;33mWarning\033[0m" ;; 4) - level=" Error " + level="\033[1;31m Error \033[0m" ;; 5) - level=" Fatal " + level="\033[1;30m Fatal \033[0m" ;; esac - echo "[$level] $2" >&2 + echo -e "[$level] $2" >&2 fi } @@ -199,7 +199,7 @@ if [[ -f /tmp/music-sync-filelist ]]; then fi CreateFileList $source $dest "" if [[ ! -f /tmp/music-sync-filelist ]]; then - VerboseOutput "Info" "Nothing to do!" + VerboseOutput 2 "Nothing to do!" exit 0 fi ConvertFiles From 1393aad7c022a45f6bc5303a1be30c5a03c322bf Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Fri, 30 Nov 2018 17:24:40 +0100 Subject: [PATCH 06/25] Added check if destination is still reachable --- music-sync.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/music-sync.sh b/music-sync.sh index d85b116..5617d73 100755 --- a/music-sync.sh +++ b/music-sync.sh @@ -173,6 +173,9 @@ ConvertFiles() { CopyFiles() { while read -r line do + if [[ ! -f "$dest" ]]; then + VerboseOutput 5 "Destination not reachable (anymore)" + fi VerboseOutput 1 "Copying: $line" if [[ "$dest/$line" = */* ]]; then mkdir -p "$dest/${line%/*}"; From 5c7c754dc3fdb9a548e4e2c882b7d500e3c78c8a Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Fri, 30 Nov 2018 17:36:54 +0100 Subject: [PATCH 07/25] Added exit codes --- README.MD | 6 ++++++ music-sync.sh | 38 ++++++++++++++++++++++++++++++++------ 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/README.MD b/README.MD index c16ff0d..3be5cda 100644 --- a/README.MD +++ b/README.MD @@ -26,6 +26,12 @@ Log levels: 5 | Fatal 6 | No logging +Exit Codes: + 1 Dependencies not met + 2 Invalid Argument + 3 Source Unreachable + 4 Destination Unreachable + ``` ## Licence diff --git a/music-sync.sh b/music-sync.sh index 5617d73..7a8dcdf 100755 --- a/music-sync.sh +++ b/music-sync.sh @@ -16,6 +16,7 @@ GetOptions() { VerboseOutput 5 "\`getopt --test\` failed" VerboseOutput 5 "Sorry, It seems that your shell is not supported" VerboseOutput 5 "If you're using MacOS or another unix-like system, please install GNU getopt" + ExecTime exit 1 fi @@ -93,6 +94,7 @@ Usage() { echo " -d, --dest The destionation folder of the music" echo " -v, --verbose <0-6> Set log level" echo " -h, --help Display this help text" + echo "" echo "Log levels:" echo " 0 | Verbose" echo " 1 | Debug" @@ -102,6 +104,12 @@ Usage() { echo " 5 | Fatal" echo " 6 | No logging" echo "" + echo "Exit Codes:" + echo " 1 Dependencies not met" + echo " 2 Invalid Argument" + echo " 3 Source Unreachable" + echo " 4 Destination Unreachable" + echo "" } VerboseOutput() { @@ -156,6 +164,12 @@ ConvertFiles() { mkdir -p /tmp/converted while read -r line do + if [[ ! -f "${source}/$file" ]]; then + VerboseOutput 5 "Source-file ${source}/$file Unreachable" + ExecTime + exit 3 + fi + VerboseOutput 1 "Converting: $line" if [[ "/tmp/converted/$line" = */* ]]; then mkdir -p "/tmp/converted/${line%/*}"; @@ -174,8 +188,17 @@ CopyFiles() { while read -r line do if [[ ! -f "$dest" ]]; then - VerboseOutput 5 "Destination not reachable (anymore)" - fi + VerboseOutput 5 "Destination unreachable" + ExecTime + exit 4 + fi + + if [[ ! -f "$line" ]]; then + VerboseOutput 5 "Source-file ${source}/$file Unreachable" + ExecTime + exit 3 + fi + VerboseOutput 1 "Copying: $line" if [[ "$dest/$line" = */* ]]; then mkdir -p "$dest/${line%/*}"; @@ -192,6 +215,12 @@ CleanUp() { VerboseOutput 1 "Done" } +ExecTime() { + termin=$(date +"%s") + difftimelps=$(($termin-$begin)) + VerboseOutput 1 "$(($difftimelps / 60)) minutes and $(($difftimelps % 60)) seconds elapsed for Script Execution." +} + GetOptions $@ if [[ $help == true ]]; then Usage @@ -208,7 +237,4 @@ fi ConvertFiles CopyFiles CleanUp - -termin=$(date +"%s") -difftimelps=$(($termin-$begin)) -VerboseOutput 1 "$(($difftimelps / 60)) minutes and $(($difftimelps % 60)) seconds elapsed for Script Execution." \ No newline at end of file +ExecTime \ No newline at end of file From ead372dd6501defef466db06c33885e61b24eb7a Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Fri, 30 Nov 2018 17:50:37 +0100 Subject: [PATCH 08/25] Added more dependency checks --- music-sync.sh | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/music-sync.sh b/music-sync.sh index 7a8dcdf..c76f4eb 100755 --- a/music-sync.sh +++ b/music-sync.sh @@ -8,9 +8,8 @@ dest="-" verbose=2 help=false -GetOptions() { - - # https://stackoverflow.com/a/29754866 +CheckDeps() { + # Check getopt ! getopt --test > /dev/null if [[ ${PIPESTATUS[0]} -ne 4 ]]; then VerboseOutput 5 "\`getopt --test\` failed" @@ -19,7 +18,20 @@ GetOptions() { ExecTime exit 1 fi - + + # Check lame + if [[ ! $(lame --version 2>/dev/null) ]]; then + VerboseOutput 5 "\`lame --version\` failed" + VerboseOutput 5 "Sorry, It seems that lame is not installed on your system" + VerboseOutput 5 "Please install lame from your repositories and make sure it is available in your \$PATH" + ExecTime + exit 1 + fi +} + +GetOptions() { + + # https://stackoverflow.com/a/29754866 # Parsing style without -s or -d source=${*: -2:1} dest=${*: -1:1} @@ -221,6 +233,7 @@ ExecTime() { VerboseOutput 1 "$(($difftimelps / 60)) minutes and $(($difftimelps % 60)) seconds elapsed for Script Execution." } +CheckDeps GetOptions $@ if [[ $help == true ]]; then Usage From ad6bc38c0231f2020ff58003d073fb037b1a4a3d Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Sat, 1 Dec 2018 00:43:17 +0100 Subject: [PATCH 09/25] Added some features * Set temp-folder * Better way of handling style without -d or -s * Added progress to converting and syncing --- README.MD | 9 ++++--- music-sync.sh | 68 ++++++++++++++++++++++++++++++++++----------------- 2 files changed, 51 insertions(+), 26 deletions(-) diff --git a/README.MD b/README.MD index 3be5cda..0e9cdbb 100644 --- a/README.MD +++ b/README.MD @@ -6,7 +6,7 @@ Script to sync music from one folder to another ## Usage: ``` -music-sync -s|--source -d|--dest +music-sync -s|--source -d|--dest -t|--temp music-sync Syncronises music from one folder to another. @@ -14,17 +14,18 @@ Syncronises music from one folder to another. Options: -s, --source The source folder of the music -d, --dest The destionation folder of the music - -v, --verbose <0-6> Set log level + -t, --temp The temporary cache for converted files (default: /tmp/converted) + -v, --verbose <0-6> Set log level (default: 2) -h, --help Display this help text Log levels: 0 | Verbose 1 | Debug - 2 | Info (Default) + 2 | Info 3 | Warning 4 | Error 5 | Fatal - 6 | No logging + 6 | Silence Exit Codes: 1 Dependencies not met diff --git a/music-sync.sh b/music-sync.sh index c76f4eb..2ba2a45 100755 --- a/music-sync.sh +++ b/music-sync.sh @@ -7,6 +7,7 @@ source="-" dest="-" verbose=2 help=false +temp="/tmp/converted" CheckDeps() { # Check getopt @@ -31,13 +32,9 @@ CheckDeps() { GetOptions() { - # https://stackoverflow.com/a/29754866 - # Parsing style without -s or -d - source=${*: -2:1} - dest=${*: -1:1} - - OPTIONS=s:d:v::h - LONGOPTS=source:,dest:,verbose::,help + # https://stackoverflow.com/a/29754866 + OPTIONS=s:d:t:v::h + LONGOPTS=source:,dest:,temp:,verbose::,help # -use ! and PIPESTATUS to get exit code with errexit set # -temporarily store output to be able to check for errors @@ -77,6 +74,10 @@ GetOptions() { dest="$2" shift 2 ;; + -t|--temp) + temp="$2" + shift 2 + ;; --) shift break @@ -88,7 +89,15 @@ GetOptions() { esac done - if [[ $dest == "" ]] || [[ $source == "" ]]; then + if [[ ! -z ${1+x} ]]; then + source=$1 + fi + + if [[ ! -z ${2+x} ]]; then + dest=$2 + fi + + if [[ $dest == "-" ]] || [[ $source == "-" ]]; then help=true fi } @@ -104,17 +113,18 @@ Usage() { echo "Options:" echo " -s, --source The source folder of the music" echo " -d, --dest The destionation folder of the music" - echo " -v, --verbose <0-6> Set log level" + echo " -t, --temp The temporary cache for converted files (default: /tmp/converted)" + echo " -v, --verbose <0-6> Set log level (default: 2)" echo " -h, --help Display this help text" echo "" echo "Log levels:" echo " 0 | Verbose" echo " 1 | Debug" - echo " 2 | Info (Default)" + echo " 2 | Info" echo " 3 | Warning" echo " 4 | Error" echo " 5 | Fatal" - echo " 6 | No logging" + echo " 6 | Silence" echo "" echo "Exit Codes:" echo " 1 Dependencies not met" @@ -173,21 +183,28 @@ CreateFileList() { } ConvertFiles() { - mkdir -p /tmp/converted + curline=0 + percentage=0 while read -r line do - if [[ ! -f "${source}/$file" ]]; then - VerboseOutput 5 "Source-file ${source}/$file Unreachable" + if [[ ! -f "${source}/$line" ]]; then + VerboseOutput 5 "Source-file ${source}/$line Unreachable" ExecTime exit 3 fi + curline=$(expr ${curline} + 1) + total=$(cat /tmp/music-sync-filelist | wc -l) + percentage=$(echo "scale=4;${curline}/${total}" | bc) + percentage=$(echo "scale=2;${percentage}*100" | bc) VerboseOutput 1 "Converting: $line" - if [[ "/tmp/converted/$line" = */* ]]; then - mkdir -p "/tmp/converted/${line%/*}"; + VerboseOutput 2 "Progress: $curline / $total (${percentage%00}%) Step 1 of 2" + + if [[ "$temp/$line" = */* ]]; then + mkdir -p "$temp/${line%/*}"; fi; - if [[ ! -f "/tmp/converted/$line" || "${source}/$file" -nt "/tmp/converted/$line" ]]; then - lame -b 192 $source/$line /tmp/converted/$line 1>/dev/null 2>/dev/null + if [[ ! -f "$temp/$line" || "${source}/$line" -nt "$temp/$line" ]]; then + lame -b 192 $source/$line $temp/$line 1>/dev/null 2>/dev/null VerboseOutput 2 "Converted: $line" else VerboseOutput 3 "$line already converted" @@ -197,6 +214,8 @@ ConvertFiles() { } CopyFiles() { + curline=0 + percentage=0 while read -r line do if [[ ! -f "$dest" ]]; then @@ -206,16 +225,22 @@ CopyFiles() { fi if [[ ! -f "$line" ]]; then - VerboseOutput 5 "Source-file ${source}/$file Unreachable" + VerboseOutput 5 "Source-file ${temp}/$line Unreachable" ExecTime exit 3 fi + curline=$(expr ${curline} + 1) + total=$(cat /tmp/music-sync-filelist | wc -l) + percentage=$(echo "scale=4;${curline}/${total}" | bc) + percentage=$(echo "scale=2;${percentage}*100" | bc) VerboseOutput 1 "Copying: $line" + VerboseOutput 2 "Progress: $curline / $total (${percentage%00}%) Step 2 of 2" + if [[ "$dest/$line" = */* ]]; then mkdir -p "$dest/${line%/*}"; fi; - cp -f $source/$line $dest/$line 1>/dev/null 2>/dev/null + cp -f $temp/$line $dest/$line 1>/dev/null 2>/dev/null done < "/tmp/music-sync-filelist" VerboseOutput 2 "Copied: $line" } @@ -223,7 +248,6 @@ CopyFiles() { CleanUp() { VerboseOutput 1 "Cleaning Up" rm "/tmp/music-sync-filelist" - rm -rf "/tmp/converted" VerboseOutput 1 "Done" } @@ -250,4 +274,4 @@ fi ConvertFiles CopyFiles CleanUp -ExecTime \ No newline at end of file +ExecTime From a0d0d30989a1318e55f980e7f5c0fa836df72cb8 Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Sat, 1 Dec 2018 09:38:03 +0100 Subject: [PATCH 10/25] Handling special char names --- music-sync.sh | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/music-sync.sh b/music-sync.sh index 2ba2a45..7773fe7 100755 --- a/music-sync.sh +++ b/music-sync.sh @@ -167,7 +167,8 @@ CreateFileList() { # ${2} /mnt/mtp/Example-Artist/Example-Album # ${3} Example-Artist/Example-Album IFS="" - sourcepath="${1}/*" + sourcepath="${1/\[/\\\[}/*" + sourcepath="${sourcepath/\]/\\\]}" for file in $sourcepath; do relfile="${file#"$1/"}" if [[ -d "${1}/$relfile" ]]; then @@ -187,6 +188,7 @@ ConvertFiles() { percentage=0 while read -r line do + if [[ ! -f "${source}/$line" ]]; then VerboseOutput 5 "Source-file ${source}/$line Unreachable" ExecTime @@ -218,13 +220,13 @@ CopyFiles() { percentage=0 while read -r line do - if [[ ! -f "$dest" ]]; then + if [[ ! -d "$dest" ]]; then VerboseOutput 5 "Destination unreachable" ExecTime exit 4 fi - if [[ ! -f "$line" ]]; then + if [[ ! -f "${temp}/$line" ]]; then VerboseOutput 5 "Source-file ${temp}/$line Unreachable" ExecTime exit 3 @@ -234,6 +236,7 @@ CopyFiles() { total=$(cat /tmp/music-sync-filelist | wc -l) percentage=$(echo "scale=4;${curline}/${total}" | bc) percentage=$(echo "scale=2;${percentage}*100" | bc) + VerboseOutput 1 "Copying: $line" VerboseOutput 2 "Progress: $curline / $total (${percentage%00}%) Step 2 of 2" From c9b24e4ec2ecdcbf778015bce9f07d2e01ead008 Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Sat, 1 Dec 2018 11:13:30 +0100 Subject: [PATCH 11/25] Added error handlers --- README.MD | 1 + music-sync.sh | 23 ++++++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/README.MD b/README.MD index 0e9cdbb..8dce52a 100644 --- a/README.MD +++ b/README.MD @@ -32,6 +32,7 @@ Exit Codes: 2 Invalid Argument 3 Source Unreachable 4 Destination Unreachable + 5 Command failed ``` diff --git a/music-sync.sh b/music-sync.sh index 7773fe7..d0f0c6e 100755 --- a/music-sync.sh +++ b/music-sync.sh @@ -131,6 +131,7 @@ Usage() { echo " 2 Invalid Argument" echo " 3 Source Unreachable" echo " 4 Destination Unreachable" + echo " 5 Command failed" echo "" } @@ -243,14 +244,18 @@ CopyFiles() { if [[ "$dest/$line" = */* ]]; then mkdir -p "$dest/${line%/*}"; fi; + cp -f $temp/$line $dest/$line 1>/dev/null 2>/dev/null + done < "/tmp/music-sync-filelist" VerboseOutput 2 "Copied: $line" } CleanUp() { VerboseOutput 1 "Cleaning Up" - rm "/tmp/music-sync-filelist" + if [[ -f /tmp/music-sync-filelist ]]; then + rm "/tmp/music-sync-filelist" + fi VerboseOutput 1 "Done" } @@ -260,6 +265,21 @@ ExecTime() { VerboseOutput 1 "$(($difftimelps / 60)) minutes and $(($difftimelps % 60)) seconds elapsed for Script Execution." } +ErrorHandler() { + VerboseOutput 5 "Error while executing $1" + CleanUp + exit 5 +} + +ExitHandler() { + VerboseOutput 5 "Aborted" + CleanUp + exit 0 +} + +trap 'ExitHandler' SIGINT +trap 'ErrorHandler $BASH_COMMAND' ERR + CheckDeps GetOptions $@ if [[ $help == true ]]; then @@ -272,6 +292,7 @@ fi CreateFileList $source $dest "" if [[ ! -f /tmp/music-sync-filelist ]]; then VerboseOutput 2 "Nothing to do!" + CleanUp exit 0 fi ConvertFiles From 3d108ecf7f6d4716f8900aa5e7931e3723b87d24 Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Sat, 1 Dec 2018 11:19:06 +0100 Subject: [PATCH 12/25] Wrong place for output --- music-sync.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/music-sync.sh b/music-sync.sh index d0f0c6e..459e4dc 100755 --- a/music-sync.sh +++ b/music-sync.sh @@ -247,8 +247,9 @@ CopyFiles() { cp -f $temp/$line $dest/$line 1>/dev/null 2>/dev/null + VerboseOutput 2 "Copied: $line" + done < "/tmp/music-sync-filelist" - VerboseOutput 2 "Copied: $line" } CleanUp() { From 4aee64d3fe588188da1d493fc6d5283f27cf2869 Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Sat, 1 Dec 2018 18:40:13 +0100 Subject: [PATCH 13/25] Added posibility to not convert --- README.MD | 1 + music-sync.sh | 57 ++++++++++++++++++++++++++++++++++----------------- 2 files changed, 39 insertions(+), 19 deletions(-) diff --git a/README.MD b/README.MD index 8dce52a..5e00777 100644 --- a/README.MD +++ b/README.MD @@ -15,6 +15,7 @@ Options: -s, --source The source folder of the music -d, --dest The destionation folder of the music -t, --temp The temporary cache for converted files (default: /tmp/converted) + -c, --convert Convert files before syncing -v, --verbose <0-6> Set log level (default: 2) -h, --help Display this help text diff --git a/music-sync.sh b/music-sync.sh index 459e4dc..cc804aa 100755 --- a/music-sync.sh +++ b/music-sync.sh @@ -5,36 +5,42 @@ begin=$(date +"%s") set -o errexit -o pipefail -o noclobber -o nounset source="-" dest="-" +convert=false verbose=2 help=false temp="/tmp/converted" CheckDeps() { - # Check getopt - ! getopt --test > /dev/null - if [[ ${PIPESTATUS[0]} -ne 4 ]]; then - VerboseOutput 5 "\`getopt --test\` failed" - VerboseOutput 5 "Sorry, It seems that your shell is not supported" - VerboseOutput 5 "If you're using MacOS or another unix-like system, please install GNU getopt" - ExecTime - exit 1 + if [[ $1 == 0 ]]; then + # Check getopt + ! getopt --test > /dev/null + if [[ ${PIPESTATUS[0]} -ne 4 ]]; then + VerboseOutput 5 "\`getopt --test\` failed" + VerboseOutput 5 "Sorry, It seems that your shell is not supported" + VerboseOutput 5 "If you're using MacOS or another unix-like system, please install GNU getopt" + ExecTime + exit 1 + fi fi - # Check lame - if [[ ! $(lame --version 2>/dev/null) ]]; then - VerboseOutput 5 "\`lame --version\` failed" - VerboseOutput 5 "Sorry, It seems that lame is not installed on your system" - VerboseOutput 5 "Please install lame from your repositories and make sure it is available in your \$PATH" - ExecTime - exit 1 + if [[ $1 == 1 ]]; then + # Check lame + echo $convert + if [[ $convert == true && ! $(lamek --version 2>/dev/null) ]]; then + VerboseOutput 5 "\`lame --version\` failed" + VerboseOutput 5 "Sorry, It seems that lame is not installed on your system" + VerboseOutput 5 "Please install lame from your repositories and make sure it is available in your \$PATH" + ExecTime + exit 1 + fi fi } GetOptions() { # https://stackoverflow.com/a/29754866 - OPTIONS=s:d:t:v::h - LONGOPTS=source:,dest:,temp:,verbose::,help + OPTIONS=s:d:t:cv::h + LONGOPTS=source:,dest:,temp:,convert,verbose::,help # -use ! and PIPESTATUS to get exit code with errexit set # -temporarily store output to be able to check for errors @@ -78,6 +84,10 @@ GetOptions() { temp="$2" shift 2 ;; + -c|--convert) + convert=true + shift + ;; --) shift break @@ -114,6 +124,7 @@ Usage() { echo " -s, --source The source folder of the music" echo " -d, --dest The destionation folder of the music" echo " -t, --temp The temporary cache for converted files (default: /tmp/converted)" + echo " -c, --convert Convert files before syncing" echo " -v, --verbose <0-6> Set log level (default: 2)" echo " -h, --help Display this help text" echo "" @@ -281,8 +292,9 @@ ExitHandler() { trap 'ExitHandler' SIGINT trap 'ErrorHandler $BASH_COMMAND' ERR -CheckDeps +CheckDeps 0 GetOptions $@ +CheckDeps 1 if [[ $help == true ]]; then Usage exit @@ -296,7 +308,14 @@ if [[ ! -f /tmp/music-sync-filelist ]]; then CleanUp exit 0 fi -ConvertFiles +if [[ $convert == true ]]; then + ConvertFiles +else + if [[ $temp != "/tmp/converted" ]]; then + VerboseOutput 2 "Conversion not enabled. Ignoring cache folder" + fi + temp=$source +fi CopyFiles CleanUp ExecTime From 493cb40ffd00a6626d70918c3abed004fab5eaed Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Wed, 5 Dec 2018 15:37:27 +0100 Subject: [PATCH 14/25] Added option to convert coverart --- README.MD | 1 + music-sync.sh | 45 +++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/README.MD b/README.MD index 5e00777..e69d679 100644 --- a/README.MD +++ b/README.MD @@ -17,6 +17,7 @@ Options: -t, --temp The temporary cache for converted files (default: /tmp/converted) -c, --convert Convert files before syncing -v, --verbose <0-6> Set log level (default: 2) + -a, --convert-art Convert files before syncing -h, --help Display this help text Log levels: diff --git a/music-sync.sh b/music-sync.sh index cc804aa..5b281ac 100755 --- a/music-sync.sh +++ b/music-sync.sh @@ -9,6 +9,7 @@ convert=false verbose=2 help=false temp="/tmp/converted" +convertart=false CheckDeps() { if [[ $1 == 0 ]]; then @@ -25,11 +26,29 @@ CheckDeps() { if [[ $1 == 1 ]]; then # Check lame - echo $convert - if [[ $convert == true && ! $(lamek --version 2>/dev/null) ]]; then + if [[ $convert == true && ! $(lame --version 2>/dev/null) ]]; then VerboseOutput 5 "\`lame --version\` failed" VerboseOutput 5 "Sorry, It seems that lame is not installed on your system" VerboseOutput 5 "Please install lame from your repositories and make sure it is available in your \$PATH" + VerboseOutput 5 "Otherwise disable conversion" + ExecTime + exit 1 + fi + # Check EyeD3 + if [[ $convertart == true && ! $(eyeD3 --version 2>/dev/null) ]]; then + VerboseOutput 5 "\`eyeD3 --version\` failed" + VerboseOutput 5 "Sorry, It seems that lame is not installed on your system" + VerboseOutput 5 "Please install lame from your repositories and make sure it is available in your \$PATH" + VerboseOutput 5 "Otherwise disable albumart conversion" + ExecTime + exit 1 + fi + # Check Imagemagick + if [[ $convertart == true && ! $(convert --version 2>/dev/null) ]]; then + VerboseOutput 5 "\`convert --version\` failed" + VerboseOutput 5 "Sorry, It seems that lame is not installed on your system" + VerboseOutput 5 "Please install lame from your repositories and make sure it is available in your \$PATH" + VerboseOutput 5 "Otherwise disable albumart conversion" ExecTime exit 1 fi @@ -39,8 +58,12 @@ CheckDeps() { GetOptions() { # https://stackoverflow.com/a/29754866 - OPTIONS=s:d:t:cv::h - LONGOPTS=source:,dest:,temp:,convert,verbose::,help + OPTIONS=s:d:t:cav::h + LONGOPTS=source:,dest:,temp:,convert,convert-art,verbose::,help + + # Parsing style without -s or -d + source=${*: -2:1} + dest=${*: -1:1} # -use ! and PIPESTATUS to get exit code with errexit set # -temporarily store output to be able to check for errors @@ -88,6 +111,10 @@ GetOptions() { convert=true shift ;; + -a|--convert-art) + convertart=true + shift + ;; --) shift break @@ -125,6 +152,7 @@ Usage() { echo " -d, --dest The destionation folder of the music" echo " -t, --temp The temporary cache for converted files (default: /tmp/converted)" echo " -c, --convert Convert files before syncing" + echo " -a, --convert-art Convert files before syncing" echo " -v, --verbose <0-6> Set log level (default: 2)" echo " -h, --help Display this help text" echo "" @@ -220,6 +248,15 @@ ConvertFiles() { if [[ ! -f "$temp/$line" || "${source}/$line" -nt "$temp/$line" ]]; then lame -b 192 $source/$line $temp/$line 1>/dev/null 2>/dev/null VerboseOutput 2 "Converted: $line" + if [[ $convertart == true ]]; then + mkdir "$temp/$line-images/" + eyeD3 --write-images "$temp/$line-images/" "$temp/$line" + convert "$temp/$line-images/FRONT_COVER.jpg" -resize 200x200 "$temp/$line-images/FRONT_COVER.jpg" + eyeD3 --remove-all-images "$temp/$line" + eyeD3 --add-image "$temp/$line-images/FRONT_COVER.jpg:FRONT_COVER" "$temp/$line" + VerboseOutput 2 "Converted cover art: $temp/$line" + fi + else VerboseOutput 3 "$line already converted" fi; From 8cee47f8d982342567050c72bd9a474098dad1b3 Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Wed, 5 Dec 2018 15:42:44 +0100 Subject: [PATCH 15/25] Updated to better deal with parameters --- music-sync.sh | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/music-sync.sh b/music-sync.sh index 5b281ac..75f649c 100755 --- a/music-sync.sh +++ b/music-sync.sh @@ -61,10 +61,6 @@ GetOptions() { OPTIONS=s:d:t:cav::h LONGOPTS=source:,dest:,temp:,convert,convert-art,verbose::,help - # Parsing style without -s or -d - source=${*: -2:1} - dest=${*: -1:1} - # -use ! and PIPESTATUS to get exit code with errexit set # -temporarily store output to be able to check for errors # -activate quoting/enhanced mode (e.g. by writing out “--options”) @@ -126,12 +122,12 @@ GetOptions() { esac done - if [[ ! -z ${1+x} ]]; then - source=$1 + if [[ ! -z "${1}" ]]; then + source="$1" fi - if [[ ! -z ${2+x} ]]; then - dest=$2 + if [[ ! -z "${2}" ]]; then + dest="$2" fi if [[ $dest == "-" ]] || [[ $source == "-" ]]; then From 8448b301c99123a4fe935a71e5772711078298ce Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Sat, 16 Mar 2019 14:39:42 +0100 Subject: [PATCH 16/25] Fixed some spell mistakes --- music-sync.sh | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/music-sync.sh b/music-sync.sh index 75f649c..dfec240 100755 --- a/music-sync.sh +++ b/music-sync.sh @@ -37,17 +37,17 @@ CheckDeps() { # Check EyeD3 if [[ $convertart == true && ! $(eyeD3 --version 2>/dev/null) ]]; then VerboseOutput 5 "\`eyeD3 --version\` failed" - VerboseOutput 5 "Sorry, It seems that lame is not installed on your system" - VerboseOutput 5 "Please install lame from your repositories and make sure it is available in your \$PATH" + VerboseOutput 5 "Sorry, It seems that eyeD3 is not installed on your system" + VerboseOutput 5 "Please install eyeD3 from your repositories and make sure it is available in your \$PATH" VerboseOutput 5 "Otherwise disable albumart conversion" ExecTime exit 1 fi - # Check Imagemagick + # Check ImageMagick if [[ $convertart == true && ! $(convert --version 2>/dev/null) ]]; then VerboseOutput 5 "\`convert --version\` failed" - VerboseOutput 5 "Sorry, It seems that lame is not installed on your system" - VerboseOutput 5 "Please install lame from your repositories and make sure it is available in your \$PATH" + VerboseOutput 5 "Sorry, It seems that ImageMagick is not installed on your system" + VerboseOutput 5 "Please install ImageMagick from your repositories and make sure it is available in your \$PATH" VerboseOutput 5 "Otherwise disable albumart conversion" ExecTime exit 1 @@ -148,7 +148,7 @@ Usage() { echo " -d, --dest The destionation folder of the music" echo " -t, --temp The temporary cache for converted files (default: /tmp/converted)" echo " -c, --convert Convert files before syncing" - echo " -a, --convert-art Convert files before syncing" + echo " -a, --convert-art Convert album-art before syncing" echo " -v, --verbose <0-6> Set log level (default: 2)" echo " -h, --help Display this help text" echo "" From b4ee0ccfcfa7a64cb95daf496e27719f72ec135b Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Sat, 16 Mar 2019 14:51:38 +0100 Subject: [PATCH 17/25] I don't like other's output --- music-sync.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/music-sync.sh b/music-sync.sh index dfec240..4d59fac 100755 --- a/music-sync.sh +++ b/music-sync.sh @@ -246,11 +246,11 @@ ConvertFiles() { VerboseOutput 2 "Converted: $line" if [[ $convertart == true ]]; then mkdir "$temp/$line-images/" - eyeD3 --write-images "$temp/$line-images/" "$temp/$line" - convert "$temp/$line-images/FRONT_COVER.jpg" -resize 200x200 "$temp/$line-images/FRONT_COVER.jpg" - eyeD3 --remove-all-images "$temp/$line" - eyeD3 --add-image "$temp/$line-images/FRONT_COVER.jpg:FRONT_COVER" "$temp/$line" - VerboseOutput 2 "Converted cover art: $temp/$line" + eyeD3 --write-images "$temp/$line-images/" "$temp/$line" 1>/dev/null 2>/dev/null + convert "$temp/$line-images/FRONT_COVER.*" -resize 200x200 "$temp/$line-images/FRONT_COVER.jpg" 1>/dev/null 2>/dev/null + eyeD3 --remove-all-images "$temp/$line" 1>/dev/null 2>/dev/null + eyeD3 --add-image "$temp/$line-images/FRONT_COVER.jpg:FRONT_COVER" "$temp/$line" 1>/dev/null 2>/dev/null + VerboseOutput 2 "Converted cover art: $line" fi else From dd38f38e898fbbf5ec830ca8260ba65c0935b230 Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Sat, 16 Mar 2019 14:52:48 +0100 Subject: [PATCH 18/25] folders with .'s at the end gave errors --- music-sync.sh | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/music-sync.sh b/music-sync.sh index 4d59fac..24079a0 100755 --- a/music-sync.sh +++ b/music-sync.sh @@ -206,13 +206,14 @@ CreateFileList() { sourcepath="${1/\[/\\\[}/*" sourcepath="${sourcepath/\]/\\\]}" for file in $sourcepath; do - relfile="${file#"$1/"}" - if [[ -d "${1}/$relfile" ]]; then + origfile="${file#"$1/"}" + relfile=$(echo ${origfile} | sed -e 's/\(\.\)*$//g') + if [[ -d "${1}/$origfile" ]]; then newdir="${3}/$relfile" newdir=${newdir#"/"} VerboseOutput 1 "Entering $newdir" - CreateFileList "${1}/$relfile" "${2}/$relfile" "$newdir" - elif [[ ! -f "${2}/$relfile" || "${1}/$relfile" -nt "${2}/$relfile" ]]; then + CreateFileList "${1}/$origfile" "${2}/$relfile" "$newdir" + elif [[ "${1}/$origfile" != *".m3u" ]] && [[ ! -f "${2}/$relfile" || "${1}/$origfile" -nt "${2}/$relfile" ]]; then echo ${3}/$relfile >> /tmp/music-sync-filelist VerboseOutput 2 "Added: ${3}/${relfile}" fi From f4bf02fd11b0a326036f441c8eb51ecfde682c8e Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Sat, 16 Mar 2019 15:05:11 +0100 Subject: [PATCH 19/25] Added possibility to change bitrate --- music-sync.sh | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/music-sync.sh b/music-sync.sh index 24079a0..267d586 100755 --- a/music-sync.sh +++ b/music-sync.sh @@ -7,6 +7,7 @@ source="-" dest="-" convert=false verbose=2 +bitrate=192 help=false temp="/tmp/converted" convertart=false @@ -58,8 +59,8 @@ CheckDeps() { GetOptions() { # https://stackoverflow.com/a/29754866 - OPTIONS=s:d:t:cav::h - LONGOPTS=source:,dest:,temp:,convert,convert-art,verbose::,help + OPTIONS=s:d:t:cab:v::h + LONGOPTS=source:,dest:,temp:,convert,convert-art,bitrate:,verbose::,help # -use ! and PIPESTATUS to get exit code with errexit set # -temporarily store output to be able to check for errors @@ -111,6 +112,10 @@ GetOptions() { convertart=true shift ;; + -b|--bitrate) + bitrate="$2" + shift 2 + ;; --) shift break @@ -149,6 +154,7 @@ Usage() { echo " -t, --temp The temporary cache for converted files (default: /tmp/converted)" echo " -c, --convert Convert files before syncing" echo " -a, --convert-art Convert album-art before syncing" + echo " -b, --bitrate When converting use this bitrate" echo " -v, --verbose <0-6> Set log level (default: 2)" echo " -h, --help Display this help text" echo "" @@ -243,7 +249,7 @@ ConvertFiles() { mkdir -p "$temp/${line%/*}"; fi; if [[ ! -f "$temp/$line" || "${source}/$line" -nt "$temp/$line" ]]; then - lame -b 192 $source/$line $temp/$line 1>/dev/null 2>/dev/null + lame -b ${bitrate} $source/$line $temp/$line 1>/dev/null 2>/dev/null VerboseOutput 2 "Converted: $line" if [[ $convertart == true ]]; then mkdir "$temp/$line-images/" From b1f47c0b06e4cbc25068677632fc9c0ba5d15788 Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Sat, 16 Mar 2019 15:12:42 +0100 Subject: [PATCH 20/25] If folder already created, do not error. --- music-sync.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/music-sync.sh b/music-sync.sh index 267d586..cf370b9 100755 --- a/music-sync.sh +++ b/music-sync.sh @@ -252,7 +252,7 @@ ConvertFiles() { lame -b ${bitrate} $source/$line $temp/$line 1>/dev/null 2>/dev/null VerboseOutput 2 "Converted: $line" if [[ $convertart == true ]]; then - mkdir "$temp/$line-images/" + mkdir -p "$temp/$line-images/" eyeD3 --write-images "$temp/$line-images/" "$temp/$line" 1>/dev/null 2>/dev/null convert "$temp/$line-images/FRONT_COVER.*" -resize 200x200 "$temp/$line-images/FRONT_COVER.jpg" 1>/dev/null 2>/dev/null eyeD3 --remove-all-images "$temp/$line" 1>/dev/null 2>/dev/null From f6dc32b4ed16ac8e041c40f544d0da327953812c Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Sat, 16 Mar 2019 17:04:47 +0100 Subject: [PATCH 21/25] I don't like unbound variables --- music-sync.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/music-sync.sh b/music-sync.sh index cf370b9..11bb409 100755 --- a/music-sync.sh +++ b/music-sync.sh @@ -127,11 +127,11 @@ GetOptions() { esac done - if [[ ! -z "${1}" ]]; then + if [[ ! -z "${1+x}" ]]; then source="$1" fi - if [[ ! -z "${2}" ]]; then + if [[ ! -z "${2+x}" ]]; then dest="$2" fi From 344efcc38b176296e6f9bd3b62337d755c2c2d9d Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Sat, 16 Mar 2019 17:07:14 +0100 Subject: [PATCH 22/25] Updated docs --- README.MD | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/README.MD b/README.MD index e69d679..57f3756 100644 --- a/README.MD +++ b/README.MD @@ -1,12 +1,12 @@ # Bash Music sync -(c) 2018 Jeroen De Meerleer +(c) 2018-2019 Jeroen De Meerleer Script to sync music from one folder to another ## Usage: ``` -music-sync -s|--source -d|--dest -t|--temp +music-sync -s|--source -d|--dest music-sync Syncronises music from one folder to another. @@ -16,8 +16,9 @@ Options: -d, --dest The destionation folder of the music -t, --temp The temporary cache for converted files (default: /tmp/converted) -c, --convert Convert files before syncing + -a, --convert-art Convert album-art before syncing + -b, --bitrate When converting use this bitrate -v, --verbose <0-6> Set log level (default: 2) - -a, --convert-art Convert files before syncing -h, --help Display this help text Log levels: @@ -39,7 +40,7 @@ Exit Codes: ``` ## Licence -Copyright 2018 Jeroen De Meerleer +Copyright 2018-2019 Jeroen De Meerleer Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: From b6cf8005e464ae88c9e02f43f95da79f47de4ed1 Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Sat, 16 Mar 2019 17:32:39 +0100 Subject: [PATCH 23/25] Added variable width for coverart --- music-sync.sh | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/music-sync.sh b/music-sync.sh index 11bb409..ca25d17 100755 --- a/music-sync.sh +++ b/music-sync.sh @@ -11,6 +11,7 @@ bitrate=192 help=false temp="/tmp/converted" convertart=false +coverartsize=200 CheckDeps() { if [[ $1 == 0 ]]; then @@ -59,8 +60,8 @@ CheckDeps() { GetOptions() { # https://stackoverflow.com/a/29754866 - OPTIONS=s:d:t:cab:v::h - LONGOPTS=source:,dest:,temp:,convert,convert-art,bitrate:,verbose::,help + OPTIONS=s:d:t:ca::b:v::h + LONGOPTS=source:,dest:,temp:,convert,convert-art::,bitrate:,verbose::,help # -use ! and PIPESTATUS to get exit code with errexit set # -temporarily store output to be able to check for errors @@ -110,6 +111,11 @@ GetOptions() { ;; -a|--convert-art) convertart=true + coverartsize=200 + if [[ $2 != "" ]]; then + coverartsize=${2} + shift + fi shift ;; -b|--bitrate) @@ -149,14 +155,14 @@ Usage() { echo "Syncronises music from one folder to another." echo "" echo "Options:" - echo " -s, --source The source folder of the music" - echo " -d, --dest The destionation folder of the music" - echo " -t, --temp The temporary cache for converted files (default: /tmp/converted)" - echo " -c, --convert Convert files before syncing" - echo " -a, --convert-art Convert album-art before syncing" - echo " -b, --bitrate When converting use this bitrate" - echo " -v, --verbose <0-6> Set log level (default: 2)" - echo " -h, --help Display this help text" + echo " -s, --source The source folder of the music" + echo " -d, --dest The destionation folder of the music" + echo " -t, --temp The temporary cache for converted files (default: /tmp/converted)" + echo " -c, --convert Convert files before syncing" + echo " -a, --convert-art Convert album-art before syncing (default width: 200)" + echo " -b, --bitrate When converting use this bitrate" + echo " -v, --verbose <0-6> Set log level (default: 2)" + echo " -h, --help Display this help text" echo "" echo "Log levels:" echo " 0 | Verbose" @@ -254,7 +260,7 @@ ConvertFiles() { if [[ $convertart == true ]]; then mkdir -p "$temp/$line-images/" eyeD3 --write-images "$temp/$line-images/" "$temp/$line" 1>/dev/null 2>/dev/null - convert "$temp/$line-images/FRONT_COVER.*" -resize 200x200 "$temp/$line-images/FRONT_COVER.jpg" 1>/dev/null 2>/dev/null + 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 eyeD3 --add-image "$temp/$line-images/FRONT_COVER.jpg:FRONT_COVER" "$temp/$line" 1>/dev/null 2>/dev/null VerboseOutput 2 "Converted cover art: $line" From 25aa38222bf767c69e6524b736d2cc975876735a Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Sat, 16 Mar 2019 17:33:56 +0100 Subject: [PATCH 24/25] Updated docs --- README.MD | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/README.MD b/README.MD index 57f3756..e91d272 100644 --- a/README.MD +++ b/README.MD @@ -12,14 +12,14 @@ music-sync Syncronises music from one folder to another. Options: - -s, --source The source folder of the music - -d, --dest The destionation folder of the music - -t, --temp The temporary cache for converted files (default: /tmp/converted) - -c, --convert Convert files before syncing - -a, --convert-art Convert album-art before syncing - -b, --bitrate When converting use this bitrate - -v, --verbose <0-6> Set log level (default: 2) - -h, --help Display this help text + -s, --source The source folder of the music + -d, --dest The destionation folder of the music + -t, --temp The temporary cache for converted files (default: /tmp/converted) + -c, --convert Convert files before syncing + -a, --convert-art Convert album-art before syncing (default width: 200) + -b, --bitrate When converting use this bitrate + -v, --verbose <0-6> Set log level (default: 2) + -h, --help Display this help text Log levels: 0 | Verbose From f44bfd0ef97087422fc83016af0ab00270aa2117 Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Sat, 16 Mar 2019 17:51:57 +0100 Subject: [PATCH 25/25] Changed naming of options to and simplified parameters --- README.MD | 7 +++---- music-sync.sh | 22 +++++++++++----------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/README.MD b/README.MD index e91d272..cb0869a 100644 --- a/README.MD +++ b/README.MD @@ -13,11 +13,10 @@ Syncronises music from one folder to another. Options: -s, --source The source folder of the music - -d, --dest The destionation folder of the music + -d, --dest The destination folder of the music -t, --temp The temporary cache for converted files (default: /tmp/converted) - -c, --convert Convert files before syncing - -a, --convert-art Convert album-art before syncing (default width: 200) - -b, --bitrate When converting use this bitrate + -c, --convert Convert files to a given bitrate in kbps before syncing (default: 192) + -a, --resize-art Resize album-art before syncing (default width: 200) -v, --verbose <0-6> Set log level (default: 2) -h, --help Display this help text diff --git a/music-sync.sh b/music-sync.sh index ca25d17..0b71f64 100755 --- a/music-sync.sh +++ b/music-sync.sh @@ -60,8 +60,8 @@ CheckDeps() { GetOptions() { # https://stackoverflow.com/a/29754866 - OPTIONS=s:d:t:ca::b:v::h - LONGOPTS=source:,dest:,temp:,convert,convert-art::,bitrate:,verbose::,help + OPTIONS=s:d:t:c::a::v::h + LONGOPTS=source:,dest:,temp:,convert::,resize-art::,verbose::,help # -use ! and PIPESTATUS to get exit code with errexit set # -temporarily store output to be able to check for errors @@ -107,9 +107,14 @@ GetOptions() { ;; -c|--convert) convert=true + bitrate=192 + if [[ $2 != "" ]]; then + bitrate=${2} + shift + fi shift ;; - -a|--convert-art) + -a|--resize-art) convertart=true coverartsize=200 if [[ $2 != "" ]]; then @@ -118,10 +123,6 @@ GetOptions() { fi shift ;; - -b|--bitrate) - bitrate="$2" - shift 2 - ;; --) shift break @@ -156,11 +157,10 @@ Usage() { echo "" echo "Options:" echo " -s, --source The source folder of the music" - echo " -d, --dest The destionation folder of the music" + echo " -d, --dest The destination folder of the music" echo " -t, --temp The temporary cache for converted files (default: /tmp/converted)" - echo " -c, --convert Convert files before syncing" - echo " -a, --convert-art Convert album-art before syncing (default width: 200)" - echo " -b, --bitrate When converting use this bitrate" + echo " -c, --convert Convert files to a given bitrate in kbps before syncing (default: 192)" + echo " -a, --resize-art Resize album-art before syncing (default width: 200)" echo " -v, --verbose <0-6> Set log level (default: 2)" echo " -h, --help Display this help text" echo ""