Monday, 29 November 2010

Converting all video files within a directory with Bash & ffmpeg

I recently found myself needing to convert around 45 .flv videos into a format which my PS3 would find acceptable. Not being a fan of doing anything manually I decided to do this from within a small script. It requires that you have ffmpeg installed, and the 'rename' program, if you need to remove spaces from the  original filenames. (If not, you may want to comment out the line beginning rename)

You can also tweak the inputformat and outputformat variables and the ffmpeg settings to make it suitable for almost any type of conversion. If you start hitting errors in the conversion, its probably because you have some codec’s or packages missing on your system, so you can also check the extra codec’s section below.


#!/bin/bash
# A script to convert videos files within a directory
# An HRH Script!! 


# Set video types
inputformat=flv
outputformat=mp4
outputdir=converted

# Remove any spaces from filenames
rename s'/ //' *.$inputformat

# check if we have an output directory
if [ -d $outputdir ]
then
echo "Directory Exists, ready to convert files"
else
mkdir $outputdir
echo "Created directory $outputdir/"
fi

# read in the files and start the conversion
for file in `ls *.$inputformat | sed "s/\.$inputformat//"`
do
    ffmpeg -i "$file"."$inputformat" -ab 56k -ar 22050 -b 600k -s 640x480 $outputdir/"$file"."$outputformat" 
done




Getting more codec’s to work in ffmpeg  -  (This was what worked for me, ffmpeg can seem like a dark art at times!)

By going through the process below, I was able to get ffmpeg converting from and to a wide range of video formats. I don't think all the packages were necessary, but it would take me forever to go back and find out which ones made the difference, so I'm just going to show you all of them!

You may also need to enable the multiverse software source, if you do not currently have this enabled.
System -> Administration -> Software Sources


# Install some extra packages which will help ffmpeg handle more formats
# This should be done before installing a fresh version of ffmpeg
sudo apt-get install ubuntu-restricted-extras gstreamer0.10-plugins-ugly-multiverse 
sudo apt-get install gstreamer0.10-plugins-ugly gstreamer0.10-ffmpeg ffmpeg2theora 
sudo apt-get install mencoder libogg0 libogg-dev libvorbis0a libvorbis-dev vorbis-tools 
sudo apt-get install imagemagick youtube-dl poppler-util yasm


# Install the Trunk version of ffmpeg
sudo apt-get purge ffmpeg          # Make sure packaged version is removed
sudo apt-get install subversion    # Skip if subversion is already installed
cd ~
svn checkout svn://svn.mplayerhq.hu/ffmpeg/trunk ffmpeg
cd ffmpeg
./configure
make
sudo make install 


Assuming the sudo make install completed without any errors, you should now be running with the latest ffmpeg installed on your system. You can check this with

ffmpeg -version

1 comment:

  1. Hi, Really like your script!
    I want to use it to batch convert files inside a folder and place the converted files in another (output) folder.
    Can you tell me where to run the script? Inside the folder where the source files are?
    What will happen when you run the script a second time? Will it skip the already converted files, and process any new files?
    best, Richard

    ReplyDelete