代码拉取完成,页面将自动刷新
#include <iostream>
extern "C" {
#include <libavformat/avformat.h>
}
using namespace std;
/*
* 编解码器(CODEC):能够进行视频和音频压缩(CO)与解压缩(DEC),是视频编解码的核心部分。
* 容器/多媒体文件(Container/File)
*常下载的电影的文件的后缀(avi,mkv,rmvb等)是其封装的方式。
* 一个视频文件通常有视频数据、音频数据以及字幕等,封装的格式决定这些数据在文件中是如何的存放的,
* 封装在一起音频、视频等数据组成的多媒体文件,也可以叫做容器(其中包含了视音频数据)。所以,只看多媒体文件的后缀名是难以知道视音频的编码方式的。
*
*流数据 Stream,例如视频流(Video Stream),音频流(Audio Stream)。流中的数据元素被称为帧Frame。
* */
int main() {
// 测试路径文件
const char *path = "../test_file/test_video.mp4";
AVFormatContext *avc = avformat_alloc_context();
AVDictionary *opts = nullptr;
// av_log_set_level(AV_LOG_DEBUG);
int ret = avformat_open_input(&avc, path, nullptr, &opts);
if (ret != 0) {
char buf[1024] = {0};
av_strerror(ret, buf, sizeof(buf) - 1);
printf("open %s failed!:%s", path, buf);
exit(ret);
}
cout << "文件打开成功" << endl;
// 文件流数据
ret = avformat_find_stream_info(avc, nullptr);
if (ret != 0) {
cout << "获取流数据信息错误" << endl;
exit(ret);
}
cout << "url: " << avc->url << endl;
cout << "stream个数:" << avc->nb_streams << endl;
cout << "视频时长(微妙):" << avc->duration << endl;
auto sec = avc->duration / AV_TIME_BASE;
cout << "总时长: " << sec / 3600 << "时" << sec % 3600 / 60 << "分" << sec % 60 << "秒" << endl;
//stream
for (int i = 0; i < avc->nb_streams; ++i) {
AVStream *pStream = avc->streams[i];
if (AVMEDIA_TYPE_AUDIO == pStream->codecpar->codec_type) {
auto s = pStream->time_base.den == 0 ? 0 : (double) pStream->time_base.num /(double) pStream->time_base.den;
cout << "音频信息:" << endl;
cout << "index: " << pStream->index << endl
<< "采样率: " << pStream->codecpar->sample_rate << " Hz" << endl
<< "采样格式: " << pStream->codecpar->format << endl
<< "音频信道数目:" << pStream->codecpar->channels << endl
<< "音频压缩编码格式:" << pStream->codecpar->codec_id << endl
<< "音频时长: " << (double)pStream->duration * s<<"秒";
}
}
// 释放资源
avformat_close_input(&avc);
return 0;
}
int main1() {
unsigned int version = avformat_version();
cout << "version:" << version << endl;
const char *config = avcodec_configuration();
cout << "config: " << config << endl;
cout << avcodec_license() << endl;
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。