DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Creating AVI Video From Mp3 File

11.07.2010
| 4210 views |
  • submit to reddit
        This script is useful for create an AVI video from a mp3 file to upload to Youtube.
A image will be placed in a black background with mp3 title in white
Author: Eduardo Mucelli R. Oliveira
Based on ideas and codes from http://ubuntuforums.org/archive/index.php/t-1244112.html and http://www.crimulus.com/2010/01/21/linux-bash-script-convert-mp3-to-avi-with-static-image-command-line

#!/bin/bash
FFMPEG=`which ffmpeg`
FPS=1                                                                                   # for a youtube video from a mp3, it is enough

if [ "$FFMPEG" = "" ] ; then
	echo "Please install ffmpeg.";
	exit 0;
fi
if [ $# != 2 ] ; then
	echo "Usage: $0 <input_file.mp3> <output_file.avi>";
	exit 0;
fi
if [ ! -f $1 ] ; then
	echo "Source mp3 '$2' not found.";
	exit 0;
fi
if [ -f $2 ] ; then
	echo "Output file '$2' exists.  Overwrite? (y/n)";
	read CONFIRM
	if [ "$CONFIRM" == "y" ] ; then
		echo "Overwriting '$2'"
	else
		if [ "$CONFIRM" == "Y" ] ; then
			echo "Overwriting '$2'"
		else
			echo "Operation canceled.";
			exit 0;
		fi
	fi
fi

TITLE=`$FFMPEG -i $1 2>&1 | grep TIT2 | cut -d: -f 2 | tr -d "'"`                           # get the title and remove "'" chars
IMAGE=/tmp/$RANDOM.gif
convert -size 1024x240 xc:black -fill white -draw "gravity Center text 0,0 '$TITLE'" $IMAGE # generate a black-background image with mp3 title in white
TIME=`$FFMPEG -i $1 2>&1 | grep Duration | cut -f1 -d, | cut -f2,3,4,5 -d:`
$FFMPEG -r $FPS -loop_input -i $IMAGE -i $1 -acodec copy -y -t $TIME $2
rm $IMAGE