Saturday, July 24, 2010

FFROGG - FFmpeg Recursive Ogg Encoding Script

What happens when you have thousands of songs that you want encoded, but either you

a) Have a command line system where no gui app can encode them for you?

b) Want more efficient use of your processor therefore do not use a gui?

c) are not good at computing and just want to get'r done?

This is one itch I just had to scratch. Now I'm aware of great apps like handbreak and I know amarok 1.4.x used to have a script to encode songs in a playlist. However, not only am I a big fan off ffmpeg, I needed to test out my bash-fu.

The scenario is this:

Joe user has tons of music spread out all over his hard drive. Lets say he has 10 000, none of which are contained in any one folder as more OCD folks would do. (Or an organized heirarchy of folders even)

Joe decides that one day, he wants to move to a patent-free codec such as .OGG format, but not only does he not have the time to FIND all his mp3's, he's clueless about how to encode them and knows of no gui apps (except for above mentioned in this article).

Joe user visits this article and decides that he wants all his files encoded into one folder on an external hard drive (which could be his mp3 player).

All Joe has to do is ensure that ffmpeg and libvorbis/libtheora are installed correctly on his *nix system and he can go about watching TV or playing with his dog/kids/wife while the machine does all the work.

Here's How:

1) Download the script @ [ http://www.mediafire.com/?lc023alg2bsfx9x ] to your home folder

2) Make sure it's executable, use chmod +x ffrogg.sh to do so.

3) Use the command sh ffrogg.sh --input=/home/$USER/ --output=/media/externaldisk/encoded --export --recursive (as long as your --input is linked to the top folder where your music is located, it should be fine)

4) Navigate to your output folder, in Joe User's case, it's /media/externaldisk/encoded/. There should be a script called "ffrogg-encodethis-[numbers].sh".

5) At his/your convenience, run the script. Example: sh ffrogg-encodethis-1279967631.sh

Technical Aspect:

Here's the really cool technical details of the script.

THIS SCRIPT IS CURRENTLY IN ALPHA, IT DOES NOT WORK 100%!

This script at its greatest potential will dynamically produce a script using the ffmpeg and theora-vorbis backend that can be run which will encode ALL RECOGNIZED AUDIO FILES that were found when it was invoked. It can recursively find all audio files starting at the specified input directory. Since this is alpha software, below is a list of what DOES work in the current version (0.01.05 ALPHA):

sh ffrogg.sh [commands]

  • --input=[folder or file]
  • --output=[folder only]
  • --logging (this is for debugging purposes only, can specify a folder with --logging=[folder] if directory is read-only)
  • --recursive (this flag is required to go into directories to find audio files. Otherwise it just looks at contents of specified directories)
  • --export (Create the script and exit. Omitting this flag will cause the script to encode before exiting, although it gives a period to exit before starting)
  • --force (No use for it yet, as the recovery handling bit is not quite implemented. Will remove temp files if they get in your way)
  • --help (Gives a list of options, some of which will not work yet)
  • --version (displays version of program)

This script creates a few temporary files which is inefficient, but this is by no means a professional work. It's also licensed under the GPL version 2 (although I may have omitted that in the script itself).

If the --output and --logging= and --save-state= flags are given without parameters, the current folder is automatically used "." .

Running the script without parameters will cause it to exit.

I may not get around to improving it much, and I do have a graphical user interface planned which would require basically a re-write of the script into a QT application, so it may not get past Alpha. Feel free to take it on yourselves as long as you give credit in the comments at the very least, to PeonDevelopments 2010.

Thanks, and happy listening.

Since I don't usually look at comments, send them to my gmail.

peon.developments --at-- gmail --dot-- com.

=-=-=-=-=
Powered by Blogilo

Friday, July 23, 2010

Scripting in Bash: Arrays, Files and Spaces

I recently encountered some strange behaviour in my bash shell while writing a script.

My script would take the contents of a folder and store them into an array. Sounds simple enough, as it should be. But it's not.

If you type verbatim:

" declare -a ARRAY=`ls -1 -x $directory` "

You will find that every space in every filename will produce its own entry in the array.

A file called "An Introduction To Bash.txt" would be seen in the array as

An

Introduction

To

Bash.txt

which is an abomination for what I need these the filenames for. It does make sense why this behaviour appears, but if we want the entire filename per entry, we need to take the filename before it's put into the $ARRAY variable and do something with it to ensure spaces either do not exist or do not denote the start of a new array token.

The former being the easiest and resourced method, I converted all spaces to underscores (temporarily) to prove my point.

The command is now " declare -a ARRAY=`ls -1 -x $directory | sed 's/ /_/g'` " and it will output filename entries properly, albeit with underscores, no spaces.

To ensure this effect is permanent and not just for show, one would need to rename (using the 'mv' command) all the files in that directory so no spaces are in the filenames.

Bash isn't one of my stronger languages, so I may have missed a blaringly obvious detail.

Also of note: I'm still working on the freeworld project, albeit slowly after gaining employment. My 1st revision document is approximately 70-80 pages, but that's not including proper use case tables and UML/flowchart diagrams. Least of all the prototypes for necessary components.

I hope to make another entry soon enough, in the coming couple weeks.

Peon out.

=-=-=-=-=
Powered by Blogilo