|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Romain Beauxis |
| 3 | + * |
| 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: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 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 |
| 20 | + * THE SOFTWARE. |
| 21 | + */ |
| 22 | + |
| 23 | +/** |
| 24 | + * Dump stream metadata |
| 25 | + */ |
| 26 | + |
| 27 | +#include "libavcodec/avcodec.h" |
| 28 | +#include "libavformat/avformat.h" |
| 29 | +#include "libavutil/timestamp.h" |
| 30 | + |
| 31 | +static int dump_stream_meta(const char *input_filename) { |
| 32 | + const AVCodec *codec = NULL; |
| 33 | + AVPacket *pkt = NULL; |
| 34 | + AVFrame *fr = NULL; |
| 35 | + AVFormatContext *fmt_ctx = NULL; |
| 36 | + AVCodecContext *ctx = NULL; |
| 37 | + AVCodecParameters *origin_par = NULL; |
| 38 | + AVStream *st; |
| 39 | + int stream_idx = 0; |
| 40 | + int result; |
| 41 | + char *metadata; |
| 42 | + |
| 43 | + result = avformat_open_input(&fmt_ctx, input_filename, NULL, NULL); |
| 44 | + if (result < 0) { |
| 45 | + av_log(NULL, AV_LOG_ERROR, "Can't open file\n"); |
| 46 | + return result; |
| 47 | + } |
| 48 | + |
| 49 | + result = avformat_find_stream_info(fmt_ctx, NULL); |
| 50 | + if (result < 0) { |
| 51 | + av_log(NULL, AV_LOG_ERROR, "Can't get stream info\n"); |
| 52 | + goto end; |
| 53 | + } |
| 54 | + |
| 55 | + if (fmt_ctx->nb_streams > 1) { |
| 56 | + av_log(NULL, AV_LOG_ERROR, "More than one stream found in input!\n"); |
| 57 | + goto end; |
| 58 | + } |
| 59 | + |
| 60 | + origin_par = fmt_ctx->streams[stream_idx]->codecpar; |
| 61 | + st = fmt_ctx->streams[stream_idx]; |
| 62 | + |
| 63 | + result = av_dict_get_string(st->metadata, &metadata, '=', ':'); |
| 64 | + if (result < 0) |
| 65 | + goto end; |
| 66 | + |
| 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"); |
| 70 | + |
| 71 | + codec = avcodec_find_decoder(origin_par->codec_id); |
| 72 | + if (!codec) { |
| 73 | + av_log(NULL, AV_LOG_ERROR, "Can't find decoder\n"); |
| 74 | + result = AVERROR_DECODER_NOT_FOUND; |
| 75 | + goto end; |
| 76 | + } |
| 77 | + |
| 78 | + ctx = avcodec_alloc_context3(codec); |
| 79 | + if (!ctx) { |
| 80 | + av_log(NULL, AV_LOG_ERROR, "Can't allocate decoder context\n"); |
| 81 | + result = AVERROR(ENOMEM); |
| 82 | + goto end; |
| 83 | + } |
| 84 | + |
| 85 | + result = avcodec_parameters_to_context(ctx, origin_par); |
| 86 | + if (result) { |
| 87 | + av_log(NULL, AV_LOG_ERROR, "Can't copy decoder context\n"); |
| 88 | + goto end; |
| 89 | + } |
| 90 | + |
| 91 | + result = avcodec_open2(ctx, codec, NULL); |
| 92 | + if (result < 0) { |
| 93 | + av_log(ctx, AV_LOG_ERROR, "Can't open decoder\n"); |
| 94 | + goto end; |
| 95 | + } |
| 96 | + |
| 97 | + pkt = av_packet_alloc(); |
| 98 | + if (!pkt) { |
| 99 | + av_log(NULL, AV_LOG_ERROR, "Cannot allocate packet\n"); |
| 100 | + result = AVERROR(ENOMEM); |
| 101 | + goto end; |
| 102 | + } |
| 103 | + |
| 104 | + fr = av_frame_alloc(); |
| 105 | + if (!fr) { |
| 106 | + av_log(NULL, AV_LOG_ERROR, "Can't allocate frame\n"); |
| 107 | + result = AVERROR(ENOMEM); |
| 108 | + goto end; |
| 109 | + } |
| 110 | + |
| 111 | + for (;;) { |
| 112 | + result = av_read_frame(fmt_ctx, pkt); |
| 113 | + if (result) |
| 114 | + goto end; |
| 115 | + |
| 116 | + if (pkt->stream_index != stream_idx) { |
| 117 | + av_packet_unref(pkt); |
| 118 | + continue; |
| 119 | + } |
| 120 | + |
| 121 | + printf("Stream ID: %d, packet PTS: %s, packet DTS: %s\n", |
| 122 | + pkt->stream_index, av_ts2str(pkt->pts), av_ts2str(pkt->dts)); |
| 123 | + |
| 124 | + if (st->event_flags & AVSTREAM_EVENT_FLAG_METADATA_UPDATED) { |
| 125 | + result = av_dict_get_string(st->metadata, &metadata, '=', ':'); |
| 126 | + if (result < 0) |
| 127 | + goto end; |
| 128 | + |
| 129 | + printf("Stream ID: %d, new metadata: %s\n", pkt->stream_index, |
| 130 | + strlen(metadata) ? metadata : "N/A"); |
| 131 | + |
| 132 | + st->event_flags &= ~AVSTREAM_EVENT_FLAG_METADATA_UPDATED; |
| 133 | + } |
| 134 | + |
| 135 | + result = avcodec_send_packet(ctx, pkt); |
| 136 | + av_packet_unref(pkt); |
| 137 | + |
| 138 | + if (result < 0) |
| 139 | + goto end; |
| 140 | + |
| 141 | + do { |
| 142 | + result = avcodec_receive_frame(ctx, fr); |
| 143 | + if (result == AVERROR_EOF) { |
| 144 | + result = 0; |
| 145 | + goto end; |
| 146 | + } |
| 147 | + |
| 148 | + if (result == AVERROR(EAGAIN)) |
| 149 | + break; |
| 150 | + |
| 151 | + if (result < 0) |
| 152 | + goto end; |
| 153 | + |
| 154 | + result = av_dict_get_string(fr->metadata, &metadata, '=', ':'); |
| 155 | + if (result < 0) |
| 156 | + goto end; |
| 157 | + |
| 158 | + printf("Stream ID: %d, frame PTS: %s, metadata: %s\n", |
| 159 | + pkt->stream_index, av_ts2str(fr->pts), |
| 160 | + strlen(metadata) ? metadata : "N/A"); |
| 161 | + } while (1); |
| 162 | + } |
| 163 | + |
| 164 | +end: |
| 165 | + av_packet_free(&pkt); |
| 166 | + av_frame_free(&fr); |
| 167 | + avformat_close_input(&fmt_ctx); |
| 168 | + avcodec_free_context(&ctx); |
| 169 | + return result; |
| 170 | +} |
| 171 | + |
| 172 | +int main(int argc, char **argv) { |
| 173 | + if (argc < 2) { |
| 174 | + av_log(NULL, AV_LOG_ERROR, "Incorrect input\n"); |
| 175 | + return 1; |
| 176 | + } |
| 177 | + |
| 178 | + if (dump_stream_meta(argv[1]) != AVERROR_EOF) |
| 179 | + return 1; |
| 180 | + |
| 181 | + return 0; |
| 182 | +} |
0 commit comments