Ch. 18 · Encoding Recipes and How to Decide

한국어日本語
8 min

Recipe 2: Web Embedding and Messenger Sharing

For web embedding, compatibility and start-up speed (faststart, fallbacks) drive the decisions; for messenger sharing, the size limit does. For limit-driven jobs, calculate the bitrate first and accept that quality depends on how hard the video is.

For web embedding: the <video> on your own site

For video played directly on your own site, the key constraint is that you can't control who is watching. So take the most widely playable combination as the base, and add efficient codecs on top of it as an option.

  • The base combination is H.264 + AAC + MP4, with the pixel format set to yuv420p. It is widely used as the most conservative choice for compatibility.
  • Put moov at the front with `-movflags +faststart`. The aim is for playback to start before the download finishes; the reason is explained in the faststart lesson.
  • Rate control is CRF. There is no size limit, and if viewers' bandwidth is a concern, consider making several resolutions or using adaptive streaming such as HLS.
  • Use efficient codecs (such as AV1) together with a fallback. In this site's measurements SVT-AV1 reached a similar or higher VMAF at 41–63% of x264's size (crf-sweep), but some devices can't play it. You can see per-device support with the codec support checker.
Web base combination example. Verify the CRF value for each video
ffmpeg -i input.mov \
  -c:v libx264 -preset medium -crf 23 -pix_fmt yuv420p \
  -c:a aac -movflags +faststart web.mp4
Markup example that puts the efficient codec first and falls back to H.264
<video controls playsinline preload="metadata" width="960">
  <source src="clip-av1.mp4" type='video/mp4; codecs="av01.0.08M.08"'>
  <source src="clip-h264.mp4" type='video/mp4; codecs="avc1.640028"'>
</video>
The codecs string has to match the file

The codecs values inside type are examples. If they don't match the file's actual profile, level, and bit depth, the browser may misjudge the source. Before you deploy, check with the codec support checker or by playing the file in several browsers. You can see the file's actual codec and resolution in the video file info inspector.

For messenger and email sharing: the size limit comes first

A video to send in a messenger usually has a 'no more than X MB' limit. Limits change by service and over time, so this site does not state any particular service's number. The mere fact that there is a limit settles the recipe: rate control is a fixed bitrate (2-pass), and you leave the calculation to a calculator.

Bitrate calculation: an example with 10 MB and 30 seconds

  1. 110 MB = 10 × 8 × 1,000,000 = 80,000,000 bit (taking 1 MB as 1,000,000 B).
  2. 2Divided by 30 seconds, the total is about 2,667 kbps.
  3. 3If the audio is 128 kbps, the video must stay within about 2,539 kbps. To allow for container overhead and error, it is safer to aim a bit lower (for example, 2,400 kbps).

If calculating by hand is a hassle, enter the duration and target size into the video size and bitrate calculator. That tool also shows which VMAF that bitrate reached in the measured data. You can continue building the command in the FFmpeg command builder.

Limit-driven: set the bitrate and run 2-pass (Linux and macOS)
ffmpeg -y -i input.mp4 -c:v libx264 -preset medium -b:v 2400k -pass 1 -an -f null /dev/null
ffmpeg -i input.mp4 -c:v libx264 -preset medium -b:v 2400k -pass 2 -c:a aac -b:a 128k -movflags +faststart share.mp4

Within a limit, the video decides the quality

Fixing the size leaves the quality open. In this site's measurements, even at the same size the results can diverge widely. In crf-sweep, the 1080p25 10 s video at x264 medium CRF 28 was 2,490 kbps for blue_sky and 11,623 kbps for riverbed. If you set 2,400 kbps as above, blue_sky is close to the bitrate CRF 28 used (2,490 kbps), but riverbed gets only about 21% of the bitrate CRF 28 used (2,400 ÷ 11,623). It means that at 1080p, a video like riverbed can lose a lot of quality (the exact VMAF can't be known from this calculation), and the ways to handle that are as follows.

  • Reduce the resolution. Even at the same bitrate, fewer pixels means more bits per pixel. Lowering the resolution is known to often help at low bitrates, but this site has not measured by resolution, so we can't say by how many percent. For the method, see the scale filter.
  • Shorten the duration. With a fixed limit, shortening the clip is the surest way to raise the bitrate.
  • Trim the frame rate or unneeded parts (black bars). Throw away useless pixels with crop and pad.
In practice: for sharing, you don't know the viewing device

In most cases you don't know whether the recipient will watch on a phone or a PC, or which codecs their app plays. The default for sharing is the conservative combination of H.264 + AAC + MP4 + yuv420p, and when the size runs short, cutting resolution and duration first carries less risk than changing the codec.

Fallback sourceFallback source

A setup where several <source> elements are listed in order inside <video>, so a browser that can't play the earlier codec uses the next source.

faststartFast start

A setting that puts the MP4's moov at the front of the file so playback can begin during the download (-movflags +faststart).

Codecs stringCodecs string

A string like avc1.640028 written in a type attribute or in HLS CODECS. It carries the codec and values such as profile and level, so the browser can judge in advance whether it can play the file. The tool 'Codec String Decoder' shows how to read one.

avc1avc1 (H.264)

The codecs-string prefix for H.264. In avc1.640028, the six characters after the dot are split into pairs: profile 0x64 (High), constraint flags 00, and level 0x28 (= 40, i.e. 4.0).

hvc1 / hev1hvc1 / hev1 (HEVC)

The codecs-string prefix for HEVC. hvc1.1.6.L123.B0 reads as profile 1 (Main), tier L (Main tier), and level 123 ÷ 30 = 4.1. The two prefixes differ in whether the parameter sets sit in the file header or may also appear inside the samples.

av01av01 (AV1)

The codecs-string prefix for AV1. av01.0.08M.08 reads as profile 0 (Main), level number 08 (= 4.0), tier M, and 8-bit.

TierTier

A further split inside each HEVC or AV1 level, with Main and High variants. In a codecs string HEVC writes it as L or H, and AV1 as M or H.

canPlayTypecanPlayType

A method that asks a browser's media element whether it can play a given format. It returns only "probably", "maybe", or an empty string, and an empty string means it cannot play it.

MediaCapabilitiesMediaCapabilities

A browser API where you pass a codec, resolution, fps and bitrate and get back whether it is supported, smooth, and power efficient. A power-efficient 'yes' is usually a sign that a hardware decoder handles it.

Check yourself

To fit a 30-second video into 10 MB (= 80,000,000 bit), what is the approximate upper limit of the overall (video + audio) average bitrate?

Check yourself

Why use -movflags +faststart on an MP4 for web embedding?