Cosmophile's Blog: Home

FFmpeg notes on video processing

Table of contents

Combine an image with a music

Image stays on the screen, music plays. Not the best way though, but needed for some platforms.

ffmpeg -loop 1 \
-i image.png -i music.m4a \
-c:v libx264 \
-tune stillimage \
-c:a aac -b:a 192k -pix_fmt yuv420p \
-shortest \
-movflags +faststart \
output.mp4

Convert videos to share them on Zoom

Zoom requires videos to be at a maximum resolution of 1080p and supports h.264-formatted mov files.

Before going into technicals, an alternative GUI-based solution on MacOS is to open the video using QuickTime Player and subsequently selecting File > Export as > 1080p flow.

ffmpeg -i input.mp4 \
-vf "scale='if(gt(iw\,1920),1920,iw)':'-2'" \
-c:v libx264 \
-crf 18 \
-preset slow \
-c:a aac \
-movflags +faststart \
output.mov

The -preset parameter does not influence the output quality but rather affects the file size. Some available options are veryslow, slow, medium, fast, and ultrafast.

The +faststart flag enables progressive streaming, which can be beneficial.

The -c:v libx264 option specifies the use of the libx264 video codec (H.264), while -c:a aac specifies the use of the aac audio format.

The -crf parameter sets the constant rate factor, with lower values generally being preferred. Typical ranges are between 18 and 23.

The video filter -vf applied here reduces the input video width to 1920, which is equivalent to 1080p, while preserving the aspect ratio; if necessary.

A loop to apply this to all mp4 files under a folder:

for f in *.mp4; do
  ffmpeg -i "$f" \
  -vf "scale='if(gt(iw\,1920),1920,iw)':'-2'" \
  -c:v libx264 \
  -crf 18 \
  -preset slow \
  -c:a aac \
  -movflags +faststart \
  "${f%.mp4}.mov"
done