2 * SubRip subtitle muxer
3 * Copyright (c) 2012 Nicolas George <nicolas.george@normalesup.org>
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "libavutil/log.h"
26 /* TODO: add options for:
32 typedef struct SRTContext{
36 static int srt_write_header(AVFormatContext *avf)
38 if (avf->nb_streams != 1 ||
39 avf->streams[0]->codec->codec_type != AVMEDIA_TYPE_SUBTITLE) {
40 av_log(avf, AV_LOG_ERROR,
41 "SRT supports only a single subtitles stream.\n");
42 return AVERROR(EINVAL);
44 if (avf->streams[0]->codec->codec_id != AV_CODEC_ID_TEXT &&
45 avf->streams[0]->codec->codec_id != AV_CODEC_ID_SRT) {
46 av_log(avf, AV_LOG_ERROR,
47 "Unsupported subtitles codec: %s\n",
48 avcodec_get_name(avf->streams[0]->codec->codec_id));
49 return AVERROR(EINVAL);
51 avpriv_set_pts_info(avf->streams[0], 64, 1, 1000);
55 static int srt_write_packet(AVFormatContext *avf, AVPacket *pkt)
57 SRTContext *srt = avf->priv_data;
58 int write_ts = avf->streams[0]->codec->codec_id != AV_CODEC_ID_SRT;
63 int64_t s = pkt->pts, e, d = pkt->duration;
67 d = pkt->convergence_duration;
68 if (s == AV_NOPTS_VALUE || d <= 0) {
69 av_log(avf, AV_LOG_ERROR, "Insufficient timestamps.\n");
70 return AVERROR(EINVAL);
73 len = snprintf(buf, sizeof(buf),
74 "%d\n%02d:%02d:%02d,%03d --> %02d:%02d:%02d,%03d\n",
76 (int)(s / 3600000), (int)(s / 60000) % 60,
77 (int)(s / 1000) % 60, (int)(s % 1000),
78 (int)(e / 3600000), (int)(e / 60000) % 60,
79 (int)(e / 1000) % 60, (int)(e % 1000));
80 avio_write(avf->pb, buf, len);
82 avio_write(avf->pb, pkt->data, pkt->size);
84 avio_write(avf->pb, "\n\n", 2);
89 AVOutputFormat ff_srt_muxer = {
91 .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
92 .mime_type = "application/x-subrip",
94 .priv_data_size = sizeof(SRTContext),
95 .write_header = srt_write_header,
96 .write_packet = srt_write_packet,
97 .flags = AVFMT_VARIABLE_FPS,
98 .subtitle_codec = AV_CODEC_ID_TEXT,