You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
3.7KB

  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. #include "libavutil/intreadwrite.h"
  25. /* TODO: add options for:
  26. - character encoding;
  27. - LF / CRLF;
  28. - byte order mark.
  29. */
  30. typedef struct SRTContext{
  31. unsigned index;
  32. } SRTContext;
  33. static int srt_write_header(AVFormatContext *avf)
  34. {
  35. SRTContext *srt = avf->priv_data;
  36. if (avf->nb_streams != 1 ||
  37. avf->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE) {
  38. av_log(avf, AV_LOG_ERROR,
  39. "SRT supports only a single subtitles stream.\n");
  40. return AVERROR(EINVAL);
  41. }
  42. if (avf->streams[0]->codecpar->codec_id != AV_CODEC_ID_TEXT &&
  43. avf->streams[0]->codecpar->codec_id != AV_CODEC_ID_SUBRIP) {
  44. av_log(avf, AV_LOG_ERROR,
  45. "Unsupported subtitles codec: %s\n",
  46. avcodec_get_name(avf->streams[0]->codecpar->codec_id));
  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. static int srt_write_packet(AVFormatContext *avf, AVPacket *pkt)
  54. {
  55. SRTContext *srt = avf->priv_data;
  56. int64_t s = pkt->pts, e, d = pkt->duration;
  57. int size, x1 = -1, y1 = -1, x2 = -1, y2 = -1;
  58. const uint8_t *p;
  59. p = av_packet_get_side_data(pkt, AV_PKT_DATA_SUBTITLE_POSITION, &size);
  60. if (p && size == 16) {
  61. x1 = AV_RL32(p );
  62. y1 = AV_RL32(p + 4);
  63. x2 = AV_RL32(p + 8);
  64. y2 = AV_RL32(p + 12);
  65. }
  66. #if FF_API_CONVERGENCE_DURATION
  67. FF_DISABLE_DEPRECATION_WARNINGS
  68. if (d <= 0)
  69. /* For backward compatibility, fallback to convergence_duration. */
  70. d = pkt->convergence_duration;
  71. FF_ENABLE_DEPRECATION_WARNINGS
  72. #endif
  73. if (s == AV_NOPTS_VALUE || d < 0) {
  74. av_log(avf, AV_LOG_WARNING,
  75. "Insufficient timestamps in event number %d.\n", srt->index);
  76. return 0;
  77. }
  78. e = s + d;
  79. avio_printf(avf->pb, "%d\n%02d:%02d:%02d,%03d --> %02d:%02d:%02d,%03d",
  80. srt->index,
  81. (int)(s / 3600000), (int)(s / 60000) % 60,
  82. (int)(s / 1000) % 60, (int)(s % 1000),
  83. (int)(e / 3600000), (int)(e / 60000) % 60,
  84. (int)(e / 1000) % 60, (int)(e % 1000));
  85. if (p)
  86. avio_printf(avf->pb, " X1:%03d X2:%03d Y1:%03d Y2:%03d",
  87. x1, x2, y1, y2);
  88. avio_printf(avf->pb, "\n");
  89. avio_write(avf->pb, pkt->data, pkt->size);
  90. avio_write(avf->pb, "\n\n", 2);
  91. srt->index++;
  92. return 0;
  93. }
  94. AVOutputFormat ff_srt_muxer = {
  95. .name = "srt",
  96. .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
  97. .mime_type = "application/x-subrip",
  98. .extensions = "srt",
  99. .priv_data_size = sizeof(SRTContext),
  100. .write_header = srt_write_header,
  101. .write_packet = srt_write_packet,
  102. .flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT,
  103. .subtitle_codec = AV_CODEC_ID_SUBRIP,
  104. };