FFmpeg
webvttenc.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Matthew Heaney
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20 
21 /**
22  * @file
23  * WebVTT subtitle muxer
24  * @see http://dev.w3.org/html5/webvtt/
25  */
26 
27 #include "avformat.h"
28 #include "internal.h"
29 
30 static void webvtt_write_time(AVIOContext *pb, int64_t millisec)
31 {
32  int64_t sec, min, hour;
33  sec = millisec / 1000;
34  millisec -= 1000 * sec;
35  min = sec / 60;
36  sec -= 60 * min;
37  hour = min / 60;
38  min -= 60 * hour;
39 
40  if (hour > 0)
41  avio_printf(pb, "%02"PRId64":", hour);
42 
43  avio_printf(pb, "%02"PRId64":%02"PRId64".%03"PRId64"", min, sec, millisec);
44 }
45 
47 {
48  AVStream *s = ctx->streams[0];
50  AVIOContext *pb = ctx->pb;
51 
52  if (ctx->nb_streams != 1 || par->codec_id != AV_CODEC_ID_WEBVTT) {
53  av_log(ctx, AV_LOG_ERROR, "Exactly one WebVTT stream is needed.\n");
54  return AVERROR(EINVAL);
55  }
56 
57  avpriv_set_pts_info(s, 64, 1, 1000);
58 
59  avio_printf(pb, "WEBVTT\n");
60 
61  return 0;
62 }
63 
65 {
66  AVIOContext *pb = ctx->pb;
67  size_t id_size, settings_size;
68  int id_size_int, settings_size_int;
69  uint8_t *id, *settings;
70 
71  avio_printf(pb, "\n");
72 
74  &id_size);
75 
76  if (id_size > INT_MAX)
77  return AVERROR(EINVAL);
78 
79  id_size_int = id_size;
80  if (id && id_size_int > 0)
81  avio_printf(pb, "%.*s\n", id_size_int, id);
82 
84  avio_printf(pb, " --> ");
86 
88  &settings_size);
89 
90  if (settings_size > INT_MAX)
91  return AVERROR(EINVAL);
92 
93  settings_size_int = settings_size;
94  if (settings && settings_size_int > 0)
95  avio_printf(pb, " %.*s", settings_size_int, settings);
96 
97  avio_printf(pb, "\n");
98 
99  avio_write(pb, pkt->data, pkt->size);
100  avio_printf(pb, "\n");
101 
102  return 0;
103 }
104 
106  .name = "webvtt",
107  .long_name = NULL_IF_CONFIG_SMALL("WebVTT subtitle"),
108  .extensions = "vtt",
109  .mime_type = "text/vtt",
111  .subtitle_codec = AV_CODEC_ID_WEBVTT,
112  .write_header = webvtt_write_header,
113  .write_packet = webvtt_write_packet,
114 };
AVOutputFormat::name
const char * name
Definition: avformat.h:510
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:53
AVFMT_VARIABLE_FPS
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:484
AVFormatContext::streams
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1281
webvtt_write_header
static int webvtt_write_header(AVFormatContext *ctx)
Definition: webvttenc.c:46
AVPacket::data
uint8_t * data
Definition: packet.h:374
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:392
webvtt_write_packet
static int webvtt_write_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: webvttenc.c:64
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: avformat.c:697
AV_PKT_DATA_WEBVTT_SETTINGS
@ AV_PKT_DATA_WEBVTT_SETTINGS
The optional settings (rendering instructions) that immediately follow the timestamp specifier of a W...
Definition: packet.h:203
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
s
#define s(width, name)
Definition: cbs_vp9.c:256
ctx
AVFormatContext * ctx
Definition: movenc.c:48
AV_CODEC_ID_WEBVTT
@ AV_CODEC_ID_WEBVTT
Definition: codec_id.h:545
AVFormatContext
Format I/O context.
Definition: avformat.h:1213
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1108
AVFormatContext::pb
AVIOContext * pb
I/O context.
Definition: avformat.h:1255
webvtt_write_time
static void webvtt_write_time(AVIOContext *pb, int64_t millisec)
Definition: webvttenc.c:30
AVFormatContext::nb_streams
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1269
AVIOContext
Bytestream IO Context.
Definition: avio.h:162
AVPacket::size
int size
Definition: packet.h:375
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
id
enum AVCodecID id
Definition: extract_extradata_bsf.c:324
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:232
AVOutputFormat
Definition: avformat.h:509
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:367
av_packet_get_side_data
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, size_t *size)
Get side information from packet.
Definition: avpacket.c:251
AVFMT_TS_NONSTRICT
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition: avformat.h:491
AVStream
Stream structure.
Definition: avformat.h:948
avformat.h
avio_printf
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2
Writes a formatted string to the context.
ff_webvtt_muxer
const AVOutputFormat ff_webvtt_muxer
Definition: webvttenc.c:105
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:61
AVPacket
This structure stores compressed data.
Definition: packet.h:351
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AV_PKT_DATA_WEBVTT_IDENTIFIER
@ AV_PKT_DATA_WEBVTT_IDENTIFIER
The optional first identifier line of a WebVTT cue.
Definition: packet.h:197
min
float min
Definition: vorbis_enc_data.h:429