FFMPEG无法显示视频的持续时间 [英] FFMPEG Can't Display The Duration Of a Video
问题描述
我试图使用ffmpeg从视频文件捕获帧,但我甚至不能获得视频的持续时间。每次当我尝试访问它与 pFormatCtx-> duration
我得到0.我知道指针初始化和包含正确的持续时间,因为如果我使用 av_dump_format(pFormatCtx,0,videoName,0);
然后,我实际上获取了有关视频的其他信息的持续时间数据。
这是我使用 av_dump_format(pFormatCtx,0,videoName,0);
时得到的:
< blockquote>
输入#0,avi,来自futurama.avi:
时间:00:21:36.28, :1135 kb / s
流#0.0:视频:mpeg4(高级简单配置文件),yuv420p,512x384
[PAR 1:1 DAR 4:3],25 tbr,25 tbn,25 tbc
流#0.1:音频:ac3,48000Hz,立体声, 192 kb / s
我不明白为什么 av_dum_format
可以显示持续时间我不能。我检查了函数定义,显示持续时间,该函数也使用pFormatCtx-> duration。它不只是持续时间其他成员变量也不显示正确的数据,当我在main.cpp
这是我的main.cpp:
externC{
#include< libavcodec / avcodec.h>
#include< libavformat / avformat.h>
#include< libswscale / swscale.h>
}
int main(int argc,char * argv []){
AVFormatContext * pFormatCtx = NULL;
const char videoName [] =futurama.avi;
//注册所有格式和编解码器。
av_register_all();
cout<< 打开视频文件;
//打开视频文件
int ret = avformat_open_input(& pFormatCtx,videoName,NULL,NULL)!= 0;
if(ret!= 0){
cout< 无法打开视频文件。 << ret;
return -1;
}
if(avformat_find_stream_info(pFormatCtx,0)< 0){
cout< 流信息问题;
return -1;
}
av_dump_format(pFormatCtx,0,videoName,0);
cout<< pFormatCtx-> bit_rate<< endl //每次的值不同,没有正确初始化。
cout<< pFormatCtx-> duration<< endl // 0
return 0;
}
我不知道它是否有帮助,但我在Ubuntu上使用QtCreator链接库静态。
感谢您的帮助。
duration属性在 time_base
单位不是毫秒或秒。
double time_base =(double)video_stream-> time_base.num /(double)video_stream - > time_base.den;
double duration =(double)video_stream-> duration * time_base * 1000.0;
持续时间现在以毫秒为单位,只需取下或ceil获得整数msec,无论您喜欢什么。
I'm trying to use ffmpeg to capture frames from a video file, but I can't even get the duration of a video. everytime when I try to access it with pFormatCtx->duration
I'm getting 0. I know the pointer initialized and contains the correct duration because if I use av_dump_format(pFormatCtx, 0, videoName, 0);
then I actually get the duration data along with other information about the video.
This is what I get when I use av_dump_format(pFormatCtx, 0, videoName, 0);
:
Input #0, avi, from 'futurama.avi':
Duration: 00:21:36.28, start: 0.000000, bitrate: 1135 kb/s
Stream #0.0: Video: mpeg4 (Advanced Simple Profile), yuv420p, 512x384
[PAR 1:1 DAR 4:3], 25 tbr, 25 tbn, 25 tbc
Stream #0.1: Audio: ac3, 48000 Hz, stereo, s16, 192 kb/s
I don't understand why av_dum_format
can display duration and I can't. I checked the function definition, to display the duration, the function also uses pFormatCtx->duration. It's not just the duration other member variables also don't display the proper data when I call them in main.cpp
Here's my main.cpp:
extern "C" {
#include<libavcodec/avcodec.h>
#include<libavformat/avformat.h>
#include<libswscale/swscale.h>
}
int main(int argc, char *argv[]) {
AVFormatContext *pFormatCtx = NULL;
const char videoName[] = "futurama.avi";
// Register all formats and codecs.
av_register_all();
cout << "Opening the video file";
// Open video file
int ret = avformat_open_input(&pFormatCtx, videoName, NULL, NULL) != 0;
if (ret != 0) {
cout << "Couldn't open the video file." << ret ;
return -1;
}
if(avformat_find_stream_info(pFormatCtx, 0) < 0) {
cout << "problem with stream info";
return -1;
}
av_dump_format(pFormatCtx, 0, videoName, 0);
cout << pFormatCtx->bit_rate << endl; // different value each time, not initialized properly.
cout << pFormatCtx->duration << endl; // 0
return 0;
}
I don't know if it helps but, I use QtCreator on Ubuntu and linked the libraries statically.
Thank you for your help.
The duration property is in time_base
units not milliseconds or seconds. The conversion to milliseconds is pretty easy,
double time_base = (double)video_stream->time_base.num / (double)video_stream->time_base.den;
double duration = (double)video_stream->duration * time_base * 1000.0;
The duration is now in msec, just take the floor or ceil to get a whole number of msec, whichever you like.
这篇关于FFMPEG无法显示视频的持续时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!