FFMPEG异常仅在调试模式下

FFMPEG exception in debug mode only

本文关键字:模式 调试 异常 FFMPEG      更新时间:2023-10-16

所以我试图在c++中使用ffmpeg。我的代码是这样的:

#include <iostream>
extern "C"
{
#include "libavcodecavcodec.h"
#include "libavformatavformat.h"
#include "libswscaleswscale.h"
}
using namespace std;
#pragma comment(lib, "dev/lib/avcodec.lib")
#pragma comment(lib, "dev/lib/avformat.lib")
#pragma comment(lib, "dev/lib/swscale.lib")
int main()
{
    avcodec_register_all();
    av_register_all();
    char inputFile[] = "video.mp4";
    AVFormatContext *pFormatCtx;
    if (avformat_open_input(&pFormatCtx, inputFile, NULL, 0) != 0) // exception occurs here
    {
        cout << "could not open file"; 
        return -1;
    }
}

这段代码在发布模式下运行,但在调试模式下,我在avformat_open_input处得到异常:

在0x0000000074BC3C35 (avformat-55.dll)中未处理的异常0xC0000005:访问冲突读取位置0 xffffffffffffffff。

我直接从ffmpeg的网站下载了dll和lib,并将它们包含在我的visual studio 2012的项目中。

提前感谢。

阅读文档。

int avformat_open_input ( AVFormatContext ** ps,
                          const char * filename,
                          AVInputFormat * fmt,
                          AVDictionary ** options 
                        )   
参数

ps:指向用户提供的AVFormatContext的指针(由avformat_alloc_context)。可能是指向NULL的指针,在这种情况下anAVFormatContext由该函数分配并写入ps。注意,用户提供的AVFormatContext将在失败时被释放。

您还没有初始化pFormatCtx,要么用avformat_alloc_context分配它,要么设置为nullptr,由avformat_open_input自动分配它。