Status: Dit is de laatste versie van dit script.
Beschrijving
Script voor het converteren van je hoge kwaliteit muziekbibliotheek (mogen zowel FLAC en MP3 bestanden zijn) naar een lagere kwaliteit (MP3 met variable bitrate). Bestaande MP3 bestanden worden alleen gekopieerd, niet geconverteerd.
Persoonlijk gebruik ik dit om mijn volledige muziekbibliotheek (bestaat origineel voor 99% uit FLAC bestanden) te kunnen syncen met de harde schijf in de auto en naar de SD-kaart in mijn telefoon (voor beiden is deze geluidskwaliteit ruim voldoende).
Update
De nodige opschoningen gedaan in de code, tevens wat andere scenario’s verwerkt, waardoor e.e.a. consistenter werkt in geval van geupdate’te bron-bestanden. Daarnaast mailen expliciet met PHP7.4 gemaakt (moet eventueel nog als package geinstalleerd worden als dat nog niet is gedaan), zodat de mails die verstuurd worden met PHP8 niet goed geformateerd worden.
Code
# Steenderen.NET
# NAS-script: Converteren naar MP3 - Versie 1.3
# Dependency 1: FFMPEG from SynoCommunity, see https://synocommunity.com/#easy-install for details.
# Dependency 2: PHP7.4 must be installed, PHP8.0 not yet supported because of mail misformatting.
IFS="`printf '\n\t'`"
# Setup
FLAC_MAP="/volume1/ music" # Folder with original FLAC-files
MP3S_MAP="/volume1/MP3s" # Folder to store the converted MP3-files
# Notification mail variables
MAIL_FROM="mail@provider.com"
MAIL_TO="mail@provider.com"
MAIL_SUBJECT="Synology NAS: MP3 conversie is voltooid"
MAIL_BODY="Beste gebruiker,\n\nDe conversie van FLAC-bestanden naar MP3-bestanden is voltooid."
MAIL_CREATE_COUNTER="Totaal aantal geconverteerde of gekopieerde bestanden: "
MAIL_DELETE_COUNTER="Totaal aantal verwijderde bestanden: "
MAIL_STARTTIME="Starttijd: "
MAIL_TIMER="Duur: "
MAIL_REPORT="Bekijk de details in het conversie-rapport: "
MAIL_SIGNATURE="Met vriendelijke groeten,\nSynology DiskStation"
FFMPEG_MAP="/volume1/@appstore/ffmpeg/ # For DSM 7 use /volume1/@appstore/ffmpeg/bin, for versions below use /usr/local/ffmpeg/bin
# No need to edit below here.
echo "Start running MP3 conversion script."
/usr/syno/bin/synologset1 sys info 0x11100000 "MP3 conversion: Start MP3 conversion from "$(cd -P -- "$(dirname -- "$0")" && pwd -P)"/convert_mp3.sh"
TIME_STAMP_START=`date` # Time stamp to store starting time
START_TIME=`date +%s` # Start time to calculate total processing time
REPORT_MAP=$(cd -P -- "$(dirname -- "$0")" && pwd -P)"/Reports"
if [[ ! -e $REPORT_MAP ]]; then
mkdir -p $REPORT_MAP # Create report-folder if it does not yet exist
fi
REPORT_FILE=$REPORT_MAP"/convert_mp3_"$(date +%Y-%m-%d)."txt" # Define report filename
ECHO_CREATE_OUT="The following files were (re)created:"
CREATE_COUNTER=0
ECHO_DELETE_OUT="The following files were deleted as the original file does no longer exist:"
DELETE_COUNTER=0
cd $FLAC_MAP
for a in `find . -name '*.flac' -print -o -name '*.mp3' -print -o -name 'folder.jpg' -print | sort`; do
if [ -f "$a" ] ; then # Check if string from find is a file (should be the case)
if [[ ${a} != *"#recycle"* ]]; then # Skip files in the recycle bin, probably not deleted for nothing ;-)
if [[ ${a} != *"@eaDir"* ]]; then # Ignore @eaDir, this are folders with thumbnails & metadata.
FILE_FOUND="${a:2}" # Remove trailing ./ from the path of the found file
if [ ${FILE_FOUND: -5} == ".flac" ]; then # Check if the original file is a FLAC-file (conversion to MP3 is needed).
FILE_CONVERTED=${FILE_FOUND%.flac}.mp3 # Generate name of MP3-file
if [[ ! -f "$MP3S_MAP/$FILE_CONVERTED" ]]; then # Check if MP3-file does not already exists (if so, no conversion needed)
FILE_PATH=$( echo ${FILE_FOUND%/*} )
if [[ ! -e $MP3S_MAP/$FILE_PATH ]]; then
mkdir -p $MP3S_MAP/$FILE_PATH # Create folder if it does not yet exist in MP3-folder.
fi
echo "- Converting $FILE_FOUND" # Show which FLAC-file is being converted to MP3
$FFMPEG_MAP/ffmpeg -loglevel warning -i $FILE_FOUND -map 0:a -acodec libmp3lame -q:a 1 "$MP3S_MAP/$FILE_CONVERTED" # Convert to MP3-file with quality Q:A 1 setting
ECHO_CREATE_OUT+=$'\n'"- $MP3S_MAP/$FILE_CONVERTED (converted FLAC-file)" # Add MP3-filename to logging
CREATE_COUNTER=$((CREATE_COUNTER+1))
elif [[ $FLAC_MAP/$FILE_FOUND -nt $MP3S_MAP/$FILE_CONVERTED ]]; then # Check if the MP3-file is older than the FLAC-file (FLAC updated after last conversion)
echo "- Converting new file-version of ${a:2}" # Newer version of FLAC file found, so MP3 file is created and overwrites (-y argument)
$FFMPEG_MAP/ffmpeg -y -loglevel warning -i $FILE_FOUND -map 0:a -acodec libmp3lame -q:a 1 "$MP3S_MAP/$FILE_CONVERTED" # Convert to MP3-file with quality Q:A 1 setting
ECHO_CREATE_OUT+=$'\n'"- $MP3S_MAP/$FILE_CONVERTED (converted new version of FLAC-file)" # Add MP3-filename to logging
CREATE_COUNTER=$((CREATE_COUNTER+1))
fi
elif [ ${FILE_FOUND: -4} == ".mp3" ] ; then # Check if the original file is an MP3-file (no conversion needed, copy instead).
if [[ ! -f "$MP3S_MAP/$FILE_FOUND" ]]; then # Check if MP3-file does not already exists.
FILE_PATH=$( echo ${FILE_FOUND%/*} )
if [[ ! -e $MP3S_MAP/$FILE_PATH ]]; then
mkdir -p $MP3S_MAP/$FILE_PATH # Create folder if it does not yet exist in MP3-folder.
fi
echo "- Copying $FILE_FOUND"
cp $FLAC_MAP/$FILE_FOUND $MP3S_MAP/$FILE_FOUND # Copy MP3-file, no need to convert if already MP3
ECHO_CREATE_OUT+=$'\n'"- $MP3S_MAP/$FILE_FOUND (copied MP3-file)"
CREATE_COUNTER=$((CREATE_COUNTER+1))
elif [[ $FLAC_MAP/$FILE_FOUND -nt $MP3S_MAP/$FILE_FOUND ]]; then # Check if the MP3-file is older than the original MP3-file (updated after last conversion)
echo "- Copying new file-version of $FILE_FOUND"
cp $FLAC_MAP/$FILE_FOUND $MP3S_MAP/$FILE_FOUND # Copy MP3-file, no need to convert if already MP3
ECHO_CREATE_OUT+=$'\n'"- $MP3S_MAP/$FILE_FOUND (copied new version of MP3-file)"
CREATE_COUNTER=$((CREATE_COUNTER+1))
fi
elif [ ${FILE_FOUND: -10} == "folder.jpg" ] ; then
if [[ ! -f "$MP3S_MAP/$FILE_FOUND" ]] || [[ $FLAC_MAP/$FILE_FOUND -nt $MP3S_MAP/$FILE_FOUND ]]; then # Check if folder.jpg does not already exists or if folder.jpg is newer in FLAC-folder
FILE_PATH=$( echo ${FILE_FOUND%/*} )
if [[ ! -e $MP3S_MAP/$FILE_PATH ]]; then
mkdir -p $MP3S_MAP/$FILE_PATH # Create folder if it does not yet exist in MP3-folder.
fi
echo "- Copying $FILE_FOUND"
cp $FLAC_MAP/$FILE_FOUND $MP3S_MAP/$FILE_FOUND # Copy folder.jpg.
ECHO_CREATE_OUT+=$'\n'"- $MP3S_MAP/$FILE_FOUND (copied album art)"
CREATE_COUNTER=$((CREATE_COUNTER+1))
fi
fi
fi
fi
fi
done
cd $MP3S_MAP
for b in `find . -name '*' -print | sort`; do
if [ -f "$b" ] ; then # Check if string from find is a file (should be the case)
if [[ ${b} != *"#recycle"* ]]; then # Skip files in the recycle bin, probably not deleted for nothing ;-)
if [[ ${b} != *"@eaDir"* ]]; then # Ignore @eaDir, this are folders with thumbnails & metadata.
FILE_FOUND="${b:2}" # Remove trailing ./ from the path of the found file
if [ ${FILE_FOUND: -4} == ".mp3" ]; then # Check if the found file is an MP3 file
POSSIBLE_ORIGINAL=${FILE_FOUND%.mp3}.flac # Generate the possible name of the original FLAC-file
if [[ ! -f "$FLAC_MAP/$POSSIBLE_ORIGINAL" ]]; then # Check if the original FLAC file (based on possible name) does not exist
if [[ ! -f "$FLAC_MAP/$FILE_FOUND" ]]; then # Check if there is also no original MP3 version of the file does
echo "- Deleting $MP3S_MAP/$FILE_FOUND"
rm -f $MP3S_MAP/$FILE_FOUND # Delete de found MP3 file, as no original file was found in FLAC-folder
ECHO_DELETE_OUT+=$'\n'"- $MP3S_MAP/$FILE_FOUND"
DELETE_COUNTER=$((DELETE_COUNTER+1))
fi
fi
elif [ ${FILE_FOUND: -10} == "folder.jpg" ] ; then # Check if the found file is the folder.jpg file
if [[ ! -f "$FLAC_MAP/$FILE_FOUND" ]]; then # Check if the original folder.jpg file does not exist
echo "- Deleting $MP3S_MAP/$FILE_FOUND"
rm -f $MP3S_MAP/$FILE_FOUND # Delete the found folder.jpg file, as no original file was found in the FLAC-folder
ECHO_DELETE_OUT+=$'\n'"- $MP3S_MAP/$FILE_FOUND"
DELETE_COUNTER=$((DELETE_COUNTER+1))
fi
else # All other files (not MP3, not folder.jpg) will be deleted to keep the folder clean
echo "- Deleting $MP3S_MAP/$FILE_FOUND"
rm -f $MP3S_MAP/$FILE_FOUND # Delete the found file
ECHO_DELETE_OUT+=$'\n'"- $MP3S_MAP/$FILE_FOUND"
DELETE_COUNTER=$((DELETE_COUNTER+1))
fi
fi
fi
fi
done
if test "$ECHO_CREATE_OUT" != "The following files were (re)created:" || test "$ECHO_DELETE_OUT" != "The following files were deleted as the original file does no longer exist:"; then
echo ""
fi
echo "- Deleting all empty folders"
find . -type d -empty -delete # All empy folders will be deleted to keep the folder clean
TIME_STAMP_END=`date`
END_TIME=`date +%s` # End time to calculate total processing time
PROCESS_SECS=$(( $END_TIME - $START_TIME )) # Calculate total processing time in seconds
PROCESS_TIME="`printf '%02dh:%02dm:%02ds\n' $(($PROCESS_SECS/3600)) $(($PROCESS_SECS%3600/60)) $(($PROCESS_SECS%60)) `"
# Check if any converting, copying or deleting was done.
if test "$ECHO_CREATE_OUT" != "The following files were (re)created:" || test "$ECHO_DELETE_OUT" != "The following files were deleted as the original file does no longer exist:"; then
if test "$ECHO_CREATE_OUT" != "The following files were (re)created:"; then
echo $"$ECHO_CREATE_OUT" >> $REPORT_FILE # Add report-out of created files to generated report file
fi
if test "$ECHO_DELETE_OUT" != "The following files were deleted as the original file does no longer exist:"; then
if test "$ECHO_CREATE_OUT" != "The following files were deleted:"; then
echo "" >> $REPORT_FILE # Add extra empty line between sections of add files and deleted files in report
fi
echo $"$ECHO_DELETE_OUT" >> $REPORT_FILE # Add report-out of deleted files to generated report file
fi
# Add summary to the report file
echo "" >> $REPORT_FILE
echo "Summary:" >> $REPORT_FILE
echo $"Converted or copied: "$CREATE_COUNTER" file(s)" >> $REPORT_FILE
echo $"Deleted: "$DELETE_COUNTER" file(s)" >> $REPORT_FILE
echo $"Start time: "$(date +"%Y-%m-%d %H:%M" -d "$TIME_STAMP_START") >> $REPORT_FILE
echo $"End time: "$(date +"%Y-%m-%d %H:%M" -d "$TIME_STAMP_END") >> $REPORT_FILE
echo $"Total processing time: "$PROCESS_TIME >> $REPORT_FILE
echo ""
echo "- Converted or copied: $CREATE_COUNTER file(s)"
echo "- Deleted: $DELETE_COUNTER file(s)"
echo "- Start time: "$(date +"%Y-%m-%d %H:%M" -d "$TIME_STAMP_START")
echo "- End time: "$(date +"%Y-%m-%d %H:%M" -d "$TIME_STAMP_END")
echo "- Total processing time: $PROCESS_TIME"
echo "- Conversion report: $REPORT_FILE"
echo ""
echo "- Sending summary to $MAIL_TO"
# Send mail with summary info and link to report with all details. Prefix texts are defined on top of this script.
php74 -r 'mail("'$MAIL_TO'", "'$MAIL_SUBJECT'", "'$MAIL_BODY'\n\n'$MAIL_CREATE_COUNTER$CREATE_COUNTER'\n'$MAIL_DELETE_COUNTER$DELETE_COUNTER'\n'$MAIL_STARTTIME$(date +"%Y-%m-%d %H:%M" -d "$TIME_STAMP_START")'\n'$MAIL_TIMER$PROCESS_TIME'\n\n'$MAIL_REPORT$REPORT_FILE'\n\n'$MAIL_SIGNATURE'", "From: '$MAIL_FROM'");';
# Add log to Synology logbook with summary info
/usr/syno/bin/synologset1 sys info 0x11100000 "MP3 conversion: Finished MP3 conversion, number of converted files: "$CREATE_COUNTER", number of deleted files: "$DELETE_COUNTER", total processing time: "$PROCESS_TIME". See all details in the conversion-report: "$REPORT_FILE;
else
# No converting, copying or deleting was done.
echo "- No files needed to be converted, copied or deleted."
echo "- Total processing time: $PROCESS_TIME"
# Add log to Synology logbook with processing time.
/usr/syno/bin/synologset1 sys info 0x11100000 "MP3 conversion: Finished MP3 conversion, no files needed to be (re)converted, copied or deleted. Total processing time: "$PROCESS_TIME;
fi
# Done!
echo "Done running MP3 conversion script."