This page looks best with JavaScript enabled

A Simple Tutorial on Using FFmpeg

 ·  ☕ 2 min read

1. Installing FFmpeg

Run this command on macOS:

1
brew install ffmpeg

2. Using FFmpeg

1
ffmpeg {1} {2} -i {3} {4} {5}

The five parts of the arguments are, in order:

  1. Global arguments, such as -y, -loglevel, -preset, which control the overall behavior
  2. Input file arguments, such as -i, -ss, -t, -stream_loop, which control how the input file is read
  3. Input file, the path to the audio/video input file to process
  4. Output file arguments, such as -c:v, -c:a, -vf, -af, which control the encoding and filtering of the output
  5. Output file, the path to the processed output audio/video file

Common output arguments:

-vcodec specifies the video codec; copy means copy only, with no encoding or decoding

-acodec specifies the audio codec; copy means copy only, with no encoding or decoding

When using the copy argument, because no encoding or decoding is done, it is much faster. If the input and output files have different codecs, it will error out. In addition, FFmpeg guesses the container format from the file extension.

3. Some Usage Scenarios

  • Inspecting video information
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
ffmpeg -i input.mp4

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'input.mp4':
  Duration: 00:01:03.11, start: 0.000000, bitrate: 257 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709), 640x360 [SAR 1:1 DAR 16:9], 210 kb/s, 30 fps, 30 tbr, 16k tbn, 60 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (HE-AAC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 39 kb/s (default)
    Metadata:
      handler_name    : SoundHandler

It contains two streams: one h264-encoded video stream and one aac-encoded audio stream.

  • Extracting the audio
1
ffmpeg -i input.mp4 -vn -acodec copy output.aac

-vn means do not process the video stream, and -acodec copy means copy the audio stream directly without encoding or decoding.

  • Converting the audio format
1
ffmpeg -i output.aac output.mp3
  • Extracting the video
1
ffmpeg -i input.mp4 -an -vcodec copy output.h264
  • Converting the video format
1
ffmpeg -i output.h264 output.mkv
  • Merging audio and video
1
ffmpeg -i output.h264 -i output.aac output-2.mp4

4. References


WeChat Official Account
WRITTEN BY
WeChat Official Account