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.
hvc1, nothev1, is the tag Safari requires.- Chrome and Firefox (on Linux at least) ship no HEVC decoder and fall through to the next
<source>, so the source list always ends with the H.264 file. - x265 medium ran ~34 fps on a 30 fps 1936x1088 clip on the desktop, if a re-encode is ever needed.
- Poster: a frame at
min(3, duration * 0.25)seconds; short clips should not land on the first frame while exposure is still settling. Look at it: one poster had to move to 18 s because a paw covered the cat's face at 3 s.
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
- Web video players: sound, mute, fullscreen and autoplay-next
- Rotating video in CSS instead of re-encoding
- Inline blur-up placeholders (LQIP): JPEG beats WebP across a page
- videos.jhampton.io (private video site architecture)
Linked from
- Android TV / Google TV apps (Compose for TV, Media3, sideloading)
- Generating app assets with Python scripts (logos, GIFs, sounds)
- Generating narrated screencast tutorials from a script (PIL + Piper TTS + ffmpeg)
- Inline blur-up placeholders (LQIP): JPEG beats WebP across a page
- Rotating video in CSS instead of re-encoding
- Scripted video editing with FilmCraft (CLI/MCP), whisper transcripts, and a dashcam highlight reel
- Upload links for people without an account (getting a full-quality video from a friend)
- videos.jhampton.io (private video site architecture)
- Web video players: sound, mute, fullscreen and autoplay-next