ffmetaenc.c 2.8 KB

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