Steenderen.NET

Thuisbasis en digitale zandbak

Script: Converteren naar MP3 – Versie 1.1

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

Verbeterde versie van het originele script. Als de originele folder niet alleen FLAC-bestanden bevat, maar ook MP3 bestanden, dan worden deze gekopieerd naar de MP3 folder. Verder meer informatie in de notificatie e-mail en het logbook. En tot slot ook nog een notificatie in DSM toegevoegd.

Code

# Script to convert FLAC-files in one folder to MP3 in another folder, or copy the file if the original file is already MP3.
# Install FFMPEG on Synology NAS via package from https://synocommunity.com/package/ffmpeg

IFS="`printf '\n\t'`"     # Remove 'space', so filenames with spaces work well.

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_COUNTER="Totaal aantal geconverteerde bestanden: "
MAIL_STARTTIME="Starttijd: "
MAIL_TIMER="Duur: "
MAIL_REPORT="Bekijk de details in het conversie-rapport: "
MAIL_SIGNATURE="Met vriendelijke groeten,\nSynology DiskStation"

TIME_STAMP=`date`                                                                                    # Time stamp to store starting time
START_TIME=`date +%s`                                                                                # Start time to calculate total processing time
REPORT_FILE=$(cd -P -- "$(dirname -- "$0")" && pwd -P)"/convert_mp3_"$(date +%Y-%m-%d)."txt"        # Define report filename

ECHO_OUT="The following MP3-files were created:"
COUNTER=0

cd $FLAC_MAP
for a in `find . -name '*.flac' -print -o -name '*.mp3' -print | sort`; do  
    if [ -e "$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.
                if [ ${a: -5} == ".flac" ] ; then
                    OUTF=${a%.flac}.mp3                                                             # Generate name of MP3-file
                    OUTF="${OUTF:2}"                                                                # Remove trailing ./ from the path
                    
                    if [[ ! -f "$MP3S_MAP/$OUTF" ]]; then                                            # Check if MP3-file does not already exists (if so, no conversion needed)
                        path=$( echo ${OUTF%/*} )
                        file=$( echo ${OUTF##/*/} )
        
                        if [[ ! -e $MP3S_MAP/$path ]]; then
                            mkdir -p $MP3S_MAP/$path                                                # Create folder if it does not yet exist in MP3-folder.
                        fi
                        echo "Converting ${a:2}"                                                    # Show which FLAC-file is being converted to MP3
                        ffmpeg -loglevel warning -i $a -acodec libmp3lame -q:a 1 "$MP3S_MAP/$file"    # Convert to MP3-file with quality Q:A 1 setting
                        ECHO_OUT+=
\n'"- $MP3S_MAP/$file (converted FLAC-file)"                    # Add MP3-filename to logging
                        COUNTER=$((COUNTER+1))
                    fi
                elif [ ${a: -4} == ".mp3" ] ; then
                    if [[ ! -f "$MP3S_MAP/${a:2}" ]]; then                                            # Check if MP3-file does not already exists (if so, no conversion needed)
                        path=$( echo ${a%/*} )
                        if [[ ! -e $MP3S_MAP/$path ]]; then
                            mkdir -p $MP3S_MAP/$path                                                # Create folder if it does not yet exist in MP3-folder.
                        fi
                        echo "Copying ${a:2}"
                        cp $FLAC_MAP/${a:2} $MP3S_MAP/${a:2}                                        # Copy MP3-file, no need to convert if already MP3
                        ECHO_OUT+=
\n'"- $MP3S_MAP/${a:2} (copied MP3-file)"
                        COUNTER=$((COUNTER+1))
                     fi
                fi
            fi
        fi
    fi
done

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)) `"

if test "$ECHO_OUT" != "The following MP3-files were created:"; then
    echo $"$ECHO_OUT" 
\n\nTotal: '$COUNTER' file(s)\nStarting time: '$(date +"%Y-%m-%d %H:%M" -d "$TIME_STAMP")'\nTotal processing time: '$PROCESS_TIME >> $REPORT_FILE
    /usr/bin/php -r 'mail("'$MAIL_TO'", "'$MAIL_SUBJECT'", "'$MAIL_BODY'\n\n'$MAIL_COUNTER$COUNTER'\n'$MAIL_STARTTIME$(date +"%Y-%m-%d %H:%M" -d "$TIME_STAMP")'\n'$MAIL_TIMER$PROCESS_TIME'\n\n'$MAIL_REPORT$REPORT_FILE'\n\n'$MAIL_SIGNATURE'", "From: '$MAIL_FROM'");';
    /usr/syno/bin/synologset1 sys info 0x11100000 "Finished MP3 conversion script. Total number of converted files: "$COUNTER". Total processing time: "$PROCESS_TIME". See all details in the conversion-report: "$REPORT_FILE;
else
    echo $"No files needed to be converted or copied.";
    /usr/syno/bin/synologset1 sys info 0x11100000 "Finished MP3 conversion script. No files needed to be converted or copied. Total processing time: "$PROCESS_TIME;
fi

Verder Bericht

Vorige Bericht

Laat een reactie achter

Deze website gebruikt Akismet om spam te verminderen. Bekijk hoe je reactie-gegevens worden verwerkt.

© 2019 Steenderen.NET

Thema door Anders Norén