Wednesday, September 22, 2010

OT : mp4 to mp3 conversion tool

Hi All I have decided to share with you something I did for my self as I found my self doing many conversion of mp4 to mp3 for my iPhone, i did an automation script that takes the path of a directory or file and convert all mp4 to mp3.

#!/bin/bash
# mp4tomp3
#
# Cool Tool created to automate the conversion process of mp4 files to mp3
#
# Creator : Shiran Guez
#
# Created under ubuntu but should work on any linux distribution 
# That have ffmpeg installed
#
#
CONVERTER_PATH=$1
if [ -z $1 ] ; then
    CONVERTER_PATH=$(echo "`pwd`")
fi
 
if [ -d $CONVERTER_PATH ] ; then
    for i in *.mp4
    do
        ffmpeg -i "$i" -f mp3 -ab 192000 -vn $(echo $i | sed 's/.\mp4/.\mp3/') 
    done
    echo "Done"
else 
    file $CONVERTER_PATH | grep "MPEG v4"
    if [ $? -eq 0 ] ;then
        ffmpeg -i "$CONVERTER_PATH" -f mp3 -ab 192000 -vn $(echo $CONVERTER_PATH | sed 's/.\mp4/.\mp3/')
    else
        echo "That is not a valid format of mp4 file to convert"
        echo "either provide a path of  directory containing mp4 extensions"
        echo "or provide a path to a valid mp4 file"
    fi
fi

I moved the script to /usr/sbin and added excutable priv “chmod +x /usr/sbin/mp4tomp3” to run it simply run the command from cli example:

sudo mp4tomp3 <path_of_dir or file_name>

or just under the directory you have files to convert

cd /to/desired/path sudo mp4tomp3

enjoy

No comments: