FFmpeg
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 "mux.h"
25 #include "libavutil/log.h"
26 #include "libavutil/intreadwrite.h"
27 
28 /* TODO: add options for:
29  - character encoding;
30  - LF / CRLF;
31  - byte order mark.
32  */
33 
34 typedef struct SRTContext{
35  unsigned index;
36 } SRTContext;
37 
39 {
40  SRTContext *srt = avf->priv_data;
41 
42  if (avf->streams[0]->codecpar->codec_id != AV_CODEC_ID_TEXT &&
44  av_log(avf, AV_LOG_ERROR,
45  "Unsupported subtitles codec: %s\n",
47  return AVERROR(EINVAL);
48  }
49  avpriv_set_pts_info(avf->streams[0], 64, 1, 1000);
50  srt->index = 1;
51  return 0;
52 }
53 
55 {
56  SRTContext *srt = avf->priv_data;
57 
58  int64_t s = pkt->pts, e, d = pkt->duration;
59  size_t size;
60  int x1 = -1, y1 = -1, x2 = -1, y2 = -1;
61  const uint8_t *p;
62 
64  if (p && size == 16) {
65  x1 = AV_RL32(p );
66  y1 = AV_RL32(p + 4);
67  x2 = AV_RL32(p + 8);
68  y2 = AV_RL32(p + 12);
69  }
70 
71  if (s == AV_NOPTS_VALUE || d < 0) {
73  "Insufficient timestamps in event number %d.\n", srt->index);
74  return 0;
75  }
76  e = s + d;
77  avio_printf(avf->pb, "%d\n%02d:%02d:%02d,%03d --> %02d:%02d:%02d,%03d",
78  srt->index,
79  (int)(s / 3600000), (int)(s / 60000) % 60,
80  (int)(s / 1000) % 60, (int)(s % 1000),
81  (int)(e / 3600000), (int)(e / 60000) % 60,
82  (int)(e / 1000) % 60, (int)(e % 1000));
83  if (p)
84  avio_printf(avf->pb, " X1:%03d X2:%03d Y1:%03d Y2:%03d",
85  x1, x2, y1, y2);
86  avio_printf(avf->pb, "\n");
87 
88  avio_write(avf->pb, pkt->data, pkt->size);
89  avio_write(avf->pb, "\n\n", 2);
90  srt->index++;
91  return 0;
92 }
93 
95  .p.name = "srt",
96  .p.long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
97  .p.mime_type = "application/x-subrip",
98  .p.extensions = "srt",
100  .p.video_codec = AV_CODEC_ID_NONE,
101  .p.audio_codec = AV_CODEC_ID_NONE,
102  .p.subtitle_codec = AV_CODEC_ID_SUBRIP,
103  .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH,
104  .priv_data_size = sizeof(SRTContext),
107 };
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:215
AVOutputFormat::name
const char * name
Definition: avformat.h:510
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame 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. Frame references ownership and permissions
AVFMT_VARIABLE_FPS
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:482
int64_t
long long int64_t
Definition: coverity.c:34
AVFormatContext::streams
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1368
SRTContext
Definition: srtenc.c:34
AVPacket::data
uint8_t * data
Definition: packet.h:539
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:557
FFOutputFormat::p
AVOutputFormat p
The public AVOutputFormat.
Definition: mux.h:65
avpriv_set_pts_info
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:862
srt_write_header
static int srt_write_header(AVFormatContext *avf)
Definition: srtenc.c:38
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
AV_PKT_DATA_SUBTITLE_POSITION
@ AV_PKT_DATA_SUBTITLE_POSITION
Subtitle event position.
Definition: packet.h:180
AVFormatContext
Format I/O context.
Definition: avformat.h:1300
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:771
AVFormatContext::pb
AVIOContext * pb
I/O context.
Definition: avformat.h:1342
FFOutputFormat
Definition: mux.h:61
AVPacket::size
int size
Definition: packet.h:540
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
size
int size
Definition: twinvq_data.h:10344
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
ff_srt_muxer
const FFOutputFormat ff_srt_muxer
Definition: srtenc.c:94
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:201
avcodec_get_name
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
Definition: utils.c:409
log.h
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:50
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:532
av_packet_get_side_data
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
AV_CODEC_ID_SUBRIP
@ AV_CODEC_ID_SUBRIP
Definition: codec_id.h:572
FF_OFMT_FLAG_MAX_ONE_OF_EACH
#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
SRTContext::index
unsigned index
Definition: srtenc.c:35
write_packet
static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
Definition: ffmpeg_mux.c:209
AVFMT_TS_NONSTRICT
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition: avformat.h:491
avformat.h
srt_write_packet
static int srt_write_packet(AVFormatContext *avf, AVPacket *pkt)
Definition: srtenc.c:54
AV_CODEC_ID_TEXT
@ AV_CODEC_ID_TEXT
raw UTF-8 text
Definition: codec_id.h:557
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
avio_printf
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2
Writes a formatted string to the context.
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:516
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
write_header
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:348
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1328
mux.h