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.

117 lines
3.7KB

  1. /*
  2. * SSA/ASS muxer
  3. * Copyright (c) 2008 Michael Niedermayer
  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. typedef struct ASSContext{
  24. unsigned int extra_index;
  25. int write_ts; // 0: ssa (timing in payload), 1: ass (matroska like)
  26. }ASSContext;
  27. static int write_header(AVFormatContext *s)
  28. {
  29. ASSContext *ass = s->priv_data;
  30. AVCodecContext *avctx= s->streams[0]->codec;
  31. uint8_t *last= NULL;
  32. if (s->nb_streams != 1 || (avctx->codec_id != AV_CODEC_ID_SSA &&
  33. avctx->codec_id != AV_CODEC_ID_ASS)) {
  34. av_log(s, AV_LOG_ERROR, "Exactly one ASS/SSA stream is needed.\n");
  35. return -1;
  36. }
  37. ass->write_ts = avctx->codec_id == AV_CODEC_ID_ASS;
  38. avpriv_set_pts_info(s->streams[0], 64, 1, 100);
  39. while(ass->extra_index < avctx->extradata_size){
  40. uint8_t *p = avctx->extradata + ass->extra_index;
  41. uint8_t *end= strchr(p, '\n');
  42. if(!end) end= avctx->extradata + avctx->extradata_size;
  43. else end++;
  44. avio_write(s->pb, p, end-p);
  45. ass->extra_index += end-p;
  46. if(last && !memcmp(last, "[Events]", 8))
  47. break;
  48. last=p;
  49. }
  50. avio_flush(s->pb);
  51. return 0;
  52. }
  53. static int write_packet(AVFormatContext *s, AVPacket *pkt)
  54. {
  55. ASSContext *ass = s->priv_data;
  56. if (ass->write_ts) {
  57. long int layer;
  58. char *p;
  59. int64_t start = pkt->pts;
  60. int64_t end = start + pkt->duration;
  61. int hh1, mm1, ss1, ms1;
  62. int hh2, mm2, ss2, ms2;
  63. p = pkt->data + strcspn(pkt->data, ",") + 1; // skip ReadOrder
  64. layer = strtol(p, &p, 10);
  65. if (*p == ',')
  66. p++;
  67. hh1 = (int)(start / 360000); mm1 = (int)(start / 6000) % 60;
  68. hh2 = (int)(end / 360000); mm2 = (int)(end / 6000) % 60;
  69. ss1 = (int)(start / 100) % 60; ms1 = (int)(start % 100);
  70. ss2 = (int)(end / 100) % 60; ms2 = (int)(end % 100);
  71. if (hh1 > 9) hh1 = 9, mm1 = 59, ss1 = 59, ms1 = 99;
  72. if (hh2 > 9) hh2 = 9, mm2 = 59, ss2 = 59, ms2 = 99;
  73. avio_printf(s->pb, "Dialogue: %ld,%d:%02d:%02d.%02d,%d:%02d:%02d.%02d,%s\r\n",
  74. layer, hh1, mm1, ss1, ms1, hh2, mm2, ss2, ms2, p);
  75. } else {
  76. avio_write(s->pb, pkt->data, pkt->size);
  77. }
  78. return 0;
  79. }
  80. static int write_trailer(AVFormatContext *s)
  81. {
  82. ASSContext *ass = s->priv_data;
  83. AVCodecContext *avctx= s->streams[0]->codec;
  84. avio_write(s->pb, avctx->extradata + ass->extra_index,
  85. avctx->extradata_size - ass->extra_index);
  86. return 0;
  87. }
  88. AVOutputFormat ff_ass_muxer = {
  89. .name = "ass",
  90. .long_name = NULL_IF_CONFIG_SMALL("SSA (SubStation Alpha) subtitle"),
  91. .mime_type = "text/x-ssa",
  92. .extensions = "ass,ssa",
  93. .priv_data_size = sizeof(ASSContext),
  94. .subtitle_codec = AV_CODEC_ID_SSA,
  95. .write_header = write_header,
  96. .write_packet = write_packet,
  97. .write_trailer = write_trailer,
  98. .flags = AVFMT_GLOBALHEADER | AVFMT_NOTIMESTAMPS | AVFMT_TS_NONSTRICT,
  99. };