Cut H.264 Video with FFMPEG using iPod Default Setting

April 3, 2012

I don’t do a ton of video work, but every so often, I need to do some converting. Mostly, this is to shrink the video size down some, so it uses fewer resources, or convert it to an FLV. Although, in the case of the latter, I usually try to steer clear of Adobe formats whenever possible.

This quick guide, shows how to cut video using some of the ffmpeg defaults, as well as making sure the audio does not get out of sync. It, of course, only scratches the surface of what you can do with ffmpeg.


Contents

  1. Handbrake vs FFMPEG
  2. The Start Second(-ss) and Duration(-t) Switch
  3. Cutting Video with FFMPEG With 640px iPod Preset
  4. Using the sameq Switch
  5. Determining Available FFMPEG Presets

Handbrake vs FFMPEG

I often use handbrake when I need to convert something for the web, as it is really easy to use and there are a bunch of useful presets. It ends up being ridiculously easy to convert a video for a website or to upload to youtube.

Not to mention, Handbrake is cross-platform, with versions for Linux, OSX, and Windows, as well as being Open Source, so it ends up working well for several of my non-technical clients.

I use ffmpeg sometimes too, but because I don’t use it a lot, I always seem to end up spending a bunch of time trying to get the settings right. ffmpeg is probably much more powerful, or at least more configurable in certain ways, but it can also be difficult for me to remember all the switches when I only use it infrequently.

Today, I needed to shorten a video, cutting off the first couple of minutes, and I did not see an obvious way to do this in handbrake. So I decided to use ffmpeg with the “-ss” command, to cut the video.

Top of Page


The Start Second(-ss) and Duration(-t) Switch

The -ss command sets the start seconds in ffmpeg.

You can also use the -t command to set the total duration time. This can also be useful if you are converting a long video and do not want to the do the entire thing before checking the quality.

Top of Page


Cutting Video with FFMPEG With 640px iPod Preset

The command:

ffmpeg -ss 178 -i input.mp4 -vcodec libx264 -vpre libx264-ipod640 -async 1 output.mp4

Note That:

  • “-ss” – This sets the start seconds, telling ffmpeg where to cut the video at.
  • “-vcodec” – The video codec must come AFTER the input file. If not, it is applied to the input file and you may get an”Unknown decoder ‘libx264’” error message
  • “-vpre” – This is the default setting, see below to find out what is available in your distro.
  • “-async 1” keeps the video audio in sync, otherwise it was out of sync and lagging behind the video. Seems like this ought to be the default!
  • Since I was only really concerned with cutting the video, I did not need to tweak size, framerate, or other settings.

    The h264 iPod preset worked fine and I ended up with the same quality as before, just a shorter video. I had initially tried one of the lossless settings, but I ended up with a huge file, much larger than the longer original.

    Top of Page

    Using the sameq Switch

    You can also use the “-sameq” switch inplace of the “vidpre” and “vcodec” switches.

    Sameq tells ffmpeg to use the same quality as the input video. However, like the vcode switch, it should be used AFTER the input file.

    Top of Page


    Determining Available FFMPEG Presets

    To see what presets are installed, you can check the “/usr/share/ffmpeg/”

    Depending on the way ffmpeg is compiled on your distro, there are probably several defaults, which can at least provide a nice starting place. You can edit them with a text editor, although you will probably want to save a new one, rather than override them.

    On Fedora 16, using RPM Fusion, there are only presets for creating H.264 and VP8 videos.

    Available Default Presets in Fedora using RPM Fusion:

    libx264-baseline.ffpreset
    libx264-lossless_fast.ffpreset
    libx264-lossless_slower.ffpreset	
    libx264-ipod320.ffpreset
    libx264-lossless_max.ffpreset
    libx264-lossless_slow.ffpreset	
    libx264-ipod640.ffpreset
    libx264-lossless_medium.ffpreset
    libx264-lossless_ultrafast.ffpreset

    In this case, although the async and sameq setting worked fine, the 640 iPod preset worked well too and it is nice to know I have some options for quickly setting up an H.264 web-video.

    Top of Page

    Advertisements

    Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out / Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out / Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out / Change )

    Google+ photo

    You are commenting using your Google+ account. Log Out / Change )

    Connecting to %s