Search code examples
ffmpegmp4m3u8http-live-streaming

With FFMPEG, can I rewrite a .ts specific segment in an HLS m3u8 format?


I am rather new at FFMPEG and starting to dig in beyond my experience with it. Is it possible to rewrite 1 or more arbitrary .ts file segments (for example, if I have edited a scene) without having to re-encode the entire movie?

In addition, if the length of that segment has changed, is it as simple as changing the segment length in the m3u8 file? For example this is the original:

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-ALLOW-CACHE:YES
#EXT-X-TARGETDURATION:8
#EXTINF:7.007000,
file0.ts
#EXTINF:5.964292,
file1.ts
#EXTINF:1.876875,
file2.ts
#EXTINF:2.293958,
file3.ts
etc...

change to:

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-ALLOW-CACHE:YES
#EXT-X-TARGETDURATION:8
#EXTINF:7.007000,
file0.ts
#EXTINF:12.023,
modified1.ts
#EXTINF:1.876875,
file2.ts
#EXTINF:2.293958,
file3.ts
etc...

You can see I would like to change file1.ts to a longer length and modify the filename. Any tips would be appreciated.


Solution

  • You should really read the pantos specification. https://datatracker.ietf.org/doc/html/draft-pantos-http-live-streaming-14

    But to answer more directly, You should increase #EXT-X-TARGETDURATION to the largest segment duration (rounded up). In your case 13. Next you need to put a discontinuity indicator whenever your codec, or continuity counter is reset. e.g.

    #EXTM3U
    #EXT-X-VERSION:3
    #EXT-X-MEDIA-SEQUENCE:0
    #EXT-X-ALLOW-CACHE:YES
    #EXT-X-TARGETDURATION:13
    #EXTINF:7.007000,
    file0.ts
    #EXTINF:12.023,
    #EXT-X-DISCONTINUITY
    modified1.ts
    #EXTINF:1.876875,
    #EXT-X-DISCONTINUITY
    file2.ts
    #EXTINF:2.293958,
    file3.ts