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.

99 lines
2.7KB

  1. /*
  2. * Metadata muxer
  3. * Copyright (c) 2010 Anton Khirnov
  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 "meta.h"
  23. static void write_escape_str(ByteIOContext *s, const uint8_t *str)
  24. {
  25. const uint8_t *p = str;
  26. while (*p) {
  27. if (*p == '#' || *p == ';' || *p == '=' || *p == '\\' || *p == '\n')
  28. put_byte(s, '\\');
  29. put_byte(s, *p);
  30. p++;
  31. }
  32. }
  33. static void write_tags(ByteIOContext *s, AVMetadata *m)
  34. {
  35. AVMetadataTag *t = NULL;
  36. while ((t = av_metadata_get(m, "", t, AV_METADATA_IGNORE_SUFFIX))) {
  37. write_escape_str(s, t->key);
  38. put_byte(s, '=');
  39. write_escape_str(s, t->value);
  40. put_byte(s, '\n');
  41. }
  42. }
  43. static int write_header(AVFormatContext *s)
  44. {
  45. put_tag(s->pb, ID_STRING);
  46. put_byte(s->pb, '1'); // version
  47. put_byte(s->pb, '\n');
  48. put_flush_packet(s->pb);
  49. return 0;
  50. }
  51. static int write_trailer(AVFormatContext *s)
  52. {
  53. int i;
  54. write_tags(s->pb, s->metadata);
  55. for (i = 0; i < s->nb_streams; i++) {
  56. put_tag(s->pb, ID_STREAM);
  57. put_byte(s->pb, '\n');
  58. write_tags(s->pb, s->streams[i]->metadata);
  59. }
  60. for (i = 0; i < s->nb_chapters; i++) {
  61. AVChapter *ch = s->chapters[i];
  62. put_tag(s->pb, ID_CHAPTER);
  63. put_byte(s->pb, '\n');
  64. url_fprintf(s->pb, "TIMEBASE=%d/%d\n", ch->time_base.num, ch->time_base.den);
  65. url_fprintf(s->pb, "START=%lld\n", ch->start);
  66. url_fprintf(s->pb, "END=%lld\n", ch->end);
  67. write_tags(s->pb, ch->metadata);
  68. }
  69. put_flush_packet(s->pb);
  70. return 0;
  71. }
  72. static int write_packet(AVFormatContext *s, AVPacket *pkt)
  73. {
  74. return 0;
  75. }
  76. AVOutputFormat ffmetadata_muxer = {
  77. .name = "ffmetadata",
  78. .long_name = NULL_IF_CONFIG_SMALL("FFmpeg metadata in text format"),
  79. .extensions = "ffmeta",
  80. .write_header = write_header,
  81. .write_packet = write_packet,
  82. .write_trailer = write_trailer,
  83. .flags = AVFMT_NOTIMESTAMPS | AVFMT_NOSTREAMS,
  84. };