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.

101 lines
2.7KB

  1. /*
  2. * Copyright (c) 2013 Matthew Heaney
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * WebVTT subtitle muxer
  23. * @see http://dev.w3.org/html5/webvtt/
  24. */
  25. #include "avformat.h"
  26. #include "internal.h"
  27. static void webvtt_write_time(AVIOContext *pb, int64_t millisec)
  28. {
  29. int64_t sec, min, hour;
  30. sec = millisec / 1000;
  31. millisec -= 1000 * sec;
  32. min = sec / 60;
  33. sec -= 60 * min;
  34. hour = min / 60;
  35. min -= 60 * hour;
  36. if (hour > 0)
  37. avio_printf(pb, "%"PRId64":", hour);
  38. avio_printf(pb, "%02"PRId64":%02"PRId64".%03"PRId64"", min, sec, millisec);
  39. }
  40. static int webvtt_write_header(AVFormatContext *ctx)
  41. {
  42. AVStream *s = ctx->streams[0];
  43. AVIOContext *pb = ctx->pb;
  44. avpriv_set_pts_info(s, 64, 1, 1000);
  45. avio_printf(pb, "WEBVTT\n");
  46. avio_flush(pb);
  47. return 0;
  48. }
  49. static int webvtt_write_packet(AVFormatContext *ctx, AVPacket *pkt)
  50. {
  51. AVIOContext *pb = ctx->pb;
  52. int id_size, settings_size;
  53. uint8_t *id, *settings;
  54. avio_printf(pb, "\n");
  55. id = av_packet_get_side_data(pkt, AV_PKT_DATA_WEBVTT_IDENTIFIER,
  56. &id_size);
  57. if (id && id_size > 0)
  58. avio_printf(pb, "%.*s\n", id_size, id);
  59. webvtt_write_time(pb, pkt->pts);
  60. avio_printf(pb, " --> ");
  61. webvtt_write_time(pb, pkt->pts + pkt->duration);
  62. settings = av_packet_get_side_data(pkt, AV_PKT_DATA_WEBVTT_SETTINGS,
  63. &settings_size);
  64. if (settings && settings_size > 0)
  65. avio_printf(pb, " %.*s", settings_size, settings);
  66. avio_printf(pb, "\n");
  67. avio_write(pb, pkt->data, pkt->size);
  68. avio_printf(pb, "\n");
  69. return 0;
  70. }
  71. AVOutputFormat ff_webvtt_muxer = {
  72. .name = "webvtt",
  73. .long_name = NULL_IF_CONFIG_SMALL("WebVTT subtitle"),
  74. .extensions = "vtt",
  75. .mime_type = "text/vtt",
  76. .flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT,
  77. .subtitle_codec = AV_CODEC_ID_WEBVTT,
  78. .write_header = webvtt_write_header,
  79. .write_packet = webvtt_write_packet,
  80. };