RSS
 

Linux BASH Script: Convert mp3 to avi with static image (command line)

21 Jan 2010

This is a script I made to take advantage of the ffmpeg package in linux to quickly convert an mp3 to avi using a static image. I personally use this technique for uploading my songs to YouTube. I originally found the conversion command here.

All you need is a linux distro with ffmpeg installed, a jpeg or png image, and an mp3. Note: It is highly likely other image formats, audio formats, and output video formats will work, but I have only used jpeg/png+mp3+avi and so cannot attest to results otherwise.

Usage: bash mp32avi.sh <image_file> <mp3_file> <output_file.avi>

Code (mp32avi.sh):

#!/bin/bash
FFMPEG=`which ffmpeg`
if [ "$FFMPEG" = "" ] ; then
	echo "Please install ffmpeg.";
	exit 0;
fi
if [ $# != 3 ] ; then
	echo "Usage: $0 <image_file> <mp3_file> <output_file.avi>";
	exit 0;
fi
if [ ! -f $1 ] ; then
	echo "Source image '$1' not found.";
	exit 0;
fi
if [ ! -f $2 ] ; then
	echo "Source mp3 '$2' not found.";
	exit 0;
fi
if [ -f $3 ] ; then
	echo "Output file '$3' exists.  Overwrite? (y/n)";
	read CONFIRM
	if [ "$CONFIRM" == "y" ] ; then
		echo "Overwriting '$3'"
	else
		if [ "$CONFIRM" == "Y" ] ; then
			echo "Overwriting '$3'"
		else
			echo "Operation canceled.";
			exit 0;
		fi
	fi
fi
TIME=`$FFMPEG -i $2 |& grep 'Duration' | awk '{ print $2; }' | sed -e 's/,//g'`
$FFMPEG -loop_input -i $1 -i $2 -acodec copy -y -t $TIME $3
 

Tags: , , , , , , , , , , , , , , , , , ,

Leave a Reply

 

 
  1. Thomas

    April 17, 2010 at 10:58 pm

    I’m getting audio very weak after using this script, almost muted. Any ideas?

     
  2. Jeremy

    April 19, 2010 at 9:34 am

    I never had that particular issue, but I would check into ffmpeg to see if there is an audio volume setting and just edit the call in the script.

    You might also try using a different audio format. I always use 128-bit mp3s (maybe you are too, just making sure all the bases are covered). Sorry for the delayed response — I just saw your comment.

     
  3. Eduardo

    November 7, 2010 at 8:01 pm

    The $TIME was not being defined by the respective command from your script. I’ve used this one insted: TIME=`ffmpeg -i $2 2>&1 | grep Duration | cut -f1 -d, | cut -f2,3,4,5 -d:`

     
    • Jeremy

      November 8, 2010 at 2:42 pm

      Good to know … it was working for me, but I would think with differing distros, package versions, and shells, there will be a few of those. Thanks for posting …

       
  4. Petr

    November 23, 2011 at 6:21 am

    Thanks a lot!

    That was help. Cheer.

     
  5. J

    April 15, 2012 at 4:04 pm

    Cool! Thanks for sharing :).

    I had to add a few “” around $2 for it to work with filenames that contain spaces.

     
    • Jeremy

      April 15, 2012 at 9:41 pm

      If you don’t mind, tell me your modifications and I’ll add them to the script.

       
  6. Niilo

    August 5, 2012 at 5:21 pm

    Just what I was looking for. Thanks! :)

     
  7. grin

    August 30, 2012 at 6:00 pm

    -loop_input is now -loop 1 but apart from that still works fine, thanks.

     
  8. b42

    January 8, 2013 at 11:53 am

    Version with updated ffmpeg parameter and escaped variables:

    #!/bin/bash
    FFMPEG=`which ffmpeg`
    if [ "$FFMPEG" = "" ] ; then
    echo “Please install ffmpeg.”;
    exit 0;
    fi
    if [ $# != 3 ] ; then
    echo “Usage: $0 “;
    exit 0;
    fi
    if [ ! -f $1 ] ; then
    echo “Source image ‘$1′ not found.”;
    exit 0;
    fi
    if [ ! -f $2 ] ; then
    echo “Source mp3 ‘$2′ not found.”;
    exit 0;
    fi
    if [ -f $3 ] ; then
    echo “Output file ‘$3′ exists. Overwrite? (y/n)”;
    read CONFIRM
    if [ "$CONFIRM" == "y" ] ; then
    echo “Overwriting ‘$3′”
    else
    if [ "$CONFIRM" == "Y" ] ; then
    echo “Overwriting ‘$3′”
    else
    echo “Operation canceled.”;
    exit 0;
    fi
    fi
    fi
    TIME=`$FFMPEG -i $2 |& grep ‘Duration’ | awk ‘{ print $2; }’ | sed -e ‘s/,//g’`
    $FFMPEG -loop_input -i $1 -i $2 -acodec copy -y -t $TIME $3

     
  9. archer

    May 22, 2013 at 9:22 am

    hi, good job!

     
  10. Encode an Audio file to Video file | Linux Tidbits

    June 18, 2013 at 2:53 pm

    [...] large audiobooks ] ). This although is good for audio posting on youtube… I found a script by Jeremy Tharp that needed a bit of a tuneup (quoting parameters for file names with spaces, fixed exit status, [...]

     
  11. Marc

    August 11, 2013 at 1:26 am

    Thanks a lot for the script.
    i’m using avconv with -loop 1 parameter to avoid warning.
    It’s a great help.

     
  12. David Cary Hart

    October 23, 2013 at 9:47 pm

    Wow. Instead try:

    ffmpeg -i sound.mp3 -i picture.jpg out.avi