企业建站用什么主机,必应网站提交入口,郑州英文网站建设,重庆网站建设狐灵传媒文章目录 前言一、需求二、源码三、运行结果 前言
本文记录用 FFmpeg 获取视频流音频流的信息#xff08;编码格式、分辨率、帧率、播放时长…#xff09;#xff0c;所用的工程基于上个博客编译成功的工程#xff1a;使用FFmpeg4.3.1的SDK官方开发包编译ffmpeg.c 一、需求… 文章目录 前言一、需求二、源码三、运行结果 前言
本文记录用 FFmpeg 获取视频流音频流的信息编码格式、分辨率、帧率、播放时长…所用的工程基于上个博客编译成功的工程使用FFmpeg4.3.1的SDK官方开发包编译ffmpeg.c 一、需求
我们经常需要知道一个媒体文件所包含的媒体流的信息比如文件格式、播放时长、码率、视音频编码格式视频分辨率帧率音频属性等信息。
如何使用 FFmpeg API 获取这些信息呢
媒体容器封装格式文件播放时长文件平均码率视频音频视频属性编码器名称、视频分辨率、帧率、编码码率音频属性编码器名称、采样率、声道数、编码码率
二、源码
ffmepg.h 文件中添加我们自定义的结构体我们后面会利用 ffmepg 的 API 函数将音视频流信息填充到各个字段
typedef struct __AVGeneralMediaInfo {char filepath[1024]; // 文件路径int64_t duration; // 时长单位微秒 time_base:1,000,000int64_t totalBitrate; // 总码率int videoStreamIndex; // 视频流索引int audioStreamIndex; // 音频流索引char videoCodecName[256]; int width; // 视频宽int height; // 视频高double frameRate; // 视频帧率char audioCodecName[256];int sampleRate; // 采样率int channels; // 声道数
} AVGeneralMediaInfo;void get_avgeneral_mediainfo(AVGeneralMediaInfo* avmi, const char* filepath);ffmepg.c 文件中添加获取音视频流的基本信息的接口
// 封装查找解码器
// type:[0:video, 1:audio]
void get_decoder_name(AVGeneralMediaInfo *avmi, AVFormatContext *avFmtctx, int type)
{int nindex -1;if (type 0) { // videonindex avmi-videoStreamIndex;}else if (type 1) { // aduionindex avmi-audioStreamIndex;}if (nindex 0) {AVCodecContext* avcodecCtx NULL;AVCodec *avcodec NULL;avcodecCtx avFmtctx-streams[nindex]-codec;avcodec avcodec_find_decoder(avcodecCtx-codec_id);if (type 0) { // videostrcpy(avmi-videoCodecName, avcodec-name);printf(videoCodecName %s\n, avmi-videoCodecName);}else if (type 1) {strcpy(avmi-audioCodecName, avcodec-long_name);printf(audioCodecName %s\n, avmi-audioCodecName);}}
}// 获取音视频流的基本信息
void get_avgeneral_mediainfo(AVGeneralMediaInfo *avmi, const char *filepath)
{int ret -1;int i 0;AVFormatContext* avFmtCtx NULL; // 大管家if (avmi NULL || filepath NULL) {return;}// 1.打开音视频文件或网络流ret avformat_open_input(avFmtCtx, filepath, NULL, NULL);if (ret 0) {printf(error avformat_open_input:%s\n, filepath);return;}// 2.打印音视频流信息av_dump_format(avFmtCtx, 0, filepath, 0);// 3.继续深入读取更多的字段avmi-duration avFmtCtx-duration; // 时长avmi-totalBitrate avFmtCtx-bit_rate; // 总码率printf(duration %lld, totalBitrate %lld\n, avmi-duration, avmi-totalBitrate);// 分别读取音视频流更多的参数for (i 0; i avFmtCtx-nb_streams; i) {AVStream* avstmp avFmtCtx-streams[i]; // 拿到具体的一路流if (avstmp-codec-codec_type AVMEDIA_TYPE_VIDEO) {avmi-videoStreamIndex i;avmi-width avstmp-codec-width;avmi-height avstmp-codec-height;// 视频帧率avg_frame_rate// fps:frames per secondif (avstmp-avg_frame_rate.num ! 0 avstmp-avg_frame_rate.den ! 0) {avmi-frameRate (double)avstmp-avg_frame_rate.num / (double)avstmp-avg_frame_rate.den;}printf(width %d, height %d, frameRate %.3lf\n, avmi-width,avmi-height,avmi-frameRate);}else if (avstmp-codec-codec_type AVMEDIA_TYPE_AUDIO) {avmi-audioStreamIndex i;avmi-channels avstmp-codec-channels;avmi-sampleRate avstmp-codec-sample_rate;printf(channel %d, sampleRate %d\n, avmi-channels, avmi-sampleRate);}}// 读取具体的解码器// avcodec_find_decoder()// 视频解码器get_decoder_name(avmi, avFmtCtx, 0);// 音频解码器get_decoder_name(avmi, avFmtCtx, 1);// releaseavformat_close_input(avFmtCtx);
}ffmpeg431_test.cpp 文件内容如下
#include iostream
extern C
{
#include ffmpeg.h
}int main(int argc, char** argv)
{AVGeneralMediaInfo* avmi new AVGeneralMediaInfo();if (avmi) {get_avgeneral_mediainfo(avmi, SampleVideo_1280x720_20mb.mp4);delete avmi;avmi NULL;}
}三、运行结果
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from SampleVideo_1280x720_20mb.mp4:Metadata:major_brand : isomminor_version : 512compatible_brands: isomiso2avc1mp41creation_time : 1970-01-01T00:00:00.000000Zencoder : Lavf53.24.2Duration: 00:01:57.31, bitrate: N/AStream #0:0(und): Video: h264 (avc1 / 0x31637661), none, 1280x720, 1048 kb/s, 25 fps, 25 tbr, 12800 tbn (default)Metadata:creation_time : 1970-01-01T00:00:00.000000Zhandler_name : VideoHandlerStream #0:1(und): Audio: aac (mp4a / 0x6134706D), 48000 Hz, 6 channels, 383 kb/s (default)Metadata:creation_time : 1970-01-01T00:00:00.000000Zhandler_name : SoundHandler
duration 117312000, totalBitrate 0
width 1280, height 720, frameRate 25.000
channel 6, sampleRate 48000
videoCodecName h264
audioCodecName AAC (Advanced Audio Coding)使用 MediaInfo 打开 SampleVideo_1280x720_20mb.mp4 可以看到与上面打印对应的参数 我的qq2442391036欢迎交流