Compressing videos

Preamble

This article explains how to reduce the filesize of videos and some popular video codecs.
This is a simple comparison of my own experiments and methods on how to reduce filesizes.
To install the tools mentioned here:

Compressing videos

To simply compress a video you can use, for example, ffmpeg -i "input.mp4" -crf 35 output.mp4. crf means "constant rate factor" and the higher you set it the more compressed the videos get. The higher the "crf" the lower the quality and the lower the filesize. Every video codec has its own interpretation of the crf value and there is no fixed number that works for every codec.

Another example to compress further: You can change the codecs to more modern ones. Simply add -vcodec h265 -acodec aac inside the ffmpeg command to change the codecs of video/audio to h264/aac respectively. (This would make the command look like ffmpeg -i "input.mp4" -crf 25 -vcodec h265 -acodec aac output.mp4)

If the framerate of the video is 60 you could lower it to 30 to save space. Simply add -r 30 inside the ffmpeg command to limit the framerate to 30.

Some videos have an extremely high resolution (e.g. 4K) and you can save a LOT of space if you limit the resolution to a lower one. Simply add -vf "scale=1280:-1" to set the resolution of the output video to HD.
(The "-1" in the height means "auto set height to match aspect ratio of input video with 1280 width".)

With all the settings combined the command would look like:

ffmpeg -i "input.mp4" -crf 25 -vcodec h265 -acodec aac -r 30 -vf "scale=1280:-1" output.mp4
This once lowered the filesize of a 4K 30min video from 3.2GB down to 800MB.

Sometimes the re-encoding of content is enough to lower the filesize.
For example, twitch.tv or Nvidia's Shadowplay use "live video streaming", which mostly uses a fixed bitrate. They do not compress videos because it would need a lot of CPU power. The less compression / "efficient filesize usage" the video has, the faster it is to encode. An example for this is the "fast" preset for video codecs, it raises filesize but lowers encoding time.
An example with a Nvidia Shadowplay video, simply re-encoding it:
user@debian:~/Videos$ ls -ahl
-rwxrwxrwx 1 user user 341M Jan 22 16:41 'divinity2_20220122_16.mp4'
user@debian:~/Videos$ ffmpeg -i "divinity2_20220122_16.mp4" -crf 25 _divinity2_20220122_16.mp4
user@debian:~/Videos$ ls -ahl _divinity2_20220122_16.mp4
-rwxrwxrwx 1 user user 54M Feb  3 10:31 _divinity2_20220122_16.mp4

An example with twitch.tv clips:

Elden Ring gameplay (running around, camera movement, action, no static screens)
Bitrate(kbit/s)SizeTypeCommand
540038.7M60FPS/FHD/source file/
670049.1M60FPS/FHD-acodec aac -vcodec h264
610044.9M30FPS/FHD-acodec aac -vcodec h264 -r 30
350026.8M60FPS/HD-acodec aac -vcodec h264 -vf "scale=1280:-1"
320023.9M30FPS/HD-acodec aac -vcodec h264 -vf "scale=1280:-1" -r 30

Vlog video (static camera, not much movement, focus on talking)
Bitrate(kbit/s)SizeTypeCommand
510038.4M60FPS/FHD/source file/
11008.99M60FPS/FHD-acodec aac -vcodec h264
10008.51M30FPS/FHD-acodec aac -vcodec h264 -r 30
4644.41M30FPS/HD-acodec aac -vcodec h264 -vf "scale=1280:-1" -r 30

Both of these videos above are taken from a single livestream.
The bitrate of action gameplay and mostly static content is very similar, as can be seen above, because live-streaming services do not compress for speed purposes.
Simply re-encoding live-stream videos are a good way to save a lot of storage space.

Codecs

There exist many different video codecs. The popular ones are:


The most widely supported one is H264, it runs on any device with a video player.
H265 is more optimized but lacks compatibility. Firefox on desktop can not play H265 encoded videos, but mobile firefox can.
H266 is very new and not widely adopted.
VP8 and VP9 are mostly used for older .webm files and are superseded by AV1.
AV1 can be in .mp4, .webm and .mkv files and is used by e.g. youtube.

H26* codecs are patented and licensed. AV1 is open and royalty-free, made by AOMedia (Google, Netflix, Cisco, ...).
AV1 is very optimized (similar to H265) but also has a very wide adoption rate and compatibility. Firefox on desktop and mobile can play AV1 videos.
H265 seems to have a low adoption and compatibility rate because of the patent and licensing problems it has. Example: https://en.wikipedia.org/wiki/H.265#Controversy

In Windows 10 the H265, H266 and AV1 video files have no preview image on the icon display, but H264 videos generate it correctly.

Encoders

AV1 is a codec but to actually encode something to the format you need an encoder.

To list all AV1 encoders in ffmpeg:

$ ffmpeg -hide_banner -encoders | grep -i av1
V....D libaom-av1           libaom AV1 (codec av1)
V....D librav1e             librav1e AV1 (codec av1)
V..... libsvtav1            SVT-AV1(Scalable Video Technology for AV1) encoder (codec av1)
V....D av1_d3d12va          D3D12VA av1 encoder (codec av1)
V....D av1_nvenc            NVIDIA NVENC av1 encoder (codec av1)
V..... av1_qsv              AV1 (Intel Quick Sync Video acceleration) (codec av1)
V....D av1_amf              AMD AMF AV1 encoder (codec av1)
V....D av1_mf               AV1 via MediaFoundation (codec av1)
V....D av1_vaapi            AV1 (VAAPI) (codec av1)
V....D av1_vulkan           AV1 (Vulkan) (codec av1)
A....D wmav1                Windows Media Audio 1

"libaom-av1" is the reference encoder for AV1, it is very very slow but has "near-perfect" execution and a higher compression rate than other encoders.
The current recommendation for AV1 is "libsvtav1", it is fast and compresses well.

For 264,265,vp8,vp9 and 266 there are only one main encoder, which will be tested below.
They are: libx264, libx265, libvpx, libvpx-vp9 and vvcenc.

Benchmarking codecs and their compression with vmaf

VMAF (Video Multi-Method Assessment Fusion) is a library to objectively compare video quality between 2 videos.
It can be used to compare different video codecs with each other in regards to "filesize-to-quality" ratio.
ffmpeg supports vmaf out-of-the-box and will be used for the comparisons below.
An example usage of libvmaf:

ffmpeg -i distorted.mpg -i reference.mpg -lavfi libvmaf=log_path=output.xml -f null -

The comparisons with libvmaf take longer than the encoding most of the time. With a 1920x1080 60fps video it ran at 0.26x speed.

The goal of this benchmark is to compare different video encoders and the time they took to convert, the resulting filesize and the vmaf score to the source video.
ffmpeg version 9.0 was used for these conversions.
3 Videos were used:

A 12 second tiktok dance video with a static background, downloaded with yt-dlp.
Source is H265 30fps 1080x1920p.

A 1 minute music video with many cuts, moving and background effects, but also some rather static shots.
Source is H264 60fps 1920x1080p.

A 22 minute anime episode, "Tomo-chan is a girl! Episode 3".
Source is H264 23fps 1280x720p.

The results:

12s tiktok dance

flagsencode timequalityfilesizesize diff
/input/0s1001857361100
-vcodec libx26413.862826518s78.5185884747648255
-vcodec libsvtav112.417688337s78.3635063050787164
-vcodec libx26523.296691749s77.4751432048236110
-vcodec libx264 -r 3013.153794256s43.4732224724905254
-vcodec libsvtav1 -r 3012.251826181s43.5840063037270163
-vcodec libx265 -r 3021.872826926s43.7077352054583110
-vcodec libx264 -crf 636.53311335s48.74403224856513
-vcodec libx265 -crf 5115.964289776s52.21139021541511
-vcodec libsvtav1 -crf 6311.452395578s63.78842229971016
-vcodec libsvtav1 -preset 535.679031488s78.5562702703378145
-vcodec libsvtav1 -preset 5 -crf 3636.787216761s78.4074362541498136
-vcodec libsvtav1 -preset 5 -crf 4237.60140495s77.241037182309898
-vcodec libsvtav1 -vf atadenoise12.967361453s78.3538803013724162
-vcodec libsvtav1 -vf hqdn3d13.033108112s78.1421302921178157
-vcodec libx265 -vf atadenoise22.369891418s77.3805412027430109
-vcodec libx265 -vf hqdn3d22.797749604s77.0679501980212106
-vcodec libx264 -vf atadenoise13.60036988s78.3852644744122255
-vcodec libx264 -vf hqdn3d14.249355655s78.0831064613542248
Observations:

1min music video

flagsencode timequalityfilesizesize diff
/input/0s10083341348100
-vcodec libx2642m18.452027487s71.3412843833588545
-vcodec libsvtav11m46.818796498s72.1592432100867725
-vcodec libx2653m57.057959191s69.6957341280251615
-vcodec libx264 -r 301m33.472165025s21.3584683222370838
-vcodec libsvtav1 -r 3059.474596951s21.6432231451270917
-vcodec libx265 -r 302m45.375872608s21.4065211141991513
-vcodec libx264 -crf 631m38.852187208s30.89366532822853
-vcodec libx265 -crf 512m59.600133385s31.01278917941722
-vcodec libsvtav1 -crf 631m43.538152334s59.39344725370953
-vcodec libsvtav1 -preset 46m23.049201576s72.3593251829262521
-vcodec libsvtav1 -preset 54m58.157046422s72.3289051854286522
-vcodec libsvtav1 -preset 63m39.611192232s72.3017761911343422
-vcodec libsvtav1 -preset 72m23.515272883s72.1182011980515923
-vcodec libsvtav1 -preset 81m48.395351859s72.1592432100867725
-vcodec libsvtav1 -preset 91m1.818236216s72.0748421960375523
-vcodec libsvtav1 -preset 1045.382436567s70.2124671922182523
-vcodec libsvtav1 -preset 1141.889777764s69.5517121931525923
-vcodec libsvtav1 -preset 1245.195070846s69.5517121931525923
-vcodec libsvtav1 -preset 1341.919028717s69.5517121931525923
-vcodec libsvtav1 -preset 5 -crf 364m46.768056974s72.2235501740333620
-vcodec libsvtav1 -preset 5 -crf 424m56.988644991s71.5662861249167614
-vcodec libsvtav1 -vf atadenoise1m53.404954076s72.1567312089142225
-vcodec libsvtav1 -vf hqdn3d1m56.136302554s70.6991222042293224
-vcodec libx265 -vf atadenoise4m4.45587605s69.4350551228860814
-vcodec libx265 -vf hqdn3d4m6.005874417s68.1005061220562014
-vcodec libx264 -vf atadenoise2m34.889483814s71.1894773715844644
-vcodec libx264 -vf hqdn3d2m40.552963929s69.6697933567859642
Observations:

22min anime episode

flagsencode timequalityfilesizesize diff
/input/0s100236968602100
-an -vcodec libx2646m15.345760508s87.34435314836113176
-an -vcodec libsvtav17m11.761984244s87.3570779775086450
-an -vcodec libx2659m55.586736357s85.8898575521712028
-an -vcodec libx264 -tune animation6m44.460372372s87.54842112601146964
-an -vcodec libx264 -crf 633m16.445515058s47.17945481216714
-an -vcodec libx264 -crf 63 -tune animation3m38.845457439s48.23533476015323
-an -vcodec libx265 -crf 518m8.995447947s49.52005458083452
-an -vcodec libsvtav1 -crf 637m4.740727534s75.700035111253115
-an -vcodec libsvtav1 -preset 518m18.19655016s87.5176439052694246
-an -vcodec libsvtav1 -preset 5 -crf 3617m50.142429102s87.4209898446620043
-an -vcodec libsvtav1 -preset 5 -crf 4218m46.927339274s86.7916755876702730
-an -vcodec libsvtav1 -vf atadenoise8m6.070107256s87.3097949704920349
-an -vcodec libsvtav1 -vf hqdn3d7m40.545940213s86.3053668825200645
-an -vcodec libx265 -vf atadenoise11m4.700751616s85.7325735366374827
-an -vcodec libx265 -vf hqdn3d10m57.35642272s84.6995705093216426
-an -vcodec libx264 -vf atadenoise6m43.135803649s87.20404013947488271
-an -vcodec libx264 -vf hqdn3d6m31.558524192s86.07493112034938161
-an -vcodec libsvtav1 -preset 422m5.078420825s87.5448988785979345
-an -vcodec libsvtav1 -preset 612m29.970540259s87.3589978780957345
-an -vcodec libsvtav1 -svtav1-params keyint=10s:aq-mode=2:tune=0:enable-overlays=1:scd=1:scm=17m39.638659687s87.3801989532313948
Observations:

Benchmark result interpretation

AV1 was consistently fast in encoding, low in filesize and high in quality compared to the original.
x265 has a very high compression, reaching the smallest filesizes in every comparison but also having a lower quality compared to the other codecs.
Denoising with atadenoise or hqdn3d helps with lowering filesize but also reduces quality.

I, personally, would recommend:


My default encoder is "libsvtav1" nowadays. 265 has lower compatibility because of its patent-problems and 264 is inefficient with its filesizes.

About GPU encoding

Modern GPUs support the option to encode and decode video streams directly on the GPU.
This is way faster than using the CPU, but also very limited in regards to compression.

Ffmpeg supports "libx265" for HEVC encoding via CPU and, for nvidia, the "hevc_nvenc" codec for GPU encoding.
Comparing these 2 is difficult, because hevc_nvenc does not support the crf flag.
The input file is 1.8MB in size, with libx265 it goes down to 1.1MB (-0.7MB) but with hevc_nvenc it goes up to 2.8MB (+1MB).
It is expected to have a lower filesize with the HEVC codec becaues it is more optimized for lower filesizes. GPU encoding prioritizes speed over filesize.

(OLD) video codec comparison

(This has been written and tested years ago, with ffmpeg v7. It is kept for historical purposes)

I am not an expert with video codecs.
I simply tested a 1 minute long clip with different encodings and input parameters. The table below shows my findings in "time taken for encoding" and "resulting filesize".

The input file is a 1 minute long clip from the "YuruCamp" movie (to be precise its from the 2nd to the 3rd minute of the movie).
The input video is h264 1920x1080 23.98fps and the audio is aac 128kb/s 48000Hz stereo.
The video has a few scenes with barely any movement and near the end firework explosions, which should make it a good clip to compare multiple codecs.

Compared codecs:

All these commands have been run with:ffmpeg -benchmark -ss 120 -i "yurucamp_movie.mp4" -t 60 $params output.mp4 , where $params will be replaced with the flags from the below table.
If no explicit video codec is mentioned then it is the default h264 codec. Every video has been converted to and from FullHD.
FlagsSize(MB)Timenotes
/none/22MB24.510sh264
-movflags +faststart22MB24.217s1:1 same as above
-fps_mode vfr22MB25.006sno difference to above
-tune stillimage27MB26.464sonly good for slideshows
-tune animation18.7MB27.419sgood for anime
-tune animation -preset slow17.5MB41.268snot worth the time
-vcodec libx2657.8MB44.043sbad compatibility
-vcodec libx264 -crf 512MB13.513svery bad pixelation
-vcodec libvpx5.1MB44.044s=vp8, .webm, fluctuating quality
-vcodec libvpx-vp913MB172.476svp9, .mp4
-vcodec librav1e9.9MB1161.541svery slow
-vcodec librav1e -speed 1013MB432.934still slow
-vcodec libaom-av1???~17minnot worth it
-vcodec libsvtav19.9MB19.499svery good compared to h265
-vcodec libsvtav1 -crf 407.4MB18.623sdefault=35
-vcodec libsvtav1 -crf 631.9MB14.580sstill watchable

Takeaways and learnings from these convertions:




2026.09.26