[FFmpeg-devel] [PATCH] WebM muxer writes WebVTT subtitle track

Matthew Heaney matthewjheaney at google.com
Mon Jun 3 22:16:32 CEST 2013


The Matroska muxer now allows WebVTT subtitle tracks to be written
while in WebM muxing mode.

WebVTT subtitle tracks have four kinds: "subtitles", "captions",
"descriptions", and "metadata". Each text track kind has a distinct
Mastroska CodecID and track type, as described in the temporal
metadata guidelines here:

http://wiki.webmproject.org/webm-metadata/temporal-metadata/webvtt-in-webm

In ffmpeg, the WebVTT kind can be specified explicitly using the
-metadata option to specify a mapping from key "kind" to one of the
kind values listed above.  The kind "subtitles" is the default if no
mapping is specified.

When the stream has codec id AV_CODEC_ID_WEBVTT, the stream packet is
serialized per the temporal metadata guidelines cited above. The
WebVTT cue is written as a Matroska block group. The block frame
comprises the WebVTT cue id, followed by the cue settings, followed by
the cue text.  (The block timestamp is synthesized from the cue
timestamp.)
---
 libavformat/matroskaenc.c | 145 ++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 127 insertions(+), 18 deletions(-)

diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c
index 99d648d..77c9fbb 100644
--- a/libavformat/matroskaenc.c
+++ b/libavformat/matroskaenc.c
@@ -560,6 +560,41 @@ static int mkv_write_tracks(AVFormatContext *s)
     ebml_master tracks;
     int i, j, ret, default_stream_exists = 0;
 
+    for (i = 0; i < s->nb_streams; i++) {
+        AVStream *st = s->streams[i];
+        AVCodecContext *codec = st->codec;
+        AVDictionaryEntry *t;
+
+        if (mkv->mode != MODE_WEBM)
+            continue;
+
+        if (codec->codec_id == AV_CODEC_ID_VP8
+            || codec->codec_id == AV_CODEC_ID_VORBIS) {
+            continue;
+        }
+
+        if (codec->codec_id != AV_CODEC_ID_WEBVTT) {
+            av_log(s, AV_LOG_ERROR,
+                   "Only VP8 video, Vorbis audio, and WebVTT subtitles are supported for WebM.\n");
+            return AVERROR(EINVAL);
+        }
+
+        t = av_dict_get(st->metadata, "kind", NULL, 0);
+
+        if (!t)
+          continue;
+
+        if (!av_strcasecmp(t->value, "subtitles")
+            || !av_strcasecmp(t->value, "captions")
+            || !av_strcasecmp(t->value, "descriptions")
+            || !av_strcasecmp(t->value, "metadata")) {
+            continue;
+        }
+
+        av_log(s, AV_LOG_ERROR, "Bad WebVTT kind specifier.\n");
+        return AVERROR(EINVAL);
+    }
+
     ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_TRACKS, avio_tell(pb));
     if (ret < 0) return ret;
 
@@ -599,8 +634,13 @@ static int mkv_write_tracks(AVFormatContext *s)
 
         if ((tag = av_dict_get(st->metadata, "title", NULL, 0)))
             put_ebml_string(pb, MATROSKA_ID_TRACKNAME, tag->value);
+
         tag = av_dict_get(st->metadata, "language", NULL, 0);
-        put_ebml_string(pb, MATROSKA_ID_TRACKLANGUAGE, tag ? tag->value:"und");
+        if (mkv->mode != MODE_WEBM || codec->codec_id != AV_CODEC_ID_WEBVTT) {
+            put_ebml_string(pb, MATROSKA_ID_TRACKLANGUAGE, tag ? tag->value:"und");
+        } else if (tag && tag->value) {
+            put_ebml_string(pb, MATROSKA_ID_TRACKLANGUAGE, tag->value);
+        }
 
         if (default_stream_exists) {
             put_ebml_uint(pb, MATROSKA_ID_TRACKFLAGDEFAULT, !!(st->disposition & AV_DISPOSITION_DEFAULT));
@@ -608,21 +648,37 @@ static int mkv_write_tracks(AVFormatContext *s)
         if (st->disposition & AV_DISPOSITION_FORCED)
             put_ebml_uint(pb, MATROSKA_ID_TRACKFLAGFORCED, 1);
 
-        // look for a codec ID string specific to mkv to use,
-        // if none are found, use AVI codes
-        for (j = 0; ff_mkv_codec_tags[j].id != AV_CODEC_ID_NONE; j++) {
-            if (ff_mkv_codec_tags[j].id == codec->codec_id) {
-                put_ebml_string(pb, MATROSKA_ID_CODECID, ff_mkv_codec_tags[j].str);
-                native_id = 1;
-                break;
+        if (mkv->mode == MODE_WEBM && codec->codec_id == AV_CODEC_ID_WEBVTT) {
+            const char *str;
+            tag = av_dict_get(st->metadata, "kind", NULL, 0);
+
+            if (!tag) {
+                str = "D_WEBVTT/SUBTITLES";
+                native_id = 0x11;
+            } else if (!av_strcasecmp(tag->value, "captions")) {
+                str = "D_WEBVTT/CAPTIONS";
+                native_id = 0x11;
+            } else if (!av_strcasecmp(tag->value, "descriptions")) {
+                str = "D_WEBVTT/DESCRIPTIONS";
+                native_id = 0x21;
+            } else if (!av_strcasecmp(tag->value, "metadata")) {
+                str = "D_WEBVTT/METADATA";
+                native_id = 0x21;
+            } else {
+                str = "D_WEBVTT/SUBTITLES";
+                native_id = 0x11;
+            }
+            put_ebml_string(pb, MATROSKA_ID_CODECID, str);
+        } else {
+            // look for a codec ID string specific to mkv to use,
+            // if none are found, use AVI codes
+            for (j = 0; ff_mkv_codec_tags[j].id != AV_CODEC_ID_NONE; j++) {
+                if (ff_mkv_codec_tags[j].id == codec->codec_id) {
+                    put_ebml_string(pb, MATROSKA_ID_CODECID, ff_mkv_codec_tags[j].str);
+                    native_id = 1;
+                    break;
+                }
             }
-        }
-
-        if (mkv->mode == MODE_WEBM && !(codec->codec_id == AV_CODEC_ID_VP8 ||
-                                        codec->codec_id == AV_CODEC_ID_VORBIS)) {
-            av_log(s, AV_LOG_ERROR,
-                   "Only VP8 video and Vorbis audio are supported for WebM.\n");
-            return AVERROR(EINVAL);
         }
 
         switch (codec->codec_type) {
@@ -715,18 +771,27 @@ static int mkv_write_tracks(AVFormatContext *s)
                 break;
 
             case AVMEDIA_TYPE_SUBTITLE:
-                put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, MATROSKA_TRACK_TYPE_SUBTITLE);
                 if (!native_id) {
                     av_log(s, AV_LOG_ERROR, "Subtitle codec %d is not supported.\n", codec->codec_id);
                     return AVERROR(ENOSYS);
                 }
+
+                if (mkv->mode == MODE_WEBM && codec->codec_id == AV_CODEC_ID_WEBVTT) {
+                    put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, native_id);
+                } else {
+                    put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, MATROSKA_TRACK_TYPE_SUBTITLE);
+                }
+
                 break;
             default:
                 av_log(s, AV_LOG_ERROR, "Only audio, video, and subtitles are supported for Matroska.\n");
                 return AVERROR(EINVAL);
         }
-        ret = mkv_write_codecprivate(s, pb, codec, native_id, qt_id);
-        if (ret < 0) return ret;
+
+        if (mkv->mode != MODE_WEBM || codec->codec_id != AV_CODEC_ID_WEBVTT) {
+            ret = mkv_write_codecprivate(s, pb, codec, native_id, qt_id);
+            if (ret < 0) return ret;
+        }
 
         end_ebml_master(pb, track);
 
@@ -1306,6 +1371,48 @@ static int mkv_write_srt_blocks(AVFormatContext *s, AVIOContext *pb, AVPacket *p
     return duration;
 }
 
+static int mkv_write_vtt_blocks(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt) {
+    MatroskaMuxContext *mkv = s->priv_data;
+    ebml_master blockgroup;
+    int id_size, settings_size, size;
+    uint8_t *id, *settings;
+    int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
+    const int flags = 0;
+    const uint8_t EOL[] = "\n";
+
+    id_size = 0;
+    id = av_packet_get_side_data(pkt, AV_PKT_DATA_WEBVTT_IDENTIFIER,
+                                 &id_size);
+
+    settings_size = 0;
+    settings = av_packet_get_side_data(pkt, AV_PKT_DATA_WEBVTT_SETTINGS,
+                                       &settings_size);
+
+    size = id_size + 1 + settings_size + 1 + pkt->size;
+
+    av_log(s, AV_LOG_DEBUG, "Writing block at offset %" PRIu64 ", size %d, "
+           "pts %" PRId64 ", dts %" PRId64 ", duration %d, flags %d\n",
+           avio_tell(pb), size, pkt->pts, pkt->dts, pkt->duration, flags);
+
+    blockgroup = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP, mkv_blockgroup_size(size));
+
+    put_ebml_id(pb, MATROSKA_ID_BLOCK);
+    put_ebml_num(pb, size+4, 0);
+    avio_w8(pb, 0x80 | (pkt->stream_index + 1));     // this assumes stream_index is less than 126
+    avio_wb16(pb, ts - mkv->cluster_pts);
+    avio_w8(pb, flags);
+    avio_write(pb, id, id_size);
+    avio_write(pb, EOL, 1);
+    avio_write(pb, settings, settings_size);
+    avio_write(pb, EOL, 1);
+    avio_write(pb, pkt->data, pkt->size);
+
+    put_ebml_uint(pb, MATROSKA_ID_BLOCKDURATION, pkt->duration);
+    end_ebml_master(pb, blockgroup);
+
+    return pkt->duration;
+}
+
 static void mkv_flush_dynbuf(AVFormatContext *s)
 {
     MatroskaMuxContext *mkv = s->priv_data;
@@ -1361,6 +1468,8 @@ static int mkv_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
 #endif
     } else if (codec->codec_id == AV_CODEC_ID_SRT) {
         duration = mkv_write_srt_blocks(s, pb, pkt);
+    } else if (codec->codec_id == AV_CODEC_ID_WEBVTT) {
+        duration = mkv_write_vtt_blocks(s, pb, pkt);
     } else {
         ebml_master blockgroup = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP, mkv_blockgroup_size(pkt->size));
         /* For backward compatibility, prefer convergence_duration. */
-- 
1.8.2.1



More information about the ffmpeg-devel mailing list