📚 Encyclopedia

Web Development · updated 2026-10-13

Encoding phone clips for the web (HEVC remux, H.264 fallback)

ffmpegvideohevch264snapchatadbfaststartsafari

The recipe used to publish phone videos on videos.jhampton.io: serve an HEVC rendition to browsers that can decode it and an H.264 file to everyone else, produced with as little generational loss as possible.

Getting the clips

Clips come off Justin's Android phone (ZTE nubia NX733J) over adb: adb shell ls -la /sdcard/DCIM/Snapchat/ to find today's, adb pull to copy. ffmpeg/ffprobe are a static build in ~/bin (a headless session must add it to PATH). That build has no hardware encoders (no vaapi/qsv/amf/nvenc).

What Snapchat writes

Mostly HEVC, portrait 1088x1936, ~7 to 9 Mbps, AAC mono 44.1 kHz, tagged hvc1. - creation_time is UTC and matches file mtime; either orders clips. - VIDEO_PROCESSING_ID identifies the same source clip across exports. Two files sharing it are one video exported twice (confirmed alongside identical byte size and duration before deleting a duplicate). - The sideways trap: a clip shot in landscape may be exported as HEVC at 1088x1936 with no rotation matrix, so it plays on its side, while its H.264 twin is correctly landscape. Check orientation by extracting a frame and looking at it; do not trust the dimensions. When it happens, use the H.264 file as the master and encode HEVC from it. - Straight-off-the-camera files (not Snapchat) are typically H.264 20 Mbps 1920x1080 with a -90 degree display matrix; the H.264 encode below bakes the rotation in.

The recipe

HEVC rendition, when the source is already HEVC: a remux, one second per file, no second generation of loss:

ffmpeg -i SRC -map 0:v:0 -map 0:a:0 -c copy -tag:v hvc1 -movflags +faststart OUT.hevc.mp4

H.264 base, the fallback everything plays:

ffmpeg -i SRC -map 0:v:0 -map 0:a:0 -c:v libx264 -preset medium -crf 23 \
  -maxrate 12M -bufsize 24M -pix_fmt yuv420p -g 60 -movflags +faststart -c:a copy OUT.mp4

About 11 Mbps out, roughly 0.4x realtime on the Ryzen 7 8845HS. -movflags +faststart (moov atom first) is not optional for streaming.

Trimming: use LosslessCut, do not build an editor

Front and back trims are a solved problem. LosslessCut (flatpak install flathub no.mifi.losslesscut) cuts on keyframes without re-encoding, which also keeps the HEVC rendition a remux; any re-encode gives that up. What genuinely does not exist off the shelf is the publishing pipeline: adb pull, probe, dedupe by VIDEO_PROCESSING_ID, catch the sideways export, order by capture time, name from a pattern, encode both renditions, poster + blur placeholder, upload, attach the alternate, fix ordering. Build that as a one-command CLI, and keep a fast "decide" phase separate from a background render. Justin's constraint: "If it's a tedious step that takes more than 10 minutes, probably not worth it" (his attention, not wall clock).

See also

Linked from

Sources: justinahampton memory (video-encoding-recipe, videos-ingest-tool-decision, videos-catalog-state), videos.jhampton.io 2026-08-15, tldr post #1766