DEV Community

Mason K
Mason K

Posted on

HEVC plays everywhere except Apple devices? Fix the hev1 tag with FFmpeg, no re-encode

TL;DR

FFmpeg writes HEVC into MP4 with the hev1 sample entry. Apple's HLS spec tells you to use hvc1. We'll detect the tag with ffprobe, fix it with -c copy -tag:v hvc1 (no re-encode), audit a whole folder with a short Python script, and fix an HLS master playlist that silently loses its CODECS attribute. Tested on FFmpeg 6.1.1 with libx265 3.5.

📦 Code: github.com/USER/repo (replace before publishing)

The symptom

An HEVC .mp4 that plays in VLC and Chrome, and shows a black frame or refuses to load on iPhone, iPad, Apple TV or QuickTime. ffprobe reports nothing wrong.

Let's reproduce it and fix it.

1. Make an HEVC file the default way

ffmpeg -f lavfi -i testsrc2=size=640x360:rate=30:duration=4 \
       -f lavfi -i sine=frequency=440:duration=4 \
       -c:v libx265 -pix_fmt yuv420p -c:a aac default.mp4
Enter fullscreen mode Exit fullscreen mode

Now ask what tag the video stream got:

ffprobe -v error -select_streams v:0 \
  -show_entries stream=codec_name,codec_tag_string,profile \
  -of default=nw=1 default.mp4
Enter fullscreen mode Exit fullscreen mode
codec_name=hevc
profile=Main
codec_tag_string=hev1
Enter fullscreen mode Exit fullscreen mode

There it is. hev1.

2. What the two tags mean

Both are valid HEVC sample entry types in MP4. The difference is where the parameter sets (VPS, SPS, PPS) live:

Tag Parameter sets Who writes it
hvc1 Only in the sample description (track header) Apple's encoders, most packagers when asked
hev1 May also be carried in-band, inside the samples FFmpeg's MP4 muxer by default

Apple's HLS Authoring Specification, rule 1.10:

You SHOULD use video formats in which the parameter sets are stored in the sample descriptions, rather than the samples. (That is, use 'avc1', 'hvc1', or 'dvh1' rather than 'avc3', 'hev1', or 'dvhe'.)

The default is not special to .mp4. In my runs, writing a .mov and remuxing from .mkv to .mp4 both produced hev1 too.

3. Fix it without re-encoding 🛠️

The bitstream is the same. Only the container label changes, so stream copy is enough:

ffmpeg -i default.mp4 -c copy -tag:v hvc1 -movflags +faststart fixed.mp4
ffprobe -v error -select_streams v:0 -show_entries stream=codec_tag_string -of csv=p=0 fixed.mp4
Enter fullscreen mode Exit fullscreen mode
hvc1
Enter fullscreen mode Exit fullscreen mode

Don't take "nothing changed" on faith. Compare size and a checksum of the video packets:

ls -l default.mp4 fixed.mp4
ffmpeg -v error -i default.mp4 -map 0:v -c copy -f md5 -
ffmpeg -v error -i fixed.mp4   -map 0:v -c copy -f md5 -
Enter fullscreen mode Exit fullscreen mode
-rw-r--r-- 1 root root 307575 Nov 16 06:06 default.mp4
-rw-r--r-- 1 root root 307575 Nov 16 06:06 fixed.mp4
MD5=9693a0e40f81bfd994f6c54ea980b6ed
MD5=9693a0e40f81bfd994f6c54ea980b6ed
Enter fullscreen mode Exit fullscreen mode

Same size, same packets. For new encodes, add the flag up front:

ffmpeg -i input.mov -c:v libx265 -crf 24 -preset medium -tag:v hvc1 \
       -c:a aac -movflags +faststart output.mp4
Enter fullscreen mode Exit fullscreen mode

⚠️ Note: -tag:v hvc1 on a non-HEVC stream fails. If a shared preset applies it to an H.264 encode you'll see this, and no output file:

[mp4 @ 0x563ce669a180] Tag hvc1 incompatible with output codec id '27' (avc1)
[out#0/mp4 @ 0x563ce5e04800] Could not write header (incorrect codec parameters ?): Invalid data found when processing input
Enter fullscreen mode Exit fullscreen mode

So set the tag per codec, not globally.

4. Audit a whole library 🔍

One file is easy. The real question is how many of these you already have. This script walks a folder, reports every hev1 file, exits non-zero if it finds any (so it works as a CI or post-encode gate), and remuxes them with --fix.

#!/usr/bin/env python3
# hevc_audit.py: find HEVC files tagged hev1 and optionally remux them to hvc1
import json, subprocess, sys
from pathlib import Path

def video_stream(path):
    out = subprocess.run(
        ["ffprobe", "-v", "error", "-select_streams", "v:0",
         "-show_entries", "stream=codec_name,codec_tag_string",
         "-of", "json", str(path)],
        capture_output=True, text=True, check=True).stdout
    streams = json.loads(out).get("streams", [])
    return streams[0] if streams else None

def retag(path):
    fixed = path.with_name(path.stem + ".hvc1" + path.suffix)
    subprocess.run(
        ["ffmpeg", "-v", "error", "-y", "-i", str(path), "-map", "0",
         "-c", "copy", "-tag:v", "hvc1", "-movflags", "+faststart", str(fixed)],
        check=True)
    return fixed

def main(root, fix):
    bad = 0
    for path in sorted(Path(root).rglob("*")):
        if path.suffix.lower() not in {".mp4", ".mov", ".m4v"} or ".hvc1" in path.name:
            continue
        s = video_stream(path)
        if not s or s["codec_name"] != "hevc":
            print(f"skip   {path.name} ({s['codec_name'] if s else 'no video'})")
            continue
        if s["codec_tag_string"] == "hvc1":
            print(f"ok     {path.name}")
            continue
        bad += 1
        if fix:
            print(f"fixed  {path.name} -> {retag(path).name}")
        else:
            print(f"HEV1   {path.name}")
    return 1 if bad and not fix else 0

if __name__ == "__main__":
    sys.exit(main(sys.argv[1], "--fix" in sys.argv))
Enter fullscreen mode Exit fullscreen mode

Against a folder with four test files:

$ python3 hevc_audit.py lib; echo "exit $?"
ok     already-ok.mp4
skip   h264.mp4 (h264)
HEV1   phone-upload.mp4
HEV1   screen-rec.mov
exit 1

$ python3 hevc_audit.py lib --fix; echo "exit $?"
ok     already-ok.mp4
skip   h264.mp4 (h264)
fixed  phone-upload.mp4 -> phone-upload.hvc1.mp4
fixed  screen-rec.mov -> screen-rec.hvc1.mov
exit 0
Enter fullscreen mode Exit fullscreen mode

It writes a new file next to the original and leaves the original alone. Swap them once you've spot-checked a few on a real Apple device.

💡 Tip: The same ffprobe call is a good one-line assertion in whatever runs after your encoder: codec is hevc and tag is not hvc1 means fail the job.

5. HLS: the tag also decides your CODECS attribute

This is the part that surprised me. Package the untagged file into fMP4 HLS with a master playlist:

ffmpeg -i default.mp4 -c copy -f hls -hls_segment_type fmp4 -hls_time 2 \
       -hls_playlist_type vod -master_pl_name master.m3u8 hls/v.m3u8
cat hls/master.m3u8
Enter fullscreen mode Exit fullscreen mode
#EXTM3U
#EXT-X-VERSION:7
#EXT-X-STREAM-INF:BANDWIDTH=658249,RESOLUTION=640x360
v.m3u8
Enter fullscreen mode Exit fullscreen mode

No CODECS. Now the same command with the tag:

ffmpeg -i default.mp4 -c copy -tag:v hvc1 -f hls -hls_segment_type fmp4 -hls_time 2 \
       -hls_playlist_type vod -master_pl_name master.m3u8 hls/v.m3u8
cat hls/master.m3u8
Enter fullscreen mode Exit fullscreen mode
#EXTM3U
#EXT-X-VERSION:7
#EXT-X-STREAM-INF:BANDWIDTH=658249,RESOLUTION=640x360,CODECS="hvc1.1.4.L63.B01,mp4a.40.2"
v.m3u8
Enter fullscreen mode Exit fullscreen mode

Without the tag, FFmpeg 6.1.1 wrote an hev1 init segment and dropped the CODECS attribute from the master playlist without a warning. Apple's spec is explicit on both counts:

  • Rule 9.1: "Your EXT-X-STREAM-INF tag MUST always provide the CODECS attribute."
  • Rule 1.5: "The container format for HEVC video MUST be fMP4." So no HEVC in .ts segments.

Players use CODECS to pick renditions they can decode before downloading anything. If your ladder mixes H.264 and HEVC variants, a missing attribute means the player is guessing.

⚠️ Note: This is what the 6.1.1 build did for me. There is an FFmpeg trac ticket (#10775, "More sensible HEVC handling for HLS creation") about this area, so newer builds may behave differently. Run the two commands above on your build and read the playlist.

6. Check what a browser will accept

On the playback side, you can ask before you load anything:

// can-play-hevc.js
const type = 'video/mp4; codecs="hvc1.1.6.L93.B0"';
console.log('canPlayType:', document.createElement('video').canPlayType(type));
console.log('MSE:', 'MediaSource' in window && MediaSource.isTypeSupported(type));
Enter fullscreen mode Exit fullscreen mode

Swap hvc1 for hev1 in the string and run it again on each browser you support. If the two answers differ on any of them, that is the tag you publish.

Things to know

  • When not to retag. hvc1 says the parameter sets in the header are valid for the whole track. A file whose resolution or parameters change mid-stream (some stitched live recordings) needs normalizing first, not just relabelling.
  • Dolby Vision has the same split. The rule names dvh1 over dvhe for the same reason.
  • What I did not test. All FFmpeg output above is from my own runs. I did not replay an hev1 file on a current Safari for this post. The recommendation rests on Apple's published rule and on years of reports that Apple players reject hev1, so confirm on a device you own.

What's next

  • Put the hevc_audit.py gate after your encode step and run it once across your existing library.
  • Next problem you'll hit with HEVC HLS: writing correct CODECS strings by hand for a multi-codec ladder. Apple's authoring spec has an appendix of values worth reading before you template them.

Top comments (1)

Collapse
 
iamaanahmad profile image
Amaan Ahmad •

Really useful — the part that surprised me most is FFmpeg silently dropping the CODECS attribute from the master playlist when the tag is hev1. The tip about gating on the ffprobe tag after the encode step is a neat way to catch this before it reaches players. Have you noticed the same silent drop with other codecs, or is it specific to HEVC here?