libavformat/srtenc.c
Go to the documentation of this file.
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 #include "libavutil/intreadwrite.h"
26 
27 /* TODO: add options for:
28  - character encoding;
29  - LF / CRLF;
30  - byte order mark.
31  */
32 
33 typedef struct SRTContext{
34  unsigned index;
35 } SRTContext;
36 
38 {
39  SRTContext *srt = avf->priv_data;
40 
41  if (avf->nb_streams != 1 ||
43  av_log(avf, AV_LOG_ERROR,
44  "SRT supports only a single subtitles stream.\n");
45  return AVERROR(EINVAL);
46  }
47  if (avf->streams[0]->codec->codec_id != AV_CODEC_ID_TEXT &&
49  avf->streams[0]->codec->codec_id != AV_CODEC_ID_SRT) {
50  av_log(avf, AV_LOG_ERROR,
51  "Unsupported subtitles codec: %s\n",
53  return AVERROR(EINVAL);
54  }
55  avpriv_set_pts_info(avf->streams[0], 64, 1, 1000);
56  srt->index = 1;
57  return 0;
58 }
59 
61 {
62  SRTContext *srt = avf->priv_data;
63  int write_ts = avf->streams[0]->codec->codec_id != AV_CODEC_ID_SRT;
64 
65  if (write_ts) {
66  int64_t s = pkt->pts, e, d = pkt->duration;
67  int size, x1 = -1, y1 = -1, x2 = -1, y2 = -1;
68  const uint8_t *p;
69 
71  if (p && size == 16) {
72  x1 = AV_RL32(p );
73  y1 = AV_RL32(p + 4);
74  x2 = AV_RL32(p + 8);
75  y2 = AV_RL32(p + 12);
76  }
77 
78  if (d <= 0)
79  /* For backward compatibility, fallback to convergence_duration. */
80  d = pkt->convergence_duration;
81  if (s == AV_NOPTS_VALUE || d < 0) {
83  "Insufficient timestamps in event number %d.\n", srt->index);
84  return 0;
85  }
86  e = s + d;
87  avio_printf(avf->pb, "%d\n%02d:%02d:%02d,%03d --> %02d:%02d:%02d,%03d",
88  srt->index,
89  (int)(s / 3600000), (int)(s / 60000) % 60,
90  (int)(s / 1000) % 60, (int)(s % 1000),
91  (int)(e / 3600000), (int)(e / 60000) % 60,
92  (int)(e / 1000) % 60, (int)(e % 1000));
93  if (p)
94  avio_printf(avf->pb, " X1:%03d X2:%03d Y1:%03d Y2:%03d",
95  x1, x2, y1, y2);
96  avio_printf(avf->pb, "\n");
97  }
98  avio_write(avf->pb, pkt->data, pkt->size);
99  if (write_ts)
100  avio_write(avf->pb, "\n\n", 2);
101  srt->index++;
102  return 0;
103 }
104 
106  .name = "srt",
107  .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
108  .mime_type = "application/x-subrip",
109  .extensions = "srt",
110  .priv_data_size = sizeof(SRTContext),
114  .subtitle_codec = AV_CODEC_ID_SUBRIP,
115 };
const char * s
Definition: avisynth_c.h:668
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.
Subtitle event position.
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:154
static int write_packet(AVFormatContext *s, AVPacket *pkt)
static int srt_write_header(AVFormatContext *avf)
y1
Definition: lab5.m:33
x1
Definition: genspecsines3.m:7
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition: avformat.h:363
set threshold d
struct SRTContext SRTContext
static int srt_write_packet(AVFormatContext *avf, AVPacket *pkt)
Format I/O context.
Definition: avformat.h:944
uint8_t
static AVPacket pkt
Definition: demuxing.c:56
AVStream ** streams
Definition: avformat.h:992
uint8_t * data
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:173
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
int64_t convergence_duration
Time difference in AVStream->time_base units from the pts of this packet to the point at which the ou...
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
int size
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:662
unsigned int nb_streams
A list of all streams in the file.
Definition: avformat.h:991
const char * name
Definition: avformat.h:378
#define AV_RL32
y2
Definition: lab5.m:34
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
enum AVMediaType codec_type
enum AVCodecID codec_id
AVIOContext * pb
I/O context.
Definition: avformat.h:977
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
x2
Definition: genspecsines3.m:8
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer 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.Buffer references ownership and permissions
static int flags
Definition: cpu.c:23
Main libavformat public API header.
raw UTF-8 text
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:355
void * priv_data
Format private data.
Definition: avformat.h:964
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:470
AVOutputFormat ff_srt_muxer
uint8_t * av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int *size)
Get side information from packet.
Definition: avpacket.c:289
This structure stores compressed data.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:190
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2