[FFmpeg-devel] [PATCH] Write WebVTT output files
Matthew Heaney
matthewjheaney at google.com
Wed Jun 12 20:45:27 CEST 2013
This revision creates a WebVTT muxer, that outputs files having the
format described in the following specification:
http://dev.w3.org/html5/webvtt/
---
doc/general.texi | 2 +-
libavformat/Makefile | 1 +
libavformat/allformats.c | 2 +-
libavformat/version.h | 2 +-
libavformat/webvttenc.c | 116 +++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 120 insertions(+), 3 deletions(-)
create mode 100644 libavformat/webvttenc.c
diff --git a/doc/general.texi b/doc/general.texi
index a83b55e..0cddf9f 100644
--- a/doc/general.texi
+++ b/doc/general.texi
@@ -957,7 +957,7 @@ performance on systems without hardware floating point support).
@item TED Talks captions @tab @tab X @tab @tab X
@item VobSub (IDX+SUB) @tab @tab X @tab @tab X
@item VPlayer @tab @tab X @tab @tab X
- at item WebVTT @tab @tab X @tab @tab X
+ at item WebVTT @tab X @tab X @tab @tab X
@item XSUB @tab @tab @tab X @tab X
@end multitable
diff --git a/libavformat/Makefile b/libavformat/Makefile
index 5438b48..3cb076b 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -393,6 +393,7 @@ OBJS-$(CONFIG_WEBM_MUXER) += matroskaenc.o matroska.o \
isom.o avc.o \
flacenc_header.o avlanguage.o wv.o
OBJS-$(CONFIG_WEBVTT_DEMUXER) += webvttdec.o subtitles.o
+OBJS-$(CONFIG_WEBVTT_MUXER) += webvttenc.o subtitles.o
OBJS-$(CONFIG_WSAUD_DEMUXER) += westwood_aud.o
OBJS-$(CONFIG_WSVQA_DEMUXER) += westwood_vqa.o
OBJS-$(CONFIG_WTV_DEMUXER) += wtvdec.o wtv.o asfdec.o asf.o asfcrypt.o \
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index 6274a75..d4a2d27 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -292,7 +292,7 @@ void av_register_all(void)
REGISTER_MUXDEMUX(WAV, wav);
REGISTER_DEMUXER (WC3, wc3);
REGISTER_MUXER (WEBM, webm);
- REGISTER_DEMUXER (WEBVTT, webvtt);
+ REGISTER_MUXDEMUX(WEBVTT, webvtt);
REGISTER_DEMUXER (WSAUD, wsaud);
REGISTER_DEMUXER (WSVQA, wsvqa);
REGISTER_MUXDEMUX(WTV, wtv);
diff --git a/libavformat/version.h b/libavformat/version.h
index 8db210e..69e9e2c 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -30,7 +30,7 @@
#include "libavutil/avutil.h"
#define LIBAVFORMAT_VERSION_MAJOR 55
-#define LIBAVFORMAT_VERSION_MINOR 8
+#define LIBAVFORMAT_VERSION_MINOR 9
#define LIBAVFORMAT_VERSION_MICRO 102
#define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
diff --git a/libavformat/webvttenc.c b/libavformat/webvttenc.c
new file mode 100644
index 0000000..f0f91a5
--- /dev/null
+++ b/libavformat/webvttenc.c
@@ -0,0 +1,116 @@
+/*
+ * Copyright (c) 2013 Matthew Heaney
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file
+ * WebVTT subtitle muxer
+ * @see http://dev.w3.org/html5/webvtt/
+ */
+
+#include "avformat.h"
+#include "internal.h"
+
+static void webvtt_write_time(AVIOContext *pb, int64_t millisec)
+{
+ int64_t sec, min, hour;
+ sec = millisec / 1000;
+ millisec -= 1000 * sec;
+ min = sec / 60;
+ sec -= 60 * min;
+ hour = min / 60;
+ min -= 60 * hour;
+
+ if (hour > 0)
+ avio_printf(pb, "%ld:", hour);
+
+ avio_printf(pb, "%02ld:%02ld.%03ld", min, sec, millisec);
+}
+
+static int webvtt_write_header(AVFormatContext *ctx)
+{
+ AVStream *s = ctx->streams[0];
+ AVIOContext *pb = ctx->pb;
+
+ avpriv_set_pts_info(s, 64, 1, 1000);
+
+ avio_printf(pb, "WEBVTT\n");
+ avio_flush(pb);
+
+ return 0;
+}
+
+static int webvtt_write_packet(AVFormatContext *ctx, AVPacket *pkt)
+{
+ AVIOContext *pb = ctx->pb;
+ int id_size, settings_size;
+ uint8_t *id, *settings;
+
+ // Write cue separator.
+
+ avio_printf(pb, "\n");
+
+ // Write cue id (but only if present).
+
+ id = av_packet_get_side_data(pkt, AV_PKT_DATA_WEBVTT_IDENTIFIER,
+ &id_size);
+
+ if (id != NULL && id_size > 0) {
+ avio_write(pb, id, id_size);
+ avio_printf(pb, "\n");
+ }
+
+ // Write timestamp and (optional) settings.
+
+ webvtt_write_time(pb, pkt->pts);
+
+ avio_printf(pb, " ---> ");
+
+ webvtt_write_time(pb, pkt->pts + pkt->duration);
+
+ settings_size = 0;
+ settings = av_packet_get_side_data(pkt, AV_PKT_DATA_WEBVTT_SETTINGS,
+ &settings_size);
+
+ if (settings != NULL && settings_size > 0) {
+ avio_printf(pb, " ");
+ avio_write(pb, settings, settings_size);
+ }
+
+ avio_printf(pb, "\n");
+
+ // Write cue text.
+
+ avio_write(pb, pkt->data, pkt->size);
+ avio_printf(pb, "\n");
+
+ avio_flush(pb);
+
+ return 0;
+}
+
+AVOutputFormat ff_webvtt_muxer = {
+ .name = "webvtt",
+ .long_name = NULL_IF_CONFIG_SMALL("WebVTT subtitle"),
+ .extensions = "vtt",
+ .mime_type = "text/vtt",
+ .subtitle_codec = AV_CODEC_ID_WEBVTT,
+ .write_header = webvtt_write_header,
+ .write_packet = webvtt_write_packet,
+};
--
1.8.3
More information about the ffmpeg-devel
mailing list