FFmpeg Video HOW TO – How to do everything you want with FFmpeg

created January 19, 2012, last updated June 23, 2015.

.
closeThis post was last updated 6 years 8 months 7 days ago, some of the information contained here may no longer be actual and any referenced software versions may have been updated!

If you want to work with ffmpeg video and audio files forget about buying any cheap commercial convertors (which are probably using FFmpeg anyway) and just take the plunge into the world of FFmpeg. It might seem a bit scary at first working just from a command line but there is nothing this impressive “Swiss Army Knife” media manipulation application cannot do.

What is FFMPEG, video and audio

What is FFmpeg? Simply put, FFmpeg is a Hyper fast Audio and Video encoder. It is a free software project that “produces libraries and programs for handling multimedia data. The name of the project comes from the MPEG video standards group, together with “FF” for “fast forward“.”

FFmpeg will do everything you want with audio and video but installing it and getting to grips with the command line options can be daunting. Here I will create a very simple list of the most common tasks you will want to do with FFmpeg video including command line options showing how to do it.

Installing ffMPEG  MAC OSX, Linux FOR USE WITH VIDEO AND AUDIO

The FFmpeg source code compiles and runs on all platforms, but getting the latest build with all the latest codecs and libraries compiled for your operating system can be troublesome. I am working mostly with Linux (Ubuntu) and OSX and found the easiest way to install the latest version of FFmpeg and libraries was to compile the source code myself.

FFMPEG INSTALL FOR  UBUNTU

For Ubuntu (10.10, 11.04, 11.10, 12.04) by far the best guide to do this comes from the Ubuntu forums, so I will not document everything use this LINK for the complete guide.

Follow these steps for the Ubuntu install.

Install the Dependencies

sudo apt-get remove ffmpeg x264 libx264-dev
sudo apt-get update
sudo apt-get install build-essential checkinstall git libfaac-dev libjack-jackd2-dev \
  libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libsdl1.2-dev libtheora-dev \
  libva-dev libvdpau-dev libvorbis-dev libx11-dev libxfixes-dev texi2html yasm zlib1g-dev

Install x264

cd
git clone git://git.videolan.org/x264
cd x264
./configure --enable-static
make
sudo checkinstall --pkgname=x264 --pkgversion="3:$(./version.sh | \
    awk -F'[" ]' '/POINT/{print $4"+git"$5}')" --backup=no --deldoc=yes \
    --fstrans=no --default

Install FFmpeg

cd
git clone --depth 1 git://source.ffmpeg.org/ffmpeg
cd ffmpeg
./configure --enable-gpl --enable-libfaac --enable-libmp3lame --enable-libopencore-amrnb \
    --enable-libopencore-amrwb --enable-libtheora --enable-libvorbis --enable-libx264 \
    --enable-nonfree --enable-postproc --enable-version3 --enable-x11grab
make
sudo checkinstall --pkgname=ffmpeg --pkgversion="5:$(date +%Y%m%d%H%M)-git" --backup=no \
  --deldoc=yes --fstrans=no --default
hash x264 ffmpeg ffplay ffprobe

 

Ignore all the compilation warnings, they are very boring. I first compiled FFMpeg on an Amazon EC2 Micro instance and the compilation took well over an hour. On my 512 Linode it took about 10 mins. Note that the compilation process is processor intensive, if you are doing this on a live server do it out of hours so as not to affect other applications.

The newest source code for FFmpeg is always here

git clone --depth 1 git://source.ffmpeg.org/ffmpeg

FFMPEG MAC OSX LION 10.7.2

UPDATE – if you are using Mountain Lion OS X 10.8 check this post out for extra steps required.

For OSX use the Homebrew package manager to compile and install the latest version of FFmpeg. If you need to install Homebrew run the following from a terminal command line

/usr/bin/ruby -e "$(curl -fsSL https://raw.github.com/gist/323731)"

—————————————–

==> This script will install:
/usr/local/bin/brew
/usr/local/Library/Formula/…
/usr/local/Library/Homebrew/…
==> The following directories will be made group writable:
/usr/local/.
==> The following directories will have their group set to admin:
/usr/local/.

Press enter to continue
==> /usr/bin/sudo /bin/chmod g+rwx /usr/local/.
==> /usr/bin/sudo /usr/bin/chgrp admin /usr/local/.
==> Downloading and Installing Homebrew…
Warning: Now install Xcode: http://developer.apple.com/technologies/xcode.html
==> Installation successful!

Now type: brew help

—————————————–

Homebrew is dependent on XCode, the Apple development application, before you can install any packages with Homebrew install the free XCode app from the Appstore. It is a big application and around a 2GB download so visit the Apps Store and go for a coffee.

With XCode installed you can install FFmpeg using brew on OSX 10.7.2 with the following command line

brew install ffmpeg --use-clang

MP4Box for MAC and LINUX

If you will be streaming video using the Apple Darwin Streaming Server you will need to hint your video files, install the gpac package using Homebrew (OSX) or apt-get (Linux) to obtain the MP4Box utility for hinting video files.

brew install gpac

apt-get install gpac

Remember the MP4Box command is case sensitive, it’s MP4Box not mp4box.

TEST FFMPEG

All being well type ffmpeg from your command line and check that the software has installed sucessfully.

ffmpeg version git-2012-01-19-1ce8377 Copyright (c) 2000-2012 the FFmpeg developers
built on Jan 19 2012 10:42:18 with gcc 4.6.1

HOW TO – How to do what you want with FFmpeg

 

Extract Audio from a Video using FFMPEG

This will extract the audio from a video file – myvideo.mp4 and create a new 44Khz 2 channel stereo 128Kbps MP3 file.

ffmpeg -i myvideo.mp4 -vn -ar 44100 -ac 2 -ab 128k -f mp3 myvideoaudio.mp3

CREATE A Video from an Image file using FFMPEG

You see this a lot on YouTube and Facebook, people actually want to share audio so they create a video that consists of one still image for the duration of the song. I have done this before with full blown video apps and its a long process for a relatively simple requirement. With FFmpeg its done ultra fast with a simple command line, an image file and an audio file:

Create a 720×576 (PAL) MP4 x264 video myvideo.mp4 from the image myimage.jpg and the audiofile myaudio.mp3

ffmpeg -s 720x576 -r 25 -loop 1 -shortest -y -i  myimage.jpg -i  myaudio.mp3 -acodec libfaac -ac 2 -ar 44100 -ab 128k -async 1 -vcodec libx264 -level 1.3 myvideo.mp4

Here is an example from Uncle Iggy

i-wanna-be-your-dog.jpg => i-wanna-be-your-dog-mp3 => myvideo.mp4

Note that I made the image 720×576 pixels to match the encode settings. 25fps is probably overkill here and results in a larger video file.

ffmpeg -s 720x576 -r 25 -loop 1 -shortest -y -i  i-wanna-be-your-dog.jpg -i I-Wanna-Be-Your-Dog.mp3 -acodec libfdk_aac -ac 2 -b:a 128k -async 1 -vcodec libx264 myvideo.mp4
I Wanna Be Your Dog jpg image 720×576

I am going to hint this too so I can add it to my Quicktime server and RTSP stream it (click the image for a demo).

MP4Box -hint myvideo.mp4

I Wanna Be Your Dog Single Image Video – click to view

(Image source – google images, music from Iggy and the Stooges.)

Now lets add a series of images to the video to make it slightly more interesting. This is a very quick method for creating a slide show type video from a series of images.

We do this by specifying the input parameter with the % wildcard, i.e. image-%3d.jpg where %3d indicates our image will be named image-XXX.jpg, i.e. image001.jpg, image002.jpg, image003.jpg etc.

Forcing FFmpeg to use a framerate of 1 frame per second (-force_fps), our images are shown with a 1 second delay between them and our resulting video at 1fps is relatively small, 4Mb or so, not much larger than the MP3 file.

For a longer delay use two images that are identical together which  would create a 2 second gap, 4 images a 4 second gap etc.

ffmpeg -s 720x576 -r 1 -force_fps -loop 1 -shortest -y -i  image%3d.jpg -i I-Wanna-Be-Your-Dog.mp3 -acodec libfdk_aac -ac 2 -b:a 128k -async 1 -vcodec libx264 myvideo.mp4
ffmpeg video – I Wanna Be Your Dog Video with Multiple Images – click to view

CREATE An FFMPEG VIDEO FROM A PHOTOSHOP IMAGE SEQUENCE

Photoshop is great for creating titles quickly using the video timeline to render text and images into 25fps image sequence frames that we can quickly convert into video using ffmpeg. To add audio to your clip trim the audio to the length of your video.

Use this command to create the video, you can see the results by clicking the image below.

ffmpeg -s 720x576 -r 25 -i  photoshop0%3d.jpg -i sample.mp3 -acodec libfdk_aac -ac 2 -b:a 128k -async 1 -vcodec libx264 myvideo.mp4
Example of a photoshop image sequence video – click to view

EXTRACT A SINGLE IMAGE FROM A VIDEO USING FFMPEG

If we want to extract a single frame image file from a video, for example to use as a movie poster image  use the following command line –

ffmpeg -i myvideo.mp4 -vframes 1 -ss 00:00:05 -f image2 image-%3d.jpg

This will extract 1 frame from myvideo.mp4 starting at the fifth second of the video creating the image file image-XXX.jpg.

BATCH CONVERT video for DARWIN RTSP streaming with ffmpeg

We want to convert existing media files to stream live with Apples Darwin media server. For a live stream everything in Darwin must be encoded with the same parameters. The best way to do this is to batch convert all your media with FFmpeg. I am going to create a bash script on my Mac to use FFmpeg to encode all MP4 files in a folder to a new MP4 folder. I will encode them to 720×576 (pal) 25fps with 2 channel stereo AAC audio at 128kbps and H.264 video at 768Kbps.

touch a new file mp4768 and make it executable, chmod +x mp4768

Edit the file and paste the following

#===================================
#the directory mp4 must be present in the current directory
for f in *.mp4;
do
ffmpeg  -i  "$f" -s 720x576 -r 25 -acodec libfdk_aac -ac 2 -b:a 128k -async 1 -vcodec libx264 -level 1.3 -b:v 768k -bt 768k "mp4/${f%.mp4}.mp4"; done #====================================

Now copy the files you want to convert into a folder, and create a subfolder called mp4. Run the bash script to convert all the files i.e. /bin/mp4768

Don’t forget to hint the files afterward for RTSP streaming with Apples Darwin Streaming media server.

This also gives us the interesting option of combining what we have learnt creating videos from image files, to create on the fly content for live Darwin streams using PHP and FFmpeg – more on that in another post!

If you have more cool FFmpeg video usage examples to add to this post please let me know!

 

Comments

This site uses Akismet to reduce spam. Learn how your comment data is processed.