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.
- Handbrake vs FFMPEG
- The Start Second(-ss) and Duration(-t) Switch
- Cutting Video with FFMPEG With 640px iPod Preset
- Using the sameq Switch
- 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.
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.
ffmpeg -ss 178 -i input.mp4 -vcodec libx264 -vpre libx264-ipod640 -async 1 output.mp4
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.
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.
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.