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 if (avf->nb_streams != 1 ||
00039 avf->streams[0]->codec->codec_type != AVMEDIA_TYPE_SUBTITLE) {
00040 av_log(avf, AV_LOG_ERROR,
00041 "SRT supports only a single subtitles stream.\n");
00042 return AVERROR(EINVAL);
00043 }
00044 if (avf->streams[0]->codec->codec_id != AV_CODEC_ID_TEXT &&
00045 avf->streams[0]->codec->codec_id != AV_CODEC_ID_SUBRIP &&
00046 avf->streams[0]->codec->codec_id != AV_CODEC_ID_SRT) {
00047 av_log(avf, AV_LOG_ERROR,
00048 "Unsupported subtitles codec: %s\n",
00049 avcodec_get_name(avf->streams[0]->codec->codec_id));
00050 return AVERROR(EINVAL);
00051 }
00052 avpriv_set_pts_info(avf->streams[0], 64, 1, 1000);
00053 return 0;
00054 }
00055
00056 static int srt_write_packet(AVFormatContext *avf, AVPacket *pkt)
00057 {
00058 SRTContext *srt = avf->priv_data;
00059 int write_ts = avf->streams[0]->codec->codec_id != AV_CODEC_ID_SRT;
00060
00061 srt->index++;
00062 if (write_ts) {
00063 char buf[64];
00064 int64_t s = pkt->pts, e, d = pkt->duration;
00065 int len;
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_ERROR, "Insufficient timestamps.\n");
00072 return AVERROR(EINVAL);
00073 }
00074 e = s + d;
00075 len = snprintf(buf, sizeof(buf),
00076 "%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 avio_write(avf->pb, buf, len);
00083 }
00084 avio_write(avf->pb, pkt->data, pkt->size);
00085 if (write_ts)
00086 avio_write(avf->pb, "\n\n", 2);
00087 avio_flush(avf->pb);
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,
00100 .subtitle_codec = AV_CODEC_ID_TEXT,
00101 };