FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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, "%"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];
49  AVIOContext *pb = ctx->pb;
50 
51  avpriv_set_pts_info(s, 64, 1, 1000);
52 
53  avio_printf(pb, "WEBVTT\n");
54  avio_flush(pb);
55 
56  return 0;
57 }
58 
60 {
61  AVIOContext *pb = ctx->pb;
62  int id_size, settings_size;
63  uint8_t *id, *settings;
64 
65  avio_printf(pb, "\n");
66 
68  &id_size);
69 
70  if (id && id_size > 0)
71  avio_printf(pb, "%.*s\n", id_size, id);
72 
73  webvtt_write_time(pb, pkt->pts);
74  avio_printf(pb, " --> ");
75  webvtt_write_time(pb, pkt->pts + pkt->duration);
76 
78  &settings_size);
79 
80  if (settings && settings_size > 0)
81  avio_printf(pb, " %.*s", settings_size, settings);
82 
83  avio_printf(pb, "\n");
84 
85  avio_write(pb, pkt->data, pkt->size);
86  avio_printf(pb, "\n");
87 
88  return 0;
89 }
90 
92  .name = "webvtt",
93  .long_name = NULL_IF_CONFIG_SMALL("WebVTT subtitle"),
94  .extensions = "vtt",
95  .mime_type = "text/vtt",
97  .subtitle_codec = AV_CODEC_ID_WEBVTT,
98  .write_header = webvtt_write_header,
99  .write_packet = webvtt_write_packet,
100 };
const char * s
Definition: avisynth_c.h:631
Bytestream IO Context.
Definition: avio.h:111
static int webvtt_write_header(AVFormatContext *ctx)
Definition: webvttenc.c:46
The optional first identifier line of a WebVTT cue.
Definition: avcodec.h:1354
enum AVCodecID id
Definition: mxfenc.c:101
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4083
int size
Definition: avcodec.h:1424
static AVPacket pkt
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition: avformat.h:483
Format I/O context.
Definition: avformat.h:1273
uint8_t
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1341
uint8_t * data
Definition: avcodec.h:1423
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:178
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1441
static void webvtt_write_time(AVIOContext *pb, int64_t millisec)
Definition: webvttenc.c:30
AVOutputFormat ff_webvtt_muxer
Definition: webvttenc.c:91
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
int void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition: aviobuf.c:198
const char * name
Definition: avformat.h:513
static int webvtt_write_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: webvttenc.c:59
Stream structure.
Definition: avformat.h:842
AVIOContext * pb
I/O context.
Definition: avformat.h:1315
Main libavformat public API header.
The optional settings (rendering instructions) that immediately follow the timestamp specifier of a W...
Definition: avcodec.h:1360
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:475
uint8_t * av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int *size)
Get side information from packet.
Definition: avpacket.c:324
float min
This structure stores compressed data.
Definition: avcodec.h:1400
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1416
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2