From 7de3460728c06c5ee182bd20070dd58cfc801cbe Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Tue, 9 Dec 2014 13:18:15 -0500 Subject: [PATCH] count audio and video frames and show some statistics --- Makefile | 4 ++-- hello.c | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 7243bb4..a557540 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ TARGET := hello OBJS := hello.o -CFLAGS := $$(pkg-config --cflags libavformat) -Wall -LDFLAGS := $$(pkg-config --libs libavformat) +CFLAGS := $$(pkg-config --cflags libavformat libavcodec) -Wall +LDFLAGS := $$(pkg-config --libs libavformat libavcodec) all: $(TARGET) diff --git a/hello.c b/hello.c index bf57d04..6ec7f52 100644 --- a/hello.c +++ b/hello.c @@ -25,6 +25,47 @@ int main(int argc, char * argv[]) av_dump_format(context, 0, argv[1], 0); + int video_stream_idx = av_find_best_stream(context, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0); + printf("Video stream index: %d\n", video_stream_idx); + int audio_stream_idx = av_find_best_stream(context, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0); + printf("Audio stream index: %d\n", audio_stream_idx); + + AVPacket pkt; + av_init_packet(&pkt); + + int n_total_frames = 0; + int n_video_frames = 0; + int n_audio_frames = 0; + int max_video_size = 0; + int total_video_size = 0; + int max_audio_size = 0; + int total_audio_size = 0; + while (av_read_frame(context, &pkt) >= 0) + { + n_total_frames++; + if (pkt.stream_index == video_stream_idx) + { + n_video_frames++; + total_video_size += pkt.size; + if (pkt.size > max_video_size) + max_video_size = pkt.size; + } + if (pkt.stream_index == audio_stream_idx) + { + n_audio_frames++; + total_audio_size += pkt.size; + if (pkt.size > max_audio_size) + max_audio_size = pkt.size; + } + } + printf("Saw %d frames: %d video and %d audio (%d other)\n", + n_total_frames, n_video_frames, n_audio_frames, + n_total_frames - n_video_frames - n_audio_frames); + printf("Video size: %d, max: %d, average: %d\n", + total_video_size, max_video_size, total_video_size / n_video_frames); + printf("Audio size: %d, max: %d, average: %d\n", + total_audio_size, max_audio_size, total_audio_size / n_audio_frames); + avformat_close_input(&context); return 0;