ffmetaenc.c 2.7 KB

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