srtenc.c 3.74 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * SubRip subtitle muxer
 * Copyright (c) 2012  Nicolas George <nicolas.george@normalesup.org>
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "avformat.h"
#include "internal.h"
#include "libavutil/log.h"
25
#include "libavutil/intreadwrite.h"
26 27 28 29 30 31 32 33 34 35 36 37 38

/* TODO: add options for:
   - character encoding;
   - LF / CRLF;
   - byte order mark.
 */

typedef struct SRTContext{
    unsigned index;
} SRTContext;

static int srt_write_header(AVFormatContext *avf)
{
39 40
    SRTContext *srt = avf->priv_data;

41
    if (avf->nb_streams != 1 ||
42
        avf->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE) {
43 44 45 46
        av_log(avf, AV_LOG_ERROR,
               "SRT supports only a single subtitles stream.\n");
        return AVERROR(EINVAL);
    }
47 48
    if (avf->streams[0]->codecpar->codec_id != AV_CODEC_ID_TEXT &&
        avf->streams[0]->codecpar->codec_id != AV_CODEC_ID_SUBRIP) {
49 50
        av_log(avf, AV_LOG_ERROR,
               "Unsupported subtitles codec: %s\n",
51
               avcodec_get_name(avf->streams[0]->codecpar->codec_id));
52 53 54
        return AVERROR(EINVAL);
    }
    avpriv_set_pts_info(avf->streams[0], 64, 1, 1000);
55
    srt->index = 1;
56 57 58 59 60 61 62
    return 0;
}

static int srt_write_packet(AVFormatContext *avf, AVPacket *pkt)
{
    SRTContext *srt = avf->priv_data;

Timothy Gu's avatar
Timothy Gu committed
63 64 65
    int64_t s = pkt->pts, e, d = pkt->duration;
    int size, x1 = -1, y1 = -1, x2 = -1, y2 = -1;
    const uint8_t *p;
66

Timothy Gu's avatar
Timothy Gu committed
67 68 69 70 71 72 73
    p = av_packet_get_side_data(pkt, AV_PKT_DATA_SUBTITLE_POSITION, &size);
    if (p && size == 16) {
        x1 = AV_RL32(p     );
        y1 = AV_RL32(p +  4);
        x2 = AV_RL32(p +  8);
        y2 = AV_RL32(p + 12);
    }
74

75 76
#if FF_API_CONVERGENCE_DURATION
FF_DISABLE_DEPRECATION_WARNINGS
Timothy Gu's avatar
Timothy Gu committed
77 78 79
    if (d <= 0)
        /* For backward compatibility, fallback to convergence_duration. */
        d = pkt->convergence_duration;
80 81
FF_ENABLE_DEPRECATION_WARNINGS
#endif
Timothy Gu's avatar
Timothy Gu committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    if (s == AV_NOPTS_VALUE || d < 0) {
        av_log(avf, AV_LOG_WARNING,
               "Insufficient timestamps in event number %d.\n", srt->index);
        return 0;
    }
    e = s + d;
    avio_printf(avf->pb, "%d\n%02d:%02d:%02d,%03d --> %02d:%02d:%02d,%03d",
                   srt->index,
                   (int)(s / 3600000),      (int)(s / 60000) % 60,
                   (int)(s /    1000) % 60, (int)(s %  1000),
                   (int)(e / 3600000),      (int)(e / 60000) % 60,
                   (int)(e /    1000) % 60, (int)(e %  1000));
    if (p)
        avio_printf(avf->pb, "  X1:%03d X2:%03d Y1:%03d Y2:%03d",
                    x1, x2, y1, y2);
    avio_printf(avf->pb, "\n");
Clément Bœsch's avatar
Clément Bœsch committed
98

99
    avio_write(avf->pb, pkt->data, pkt->size);
Clément Bœsch's avatar
Clément Bœsch committed
100
    avio_write(avf->pb, "\n\n", 2);
101
    srt->index++;
102 103 104 105 106
    return 0;
}

AVOutputFormat ff_srt_muxer = {
    .name           = "srt",
107
    .long_name      = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
108 109 110 111 112
    .mime_type      = "application/x-subrip",
    .extensions     = "srt",
    .priv_data_size = sizeof(SRTContext),
    .write_header   = srt_write_header,
    .write_packet   = srt_write_packet,
113
    .flags          = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT,
114
    .subtitle_codec = AV_CODEC_ID_SUBRIP,
115
};