Updated Daily.

Tuesday, August 12 gave rise to...

Use FFmpeg!

<!-- USE FFMPEG -->



FFmpeg is the Swiss Army Knife of command line tools for video and audio. It does many things you already want; for example, FFmpeg makes it easy to convert between almost any video or audio format you can think of. It also does some cool things you may not have considered trying before, like facilitating the usage of YouTube as a gigantic downloadable mp3 library. Also, considering that Flash video is the more or less the de facto official video format of the web (and thereby appears to be the future of video on the web), FLV is a very important format to be able to convert from and to. FFmpeg makes this work incredibly easy.

Let's start this tutorial/guide with the mp3 stuff, then move on to more basic/intended usage, and then go on to something cool.

Extracting mp3 from YouTube videos (2 steps):

Let's say you like a song you heard on YouTube, but cannot find the mp3 anywhere online and there's nothing available at the record store. Back in the 1980s you would have been screwed -- you might have been upset enough to try a new drug called "crack" cocaine. But now with The Internet and FFmpeg, you can avoid becoming addicted to crack by downloading a DRM-free mp3 file that will play anywhere, directly from the YouTube video itself! Here's how:

  • First, download the video from YouTube. You can find the .flv file in the source code, but services like KeepVid have utilized YouTube's API to easily allow you to download the video directly from YouTube's server's in a higher quality .mp4 format.
  • Second, take the video, "video.mp4" and run it through FFmpeg like this:
    ffmpeg -i video.mp4 -ab 192k audio.mp3
    This will give you an mp3 with a fairly standard quality of 192kbps. You can make that value higher or lower if it pleases you, but remember your source: audio from a YouTube video. The saying, "You can't polish a turd," is true.

Extracting a clip from a longer video (1 step):

Let's say you really like a segment of a movie, and want to share it with the world. Here's how you can post it to YouTube using free software, make sure the audio syncs up with the video (often times it doesn't, if a certain option is not included in the command) and upload it faster than the dopes uploading video in mpeg or avi format:
ffmpeg -ss 00:06:09 -t 00:00:15 -i mmm.avi -async 1 -qscale 3.5 -vcodec flv seriously.flv
The first part of the command -ss 00:06:09 -t 00:00:15 means to start at 00:06:09 into the movie, and to play for 15 seconds. -i mmm.avi is where you tell FFmpeg to use mmm.avi as the input file. Yes, that's right -- you define the segment before you define the input file.

-async 1 is optional, but it is very helpful for a situation where the audio in your new cropped clip does not sync up with the video.

-qscale 3.5 is a variable command that changes the quality of your cropped clip. 3.5 is a good value here for YouTube -- it seems about equal to the quality of YouTube videos. A lower value means a higher quality. -sameq is a different option that serves the same function. It means to convert the video losslessly -- no loss in quality. Use sameq if you cannot tolerate any loss in quality from source to destination. When you're happy (or frustrated to the point of not being able to work any more) with the results, simply upload to YouTube.

If you want to convert a file from one format to another (losslessly), use this command:
ffmpeg -i input.mp4 -sameq output.mpg
Remember, the -sameq option means to use the same quality (lossless).

If you want to do something cool, like make a mux (or music video, if you prefer that term) a la Pink Floyd + The Wizard of Oz, try this:
ffmpeg -ss 00:04:25 -t 00:05:15 -i superman.avi -i grandma.mp3 -map 0:0 -map 1:0 -qscale 3.5 -vcodec flv killer.flv
When you want to make a simple video like what we're describing, this is a good way to do it. There are a couple of examples here.

The only option we haven't gone over yet here is -map. This is used in conjunction with the two input files to tell FFmpeg where to put them in the output. Here we have the first input file going to stream position 0 on output file 0 and the second input file is going to position 1. -map options correspond to the position in the command of input files -- so the first -map tells FFmpeg where to map the first input file specified in the command, the second tells where to map the second input file, and so on. It will make more sense if you try it. Use the format above if you get lost.

If you want to do something cooler, like this, where multiple video files are combined (see intro and outro), you will need to use another command line tool called cat. This tool concatenates files (it joins them together). It doesn't work with all video formats -- for instance, it will not work with .flv, but it will work with .avi. It also doesn't play nicely with certain differences between files, such as differences in video dimensions or bitrate. To really use this tool well you may need to consult the man page for ffmpeg or some mailing lists to see how other people solved their problems...but if you just need to join two files, here's how you do it:
cat mick.avi killar.avi > killar-milk.avi
And after this, you should run the resulting file killar-milk.avi through FFmpeg to rebuild the part of the file that tell video players their duration. If you don't, you probably won't be able to see the entire new video.

Since videos with different resolutions are incompatible, here's a hint that should help you combine two videos of different resolutions: you can resize videos with the -s option. the format is wxh (width x height) so if you want your video to be 480x196, your option looks like this:
-s 480x196

Hope all this helps. Leave questions and comments in the comments area. Try combining these commands in new and novel ways to achieve interesting results. Really freak it. At the very least, post something online that has never been posted before; it's easy.

When you use the right tools, the Internet is your oyster.


FFmpeg is available for Linux, Mac, and Windows. Officially it's only available to be compiled from source, but you can use Google to easily find a build for your operating system. If you use Linux, check your favorite repository for your chosen distro -- if it isn't included, abandon your distro for a good one.

ffmpeg is pretty fucking cool.

0 comments:

Post a Comment