srtenc.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. #include "avformat.h"
  22. #include "internal.h"
  23. #include "libavutil/log.h"
  24. /* TODO: add options for:
  25. - character encoding;
  26. - LF / CRLF;
  27. - byte order mark.
  28. */
  29. typedef struct SRTContext{
  30. unsigned index;
  31. } SRTContext;
  32. static int srt_write_header(AVFormatContext *avf)
  33. {
  34. if (avf->nb_streams != 1 ||
  35. avf->streams[0]->codec->codec_type != AVMEDIA_TYPE_SUBTITLE) {
  36. av_log(avf, AV_LOG_ERROR,
  37. "SRT supports only a single subtitles stream.\n");
  38. return AVERROR(EINVAL);
  39. }
  40. if (avf->streams[0]->codec->codec_id != AV_CODEC_ID_TEXT &&
  41. avf->streams[0]->codec->codec_id != AV_CODEC_ID_SUBRIP &&
  42. avf->streams[0]->codec->codec_id != AV_CODEC_ID_SRT) {
  43. av_log(avf, AV_LOG_ERROR,
  44. "Unsupported subtitles codec: %s\n",
  45. avcodec_get_name(avf->streams[0]->codec->codec_id));
  46. return AVERROR(EINVAL);
  47. }
  48. avpriv_set_pts_info(avf->streams[0], 64, 1, 1000);
  49. return 0;
  50. }
  51. static int srt_write_packet(AVFormatContext *avf, AVPacket *pkt)
  52. {
  53. SRTContext *srt = avf->priv_data;
  54. int write_ts = avf->streams[0]->codec->codec_id != AV_CODEC_ID_SRT;
  55. srt->index++;
  56. if (write_ts) {
  57. char buf[64];
  58. int64_t s = pkt->pts, e, d = pkt->duration;
  59. int len;
  60. if (d <= 0)
  61. /* For backward compatibility, fallback to convergence_duration. */
  62. d = pkt->convergence_duration;
  63. if (s == AV_NOPTS_VALUE || d <= 0) {
  64. av_log(avf, AV_LOG_ERROR, "Insufficient timestamps.\n");
  65. return AVERROR(EINVAL);
  66. }
  67. e = s + d;
  68. len = snprintf(buf, sizeof(buf),
  69. "%d\n%02d:%02d:%02d,%03d --> %02d:%02d:%02d,%03d\n",
  70. srt->index,
  71. (int)(s / 3600000), (int)(s / 60000) % 60,
  72. (int)(s / 1000) % 60, (int)(s % 1000),
  73. (int)(e / 3600000), (int)(e / 60000) % 60,
  74. (int)(e / 1000) % 60, (int)(e % 1000));
  75. avio_write(avf->pb, buf, len);
  76. }
  77. avio_write(avf->pb, pkt->data, pkt->size);
  78. if (write_ts)
  79. avio_write(avf->pb, "\n\n", 2);
  80. avio_flush(avf->pb);
  81. return 0;
  82. }
  83. AVOutputFormat ff_srt_muxer = {
  84. .name = "srt",
  85. .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
  86. .mime_type = "application/x-subrip",
  87. .extensions = "srt",
  88. .priv_data_size = sizeof(SRTContext),
  89. .write_header = srt_write_header,
  90. .write_packet = srt_write_packet,
  91. .flags = AVFMT_VARIABLE_FPS,
  92. .subtitle_codec = AV_CODEC_ID_TEXT,
  93. };