libav

Examples

Ask for help

The help file seems to be more up-to-date than the man. Also has a section for x264 encoding options.

avconv -h
Simple x264 encoding, copy audio
avconv -i input.mp4 -c:v libx264 -c:a copy output.mp4
Set frame size
avconv -i input.mp4 -c:v libx264 -s <640x480|hd480|hd720|hd1080> -c:a copy output.mp4
Set aspect ratio
avconv -i input.mp4 -c:v libx264 -aspect <16:9|4:3> -c:a copy output.mp4
x264 encoding, set CRF
avconv -i input.mp4 -c:v libx264 -crf 20 -c:a copy output.mp4
x264 encoding, set CRF
avconv -i input.mp4 -c:v libx264 -crf 20 -c:a copy output.mp4
x264 encoding, set to baseline video profile
avconv -i input.mp4 -c:v libx264 -profile:v baseline output.mp4

Video Sizes

avconv -i input.mp4 -s hd1080 output.mp4
  • hd480 (854×480)
  • hd720 (1280×720)
  • hd1080 (1920×1080)
  • vga (640×480)
  • qvga (320×240)
  • qqvga (160×120)

Aspect Ratios

avconv -i input.mp4 -s hd480 -aspect 16:9 output.mp4
  • 4:3
  • 16:9
  • 1.3333
  • 1.7777
Metadata

MP4 files generated by libav are not, by default, setup for streaming over HTTP. The reason for this is that part of the metadata is located at the end of the file. When that happens, the client needs to download the entire video before playback.

qt-faststart (libav) will fix that.

qt-faststart input.mp4 output.mp4

Alternatively, you can use the python wrapper script qtfaststart, which has more features.

qtfaststart input.mp4 output.mp4

To copy metadata when encoding a file:

avconv -i source.mp3 -map_metadata 0 output.m4a

libx264 encoding

Encoding Raw Video

If you want to use x264 to encode the video, it's simple to use libav to re-encode the original source to raw video, and then encode using x264, then remux using libav again. YUVMPEG2 is a simple format to use that contains frame data so that you don't need to pass anything to x264.

If you need your video resized, use libav, since x264 does not support that feature.

An example:

avconv -i input.mp4 -s hd480 -an input.y4m
avconv -i input.mp4 -vn -c copy input.aac
x264 -o input.264 input.y4m
avconv -i input.264 -i input.aac -c copy output.mp4
Presets

Use -preset to set the x264 preset.

avconv -i video.mpg -c:v libx264 -preset medium x264.mp4
Profiles

Main profile, 3.1 requires at least 720×[email protected] (?)

Gotchas

Lowering bitrate of AAC

On one sample, libav had difficulty creating a new AAC container from a source AAC file when lowering the bitrate (say, 256k to 96k). Playback with MPlayer would squelch and terminate quickly. The solution was to convert it to an MP4 container.

avconv -i input.aac -ab 96k output.aac.mp4
Preset speeds

The veryslow preset provides hiqh quality encodes at the cost of encoding speed, but the tradeoff seems negligible. Using the placebo preset, however, will dramatically increase the amount of encoding time, measured in hours.

MP3 encoding

Sometimes libav will have issues encoding MP3s, where the length is incorrectly read in some players. The workaround is to decode the MP3 to a PCM file, and then reencode it using lame directly.

avconv -i audio.mp3 audio.wav
lame audio.wav

Lame will encode at 128k by default.