]> git.sesse.net Git - ffmpeg/blob - libavformat/srtenc.c
Merge commit 'bfe5454cd238b16e7977085f880205229103eccb'
[ffmpeg] / libavformat / srtenc.c
1 /*
2  * SubRip subtitle muxer
3  * Copyright (c) 2012  Nicolas George <nicolas.george@normalesup.org>
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "avformat.h"
23 #include "internal.h"
24 #include "libavutil/log.h"
25
26 /* TODO: add options for:
27    - character encoding;
28    - LF / CRLF;
29    - byte order mark.
30  */
31
32 typedef struct SRTContext{
33     unsigned index;
34 } SRTContext;
35
36 static int srt_write_header(AVFormatContext *avf)
37 {
38     SRTContext *srt = avf->priv_data;
39
40     if (avf->nb_streams != 1 ||
41         avf->streams[0]->codec->codec_type != AVMEDIA_TYPE_SUBTITLE) {
42         av_log(avf, AV_LOG_ERROR,
43                "SRT supports only a single subtitles stream.\n");
44         return AVERROR(EINVAL);
45     }
46     if (avf->streams[0]->codec->codec_id != AV_CODEC_ID_TEXT &&
47         avf->streams[0]->codec->codec_id != AV_CODEC_ID_SUBRIP &&
48         avf->streams[0]->codec->codec_id != AV_CODEC_ID_SRT) {
49         av_log(avf, AV_LOG_ERROR,
50                "Unsupported subtitles codec: %s\n",
51                avcodec_get_name(avf->streams[0]->codec->codec_id));
52         return AVERROR(EINVAL);
53     }
54     avpriv_set_pts_info(avf->streams[0], 64, 1, 1000);
55     srt->index = 1;
56     return 0;
57 }
58
59 static int srt_write_packet(AVFormatContext *avf, AVPacket *pkt)
60 {
61     SRTContext *srt = avf->priv_data;
62     int write_ts = avf->streams[0]->codec->codec_id != AV_CODEC_ID_SRT;
63
64     if (write_ts) {
65         int64_t s = pkt->pts, e, d = pkt->duration;
66
67         if (d <= 0)
68             /* For backward compatibility, fallback to convergence_duration. */
69             d = pkt->convergence_duration;
70         if (s == AV_NOPTS_VALUE || d < 0) {
71             av_log(avf, AV_LOG_WARNING,
72                    "Insufficient timestamps in event number %d.\n", srt->index);
73             return 0;
74         }
75         e = s + d;
76         avio_printf(avf->pb, "%d\n%02d:%02d:%02d,%03d --> %02d:%02d:%02d,%03d\n",
77                        srt->index,
78                        (int)(s / 3600000),      (int)(s / 60000) % 60,
79                        (int)(s /    1000) % 60, (int)(s %  1000),
80                        (int)(e / 3600000),      (int)(e / 60000) % 60,
81                        (int)(e /    1000) % 60, (int)(e %  1000));
82     }
83     avio_write(avf->pb, pkt->data, pkt->size);
84     if (write_ts)
85         avio_write(avf->pb, "\n\n", 2);
86     avio_flush(avf->pb);
87     srt->index++;
88     return 0;
89 }
90
91 AVOutputFormat ff_srt_muxer = {
92     .name           = "srt",
93     .long_name      = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
94     .mime_type      = "application/x-subrip",
95     .extensions     = "srt",
96     .priv_data_size = sizeof(SRTContext),
97     .write_header   = srt_write_header,
98     .write_packet   = srt_write_packet,
99     .flags          = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT,
100     .subtitle_codec = AV_CODEC_ID_SUBRIP,
101 };