一 、获取文件时长
ffmpeg在打开文件成功后就在 文件上下文结构体 AVFormatContext 中保存了文件的总时长。 但是ffmpeg中各种时间参数通常不是直接通过以秒或者毫秒为单位的,而是以多少个时间基来表示的,文件上下文中的时间基是秒和毫秒的换算单位,而音视频流中则是采样率,下面通过具体的代码来展示通过文件上下文,音视频流信息分别来获取总时长。
①通过文件参数获取时长
// AVFormatContext 中的 duration保存的时长是微妙,
AVFormatContext* m_filefmt_ctx;
int64_t total_second = m_filefmt_ctx->duration / AV_TIME_BASE;
//int64_t total_second = m_filefmt_ctx->duration * AV_TIME_BASE_Q;
cout<<"file hour:"<< total_second /3600<<endl;
cout <<"file min:" << (total_second %3600)/60 << endl;
cout << "file s:" << total_second % 60 << endl;
②通过音视频流信息获取时长
//视频流 time_base
int64_t = total_second = m_vdecode.m_stream->duration* av_q2d(m_vdecode.m_stream->time_base);
cout << "video hour:" << total_second / 3600 << endl;
cout << "video min:" << (total_second % 3600) / 60 << endl;
cout << "video s:" << total_second % 60 << endl;
//音频流 time_base是音频采样率
total_second = m_adecode.m_stream->duration * av_q2d(m_adecode.m_stream->time_base);
cout << "audio hour:" << total_second / 3600 << endl;
cout << "audio min:" << (total_second % 3600) / 60 << endl;
cout << "audio s:" << total_second % 60 << endl;