=====================================================
相关代码可以参考我的:
=====================================================
一般视频的信息主要包括以下信息,而这些信息绝大部分都可以再 AVFormatContext 中获得
播放时长duration
比特率 bit_rate
帧率fps
视频帧数nb_frame
分辨率
视频编码器名称
1. 通过 avformat_open_input 打开流
2. 再调用 avformat_find_stream_info 找到流的信息
AVFormatContext *pFormatCtx;
char filepath[]="D:\\test.mp4";
if(avformat_open_input(&pFormatCtx,filepath,NULL, NULL)!=0){
printf("Couldn't open input stream.\n");
return -1;
}
if(avformat_find_stream_info(pFormatCtx,NULL)<0){
printf("Couldn't find stream information.\n");
return -1;
}
到这里我们已经可以从 AVFormatContext 中读取 播放时长duration 和 比特率 bit_rate了
fprintf(stderr, "duration = %.2fs\n", (double)pFormatCtx->duration / 1000000);
fprintf(stderr, "bitrate = %.2fkb/s\n", (double)pFormatCtx->bit_rate / 1000);
打印结果为:
duration = 245.48s
bitrate = 781.84kb/s
3.想要获取其他信息,需要从AVFormatContext 找到视频流信息
int videoindex=-1;
for(i=0; i<pFormatCtx->nb_streams; i++) {
if(pFormatCtx->streams[i]->codecpar->codec_type==AVMEDIA_TYPE_VIDEO){
videoindex=i;
break;
}
}
if(videoindex==-1){
printf("Didn't find a video stream.\n");
return -1;
}
找到之后我们就能获取 帧率fps 、视频帧数nb_frame 和分辨率了
AVStream * pVStream = pFormatCtx->streams[videoindex];
fprintf(stderr, "nb_frames = %lld\n", pVStream->nb_frames);
fprintf(stderr, "fps = %.2f\n", (double)pVStream->nb_frames / pFormatCtx->duration * 1000000);
fprintf(stderr, "fps = %.2f\n", (double)pVStream->avg_frame_rate.num / pVStream->avg_frame_rate.den);
fprintf(stderr, "width = %d, height = %d\n", pVStream->codecpar->width, pVStream->codecpar->height);
打印结果:
nb_frames = 5883
fps = 23.96 (通过总帧数 / 时长 计算出的fps)
fps = 23.98(直接读取出来的fps)
width = 768, height = 432
4.获取编码名称,需要先通过 avcodec_find_decoder 找到解码器
AVCodecParameters *pCodecParam=pFormatCtx->streams[videoindex]->codecpar;
AVCodec * codec = avcodec_find_decoder(pCodecParam->codec_id);
if (!codec) {
fprintf(stderr, "Codec not found\n");
exit(1);
}
fprintf(stderr, "codecName = %s\n", codec->long_name);
打印结果:
codecName = H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10
5.最后,其实可以使用 av_dump_format 直接打印出相关信息
fprintf(stderr, "------------- File Information ------------------\n");
av_dump_format(pFormatCtx,0,filepath,0);
fprintf(stderr, "-------------------------------------------------\n");
打印结果:
------------- File Information ------------------
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'D:\output.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
artist : yinyuetai.com
album : Yinyuetai
date : 09/29/19 20:39:28
encoder : Lavf58.12.100
comment : Yinyuetai-1TR1166
Duration: 00:04:05.48, start: 0.000000, bitrate: 781 kb/s
Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p, 768x432, 681 kb/s, 23.98 fps, 23.98 tbr, 24k tbn,
47.95 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 95 kb/s (default)
Metadata:
handler_name : SoundHandler
-------------------------------------------------
参考:
https://blog.csdn.net/leixiaohua1020/article/details/8652605
https://blog.csdn.net/qq_41824928/article/details/103611366
https://blog.csdn.net/zhoubotong2012/article/details/79340722