FFMPEG: Convert & Edit Video via Command Line

FFMPEG is a free software that lets you create/edit/convert videos via command line. You can download and install FFMPEG for Linux, Windows, and Mac Operating System.

FFMPEG provides a lot of features that you can apply to an video like:

– get detailed information of the video
– record video
– convert video to audio
– convert video from one format to another
– extract audio from video
– extract image from video
– resize video
– crop video
– join videos

The basic style of the command of FFMPEG is as follows:

ffmpeg [global_options] {[input_file_options] -i input_url} ... {[output_file_options] output_url} ...

Get Video Information

Video information can be fetched using the -i parameter followed by the full path to the video.

ffmpeg -i /path/to/my-video.mp4

Output:

ffmpeg version 3.4.2 Copyright (c) 2000-2018 the FFmpeg developers
  built with Apple LLVM version 9.0.0 (clang-900.0.39.2)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/3.4.2 --enable-shared --enable-pthreads --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --disable-jack --enable-gpl --enable-libmp3lame --enable-libx264 --enable-libxvid --enable-opencl --enable-videotoolbox --disable-lzma
  libavutil      55. 78.100 / 55. 78.100
  libavcodec     57.107.100 / 57.107.100
  libavformat    57. 83.100 / 57. 83.100
  libavdevice    57. 10.100 / 57. 10.100
  libavfilter     6.107.100 /  6.107.100
  libavresample   3.  7.  0 /  3.  7.  0
  libswscale      4.  8.100 /  4.  8.100
  libswresample   2.  9.100 /  2.  9.100
  libpostproc    54.  7.100 / 54.  7.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'my-video.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: isommp42
    creation_time   : 2018-03-09T07:24:13.000000Z
    com.android.version: 6.0.1
  Duration: 00:00:19.80, start: 0.000000, bitrate: 997 kb/s
    Stream #0:0(eng): Video: h264 (Baseline) (avc1 / 0x31637661), yuv420p, 320x240, 514 kb/s, SAR 1:1 DAR 4:3, 29.97 fps, 30 tbr, 90k tbn, 180k tbc (default)
    Metadata:
      rotate          : 90
      creation_time   : 2018-03-09T07:24:13.000000Z
      handler_name    : VideoHandle
    Side data:
      displaymatrix: rotation of -90.00 degrees
    Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 156 kb/s (default)
    Metadata:
      creation_time   : 2018-03-09T07:24:13.000000Z
      handler_name    : SoundHandle
At least one output file must be specified

At the beginning of the above output, we can see the FFMPEG version and configuration information followed by the actual information of the video.

If you want to skip the FFMPEG version and configuration information display and want the video information only then you can add another parameter -hide_banner to the command.

ffmpeg -i my-video.mp4 -hide_banner

Output:

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'my-video.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: isommp42
    creation_time   : 2018-03-09T07:24:13.000000Z
    com.android.version: 6.0.1
  Duration: 00:00:19.80, start: 0.000000, bitrate: 997 kb/s
    Stream #0:0(eng): Video: h264 (Baseline) (avc1 / 0x31637661), yuv420p, 320x240, 514 kb/s, SAR 1:1 DAR 4:3, 29.97 fps, 30 tbr, 90k tbn, 180k tbc (default)
    Metadata:
      rotate          : 90
      creation_time   : 2018-03-09T07:24:13.000000Z
      handler_name    : VideoHandle
    Side data:
      displaymatrix: rotation of -90.00 degrees
    Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 156 kb/s (default)
    Metadata:
      creation_time   : 2018-03-09T07:24:13.000000Z
      handler_name    : SoundHandle
At least one output file must be specified

Crop Video

The command for cropping video is:

ffmpeg -i input.mp4 -filter:v crop=w:h:x:y output.mp4

where,

– -filter:v means video filter to output
– crop=w:h:x:y means crop filter
– here,
– w = width of the cropped output video
– h = height of the cropped output video
– x = x-coordinate position of the input video from where we want to crop the video
– y = y-coordinate position of the input video from where we want to crop the video

ffmpeg -i input.mp4 -filter:v crop=100:100:12:34 output.mp4

// OR

ffmpeg -i input.mp4 -filter:v crop=w=100:h=100:x=12:y=34 output.mp4

More examples on crop video: here

Resize / Scale Video

The command for resizing video is:

ffmpeg -i input.mp4 -filter:v scale=200:200  output.mp4

// OR

ffmpeg -i input.mp4 -vf scale=200:200  output.mp4

where,

– -filter:v or -vf means video filter to output
– scale=w:h means scale filter
– here,
– w = width of the resized output video
– h = height of the resized output video

ffmpeg -i input.mp4 -filter:v scale=w=200:h=100  output.mp4

// OR

ffmpeg -i input.mp4 -filter:v scale=200x100  output.mp4

Double the size of the video

ffmpeg -i input.mp4 -filter:v scale=w=2*iw:h=2*ih output.mp4

Half the size of the video

ffmpeg -i input.mp4 -filter:v scale=w=iw/2:h=ih/2 output.mp4

More examples on resize video / scale video: here

Extract clip from video

You can extract a clip from a video using FFMPEG. A clip can like first 10 seconds of the video, or 20 seconds of the video starting at 10 seconds.

Here’s an example command:

ffmpeg -ss 00:00:10.0 -i input.mp4 -c copy -t 00:00:20.0 output.mp4

// OR

ffmpeg -ss 10 -i input.mp4 -c copy -t 20 output.mp4

where,

-ss means the position in the input file from where you want to clip the video
-t means the position in the input file where the clipping is stopped

They have two formats:

[-][HH:]MM:SS[.m…]

where,

HH = number of hours
MM = number of minutes for a maximum of 2 digits
SS = number of seconds for a maximum of 2 digits
m = decimal value for seconds (SS) (optional)

OR

[-]S+[.m…]

where,

S = number of seconds
m = decimal value for seconds (SS) (optional)

In both expressions, the optional ‘-’ indicates negative duration.

Join multiple videos

https://ffmpeg.org/faq.html#How-can-I-join-video-files_003f

https://trac.ffmpeg.org/wiki/Concatenate#protocol

Convert video from one format to another

Convert video to FLV format

ffmpeg -i input.mp4 output.flv -hide_banner

Convert video to MKV format

ffmpeg -i input.mp4 output.mkv -hide_banner

Convert video to animated GIF format

ffmpeg -i input.mp4 output.gif -hide_banner

Extract Audio from Video

Here’s the command to convert video into audio:

ffmpeg -i input.mp4 -vn -acodec copy output.aac

where,

-acodec = set the audio codec
-vn = skip inclusion of video in the output (-an will skip inclusion of audio in the output)

Convert video to MP3 audio format

ffmpeg -i input.mp4 -vn output.mp3

// OR

ffmpeg -i input.mp4 -vn -acodec mp3 output.mp3

Convert video to AAC audio format

ffmpeg -i input.mp4 -vn -acodec aac output.aac

Convert video to animated GIF format

ffmpeg -i input.mp4 output.gif -hide_banner

Extract only a part of the video to MP3 audio format

This command will extract audio from the input video. Plus, it will extract the audio starting at 10 seconds of the video and continue extracting for 20 seconds after that position.

ffmpeg -i input.mp4 -ss 10 -t 20 -vn -acodec mp3 output.mp3

Extract Images from Video

Extract image every frame of the video

Note: This will create a lots of images if you have longer video.

ffmpeg -i input.mp4 output%d.jpg

Extract image every second of the video

ffmpeg -i input.mp4 -vf fps=1 output%d.png

The output files will be output1.png, output2.png, output3.png, and so on.

Extract image every 5 second of the video

ffmpeg -i input.mp4 -vf fps=1/5 output%03d.png

The output files will be output001.png, output002.png, output003.png, and so on.

Extract image every minute of the video

ffmpeg -i input.mp4 -vf fps=1/60 output%04d.png

The output files will be output0001.png, output0002.png, output0003.png, and so on.

Extract image every 5 minutes of the video

ffmpeg -i input.mp4 -vf fps=1/300 output%d.png

The output files will be output1.png, output2.png, output3.png, and so on.

Extract 5 images after 5 seconds of the video

ffmpeg -i input.mp4 -ss 00:00:05.000 -vframes 5 thumb%04d.jpg

The output files will be thumb0001.jpg, thumb0002.jpg, thumb0003.jpg, thumb0004.jpg and thumb0005.jpg.

Extract image every I-frame (KeyFrame)

ffmpeg -i input.flv -vf "select='eq(pict_type,PICT_TYPE_I)'" -vsync vfr thumb%04d.png

The output files will be thumb0001.png, thumb0002.png, thumb0003.png, and so on.

Hope this helps. Thanks.