00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "avformat.h"
00023 #include "internal.h"
00024 #include "libavutil/log.h"
00025
00026
00027
00028
00029
00030
00031
00032 typedef struct SRTContext{
00033 unsigned index;
00034 } SRTContext;
00035
00036 static int srt_write_header(AVFormatContext *avf)
00037 {
00038 SRTContext *srt = avf->priv_data;
00039
00040 if (avf->nb_streams != 1 ||
00041 avf->streams[0]->codec->codec_type != AVMEDIA_TYPE_SUBTITLE) {
00042 av_log(avf, AV_LOG_ERROR,
00043 "SRT supports only a single subtitles stream.\n");
00044 return AVERROR(EINVAL);
00045 }
00046 if (avf->streams[0]->codec->codec_id != AV_CODEC_ID_TEXT &&
00047 avf->streams[0]->codec->codec_id != AV_CODEC_ID_SUBRIP &&
00048 avf->streams[0]->codec->codec_id != AV_CODEC_ID_SRT) {
00049 av_log(avf, AV_LOG_ERROR,
00050 "Unsupported subtitles codec: %s\n",
00051 avcodec_get_name(avf->streams[0]->codec->codec_id));
00052 return AVERROR(EINVAL);
00053 }
00054 avpriv_set_pts_info(avf->streams[0], 64, 1, 1000);
00055 srt->index = 1;
00056 return 0;
00057 }
00058
00059 static int srt_write_packet(AVFormatContext *avf, AVPacket *pkt)
00060 {
00061 SRTContext *srt = avf->priv_data;
00062 int write_ts = avf->streams[0]->codec->codec_id != AV_CODEC_ID_SRT;
00063
00064 if (write_ts) {
00065 int64_t s = pkt->pts, e, d = pkt->duration;
00066
00067 if (d <= 0)
00068
00069 d = pkt->convergence_duration;
00070 if (s == AV_NOPTS_VALUE || d < 0) {
00071 av_log(avf, AV_LOG_WARNING,
00072 "Insufficient timestamps in event number %d.\n", srt->index);
00073 return 0;
00074 }
00075 e = s + d;
00076 avio_printf(avf->pb, "%d\n%02d:%02d:%02d,%03d --> %02d:%02d:%02d,%03d\n",
00077 srt->index,
00078 (int)(s / 3600000), (int)(s / 60000) % 60,
00079 (int)(s / 1000) % 60, (int)(s % 1000),
00080 (int)(e / 3600000), (int)(e / 60000) % 60,
00081 (int)(e / 1000) % 60, (int)(e % 1000));
00082 }
00083 avio_write(avf->pb, pkt->data, pkt->size);
00084 if (write_ts)
00085 avio_write(avf->pb, "\n\n", 2);
00086 avio_flush(avf->pb);
00087 srt->index++;
00088 return 0;
00089 }
00090
00091 AVOutputFormat ff_srt_muxer = {
00092 .name = "srt",
00093 .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
00094 .mime_type = "application/x-subrip",
00095 .extensions = "srt",
00096 .priv_data_size = sizeof(SRTContext),
00097 .write_header = srt_write_header,
00098 .write_packet = srt_write_packet,
00099 .flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT,
00100 .subtitle_codec = AV_CODEC_ID_SUBRIP,
00101 };