Gnuru.org
Productive Linux


Subscribe

 Subscribe via Feedburner in a reader

Enter your email address:

Delivered by FeedBurner


Login
Login:
Password:



Don't have an account?
Sign up to Gnuru.org
Forgot your password?

Convert m4a to mp3 Script
20 April 2011 @ 13:45 BST
by Paul

Some media players can't play protected m4a's. This script converts the m4a passed on the command line to an mp3 that can then be copied to your selected media player:

#!/usr/bin/bash
#
# copy and convert
#
# Transcode m4a file to mp3
#
# This script is heavily based on the original here:
# http://www.minigeek.org/2007/07/linux-m4a-to-mp3-conversion-script
#
# Naturally, changes are licensed under the GPL
#

orig=$1
temp_wav=`tempfile --directory ~/tmp --suffix=".wav"`

mp3=`echo "$orig"|sed -e 's/\.m4a/\.mp3/'`
temp_trackinfo=`tempfile --directory ~/tmp --suffix=".txt"`

# deal with track info
faad -i ${orig} 2>$temp_trackinfo
sed -i '23s/unknown: /title: /' $temp_trackinfo
sed -i '24s/unknown: /artist: /' $temp_trackinfo

year=`grep 'date: ' $temp_trackinfo|sed -e 's/date: //'`

sed -i 's/unknown: iTunes/iTunes: iTunes/' $temp_trackinfo

genrecount=`grep -c 'genre: ' $temp_trackinfo`
unknowncount=`grep -c 'unknown: ' $temp_trackinfo`

if [ "$genrecount" -eq 1 ] && [ "$unknowncount" -eq 2 ]; then
    sed -i '25s/unknown: /composer: /' $temp_trackinfo
    sed -i '26s/unknown: /album: /' $temp_trackinfo
    genre=`grep 'genre: ' $temp_trackinfo|sed -e 's/genre: //'`
fi

if [ "$genrecount" -eq 1 ] && [ "$unknowncount" -eq 1 ]; then
    sed -i '25s/unknown: /album: /' $temp_trackinfo
    genre=`grep 'genre: ' $temp_trackinfo|sed -e 's/genre: //'`
fi

if [ "$genrecount" -eq 0 ] && [ "$unknowncount" -eq 3 ]; then
    sed -i '25s/unknown: /composer: /' $temp_trackinfo
    sed -i '26s/unknown: /album: /' $temp_trackinfo
    sed -i '27s/unknown: /genre: /' $temp_trackinfo
    genre='other'
fi

if [ "$genrecount" -eq 0 ] && [ "$unknowncount" -eq 2 ]; then
    sed -i '25s/unknown: /album: /' $temp_trackinfo
    sed -i '26s/unknown: /genre: /' $temp_trackinfo
    genre='other'
fi
	
title=`grep 'title: ' $temp_trackinfo|sed -e 's/title: //'`
artist=`grep 'artist: ' $temp_trackinfo|sed -e 's/artist: //'`
album=`grep 'album: ' $temp_trackinfo|sed -e 's/album: //'`
track=`grep 'track: ' $temp_trackinfo|sed -e 's/track: //'`

## convert to wav

faad -o $temp_wav $orig

## convert to mp3
lame --alt-preset 192 --id3v2-only --tt "$title" --ta "$artist" --tl "$album" --tg "$genre" --tn "$track" --ty "$year" "$temp_wav" "$mp3"

## clean-up

rm $temp_wav
rm $temp_trackinfo

exit 0



iTunes have a bit more options for tags than shown in this scripts.

I have made a similar script, with genre checking, albumartist, and playlist generation.

http://code.ecomerc.com/Articles/iTunesConvert/

Peter


Posted by Peter on 2012-03-12 13:33:36.
Comments disabled