Saturday, March 14, 2015

Batch convert mp3 audio to open source Ogg Vorbis using avconv (aka ffmpeg)

This is the single command I created to batch convert a library of mp3 audio to Ogg Vorbis. This can be used in bash in Ubuntu. It should also work on OSX, possibly with some minor modification. On Windows, I would recommend tweaking this to work in Powershell.
find . -type f -name '*.mp3' | parallel avconv -i {} -threads auto -c:a libvorbis -qscale 6 -map_metadata 0 -vn {.}.ogg; find . -type f -name '*.mp3' -exec bash -c 'touch -r "$0" "${0/%mp3/ogg}"' '{}' \; && find . -type f -name '*.mp3' -delete && find -name "*:*" -type d | rename 's/:/_/g' && find -name "*:*" -type f | rename 's/:/_/g' && echo complete
Now, let's break it down.
 find . -type f -name '*.mp3' |
"find" will identify all files matching the expression, which in this case is anything with the *.mp3 file extension. "find" is recursive, meaning it will search through subdirectories. So, if you run the command at the top of your music directory, it will search through all of your subfolders for artists and albums. Another strategy I've seen is to use a loop, but in my opinion, it's not as quick. The pipe "|" will send those matching files to the next part of the command.
 parallel
Parallel will allocate multiple conversion jobs across the available threads on your CPU. Essentially, this makes sure we are using the maximum available power of your CPU. This will massively improve the efficiency of this task.
avconv -i {} -threads auto -c:a libvorbis -qscale 6 -map_metadata 0 -vn {.}.ogg;
Avconv is an equivalent of ffmpeg. For most purposes, any documentation you find on ffmpeg applies to avconv, and vice versa. "-i {}" will take the "find" output through the pipe "|." "-qscale 6" designates a constant quality with a variable bit rate. This will generally be around 256kbps, which means you're getting approximately the same quality as Amazon Music Store mp3 downloads. The semicolon ";" at the end of this command is critical. This tells the computer to make sure all parallel processing jobs complete before moving on to the next command.
&& find . -type f -name '*.mp3' -delete && find -name "*:*" -type d | rename 's/:/_/g' && find -name "*:*" -type f | rename 's/:/_/g' && echo complete
"&&" tells the computer to wait for the previous command to finish before running the next command. What you see above is language that will seek out the original mp3 files and delete them. Next, it will remove colons ":" that are considered messy characters in the names of directories and files. If you use services like rsync, then you have to remove messy characters.

No comments:

Post a Comment