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. 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 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.

16 comments:

Anonymous said...

Haha!

Fuckin ffmpeg indeed.

It is the leetest.

Anonymous said...

Thanks, now I know what to do with an interrupted dvd to xvid rip (by mencoder) and a bit too long piece from the end of the same video.
Cheers
KR

Anonymous said...

aside from your language issues a pretty good tutorial. I would like to see a tutorial with ALL the options used and explained though. so ffar it's been bits and pieces.
Paul

Anonymous said...

-sameq does not mean it's lossless; It means it's close to lossless.

Lethea said...

hey I need help with ffmpeg ?
can u help me ?

Gow Tham said...

-sameq does not mean it's lossless; It means it's close to lossless. No one defies schlk ! http://superuser.com/questions/478549/what-is-the-sameq-option-in-ffmpeg-does-it-mean-same-quality

burek said...

Please update your article and remove "-sameq" option, because it doesn't do what you think it does. We (at FFmpeg) have a lot of trouble explaining this to people, that catch random ffmpeg command lines on the internet and then come to our support channel to complain about how things don't work. Thank you in advance.

Noah said...

Thanks for your comments! The original post has been updated and I have removed references to the -sameq option.

victorinox knives said...

just found your lovely blog :) i like-y what i see!

south mp3 said...

wonderful information, I had come to know about your blog from my friend nandu , hyderabad,i have read atleast 7 posts of yours by now, and let me tell you, your website gives the best and the most interesting information. This is just the kind of information that i had been looking for, i'm already your rss reader now and i would regularly watch out for the new posts, once again hats off to you! Thanks a ton once again

Online tv said...

too good piece of information, I had come to know about your site from my friend sajid, bangalore,i have read atleast 11 posts of yours by now, and let me tell you, your web-page gives the best and the most interesting information.

Tamilan Raghavan said...

Good post
Free Classifieds in India
Bollywood Latest Photos

rhul said...

It is very helpfull and thanks for the post :)

ram said...

Very useful information, nice post thanks a lot.

celeb said...

nice post.thank you for sharing.

mp3 songs said...

This is just the kind of information that i had been looking for, i'm already your rss reader now and i would regularly watch out for the new posts, once again hats off to you! Thanks a ton once again

Post a Comment