FFmpeg
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
api-dump-stream-meta-test.c
Go to the documentation of this file.
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;
37  AVCodecParameters *origin_par = NULL;
38  AVStream *st;
39  int stream_idx = 0;
40  int result;
41  char *metadata;
42 
44  if (result < 0) {
45  av_log(NULL, AV_LOG_ERROR, "Can't open file\n");
46  return result;
47  }
48 
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");
71 
72  codec = avcodec_find_decoder(origin_par->codec_id);
73  if (!codec) {
74  av_log(NULL, AV_LOG_ERROR, "Can't find decoder\n");
76  goto end;
77  }
78 
79  ctx = avcodec_alloc_context3(codec);
80  if (!ctx) {
81  av_log(NULL, AV_LOG_ERROR, "Can't allocate decoder context\n");
82  result = AVERROR(ENOMEM);
83  goto end;
84  }
85 
87  if (result) {
88  av_log(NULL, AV_LOG_ERROR, "Can't copy decoder context\n");
89  goto end;
90  }
91 
92  result = avcodec_open2(ctx, codec, NULL);
93  if (result < 0) {
94  av_log(ctx, AV_LOG_ERROR, "Can't open decoder\n");
95  goto end;
96  }
97 
98  pkt = av_packet_alloc();
99  if (!pkt) {
100  av_log(NULL, AV_LOG_ERROR, "Cannot allocate packet\n");
101  result = AVERROR(ENOMEM);
102  goto end;
103  }
104 
105  fr = av_frame_alloc();
106  if (!fr) {
107  av_log(NULL, AV_LOG_ERROR, "Can't allocate frame\n");
108  result = AVERROR(ENOMEM);
109  goto end;
110  }
111 
112  for (;;) {
114  if (result)
115  goto end;
116 
117  if (pkt->stream_index != stream_idx) {
119  continue;
120  }
121 
122  printf("Stream ID: %d, packet PTS: %s, packet DTS: %s\n",
124 
126  result = av_dict_get_string(st->metadata, &metadata, '=', ':');
127  if (result < 0)
128  goto end;
129 
130  printf("Stream ID: %d, new metadata: %s\n", pkt->stream_index,
131  strlen(metadata) ? metadata : "N/A");
132  av_free(metadata);
133 
135  }
136 
139 
140  if (result < 0)
141  goto end;
142 
143  do {
145  if (result == AVERROR_EOF) {
146  result = 0;
147  goto end;
148  }
149 
150  if (result == AVERROR(EAGAIN))
151  break;
152 
153  if (result < 0)
154  goto end;
155 
156  result = av_dict_get_string(fr->metadata, &metadata, '=', ':');
157  if (result < 0)
158  goto end;
159 
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");
163  av_free(metadata);
164  } while (1);
165  }
166 
167 end:
169  av_frame_free(&fr);
172  return result;
173 }
174 
175 int main(int argc, char **argv) {
176  if (argc < 2) {
177  av_log(NULL, AV_LOG_ERROR, "Incorrect input\n");
178  return 1;
179  }
180 
181  if (dump_stream_meta(argv[1]) != AVERROR_EOF)
182  return 1;
183 
184  return 0;
185 }
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: packet.c:430
AVCodec
AVCodec.
Definition: codec.h:172
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
printf
__device__ int printf(const char *,...)
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:47
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
metadata
Stream codec metadata
Definition: ogg-flac-chained-meta.txt:2
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:63
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:410
AVFormatContext::streams
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1333
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:512
av_read_frame
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition: demux.c:1529
av_packet_free
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: packet.c:75
avformat_close_input
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition: demux.c:367
main
int main(int argc, char **argv)
Definition: api-dump-stream-meta-test.c:175
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:51
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
dump_stream_meta
static int dump_stream_meta(const char *input_filename)
Dump stream metadata.
Definition: api-dump-stream-meta-test.c:31
avformat_open_input
int avformat_open_input(AVFormatContext **ps, const char *url, const AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition: demux.c:217
avcodec_alloc_context3
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition: options.c:149
avcodec_receive_frame
int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Return decoded output data from a decoder or encoder (when the AV_CODEC_FLAG_RECON_FRAME flag is used...
Definition: avcodec.c:702
ctx
AVFormatContext * ctx
Definition: movenc.c:49
AVFormatContext
Format I/O context.
Definition: avformat.h:1265
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:768
avcodec_parameters_to_context
int avcodec_parameters_to_context(AVCodecContext *codec, const struct AVCodecParameters *par)
Fill the codec context based on the values from the supplied codec parameters.
result
and forward the result(frame or status change) to the corresponding input. If nothing is possible
NULL
#define NULL
Definition: coverity.c:32
avcodec_free_context
void avcodec_free_context(AVCodecContext **avctx)
Free the codec context and everything associated with it and write NULL to the provided pointer.
Definition: options.c:164
AVStream::metadata
AVDictionary * metadata
Definition: avformat.h:825
avcodec_open2
int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition: avcodec.c:143
AVSTREAM_EVENT_FLAG_METADATA_UPDATED
#define AVSTREAM_EVENT_FLAG_METADATA_UPDATED
Definition: avformat.h:863
avcodec_find_decoder
const AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
Definition: allcodecs.c:1018
AVFormatContext::nb_streams
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1321
avformat_find_stream_info
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
Read packets of a media file to get stream information.
Definition: demux.c:2507
fmt_ctx
static AVFormatContext * fmt_ctx
Definition: decode_filter_audio.c:46
AVStream::event_flags
int event_flags
Flags indicating events happening on the stream, a combination of AVSTREAM_EVENT_FLAG_*.
Definition: avformat.h:856
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:534
av_packet_alloc
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: packet.c:64
avcodec_get_name
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
Definition: utils.c:406
avcodec_send_packet
int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
Supply raw packet data as input to a decoder.
Definition: decode.c:704
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:528
avcodec.h
AVStream
Stream structure.
Definition: avformat.h:745
avformat.h
AVCodecContext
main external API structure.
Definition: avcodec.h:431
AVFrame::metadata
AVDictionary * metadata
metadata.
Definition: frame.h:688
AVPacket::stream_index
int stream_index
Definition: packet.h:537
input_filename
static const char * input_filename
Definition: ffplay.c:306
AVERROR_DECODER_NOT_FOUND
#define AVERROR_DECODER_NOT_FOUND
Decoder not found.
Definition: error.h:54
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:512
av_dict_get_string
int av_dict_get_string(const AVDictionary *m, char **buffer, const char key_val_sep, const char pairs_sep)
Get dictionary entries as a string.
Definition: dict.c:260
timestamp.h
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
av_ts2str
#define av_ts2str(ts)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: timestamp.h:54