ShinChven's Blog

My Frequently Used FFmpeg Commands

Sat Aug 06 2022

Compress Video with Given Encoding Parameters

ffmpeg -i input.mp4 -c:v libx264 -b:v 6000k -b:a 192k -vf scale=1920:1080 output.mp4
  • -i: Input file
  • -c:v: Video codec
  • -b:v: Video bitrate, 50m for 4K and 6000k for 1080P are advised.
  • -b:a: Audio bitrate
  • -vf scale=1920:1080: Filter, scale video resolution
  • output.mp4: Output file

Hardware Acceleration

FFmpeg has a lot of hardware acceleration supports for each platform, for example, videotoolbox is the hardware acceleration codec for macOS:

ffmpeg -i input.mp4 -c:v hevc_videotoolbox -tag:v hvc1 output.mp4
ffmpeg -i input.mp4 -c:v h264_videotoolbox output.mp4
  • -c:v hevc_videotoolbox: Use hevc_videotoolbox as the video codec
  • -tag:v hvc1: Set the video tag to hvc1 for Final Cut Pro HEVC support.


ffmpeg -i input.webm -c:v libx264 -vf scale=1920:1080,zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=tonemap=hable:desat=0,zscale=t=bt709:m=bt709:r=tv,format=yuv420p output.mp4
  • -vf: Filters
  • scale=1920:1080: Scale video resolution
  • zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=tonemap=hable:desat=0,zscale=t=bt709:m=bt709:r=tv,format=yuv420p: SDR Color grading

Copy Audio or Video Streams

ffmpeg -i input.mp4 -c:v copy -c:a copy output.mkv
  • -c:v copy: Copy video codec
  • -c:a copy: Copy audio codec

Copy Audio out of a Video File

ffmpeg -i input.mp4 -vn -acodec copy output.aac
  • -vn: Disable video
  • -acodec copy: Copy audio codec

Convert Video with Similar Bitrate

Tips: Convert video with similar or higher bitrate preserves the quality of the original video well.

Firstly, we need to get the video and audio bit_rate of the input file:

ffprobe -v quiet -print_format json -show_entries stream=index,codec_name,codec_type,bit_rate input.mp4

Let's assume the video bit_rate is 6000k and the audio bitrate is 192k, then we can convert the video with similar bitrate:

ffmpeg -i input.mp4 -c:v libx264 -b:v 6000k -c:a aac -b:a 192k output.mp4
  • -c:v libx264: Use libx264 as the video codec
  • -b:v 6000k: Set the video bit_rate to 6000k
  • -c:a aac: Use aac as the audio codec
  • -b:a 192k: Set the audio bit_rate to 192k

Merge Audio and Video Files

ffmpeg -i input.mp4 -i input.aac -c copy output.mkv
  • -i input.mp4: Input video file
  • -i input.aac: Input audio file
  • -c: Copy

Cut Video

I wrote a nodejs wrapper of FFmpeg to cut video into clips with given timestamps in a config file, which I think is a better way to cut video.

# Cut by timestamp
ffmpeg -ss 00:00:30.0 -i input.wmv -t 00:00:10.0 -c copy output.wmv
# Cut by frame number
ffmpeg -ss 30 -i input.wmv -t 10 -c copy output.wmv
  • Arguments' order is important, fail to keep them in the following order -ss <START_TIME> -i <INPUT_FILE> -t <END_TIME> [other] will result in getting a corrupted video. Official document on this matter.

  • -ss: Start time

  • -i: Input file

  • -t: Duration

  • -c: Copy content

  • output.mp4: Output file

Merge Files in Directory

ffmpeg -f concat -safe 0 -i <(for f in *.mp4; do echo "file '$f'"; done) -c copy output.mp4
  • -f: File format
  • -safe: Safe mode
  • -i: Input file
  • -c: Copy
  • output.mp4: Output file
  • *.mp4: Files in directory
  • -i <(for f in *.mp4; do echo "file '$f'"; done): Command to generate input file
  • -c copy: Command to copy
  • output.mp4: Output file

Remove Tag or Chapters

ffmpeg -i input.mp4 -map_metadata -1 -map_chapters -1 -vcodec copy -acodec copy output.mp4
  • -map_metadata -1: Remove metadata
  • -map_chapters -1: Remove chapters
  • -vcodec copy: Copy video codec
  • -acodec copy: Copy audio codec