.png
files will have a PNG format (with the rare exception of PNG bombs that imgproxy can protect against )..mp4
, .wmv
, .webm
or .mov
) refers only to the container. While video files consist of three different components:.mp4
file extension, we can only say that the MP4 container was used. But the codecs in it can be different - the author could take H.264 and AAC, AV1 and Opus, or something else.If you wondered exactly how AV1 managed to beat the rest of the codecs in compression, read the technical details in the Habré translations:
" Next-generation video: introducing AV1 "
" The codec of the new generation AV1: corrective directional filter CDEF "
brew install ffmpeg
wget https://johnvansickle.com/ffmpeg/releases/ffmpeg-release-amd64-static.tar.xz
tar -xf ffmpeg-release-amd64-static.tar.xz
sudo cp ffmpeg-4.1-64bit-static/ff* /usr/local/bin/
.av1.mp4
, .hevc.mp4
and .h264.mp4
postfixes. Do not be scared by the long team, we will analyze it all: # SOURCE.mov - ffmpeg -i SOURCE.mov -map_metadata -1 -c:a libfdk_aac -c:v libx264 -crf 30 -profile:v main -pix_fmt yuv420p -movflags +faststart -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" video.h264.mp4
video.h264.mp4
. If the quality is good and the size is large, try increasing -crf
( -crf 35
then -crf 40
). This option will reduce the file size at the cost of reduced quality. Selecting a balance of quality and size is an art.If the original video file does not exist, then you can convert the old H.264 file to AV1.
ffmpeg -i SOURCE.mov -map_metadata -1 -c:a libopus -c:v libaom-av1 -crf 40 -b:v 0 -pix_fmt yuv420p -movflags +faststart -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -strict experimental video.av1.mp4
-crf
with -crf
for the perfect balance between quality and size. ffmpeg -i SOURCE.mov -map_metadata -1 -c:a libfdk_aac -c:v libx265 -crf 30 -pix_fmt yuv420p -movflags +faststart -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" video.hevc.mp4
video.h264.mp4
, video.hevc.mp4
and video.av1.mp4
to the root of your site.-i SOURCE.mov
indicates the incoming file, from where FFmpeg will take video and audio streams, compress them and pack them into a new container.-map_metadata -1
will remove the meta information from the video (for example, the program in which the video was created). In the Web, such information is rarely useful.-c:a libopus
or -c:a libfdk_aac
set audio codecs. If you don't need sound, replace them with -an
.-c:v libaom-av1
selects a video codec - a library that compresses the frames of a video stream.-crf 40
- Constant Rate Factor, balance of quality and size. It's like a JPEG quality slider, only it goes in a different direction (0 is the best quality and the largest file). The CRF scale is different for H.264 and AV1 - for H.264 it is up to 51, for AV1 it is up to 61. The CRF for AV1 and H.264 will be different.Facebook picked up an approximate correspondence between CRF values ​​for H.264 and AV1:
19 → 27, 23 → 33, 27 → 39, 31 → 45, 35 → 51, 39 → 57.
-profile:v main
used by H.264 to select a codec profile . Only “Main” will work in Safari.-b:v 0
sets the minimum bit rate for AV1, so that the video has a constant quality.-pix_fmt yuv420p
(pixel format) is a tricky way to reduce file size. It leaves the original resolution for brightness, but reduces the resolution for color. Our eyes see the color worse, so they do not notice this trick. Remove this option if in your case it will interfere.-movflags +faststart
moves everything important to the beginning of the file so that the browser can play the video before the download is complete.-vf "scale=trunc(iw/2)*2:trunc(ih/2)*2"
will resize the sides of the video to the nearest even ones (some codecs can work with a resolution of 300 Ă— 200 and 302 Ă— 200, but will not work with 301 Ă— 200). If you are sure that the resolution is divided by 2 everywhere, you can remove this option.-strict experimental
needed for AV1, its encoder is still experimental.video.av1.mp4
sets the name of the final file.type
attribute. And I advise you to read about the options in <video> . <video controls width="600" height="400"> <source src="video.hevc.mp4" type="video/mp4; codecs=hevc,mp4a.40.2" /> <source src="video.av1.mp4" type="video/mp4; codecs=av01.0.05M.08,opus" /> <source src="video.h264.mp4" type="video/mp4; codecs=avc1.4D401E,mp4a.40.2" /> </video>
if…else
expressions - the browser reads them from top to bottom until it finds one whose type
it supports.type
you can specify the entire file format: container ( video/mp4
for MP4), video codec ( av01.0.05M.08
for AV1, hevc
for HEVC and avc1.4D401E
for H.264) and audio codec ( opus
for Opus and mp4a.40.2
for AAC). ffmpeg -i IMAGE.gif -map_metadata -1 -an -c:v libx264 -crf 30 -profile:v main -pix_fmt yuv420p -movflags +faststart -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" video.h264.mp4
ffmpeg -i IMAGE.gif -map_metadata -1 -an opus -c:v libaom-av1 -crf 50 -b:v 0 -pix_fmt yuv420p -movflags +faststart -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -strict experimental video.av1.mp4
animation.h264.mp4
and animation.av1.mp4
into the HTML. <video autoplay loop muted playsinline width="300" height="200"> <source src="animation.av1.mp4" type="video/mp4; codecs=av01.0.05M.08" /> <source src="animation.h264.mp4" type="video/mp4" /> </video>
autoplay
and loop
options make a gif of video, a looped video that immediately plays after the page loads. playsinline
blocks Safari from opening the video to full screen when clicking on the video.Source: https://habr.com/ru/post/442020/
All Articles