FFmpeg获取视频文件详细信息

2023-03-03 15:12:50 2817人已围观 36已点赞 9人已收藏

简介本文向大家介绍FFmpeg获取视频文件详细信息,感兴趣的朋友可以参考一下。

代码

// 显示文件信息
bool ShowFileInfos(const char* filePathIn)
{
	AVFormatContext* pFormatCtx;
	if (!(pFormatCtx = avformat_alloc_context())) {
		sprintf(m_chLastError, "CFFmpegHelper::ShowFileInfos() avformat_alloc_context() failed: %d", AVERROR(ENOMEM));
		return false;
	}
	int re = avformat_open_input(&pFormatCtx, filePathIn, 0, 0);
	if (re != 0) // 打开文件失败
	{
		avformat_close_input(&pFormatCtx);
		char errorbuf[1024] = { 0 };
		av_strerror(re, errorbuf, sizeof(errorbuf));
		sprintf(m_chLastError, "CFFmpegHelper::ShowFileInfos() open %s failed: %s", filePathIn, errorbuf);
		return false;
	}
	re = avformat_find_stream_info(pFormatCtx, NULL);
	if (re != 0)
	{
		avformat_close_input(&pFormatCtx);
		char errorbuf[1024] = { 0 };
		av_strerror(re, errorbuf, sizeof(errorbuf));
		sprintf(m_chLastError, "CFFmpegHelper::ShowFileInfos() avformat_find_stream_info() failed: %s", errorbuf);
		return false;
	}
	// 得到视频总时长的毫秒数并转换成hh:mm:ss形式
	int tns, thh, tmm, tss;
	tns = (pFormatCtx->duration) / AV_TIME_BASE;
	thh = tns / 3600;
	tmm = (tns % 3600) / 60;
	tss = (tns % 60);
	char timelong[100] = { 0 };
	snprintf(timelong, 100, "%02d:%02d:%02d", thh, tmm, tss);
	printf("================文件信息基本信息===============\n");
	printf("视频时长:%s\n", timelong);
	printf("开始时间:%d\n", pFormatCtx->start_time / AV_TIME_BASE);
	printf("比 特 率:%d kb/s \n", pFormatCtx->bit_rate / 1000);
	printf("轨道数目:%d\n", pFormatCtx->nb_streams);
	printf("==============================================\n");

	// 打印MetaData信息
	printf("---------------- File Information ---------------\n");
	av_dump_format(pFormatCtx, 0, filePathIn, 0);
	printf("---------------- File Information ---------------\n");

	avformat_close_input(&pFormatCtx);
	return true;
}

使用:

const char* filePathIn = "C:\\Users\\Administrator\\Desktop\\城市风光.mp4";
ShowFileInfos(filePathIn);

结果示例:

C++,FFmpeg,获取视频文件详细信息

更多为你推荐