Friday, November 13, 2009

Using ffmpeg to manage video

This is going to be a quick and dirty how-to on ffmpeg and you can find this stuff all over the net (well except the slow motion trick, I couldn't find that anywhere). Here goes:

Where to get ffmpeg
Ubuntu - sudo apt-get install ffmpeg
Windows - Install ImageMagick or Use a binary someone has built
Mac - Aren't you using Final Cut anyway? [Edit: Use this guide to build ffmpeg on the Mac]

Easiest way to convert video
ffmpeg -i video.wmv -sameq video.mpg

Create video from a series of images
Assuming your images are named like this - IMG_0001.JPG, IMG_0002.JPG...
Also, I'm setting the frame rate low here.
ffmpeg -r 15 -b 1800 -i IMG_%04d.JPG movie.mpg

Create a time lapse from a regular video
movie.mpg is the original, and I'm going to save every 5th frame. Then we stitch them back together.
ffmpeg -i movie.mpg -r 5 -f image2 %d.jpg
ffmpeg -b 1800 -i %d.jpg tt_movie.mpg

Make a video slow motion (kind of a hack, but I couldn't find another way)
This will make images from every frame in the clip. Then you stitch them back together with a different frame rate. The lower the rate, the slower the vid.
ffmpeg -i movie.mpg %d.jpeg
ffmpeg -r 10 -b 1800 -i %d.jpg tt_movie.mpg

Add audio to a video
ffmpeg -sameq -ar 22050 -ab 32k -i song.mp3 -i video.mpg videoWithMusic.mpg

Make an image into a video clip (for an intro or credits or something)
This will play for 10 seconds
ffmpeg -loop_input -i image.jpg -t 10 -r 30 -qscale 2 vid.mpg

Trim a video
This will clip the first 30 seconds of the video
ffmpeg -i video.mpg -sameq -ss 00:00:00 -t 00:00:30 trim.mpg

Stitch clips together (no ffmpeg needed)

This only works with a few formats. I tend to always work with mpeg, so this works great for me
cat video.mpg video2.mpg video3.mpg > finalVid.mpg