Skip to content

Instantly share code, notes, and snippets.

@ajdungan
Last active July 26, 2024 16:38
Show Gist options
  • Save ajdungan/aa5599d1c493d4eae98593e830bf0e64 to your computer and use it in GitHub Desktop.
Save ajdungan/aa5599d1c493d4eae98593e830bf0e64 to your computer and use it in GitHub Desktop.
whatsapp friendly video ffmpeg convert

ffmpeg commands to encode whatsapp friendly videos

Using these can make videos be able to be played natively within whatsapp. Also very helpful to make videos to be a small file size that is still decent quality. For example: to fit as an email attachment to to share with persons who have limted internet bandwidth).

Really long videos may not work or need to be further compressed or split into multiple files (possible to write/find a script to control for this). Size restrictions might change over time currently they are ________?

Method 1 - more options and filters (best option, usually)

source: https://newspaint.wordpress.com/2020/05/17/using-ffmpeg-to-encode-a-video-for-whatsapp/

Note: Main setting you might want to adjust:

Change -crf option to higher for smaller video size, but worse quality (max 51, normal default 23, set here @30). adjust as needed for file size/quality. What is accepable quality might change on video/video content.


ffmpeg -i VIDEOINPUT.mp4
-vf "scale=-2:480" \
-c:v libx264 \
-preset slower \
-crf 30 \
-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 \
output.mp4

one line code (same as above)


ffmpeg -i VIDEOINPUT.mp4 -vf "scale=-2:480" -c:v libx264 -preset slow -crf 30 -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 output.mp4

one line code #2 fewer options


ffmpeg -i VIDEOINPUT.mp4 -vf "scale=-2:480" -c:v libx264 -preset slow -crf 30 -profile:v baseline -level 3.0 -pix_fmt yuv420p -r 25 -g 50 -c:a aac -b:a 160k -r:a 44100 -f mp4 output.mp4

options explained (and possibly tweak)


  • -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 command which makes the audio as loud as possible (may not always work for you, test before using)
  • -f mp4 = output file as mp4 format

Method 2: simpler but maybe less reliable + larger files

source: https://ostechnix.com/convert-videos-to-whatsapp-video-format-with-ffmpeg/


ffmpeg -i VIDEOINPUT.mp4 -vcodec libx264 -acodec aac output.mp4

@ajdungan
Copy link
Author

shotcut

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment