This add support for WebVTT subtitles in HLS streams. similar like for
separate audio streams, it supports all available WebVTT streams in all
renditions. No new options are added, it just works and provides subtitles
streams like other demuxers.

The code prevents downloading subtitle segments which are much farther in
the future than the main segments to avoid loading hundreds of subtitle
segments in advance.

orig, 2022/10; scenarios tested: ffmpeg transcoding, ffprobe, MPV player

Example command for testing:

ffmpeg -i
https://playertest.longtailvideo.com/adaptive/elephants_dream_v4/index.m3u8
-map v:0 -map a:0 -map s:1 -map s:0 -c:v copy -c:a copy -c:s srt -to 120s
test1.mkv

...produces an MKV with two SRT sub tracks from WebVTT subs via HLS.

Update V2

 * Remove unnecessary assignment
 * Fixed indentation, reorder if-block
 * Inline string constant in code (as per review by Andreas Reinhardt)

softworkz (2):
  avformat/hls demuxer: Add WebVTT subtitle support
  avformat/webvttdec: Add webvtt extension and MIME type

 libavformat/hls.c       | 213 +++++++++++++++++++++++++++++++++-------
 libavformat/webvttdec.c |   3 +-
 2 files changed, 178 insertions(+), 38 deletions(-)


base-commit: e18f87ed9f9f61c980420b315dc8ecb308831bc5
Published-As: 
https://github.com/ffstaging/FFmpeg/releases/tag/pr-ffstaging-53%2Fsoftworkz%2Fsubmit_hlsvtt-v2
Fetch-It-Via: git fetch https://github.com/ffstaging/FFmpeg 
pr-ffstaging-53/softworkz/submit_hlsvtt-v2
Pull-Request: https://github.com/ffstaging/FFmpeg/pull/53

Range-diff vs v1:

 1:  c2e1404a42 ! 1:  3f9ee79232 avformat/hls demuxer: Add WebVTT subtitle 
support
     @@ Commit message
          Signed-off-by: softworkz <softwo...@hotmail.com>
      
       ## libavformat/hls.c ##
     -@@
     - #define MPEG_TIME_BASE 90000
     - #define MPEG_TIME_BASE_Q (AVRational){1, MPEG_TIME_BASE}
     - 
     -+static char *vtt_sample = "WEBVTT\n";
     -+
     - /*
     -  * An apple http stream consists of a playlist with media segment files,
     -  * played sequentially. There may be several playlists with the same
      @@ libavformat/hls.c: struct playlist {
            * playlist, if any. */
           int n_init_sections;
     @@ libavformat/hls.c: struct playlist {
       };
       
       /*
     -@@ libavformat/hls.c: static struct playlist *new_playlist(HLSContext *c, 
const char *url,
     -         return NULL;
     -     }
     -     pls->seek_timestamp = AV_NOPTS_VALUE;
     -+    pls->is_subtitle = 0;
     - 
     -     pls->is_id3_timestamped = -1;
     -     pls->id3_mpegts_timestamp = AV_NOPTS_VALUE;
      @@ libavformat/hls.c: static struct rendition *new_rendition(HLSContext 
*c, struct rendition_info *inf
               return NULL;
           }
     @@ libavformat/hls.c: static struct rendition *new_rendition(HLSContext 
*c, struct
               rend->playlist = new_playlist(c, info->uri, url_base);
      -        if (rend->playlist)
      +        if (rend->playlist) {
     -             dynarray_add(&rend->playlist->renditions,
     --                         &rend->playlist->n_renditions, rend);
     -+                        &rend->playlist->n_renditions, rend);
      +            if (type == AVMEDIA_TYPE_SUBTITLE) {
      +                rend->playlist->is_subtitle = 1;
      +                rend->playlist->is_id3_timestamped = 0;
      +            }
     +             dynarray_add(&rend->playlist->renditions,
     +                          &rend->playlist->n_renditions, rend);
      +        }
           }
       
     @@ libavformat/hls.c: reload:
      +    }
      +    return ret;
      +}
     -+
     + 
      +static int read_data_continuous(void *opaque, uint8_t *buf, int buf_size)
      +{
      +    struct playlist *v = opaque;
     @@ libavformat/hls.c: reload:
      +    ret = reload_playlist(v, c);
      +    if (ret < 0)
      +        return ret;
     - 
     ++
      +    seg = current_segment(v);
      +
      +    if (!v->input || (c->http_persistent && v->input_read_done)) {
     @@ libavformat/hls.c: static int hls_read_header(AVFormatContext *s)
      -        ffio_init_context(&pls->pb, pls->read_buffer, 
INITIAL_BUFFER_SIZE, 0, pls,
      -                          read_data, NULL, NULL);
      +        if (pls->is_subtitle)
     -+            ffio_init_context(&pls->pb, (unsigned 
char*)av_strdup(vtt_sample), (int)strlen(vtt_sample), 0, pls,
     -+                                        NULL, NULL, NULL);
     ++            ffio_init_context(&pls->pb, (unsigned 
char*)av_strdup("WEBVTT\n"), (int)strlen("WEBVTT\n"), 0, pls,
     ++                                       NULL, NULL, NULL);
      +        else
      +            ffio_init_context(&pls->pb, pls->read_buffer, 
INITIAL_BUFFER_SIZE, 0, pls,
      +                                        read_data_continuous, NULL, 
NULL);
 2:  aff0dd2be2 = 2:  7550e41042 avformat/webvttdec: Add webvtt extension and 
MIME type

-- 
ffmpeg-codebot
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

Reply via email to