NAS Backup & Convert

For reasons that are FAR too long I have decided to convert all my WAV files to flac on my Synology NAS. Additionally, I want any new WAV files I add to my wav directory structure to be identified and converted without my manual intervention. This means that I need a script which I will then call nightly using the Synology Task Scheduler.

The script needs to:

ID the files to be converted;
Convert them to a new location;
Copy across any jpg and pdf files associated with the WAV files.

All using some simple some simple BASH coding.

Instructions:
On your PC create a text file and save it as SomeFileName.bat the extension is not important, but by using bat you will know it is a programmatic file.

At the top of the file type:
#!/bin/sh
echo “Starting SomeFileName.bat”

The echo simply gives some feedback which you can put into a log file.

Now comes the line that does the work:
find /volume1/Music/ -type f -iname “.wav" -print | while IFS= read file; do mkdir -p /volume1/flac"dirname "$file"" && ffmpeg -n -loglevel 16 -i “$file” -compression_level 0 /volume1/flac/"${file%.}.flac” < /dev/null; done

Here ‘find’ is creating a list of files in the Music directory looking for files that have the extension of wav.
Below I give an example of a line to do file backup, that example includes the flag, -mtime -25. That limits the files found to recent files. This can be added here once you have done the bulk convert.
The identified files are passed into a ‘while’ loop one at a time.
The directory name is taken from the passed in $file variable and a directory created at the target location if needed.
The file is then converted IF it doesn’t already exist, the -n flag in ffmpeg.
ffmpeg is used to create the flac file using the lowest level of compression.

ffmpeg is available for loading onto the synology as a package. Google is your friend.

Now to copy the jpg and pdf files, same instruction with variations, I also include a line to pick up any flac files that I had been using and mistakenly not converted to wav:
find /volume1/Music -type f -iname “.flac" -print | while read file; do mkdir -p /volume1/flac"dirname "$file"" && cp -a “$file” /volume1/flac"$file"; done
find /volume1/Music -type f -iname "
.jpg” -print | while read file; do cp -a “$file” /volume1/flac"$file"; done
find /volume1/Music -type f -iname “*.pdf” -print | while read file; do cp -a “$file” /volume1/flac"$file"; done

Now save the file and place it on the NAS, you will need to set it up as an executable file to run at a scheduled time via Task Scheduler.

Copy Music for Backup:
find /volume1/Music -type f -mtime -25 -print | while read file; do mkdir -p /volumeUSB1/usbshare/BU"dirname "$file"" && cp -a -n “$file” /volumeUSB1/usbshare/BU"$file"; done

Here ‘find’ is creating a list of files in the Music directory looking for files that are recent, -mtime -25.
These files are passed into a ‘while’ loop one at a time.
The directory name is taken from the passed in $file variable and a directory created at the target location if needed, in my case an external USB HDD.
The file is then copied if it doesn’t already exist.

Hope someone finds this useful.

M

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.