2 * Copyright (c) 2025 Romain Beauxis
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * Dump stream metadata
27 #include "libavcodec/avcodec.h"
28 #include "libavformat/avformat.h"
29 #include "libavutil/timestamp.h"
31 static int dump_stream_meta(const char *input_filename
) {
32 const AVCodec
*codec
= NULL
;
35 AVFormatContext
*fmt_ctx
= NULL
;
36 AVCodecContext
*ctx
= NULL
;
37 AVCodecParameters
*origin_par
= NULL
;
43 result
= avformat_open_input(&fmt_ctx
, input_filename
, NULL
, NULL
);
45 av_log(NULL
, AV_LOG_ERROR
, "Can't open file\n");
49 result
= avformat_find_stream_info(fmt_ctx
, NULL
);
51 av_log(NULL
, AV_LOG_ERROR
, "Can't get stream info\n");
55 if (fmt_ctx
->nb_streams
> 1) {
56 av_log(NULL
, AV_LOG_ERROR
, "More than one stream found in input!\n");
60 origin_par
= fmt_ctx
->streams
[stream_idx
]->codecpar
;
61 st
= fmt_ctx
->streams
[stream_idx
];
63 result
= av_dict_get_string(st
->metadata
, &metadata
, '=', ':');
67 printf("Stream ID: %d, codec name: %s, metadata: %s\n", stream_idx
,
68 avcodec_get_name(origin_par
->codec_id
),
69 strlen(metadata
) ? metadata
: "N/A");
72 codec
= avcodec_find_decoder(origin_par
->codec_id
);
74 av_log(NULL
, AV_LOG_ERROR
, "Can't find decoder\n");
75 result
= AVERROR_DECODER_NOT_FOUND
;
79 ctx
= avcodec_alloc_context3(codec
);
81 av_log(NULL
, AV_LOG_ERROR
, "Can't allocate decoder context\n");
82 result
= AVERROR(ENOMEM
);
86 result
= avcodec_parameters_to_context(ctx
, origin_par
);
88 av_log(NULL
, AV_LOG_ERROR
, "Can't copy decoder context\n");
92 result
= avcodec_open2(ctx
, codec
, NULL
);
94 av_log(ctx
, AV_LOG_ERROR
, "Can't open decoder\n");
98 pkt
= av_packet_alloc();
100 av_log(NULL
, AV_LOG_ERROR
, "Cannot allocate packet\n");
101 result
= AVERROR(ENOMEM
);
105 fr
= av_frame_alloc();
107 av_log(NULL
, AV_LOG_ERROR
, "Can't allocate frame\n");
108 result
= AVERROR(ENOMEM
);
113 result
= av_read_frame(fmt_ctx
, pkt
);
117 if (pkt
->stream_index
!= stream_idx
) {
118 av_packet_unref(pkt
);
122 printf("Stream ID: %d, packet PTS: %s, packet DTS: %s\n",
123 pkt
->stream_index
, av_ts2str(pkt
->pts
), av_ts2str(pkt
->dts
));
125 if (st
->event_flags
& AVSTREAM_EVENT_FLAG_METADATA_UPDATED
) {
126 result
= av_dict_get_string(st
->metadata
, &metadata
, '=', ':');
130 printf("Stream ID: %d, new metadata: %s\n", pkt
->stream_index
,
131 strlen(metadata
) ? metadata
: "N/A");
134 st
->event_flags
&= ~AVSTREAM_EVENT_FLAG_METADATA_UPDATED
;
137 result
= avcodec_send_packet(ctx
, pkt
);
138 av_packet_unref(pkt
);
144 result
= avcodec_receive_frame(ctx
, fr
);
145 if (result
== AVERROR_EOF
) {
150 if (result
== AVERROR(EAGAIN
))
156 result
= av_dict_get_string(fr
->metadata
, &metadata
, '=', ':');
160 printf("Stream ID: %d, frame PTS: %s, metadata: %s\n",
161 pkt
->stream_index
, av_ts2str(fr
->pts
),
162 strlen(metadata
) ? metadata
: "N/A");
168 av_packet_free(&pkt
);
170 avformat_close_input(&fmt_ctx
);
171 avcodec_free_context(&ctx
);
175 int main(int argc
, char **argv
) {
177 av_log(NULL
, AV_LOG_ERROR
, "Incorrect input\n");
181 if (dump_stream_meta(argv
[1]) != AVERROR_EOF
)