count audio and video frames and show some statistics

This commit is contained in:
Josh Holtrop 2014-12-09 13:18:15 -05:00
parent 78a11fb3bf
commit 7de3460728
2 changed files with 43 additions and 2 deletions

View File

@ -1,7 +1,7 @@
TARGET := hello TARGET := hello
OBJS := hello.o OBJS := hello.o
CFLAGS := $$(pkg-config --cflags libavformat) -Wall CFLAGS := $$(pkg-config --cflags libavformat libavcodec) -Wall
LDFLAGS := $$(pkg-config --libs libavformat) LDFLAGS := $$(pkg-config --libs libavformat libavcodec)
all: $(TARGET) all: $(TARGET)

41
hello.c
View File

@ -25,6 +25,47 @@ int main(int argc, char * argv[])
av_dump_format(context, 0, argv[1], 0); 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); avformat_close_input(&context);
return 0; return 0;