ffmpeg

Command-line tools for processing video and audio files

18 Sep 2022

FFmpeg is a free and open-source software project consisting of a suite of libraries and programs for handling video, audio, and other multimedia files and streams. At its core is the command-line ffmpeg tool itself, designed for processing of video and audio files. It is widely used for format transcoding, basic editing (trimming and concatenation), video scaling, video post-production effects and standards compliance (SMPTE, ITU).

FFmpeg also includes other tools: ffplay, a simple media player and ffprobe, a command-line tool to display media information. Among included libraries are libavcodec, an audio/video codec library used by many commercial and free software products, libavformat (Lavf), an audio/video container mux and demux library, and libavfilter, a library for enhancing and editing filters through a Gstreamer-like filtergraph.

FFmpeg is part of the workflow of many other software projects, and its libraries are a core part of software media players such as VLC, and has been included in core processing for YouTube and Bilibili. Encoders and decoders for many audio and video file formats are included, making it highly useful for the transcoding of common and uncommon media files.

You can use Homebrew to install ffprobe (installed along with ffmpeg):

brew install ffmpeg

Else:

Download at:

Commands

Optimise video

06 Jun 2024

ffmpeg -i input.mp4 -vcodec libx265 -crf 20 -preset slow -acodec aac -b:a 128k output.mp4
   -vcodec libx265: Use the H.265 codec.   -crf 28: Constant Rate Factor, lower values mean better quality. 28 is a good starting point.   -preset slow: Encoding speed vs compression ratio. Slower presets result in better compression.   -acodec aac -b:a 128k: Use AAC for audio and set the audio bitrate to 128 kbps.

The crf scale ranges from 0 to 51:

•   crf 0: Lossless encoding (highest quality, very large file size).
•   crf 23: Default value, providing a good balance between quality and file size.
•   crf 51: Lowest quality (smallest file size).

In practice, the following ranges are commonly used:

   18-22: High quality (visually lossless). Suitable for archiving or high-quality viewing.   23-28: Standard quality. Good for general-purpose use, offering a balance between quality and file size.   28-34: Low quality. Useful for scenarios where file size is a more critical factor than quality, such as streaming over limited bandwidth.

To use VBR (Variable Bit Rate) instead of CRF:

ffmpeg -i input.mp4 -vcodec libx265 -crf 28 -preset slow -b:v 1000k -maxrate 1500k -bufsize 2000k -acodec aac -q:a 2 output.mp4

With 73Mb video source file:
- 120Mb at CRF 0
- 4Mb at CRF 28 (but lower quality)
- 6Mb at VBR 1000k (better quality than CRF 28)

optimise for Powerpoint

ffmpeg -i input.mp4 -vcodec libx264 -pix_fmt yuv420p -profile:v baseline -level:v 3.1 -preset slow -crf 30 -maxrate 1000k -bufsize 2000k -acodec aac -b:a 128k -movflags +faststart output.mp4

913Kb file from 11.4Mb!

or

12 Oct 2024

ffmpeg -i /input/folder/file_raw.mov -c:v libx264 -crf 23 -preset medium -c:a aac -b:a 128k /output/folder/file_light.mp4

optimise for sharing

09 Jun 2024

ffmpeg -i input.mp4 -vcodec libx264 -crf 28 -preset slow -maxrate 2M -bufsize 4M -vf "scale=-2:720" -acodec aac -b:a 128k -movflags +faststart output.mp4

16Mb from 177Mb file, in 12s.

Remove Audio

ffmpeg -i input.mp4 -an -vcodec copy output.mp4

• -an tells FFMPEG to remove the audio stream.
• -vcodec copy tells FFMPEG to copy the video stream without re-encoding it.

Changing Frame Rate

ffmpeg -i <input> -r 24 <output>

Scaling

Scale to 1280×720 or smaller if needed:

ffmpeg -i <input> -vf "scale=1280:720:force_original_aspect_ratio=decrease" <outpu

http://trac.ffmpeg.org/wiki/Scaling%20(resizing)%20with%20ffmpeg

https://superuser.com/questions/547296/

Fading

Simple fade-in and fade-out at a specific time for a specific duration.

ffmpeg -i <input> -filter:v \
  "fade=t=in:st=0:d=5,fade=t=out:st=30:d=5" \
  <output>

Drawing Text

Complex system for printing text on video:

ffmpeg -i <input> -vf \
drawtext="text='Test Text':x=100:y=50:\
fontsize=24:fontcolor=yellow:box=1:boxcolor=red" \
<output>

Various options related to font family, size, position, color, …

See: http://ffmpeg.org/ffmpeg-all.html#drawtext-1

Timeline Editing

Enable filters only at a specific point in time.

Example:

Show a watermark in the top left corner
Between seconds 1 and 2 only

ffmpeg -i <video> -i <watermark> -filter_complex \
    "[0:v][1:v]overlay=10:10:enable='between(t,1,2)'[outv]" \
    -map "[outv]" <output>

See: http://ffmpeg.org/ffmpeg-all.html#Timeline-editing

Resources

links

social