FFmpeg
Loading...
Searching...
No Matches
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 https://www.w3.org/TR/webvtt1/
25 */
26
27#include "avformat.h"
28#include "internal.h"
29#include "mux.h"
30
31static void webvtt_write_time(AVIOContext *pb, int64_t millisec)
32{
33 int64_t sec, min, hour;
34 sec = millisec / 1000;
35 millisec -= 1000 * sec;
36 min = sec / 60;
37 sec -= 60 * min;
38 hour = min / 60;
39 min -= 60 * hour;
40
41 if (hour > 0)
42 avio_printf(pb, "%02"PRId64":", hour);
43
44 avio_printf(pb, "%02"PRId64":%02"PRId64".%03"PRId64"", min, sec, millisec);
45}
46
48{
49 AVStream *s = ctx->streams[0];
50 AVIOContext *pb = ctx->pb;
51
52 avpriv_set_pts_info(s, 64, 1, 1000);
53
54 avio_printf(pb, "WEBVTT\n");
55
56 return 0;
57}
58
60{
61 AVIOContext *pb = ctx->pb;
62 size_t id_size, settings_size;
63 int id_size_int, settings_size_int;
64 uint8_t *id, *settings;
65
66 avio_printf(pb, "\n");
67
69 &id_size);
70
71 if (id_size > INT_MAX)
72 return AVERROR(EINVAL);
73
74 id_size_int = id_size;
75 if (id && id_size_int > 0)
76 avio_printf(pb, "%.*s\n", id_size_int, id);
77
78 webvtt_write_time(pb, pkt->pts);
79 avio_printf(pb, " --> ");
80 webvtt_write_time(pb, pkt->pts + pkt->duration);
81
83 &settings_size);
84
85 if (settings_size > INT_MAX)
86 return AVERROR(EINVAL);
87
88 settings_size_int = settings_size;
89 if (settings && settings_size_int > 0)
90 avio_printf(pb, " %.*s", settings_size_int, settings);
91
92 avio_printf(pb, "\n");
93
94 avio_write(pb, pkt->data, pkt->size);
95 avio_printf(pb, "\n");
96
97 return 0;
98}
99
101 .p.name = "webvtt",
102 .p.long_name = NULL_IF_CONFIG_SMALL("WebVTT subtitle"),
103 .p.extensions = "vtt",
104 .p.mime_type = "text/vtt",
106 .p.video_codec = AV_CODEC_ID_NONE,
107 .p.audio_codec = AV_CODEC_ID_NONE,
108 .p.subtitle_codec = AV_CODEC_ID_WEBVTT,
109 .write_header = webvtt_write_header,
110 .write_packet = webvtt_write_packet,
111 .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH |
113};
const FFOutputFormat ff_webvtt_muxer
Definition webvttenc.c:100
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:834
Main libavformat public API header.
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition avformat.h:501
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition avformat.h:507
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2
Writes a formatted string to the context.
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition aviobuf.c:206
#define s(width, name)
Definition cbs_vp9.c:198
long long int64_t
Definition coverity.c:34
#define min(a, b)
static AVPacket * pkt
enum AVCodecID id
Definition dts2pts.c:607
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
@ AV_CODEC_ID_WEBVTT
Definition codec_id.h:584
@ AV_PKT_DATA_WEBVTT_SETTINGS
The optional settings (rendering instructions) that immediately follow the timestamp specifier of a W...
Definition packet.h:199
@ AV_PKT_DATA_WEBVTT_IDENTIFIER
The optional first identifier line of a WebVTT cue.
Definition packet.h:193
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, size_t *size)
Get side information from packet.
Definition packet.c:252
#define AVERROR(e)
Definition error.h:45
static int webvtt_write_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition webvttenc.c:59
static void webvtt_write_time(AVIOContext *pb, int64_t millisec)
Definition webvttenc.c:31
static int webvtt_write_header(AVFormatContext *ctx)
Definition webvttenc.c:47
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define FF_OFMT_FLAG_MAX_ONE_OF_EACH
If this flag is set, it indicates that for each codec type whose corresponding default codec (i....
Definition mux.h:50
#define FF_OFMT_FLAG_ONLY_DEFAULT_CODECS
If this flag is set, then the only permitted audio/video/subtitle codec ids are AVOutputFormat....
Definition mux.h:59
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
This structure stores compressed data.
Definition packet.h:580
Stream structure.
Definition avformat.h:766
static AVFormatContext * ctx
Definition movenc.c:49