newspaint

Documenting Problems That Were Difficult To Find The Answer To

Using FFmpeg to Encode a Video for WhatsApp

WhatsApp seems very picky about what video formats it will actually forward. FFmpeg can be used to process a video into a format that WhatsApp is more likely to accept for transmission.

The settings I have used are:

-vf "scale=-2:480" \
-c:v libx264 \
-preset slow \
-crf 21 \
-profile:v baseline \
-level 3.0 \
-pix_fmt yuv420p \
-r 25 \
-g 50 \
-c:a aac \
-b:a 160k \
-r:a 44100 \
-af "aformat=channel_layouts=stereo,highpass=f=40,volume=1.40,compand=attacks=0.1 0.1:decays=3.0 3.0:points=-900/-900 -100/-70 -70/-45 -45/-30 -40/-15 -20/-9 -10/-6 0/-4:soft-knee=0.50:gain=0:volume=-900:delay=3.0" \
-f mp4 \

Here’s an explanation of what the lines do (you can choose what to include yourself):

  • -vf “scale=-2:480” – sets up a video filter to scale the image, in this case it dynamically scales the width to a number divisible by 2, and sets the height to 480 pixels
  • -c:v libx264 – sets the video codec to H.264
  • -preset slow – chooses how much time to spend encoding the video, the slower the smaller the output (usually)
  • -crf 21 – chooses the constant rate factor (CRF) which determines what image quality the output will try and achieve (0 is lossless, 23 is default, 51 is worst quality possible)
  • -profile:v baseline – chooses the specific H.264 profile, some devices (maybe WhatsApp) only support the more limited baseline or main profiles
  • -level 3.0 – seems to be necessary with the above baseline flag to be as compatible with as many devices as possible, including Android
  • -pix_fmt yuv420p – QuickTime compatibility
  • -r 25 -g 50 – 25 frames per second, and an index frame every 50 frames
  • -c:a aac – set audio codec to AAC
  • -b:a 160k – set bitrate of audio to 160k
  • -r:a 44100 – set audio sampling rate to 44.1KHz
  • -af “aformat=channel_layouts=stereo,highpass=f=40,volume=1.40,compand=attacks=0.1 0.1:decays=3.0 3.0:points=-900/-900 -100/-70 -70/-45 -45/-30 -40/-15 -20/-9 -10/-6 0/-4:soft-knee=0.50:gain=0:volume=-900:delay=3.0” – whole host of audio filters going on here, the big one is the compand which makes the audio as loud as possible (may not always work for you, test before using)
  • -f mp4 – output file as mp4 format

You should add the input file (with the -i flag) at the start of the ffmpeg command, and finish with the path/name of the file (with the .mp4 extension) you wish to output to.

Leave a comment