[FFmpeg-devel] [PATCH] Write WebVTT output files

Clément Bœsch ubitux at gmail.com
Wed Jun 12 09:11:15 CEST 2013


On Tue, Jun 11, 2013 at 04:24:27PM -0700, Matthew Heaney wrote:
> This revision creates a WebVTT muxer, that outputs files having the
> format described in the following specification:
> 
> http://dev.w3.org/html5/webvtt/
> ---
>  libavformat/Makefile     |   1 +
>  libavformat/allformats.c |   1 +
>  libavformat/webvttenc.c  | 139 +++++++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 141 insertions(+)
>  create mode 100644 libavformat/webvttenc.c
> 

Missing update of doc/general.texi and minor bump in lavf/version.h

> 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..f947e19 100644
> --- a/libavformat/allformats.c
> +++ b/libavformat/allformats.c
> @@ -293,6 +293,7 @@ void av_register_all(void)
>      REGISTER_DEMUXER (WC3,              wc3);
>      REGISTER_MUXER   (WEBM,             webm);

>      REGISTER_DEMUXER (WEBVTT,           webvtt);
> +    REGISTER_MUXER   (WEBVTT,           webvtt);

REGISTER_MUXDEMUX(WEBVTT, webvtt);

>      REGISTER_DEMUXER (WSAUD,            wsaud);
>      REGISTER_DEMUXER (WSVQA,            wsvqa);
>      REGISTER_MUXDEMUX(WTV,              wtv);
> diff --git a/libavformat/webvttenc.c b/libavformat/webvttenc.c
> new file mode 100644
> index 0000000..45cdec9
> --- /dev/null
> +++ b/libavformat/webvttenc.c
> @@ -0,0 +1,139 @@
> +/*
> + * 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"
> +

> +typedef struct WebVTTMuxContext {
> +    const AVClass *class;
> +} WebVTTMuxContext;
> +

If you don't plan to store anything in the context, you can drop this.

> +static void webvtt_write_time(AVIOContext *pb,
> +                              int64_t pts,
> +                              const AVRational* time_base)
> +{
> +    int64_t sec, min, hour;

> +    int64_t millisec = (pts * time_base->num * 1000) / time_base->den;

Use avpriv_set_pts_info() in the write_header function instead. The
rescale will be done internally, and you won't need to pass the time base
along.

> +    sec = millisec / 1000;
> +    millisec -= 1000 * sec;
> +    min = sec / 60;
> +    sec -= 60 * min;
> +    hour = min / 60;
> +    min -= 60 * hour;
> +

> +#if 1
> +    // TODO(matthewjheaney at google.com): the timestamp parsing in webvttdec.c
> +    // is broken, so for now just output a timestamp in the format we can read.
> +    avio_printf(pb, "%ld:%02ld:%02ld.%03ld", hour, min, sec, millisec);

I'm not sure how "broken" the TS parsing in our demuxer is, but this code
certainly is simpler (and thus preferred) than the following anyway...

> +#else
> +    if (hour > 0) {
> +        avio_printf(pb, "%ld:%02ld:%02ld", hour, min, sec);
> +    } else if (min > 0) {
> +        avio_printf(pb, "%ld:%02ld", min, sec);
> +    } else {
> +        avio_printf(pb, "%ld", sec);
> +    }
> +
> +    if (millisec > 0) {
> +        avio_printf(pb, ".%03ld", millisec);
> +    }
> +#endif
> +}
> +
> +static int webvtt_write_header(AVFormatContext *ctx)
> +{
> +    AVIOContext *pb = ctx->pb;
> +    const char header[] = "WEBVTT\n";
> +

> +    avio_write(pb, header, strlen(header));

or just avio_printf("WEBVTT\n")

> +    avio_flush(pb);
> +
> +    return 0;
> +}
> +
> +static int webvtt_write_packet(AVFormatContext *ctx, AVPacket *pkt)
> +{
> +    AVStream     *s  = ctx->streams[0];
> +    AVIOContext  *pb = ctx->pb;
> +    const char space[] = " ";
> +    const char EOL[] = "\n";
> +    const char arrow[] = "-->";
> +    int id_size, settings_size;
> +    uint8_t *id, *settings;
> +
> +    // Write cue separator.
> +
> +    avio_write(pb, EOL, 1);
> +
> +    // Write cue id (but only if present).
> +
> +    id_size = 0;

You don't need to init id_size and settings_size if you check for the
output pointer.

> +    id = av_packet_get_side_data(pkt, AV_PKT_DATA_WEBVTT_IDENTIFIER,
> +                                 &id_size);
> +
> +    if (id_size > 0) {
> +        avio_write(pb, id, id_size);
> +        avio_write(pb, EOL, 1);
> +    }
> +

Any reason you sometimes use avio_printf and sometimes a bunch of
successive avio_write?

> +    // Write timestamp and (optional) settings.
> +
> +    webvtt_write_time(pb, pkt->pts, &s->time_base);
> +

> +    avio_write(pb, space, strlen(space));
> +    avio_write(pb, arrow, strlen(arrow));
> +    avio_write(pb, space, strlen(space));
> +

If you used defines instead of const char array, you could simply
avio_printf(pb, SPACE ARROW SPACE), or a avio_write with a sizeof(X)-1.

> +    webvtt_write_time(pb, pkt->pts + pkt->duration, &s->time_base);
> +
> +    settings_size = 0;
> +    settings = av_packet_get_side_data(pkt, AV_PKT_DATA_WEBVTT_SETTINGS,
> +                                       &settings_size);
> +
> +    if (settings_size > 0) {
> +        avio_write(pb, space, strlen(space));
> +        avio_write(pb, settings, settings_size);
> +    }
> +
> +    avio_write(pb, EOL, 1);
> +
> +    // Write cue text.
> +
> +    avio_write(pb, pkt->data, pkt->size);
> +    avio_write(pb, EOL, 1);
> +
> +    return 0;
> +}
> +
> +AVOutputFormat ff_webvtt_muxer = {
> +    .name              = "webvtt",
> +    .long_name         = NULL_IF_CONFIG_SMALL("WebVTT subtitle"),
> +    .extensions        = "vtt",
> +    .mime_type         = "text/vtt",
> +    .priv_data_size    = sizeof(WebVTTMuxContext),
> +    .subtitle_codec    = AV_CODEC_ID_WEBVTT,
> +    .write_header      = webvtt_write_header,
> +    .write_packet      = webvtt_write_packet,
> +};

-- 
Clément B.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: not available
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20130612/332c91f4/attachment.asc>


More information about the ffmpeg-devel mailing list