• Home
  • Category: Video

Convert and split video files on the Linux command line

avconv is a nifty utility for converting media files from the command line from linux.
According to the libav documentation page,

avconv is a very fast video and audio converter that can also grab from a live audio/video source. It can also convert between arbitrary sample rates and resize video on the fly with a high quality polyphase filter.

Installing avconv on Debian/Ubuntu

#apt-get install libav-tools

Compiling avconv from source

See my post

General syntax for avconv

avconv [global options] [[infile options][‘-i’ infile]]… {[outfile options] outfile}…

Main options:
‘-i filename (input)’
input file name

‘-y (global)’
Overwrite output files without asking.

‘-c[:stream_specifier] codec (input/output,per-stream)’
‘-codec[:stream_specifier] codec (input/output,per-stream)’

Select an encoder (when used before an output file) or a decoder (when used before an input file) for one or more streams. codec is the name of a decoder/encoder or a special value copy (output only) to indicate that the stream is not to be reencoded.

For example

avconv -i INPUT -map 0 -c:v libx264 -c:a copy OUTPUT

encodes all video streams with libx264 and copies all audio streams.

‘-ss position (input/output)’
When used as an input option (before -i), seeks in this input file to position. Note the in most formats it is not possible to seek exactly, so avconv will seek to the closest seek point before position. When transcoding and ‘-accurate_seek’ is enabled (the default), this extra segment between the seek point and position will be decoded and discarded. When doing stream copy or when ‘-noaccurate_seek’ is used, it will be preserved.

When used as an output option (before an output filename), decodes but discards input until the timestamps reach position.

position may be either in seconds or in hh:mm:ss[.xxx] form.

‘-t duration (output)’
Stop writing the output after its duration reaches duration. duration may be a number in seconds, or in hh:mm:ss[.xxx] form.

For more detailed information on these and other options, refer the libav doku.

Splitting a video file with avconv

#avconv -i 'Supernatural S01E20 HDTV.mp4' -vcodec copy -acodec copy -ss 00:00:30 -t 00:00:15 'seg2.mp4'

-ss is the start segment
-t is the duration

Getting media file information with avconv

Run avconv without just the input file name.

#avconv -i supernatural_s04e07.mp4
avconv version 0.8.12-6:0.8.12-1, Copyright (c) 2000-2014 the Libav developers
  built on Jun  1 2014 17:03:01 with gcc 4.7.2
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'supernatural_s04e07.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf53.21.1
  Duration: 00:40:52.53, start: 0.000000, bitrate: 525 kb/s
    Stream #0.0(und): Video: h264 (Main), yuv420p, 624x352 [PAR 1:1 DAR 39:22], 360 kb/s, 23.98 fps, 23.98 tbr, 2997003.00 tbn, 47.95 tbc
    Stream #0.1(und): Audio: mp3, 48000 Hz, 2 channels, s16, 160 kb/s
At least one output file must be specified

Get the duration of a video with avconv

avconv -i "$1" 2>&1 | grep "Duration"| cut -d ' ' -f 4 | sed s/,//

Eg:

avconv -i supernatural_s04e07.mp4 2>&1 | grep "Duration"| cut -d ' ' -f 4 | sed s/,//
00:40:52.53

For more details on avconv, refer the libav documentation page
For libx264 details, refer: https://libav.org/avconv.html#libx264

Tips (More about CRF:
‘-crf float’
Select the quality for constant quality mode.

The Constant Rate Factor (CRF) is the default quality setting for the x264 encoder. You can set the values between 0 and 51, where lower values would result in better quality (at the expense of higher file sizes). Sane values are between 18 and 28. The default for x264 is 23, so you can use this as a starting point.

If you’re unsure about what CRF to use, begin with 23 and change it according to your subjective impression of the output. Is the quality good enough? No? Then set a lower CRF. Is the file size too high? Choose a higher CRF. A change of ±6 should result in about half/double the file size, although your results might vary.

Converting a 1080P video into 640×480 Video:

avconv -i source.mp4 -s vga -c:v libx264 -preset ultrafast -c:a copy output.mp4