FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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  av_log(avf, AV_LOG_ERROR,
50  "Unsupported subtitles codec: %s\n",
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 
60 {
61  SRTContext *srt = avf->priv_data;
62 
63  // TODO: reindent
64  int64_t s = pkt->pts, e, d = pkt->duration;
65  int size, x1 = -1, y1 = -1, x2 = -1, y2 = -1;
66  const uint8_t *p;
67 
69  if (p && size == 16) {
70  x1 = AV_RL32(p );
71  y1 = AV_RL32(p + 4);
72  x2 = AV_RL32(p + 8);
73  y2 = AV_RL32(p + 12);
74  }
75 
76  if (d <= 0)
77  /* For backward compatibility, fallback to convergence_duration. */
78  d = pkt->convergence_duration;
79  if (s == AV_NOPTS_VALUE || d < 0) {
81  "Insufficient timestamps in event number %d.\n", srt->index);
82  return 0;
83  }
84  e = s + d;
85  avio_printf(avf->pb, "%d\n%02d:%02d:%02d,%03d --> %02d:%02d:%02d,%03d",
86  srt->index,
87  (int)(s / 3600000), (int)(s / 60000) % 60,
88  (int)(s / 1000) % 60, (int)(s % 1000),
89  (int)(e / 3600000), (int)(e / 60000) % 60,
90  (int)(e / 1000) % 60, (int)(e % 1000));
91  if (p)
92  avio_printf(avf->pb, " X1:%03d X2:%03d Y1:%03d Y2:%03d",
93  x1, x2, y1, y2);
94  avio_printf(avf->pb, "\n");
95 
96  avio_write(avf->pb, pkt->data, pkt->size);
97  avio_write(avf->pb, "\n\n", 2);
98  srt->index++;
99  return 0;
100 }
101 
103  .name = "srt",
104  .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
105  .mime_type = "application/x-subrip",
106  .extensions = "srt",
107  .priv_data_size = sizeof(SRTContext),
111  .subtitle_codec = AV_CODEC_ID_SUBRIP,
112 };
const char * s
Definition: avisynth_c.h:631
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
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
Subtitle event position.
Definition: avcodec.h:1341
static int srt_write_header(AVFormatContext *avf)
Definition: srtenc.c:37
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
static int srt_write_packet(AVFormatContext *avf, AVPacket *pkt)
Definition: srtenc.c:59
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
ptrdiff_t size
Definition: opengl_enc.c:101
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
#define av_log(a,...)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
int64_t convergence_duration
Time difference in AVStream->time_base units from the pts of this packet to the point at which the ou...
Definition: avcodec.h:1467
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:861
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1329
unsigned index
Definition: srtenc.c:34
const char * name
Definition: avformat.h:513
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
Definition: utils.c:3035
enum AVMediaType codec_type
Definition: avcodec.h:1510
enum AVCodecID codec_id
Definition: avcodec.h:1519
AVIOContext * pb
I/O context.
Definition: avformat.h:1315
static int flags
Definition: cpu.c:47
Main libavformat public API header.
raw UTF-8 text
Definition: avcodec.h:512
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:475
void * priv_data
Format private data.
Definition: avformat.h:1301
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:493
AVOutputFormat ff_srt_muxer
Definition: srtenc.c:102
uint8_t * av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int *size)
Get side information from packet.
Definition: avpacket.c:324
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
This structure stores compressed data.
Definition: avcodec.h:1400
static int write_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: v4l2enc.c:86
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1416
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:240
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2