我正在尝试使用ffmpeg从视频文件中捕获帧,但我甚至无法获得视频的持续时间。每次我尝试使用
我不明白为什么
以下是我的代码:
pFormatCtx->duration
访问它时,都会得到0。我知道指针已初始化并包含正确的持续时间,因为如果我使用av_dump_format(pFormatCtx, 0, videoName, 0);
,那么我实际上会得到视频的持续时间数据以及其他信息。当我使用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
我不明白为什么
av_dump_format
可以显示持续时间,而我不能。我检查了函数定义,为了显示持续时间,该函数也使用了pFormatCtx->duration
。当我在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;
}
我不知道这是否有帮助,但我在Ubuntu上使用QtCreator,并静态链接了库。
av_dump_format
在读取pFormatCtv->duration
之前进行了某些操作,使该字段有效。换句话说,在持续时间变为有效之前必须执行其他代码。追踪一些可行的代码,你应该能找到缺少的部分。顺便问一下,您对此问题还感兴趣吗? - Randall Cook