2 * Copyright (c) 2013 Matthew Heaney
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * WebVTT subtitle muxer
24 * @see http://dev.w3.org/html5/webvtt/
31 static void webvtt_write_time(AVIOContext *pb, int64_t millisec)
33 int64_t sec, min, hour;
34 sec = millisec / 1000;
35 millisec -= 1000 * sec;
42 avio_printf(pb, "%02"PRId64":", hour);
44 avio_printf(pb, "%02"PRId64":%02"PRId64".%03"PRId64"", min, sec, millisec);
47 static int webvtt_write_header(AVFormatContext *ctx)
49 AVStream *s = ctx->streams[0];
50 AVCodecParameters *par = ctx->streams[0]->codecpar;
51 AVIOContext *pb = ctx->pb;
53 if (ctx->nb_streams != 1 || par->codec_id != AV_CODEC_ID_WEBVTT) {
54 av_log(ctx, AV_LOG_ERROR, "Exactly one WebVTT stream is needed.\n");
55 return AVERROR(EINVAL);
58 avpriv_set_pts_info(s, 64, 1, 1000);
60 avio_printf(pb, "WEBVTT\n");
65 static int webvtt_write_packet(AVFormatContext *ctx, AVPacket *pkt)
67 AVIOContext *pb = ctx->pb;
68 size_t id_size, settings_size;
69 int id_size_int, settings_size_int;
70 uint8_t *id, *settings;
72 avio_printf(pb, "\n");
74 id = av_packet_get_side_data(pkt, AV_PKT_DATA_WEBVTT_IDENTIFIER,
77 if (id_size > INT_MAX)
78 return AVERROR(EINVAL);
80 id_size_int = id_size;
81 if (id && id_size_int > 0)
82 avio_printf(pb, "%.*s\n", id_size_int, id);
84 webvtt_write_time(pb, pkt->pts);
85 avio_printf(pb, " --> ");
86 webvtt_write_time(pb, pkt->pts + pkt->duration);
88 settings = av_packet_get_side_data(pkt, AV_PKT_DATA_WEBVTT_SETTINGS,
91 if (settings_size > INT_MAX)
92 return AVERROR(EINVAL);
94 settings_size_int = settings_size;
95 if (settings && settings_size_int > 0)
96 avio_printf(pb, " %.*s", settings_size_int, settings);
98 avio_printf(pb, "\n");
100 avio_write(pb, pkt->data, pkt->size);
101 avio_printf(pb, "\n");
106 const FFOutputFormat ff_webvtt_muxer = {
108 .p.long_name = NULL_IF_CONFIG_SMALL("WebVTT subtitle"),
109 .p.extensions = "vtt",
110 .p.mime_type = "text/vtt",
111 .p.flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT,
112 .p.subtitle_codec = AV_CODEC_ID_WEBVTT,
113 .write_header = webvtt_write_header,
114 .write_packet = webvtt_write_packet,