3 * Copyright (c) 2010 Anton Khirnov
5 * This file is part of FFmpeg.
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.
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.
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
26 static void write_escape_str(ByteIOContext *s, const uint8_t *str)
28 const uint8_t *p = str;
31 if (*p == '#' || *p == ';' || *p == '=' || *p == '\\' || *p == '\n')
38 static void write_tags(ByteIOContext *s, AVMetadata *m)
40 AVMetadataTag *t = NULL;
41 while ((t = av_metadata_get(m, "", t, AV_METADATA_IGNORE_SUFFIX))) {
42 write_escape_str(s, t->key);
44 write_escape_str(s, t->value);
49 static int write_header(AVFormatContext *s)
51 put_tag(s->pb, ID_STRING);
52 put_byte(s->pb, '1'); // version
53 put_byte(s->pb, '\n');
54 put_flush_packet(s->pb);
58 static int write_trailer(AVFormatContext *s)
62 write_tags(s->pb, s->metadata);
64 for (i = 0; i < s->nb_streams; i++) {
65 put_tag(s->pb, ID_STREAM);
66 put_byte(s->pb, '\n');
67 write_tags(s->pb, s->streams[i]->metadata);
70 for (i = 0; i < s->nb_chapters; i++) {
71 AVChapter *ch = s->chapters[i];
72 put_tag(s->pb, ID_CHAPTER);
73 put_byte(s->pb, '\n');
74 url_fprintf(s->pb, "TIMEBASE=%d/%d\n", ch->time_base.num, ch->time_base.den);
75 url_fprintf(s->pb, "START=%"PRId64"\n", ch->start);
76 url_fprintf(s->pb, "END=%"PRId64"\n", ch->end);
77 write_tags(s->pb, ch->metadata);
80 put_flush_packet(s->pb);
85 static int write_packet(AVFormatContext *s, AVPacket *pkt)
90 AVOutputFormat ffmetadata_muxer = {
92 .long_name = NULL_IF_CONFIG_SMALL("FFmpeg metadata in text format"),
93 .extensions = "ffmeta",
94 .write_header = write_header,
95 .write_packet = write_packet,
96 .write_trailer = write_trailer,
97 .flags = AVFMT_NOTIMESTAMPS | AVFMT_NOSTREAMS,