flac2mp3 : a mass flac to mp3 converter bash script for synology and ubuntu

Read the disclaimer

This bash script searches recursively directories and converts .flac to mp3.
A text database keeps an inventory of converted files, enabling the script to run on a schedule.
The original file tree structure is mirrored to the converted file tree structure.

You can get the script here : https://github.com/youpla/bash_flac2mp3

Since DSM 6, synology provides a bash implementation that is close to what you would find on Linux systems, enabling easily bash code porting.
This script uses different compiled tools to achieve its goal.

There are GUI tools that manage flac files conversion.
Most of those tools achieve what this script does not, they are multi-threaded, optimized for speed and highly customizable.
flac2mp3 on the other hand is more of a slow background process you can run on a synology or linux shell.

The script has been tested on Synology and Ubuntu (18).

Prerequisities

In order to work, this script need the following tools to perform the different tasks

flac to mp3 synology ubuntu dsm

  • metaflac (flac)
  • cpulimit
  • lame
  • imagemagick (is builtin in synology)
  • ffmpeg (is builtin in synology)
  • id3

Synology

You can compile those tools following those different walk through:

Ubuntu

Depending on the version, some tools may already be installed

Run from a terminal :
sudo apt install cpulimit flac imagemagick

id3: pre-compiled debian/ubuntu binaries can be downloaded here : https://squell.github.io/id3/

awk : the gawk implementation is used

Customization

The script is piloted by a settings file named "init.txt"

There is no " quotation mark to use in init.txt , even if one of your path contains a space character.

The .txt extension is being preferred to allow the use of the built in synology gui text editor.


flac to mp3 synology ubuntu dsm


Please note the file format : utf-8

Settings

  • pathflacsource: full source path, no relative path
  • pathmp3dest: full destination path, no relative path
  • pathexclus-x : optional path exclusion, can be deleted
  • coverlist : cover file names, case insensitive, can be deleted
  • coverfolderlist : cover folder names, case insensitive, can be deleted
  • filethumbnail : thumbnail file name to be added to the destination folder 
  • waittime : comparison delay, can be deleted
  • valcpulimit : cpulimit threshold
  • mp3quality: default is 0, can be deleted
  • picturesize : default is 200x200, can be deleted
  • volume : default is 0, can be deleted
  • smbpattern-x smbreplace-x: is a set of pattern to replace unix file name by windows file names, can be deleted

More details

pathexclus-x  :
this is a setting that removes the paths starting with a pattern
  • source path : /home/userprofile/demo
    • subdirs:
      • /home/userprofile/demo/household-member-1
      • /home/userprofile/demo/household-member-2
  • pathexclus-1=household-member-2
  • result:
    • only "/home/userprofile/demo/household-member-1" is processed
coverlist :
Is a list of file names that can be used as a front cover for the mp3 file.
The search order is:
  • flac file root (folder containing the flac file)
  • then coverfolderlist (folders)
  • then the flac file root folder recursively
  • as soon a match is found, the search stops
    • this means 
      • the order of coverlist matters
      • the order of coverfolderlist matters
coverfolderlist :
Are the subfolders to be searched within the folder containing the flac file. Can be deleted. 

filethumbnail :
Has a default value of "Folder.jpg" if omitted.
Folder.jpg is only added if a cover file is found

waittime :
Before processing any file, the script checks if the file size is the same over a short elapsed time. This parameter provides the time gap between the first and the second check.
It can be set to 0.
If unset the waittime is 5 seconds

mp3quality:
A value from 0 to 6 as decribed here :
http://wiki.hydrogenaud.io/index.php?title=LAME#Recommended_encoder_settings
Value is 0 if omitted.

picturesize :
Embedded picture size.
Default size 200 if omitted.
Note: the original picture shape is preserved, even if the limiting shape is a square.

volume:
value to modify the volume, you can use the value documented in https://trac.ffmpeg.org/wiki/AudioVolume
Default is 0 if omitted.
smbpattern-x smbreplace-x :
Allows to change the file name in order to be compatible with windows systems and windows smb shares.
It will only replace the incompatible characters in the file name. The file tags will remain the same.
You can specify up to 6 patterns, based on the following list:

  • <
  • >
  • :
  • \
  • |
  • ?
Pattern grouping is possible.


Note: the double \\ in the sample "init.txt" file is not a typo. (see : https://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_03.html )

converter:
Allows you to specify avconv. At some point avconv was distributed with older versions of ubuntu.
Can be omitted, default value: ffmpeg

Running the script on synology.


Steps:
  • Compile and install all the prerequisites
  • Create a directory for your script. You can create the directory in the web gui.
  • Then in the shell navigate to it : /volume1/yourdir
  • download the script
    • curl https://raw.githubusercontent.com/youpla/bash_flac2mp3/master/flac2mp3.sh -O
    • curl https://raw.githubusercontent.com/youpla/bash_flac2mp3/master/init.txt -O
  • check if the script is executable with : ls -al flac2mp3.sh
    • has to be (at least)  : -r--r--r-x 
    • where x is the executable for your owner / group / user
    • user should be executable
  • to render it executable (if it is not) use : chmod u+x flac2mp3.sh
  • adapt the init.txt file (see previous section)
  • run the script ./flac2mp3.sh

Script detail

File tree

Source

flac to mp3 synology ubuntu dsm

Destination


flac to mp3 synology ubuntu dsm
  • Source variable : pathflacsource=/home/userprofile/demo
    • example:
      /home/userprofile/demo/household-member-1/artist/album
  • Destination variable : pathmp3dest=/home/userprofile/tmp/mp3
    • example:
      /home/userprofile/tmp/mp3/household-member-1/artist/album

Database

Located : 
  • scriptdir/db/base.txt
Each processed file is written in the database.
Only the short path is written in the database. The shortpath consist of the full path minus the pathflacsourcevariable
  • full path : /home/userprofile/demo/household-member-1/artist/album/file.flac
  • source : /home/userprofile/demo
  • short path : /household-member-1/artist/album/file.flac
If the short path already exist within the database, the script ignores the flac file.
The database is a text file and can be edited. You can delete an entry in order to reprocess a file.

Directories

The script creates 3 sub-directories
  • db : stores the text databases
  • tmp : stores the temp files
  • log : stores the log file

Code

There are two main functions
  • fileprocessing
    • lists all .flac files in pathflacsourcevariable 
    • substracts the files already listed in base.txt
    • processes the delta list
  • processingcore
    • extract tags
    • extract picture
    • resize picture
    • convert to mp3
    • add folder thumbnail
    • changes smb names

Notes

lame has been prefered to ffmpeg for mp3 compression.
  • https://linux.die.net/man/1/lame 
    • provides the -q settings
  • https://trac.ffmpeg.org/wiki/Encode/MP3
    • I couldn't find the -q setting
This is about providing a tool that can suit all quality expectation.




Popular posts from this blog

cuesplitflac : a mass flac splitting script for synology and ubuntu

Compiling "file" with spksrc for synology NAS

Compiling cpulimit with spksrc for synology NAS