Convert M4A into MP3 with GNU/Linux, recursive and multithreaded

On a similar thread as an old post, Convert M4A into MP3 with GNU/Linux, I needed to convert a bunch of iTunes M4A unprotected audio files into standard MP3 files. With iTunes’ fondness for sorting by artist first, then by album, I ended up with a ton of single-file folders, making any scripting a bit of a pain. Also, converting a bunch of M4A files to MP3 is something that should be done in parallel on a multi-core CPU.

Here’s a snippet using find and xargs‘ multi-threading support to recursively find all M4A files and convert them to MP3 with as many threads as you want.

First, here’s the convm4a script that is called from the find/xargs line. Call it with a single argument, the location of an M4A file. The script with change to that directory and convert the file to a similarly-named MP3 file. Put this somewhere on your PATH (possibly in ~/bin/) and be sure to chmod +x convm4a.
#!/bin/bash

DIRNAME=`dirname "$1"`
FILENAME=`basename "$1" .m4a`
#echo "$DIRNAME"
cd "$DIRNAME"
faad -o - "${FILENAME}.m4a" | lame - "${FILENAME}.mp3" &> /dev/null

Test out that script to make sure it works. Then, run this line from the top of the directory full of your M4A files:

find -name '*\.m4a' -type f -print0 | xargs -P 4 -n 1 -0 convm4a

The -P 4 means to use four concurrent executions of convm4a, and the -n 1 means to only pass one argument to each invocation of convm4a. Change the number 4 to reflect the number of threads you want to run. You probably don’t want to exceed the number of CPU cores in your computer.

Perl and CPAN

Perl is a scripting language. Some people really like it. I have my own feelings about it, but that’s for another day. Perl has a system to install add-on modules called CPAN, that competes and conflicts with the native package manager on a Linux install, but sometimes the only way to install a random package is to use CPAN. Here’s a quick little guide on how to do that. Standard disclaimers apply.


sudo perl -MCPAN -e shell
install Bundle:CPAN
(press enter a bunch of times to allow CPAN to access the web and do updates)
reload cpan
install Module::Whatever

Repeat that last line for each package the perl script requires. Takes a lot of trial-and-error to find all the packages you need to install. Good luck.

Cross-platform animation of still frames with MEncoder

Many times I’ve needed to take a series of still frames and create a video file to view them as an animation. Sometimes the images come from a simulation, and sometimes the images are from a video editing project where I had to edit individual frames. Regardless, I’ve spent a while searching for the best magic codes to provide to MEncoder to produce a universally compatible output video. I’ve tested video files created this way and they worked in QuickTime on OSX 10.6, Totem on Ubuntu Linux 9.10 (with the ubuntu-restricted-extras codec pack installed), and Windows XP with the K-Lite codec pack installed. Let me know if files produced this way don’t work for your system.

Since MEncoder’s default and preferred container format is AVI, I used that instead of MPEG. I used the default mpeg4 code, which should be a recent DivX or XviD codec, widely available on all common operating systems. I used MP3 encoding for the audio, sourced from a standalone .wav file created separately for this animation.

mencoder mf://*.png -mf fps=25:type=png -ovc lavc -lavcopts vcodec=mpeg4 -audiofile audio.wav -oac mp3lame -o output_video.avi

Build an AVR-GCC Toolchain

The main benefit of using Atmel’s line of AVR microcontrollers is the well established avr-gcc toolchain, based on the free and open source gcc compiler. There is a nicely packaged distribution for Windows, but nothing nice for Linux users. Given that a compiler toolchain is fairly important, I thought I should try building my own. Reproduced below are my notes from installing all the components of the avr-gcc toolchain.

Click through for a detailed guide on how to download, compile, and install the necessary tools to get an avr-gcc toolchain going with Linux. Also includes a link to download the completed package to save yourself the time of building the toolchain from scratch, if you want.

http://www.mbeckler.org/microcontrollers/avrgcc_build/

21 Nov 2009, 1:45pm

by Layne

8 comments

VirtualBox USB with Ubuntu 9.10 Karmic

After you upgrade to Ubuntu 9.10 Karmic Koala, your VirtualBox USB devices will likely be all greyed out. To get VirtualBox USB support working again, you need to add yourself to the vboxusers group. To do this, use the System menu at the top of the screen, then to Administration, then to Users and Groups. Click on the keys at the bottom of the window and enter your user password to be able to make changes. Then, double-click your username and select the User Privileges tab. Scroll down to the bottom and select Use VirtualBox. Logout and log back in and you should be able to use USB devices with your guest OS.

24 Jul 2009, 6:00am

by Layne

1 comment

Convert M4A into MP3 with GNU/Linux

I ripped some CDs with iTunes, but accidentally used their silly M4A format. Using the free FAAD program included in my debian install, I was able to use this Bash snippet to convert the files to mp3:

for i in *; do faad -o - "$i" | lame - "`basename "$i" .m4a`.mp3"; done

Be sure to include the double-quotes around the $i, otherwise filenames with spaces might cause errors.

Update: I figured out a way to add recursive multithreading support. Check it out!

Tab completion and symlinked directories

Tab Completion is a very useful and time saving features of most modern command line interpreters. When you try to tab complete a symlinked directory, by default in BASH, it won’t put a trailing slash, even if the directory name is un-ambiguous. Example:

Normal directory:
$ cd Pic<tab>
$ cd Pictures/

Symlinked directory:
$ cd Pic<tab>
$ cd Pictures<tab>
$ cd Pictures/

Solution:
To tweak your shell to treat symlinked directories the same as regular directories for the purposes of tab completion, you can add these two lines to your ~/.inputrc file:

set mark-directories On
set mark-symlinked-directories On

How to compile The Fabulous Logic Analyzer on Gentoo Linux

Linux being Linux, most distributions seem to keep things in different places, something that tends to mess up compiling software (although it shouldn’t!). I wanted to try out a program called The Fabulous Logic Analyzer, which uses your PC’s parallel port as a digital logic analyzer for debugging all kinds of digital circuitry. Unfortunately, the source build didn’t work out of the box, and the only instructions I could find were for Ubuntu. After consulting a Gentoo expert I know from college, we eventually got it working:

  1. Download and unpack source package: http://prdownload.berlios.de/tfla-01/tfla-01-0.1.4.tar.bz2

  2. The big challenge was finding the proper paths for these environment variables. Use these two export commands to set the build environment variables.

    export PATH=/usr/qt/3/bin:$PATH
    export QMAKESPEC=/usr/qt/3/mkspecs/linux-g++

  3. Use the qmake program to create a standard Makefile for this project:

    qmake tfla-01.pro

  4. Build the source:

    make

  5. Look in the bin/ directory, and you should have a freshly compiled binary, hot from the gcc oven.

Arch Linux Package for TFLA

An MD5 sum of my very own

In the course of my job, I have a small hardware testing program that I am in charge of maintaining and making improvements on. Since the test software is used to interact between two pieces of hardware, it’s pretty important that both units have the same software running. While the Subversion $Revision$ keyword substitution works reasonably well most times, when I make a series of changes without committing the changes, I can have multiple versions of the executable with the same $Revision$ value. I needed a better way to easily allow end-users to tell two versions apart. My solution was to compute the MD5 checksum of the executable, and display that as part of the help menu.
more »

Integer Square-root Function

While browsing the Linux kernel source this morning at work, I stumbled upon this little gem. As you may already know, kernel code can’t use any floating-point calculations, since the state of the Floating Point Unit (FPU) isn’t saved during context switches to kernel-space. This function computes the integer square root of the provided integer. It returns the largest integer less than or equal to the true square root of the input number. Code and sample output provided after the break.
more »