PR #20749 opened by anthonybajoua
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/20749
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/20749.patch

# Problem
* With `hybrid_fragmented` set, when `delay_moov` writes the `ftyp` atom, this 
is already after the `free` atom. 
    * This is the location where the `mdat` is overwritten during the soft 
remux.

# Solution
* Write the `ftyp` before the free atom in the hybrid mp4 codepath, 
deactivating the overwritten one when `hybrid_fragmented` is set


# Test Plan:
```
❯ ./ffmpeg -loglevel error -i bbb.mp4 -c copy -movflags 
hybrid_fragmented+delay_moov out.mp4
❯ mp4dump out.mp4 | grep "ftyp"
❯ git diff
❯ make
CC      libavformat/movenc.o
AR      libavformat/libavformat.a
LD      ffmpeg_g
STRIP   ffmpeg
LD      ffplay_g
STRIP   ffplay
LD      ffprobe_g
STRIP   ffprobe
❯ git --no-pager diff
diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index eabc1b95ca..91ad954acb 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -6536,7 +6536,8 @@ static int mov_flush_fragment(AVFormatContext *s, int 
force)
             mov->tracks[i].data_offset = pos + moov_size + 8;

         avio_write_marker(s->pb, AV_NOPTS_VALUE, AVIO_DATA_MARKER_HEADER);
-        if (mov->flags & FF_MOV_FLAG_DELAY_MOOV)
+        if (mov->flags & FF_MOV_FLAG_DELAY_MOOV
+            && !(mov->flags & FF_MOV_FLAG_HYBRID_FRAGMENTED))
             mov_write_identification(s->pb, s);
         if ((ret = mov_write_moov_tag(s->pb, mov, s)) < 0)
             return ret;
@@ -8432,6 +8433,9 @@ static int mov_write_header(AVFormatContext *s)
             !mov->max_fragment_duration && !mov->max_fragment_size)
             mov->flags |= FF_MOV_FLAG_FRAG_KEYFRAME;
         if (mov->flags & FF_MOV_FLAG_HYBRID_FRAGMENTED) {
+            if (mov->flags & FF_MOV_FLAG_DELAY_MOOV) {
+                mov_write_identification(pb, s);
+            }
             avio_wb32(pb, 8); // placeholder for extended size field (64 bit)
             ffio_wfourcc(pb, mov->mode == MODE_MOV ? "wide" : "free");
             mov->mdat_pos = avio_tell(pb);
❯ ./ffmpeg -loglevel error -i bbb.mp4 -c copy -movflags 
hybrid_fragmented+delay_moov out.mp4
File 'out.mp4' already exists. Overwrite? [y/N] y
❯ mp4dump out.mp4 | grep "ftyp"
[ftyp] size=8+28
```



From 7c2b4ec21acb6c8a4c82b4e51210d219773b2161 Mon Sep 17 00:00:00 2001
From: Anthony Bajoua <[email protected]>
Date: Sat, 25 Oct 2025 02:10:06 -0700
Subject: [PATCH] Output ftyp atom with hybrid_fragmented+delay_moov

---
 libavformat/movenc.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index eabc1b95ca..91ad954acb 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -6536,7 +6536,8 @@ static int mov_flush_fragment(AVFormatContext *s, int 
force)
             mov->tracks[i].data_offset = pos + moov_size + 8;
 
         avio_write_marker(s->pb, AV_NOPTS_VALUE, AVIO_DATA_MARKER_HEADER);
-        if (mov->flags & FF_MOV_FLAG_DELAY_MOOV)
+        if (mov->flags & FF_MOV_FLAG_DELAY_MOOV
+            && !(mov->flags & FF_MOV_FLAG_HYBRID_FRAGMENTED))
             mov_write_identification(s->pb, s);
         if ((ret = mov_write_moov_tag(s->pb, mov, s)) < 0)
             return ret;
@@ -8432,6 +8433,9 @@ static int mov_write_header(AVFormatContext *s)
             !mov->max_fragment_duration && !mov->max_fragment_size)
             mov->flags |= FF_MOV_FLAG_FRAG_KEYFRAME;
         if (mov->flags & FF_MOV_FLAG_HYBRID_FRAGMENTED) {
+            if (mov->flags & FF_MOV_FLAG_DELAY_MOOV) {
+                mov_write_identification(pb, s);
+            }
             avio_wb32(pb, 8); // placeholder for extended size field (64 bit)
             ffio_wfourcc(pb, mov->mode == MODE_MOV ? "wide" : "free");
             mov->mdat_pos = avio_tell(pb);
-- 
2.49.1

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to