您的位置:首页 > 其它

用FFmpeg SDK计算MP3文件的时长

2017-05-09 08:59 405 查看

说明

首先需要编译FFmpeg,这个网上已经有很多资料了,这里略过。可参见:VS编译FFmpeg

关于FFmpeg SDK的使用,可以参见:An ffmpeg and SDL Tutorial

计算MP3文件时长

主要利用
avformat_find_stream_info
读取文件信息,
AVFormatContext
中的成员变量
duration
用来描述MP3文件的时长。注意
duration
的值为
实际秒数*AV_TIME_BASE


extern "C"
{
#include "libavformat/avformat.h"
};

double duration(const char *filename)
{
av_register_all();

AVFormatContext *pFormatCtx = NULL;

// Open video file
if(avformat_open_input(&pFormatCtx, filename, NULL, NULL) != 0)
{
return -1; // Couldn't open file
}

avformat_find_stream_info(pFormatCtx, NULL);

double duration = (double)pFormatCtx->duration / AV_TIME_BASE;

avformat_close_input(&pFormatCtx);
avformat_free_context(pFormatCtx);

return duration;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: