iOS Swift 上的 FFMpeg

FFMpeg on iOS Swift

我正在尝试通过本教程学习 FFMpeg:http://dranger.com/ffmpeg/tutorial01.html

我希望只要将 C 代码翻译成 swift 就能让我振作起来 运行 但我想我错了

我尝试转换以下代码:

AVFormatContext *pFormatCtx = NULL;
// Open video file
if(avformat_open_input(&pFormatCtx, argv[1], NULL, 0, NULL)!=0) {}

至:

let pFormatCtx : UnsafeMutablePointer<UnsafeMutablePointer<AVFormatContext>> = nil
// Open video file
if avformat_open_input(pFormatCtx, path, nil, opaque) != 0 {}

此代码中断于:if avformat_open_input(pFormatCtx, path, nil, opaque) != 0 {} 带有 EXC_BAD_ACCESS错误

谁能猜出这是怎么回事??

顺便说一下,我的 FFMpeg 库编译没有问题,所以我认为我编译或导入它的方式可能没有问题。我认为我可能传递了错误的论点:/任何猜测??

部分解决方案和背景说明可以在这里找到:http://en.swifter.tips/pointer-memory/

基本上,UnsafeMutablePointer 在使用前必须 allocated

要使上面的代码正常工作,试试这个:

let path = ...

let formatContext = UnsafeMutablePointer<UnsafeMutablePointer<AVFormatContext>>.alloc(1)

if (avformat_open_input(formatContext, path, nil, nil) != 0) {
    // TODO: Error handling
}

完成后,不要忘记调用 formatContext.destroy()

首先我使用 Swift 2 和 xCode 7.2 ...

解决方案是将格式上下文创建为 "UnsafeMutablePointer< AVFormatContext >",然后通过 avformat_open_input[=19 传递其地址=] 方法。这是对我有用的代码:

var formatContext = UnsafeMutablePointer<AVFormatContext>()

if avformat_open_input(&formatContext, path, nil, nil) != 0 {
    print("Couldn't open file")
    return
}

希望对您有所帮助。