【问题标题】:FFMpeg on iOS SwiftiOS Swift 上的 FFMpeg
【发布时间】:2016-05-11 04:20:19
【问题描述】:

我正在尝试通过本教程学习 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 库编译没有问题,所以我认为我编译或导入它的方式可能没有问题。我认为我可能传递了错误的论点:/有什么猜测吗??

【问题讨论】:

    标签: ios swift ffmpeg xcode7


    【解决方案1】:

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

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

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

    希望这会有所帮助。

    【讨论】:

    • 我认为这与我的解决方案类似(您使用的是可变的var 变量),而我使用的是let 常量。像您所做的那样将 &amp; 添加到变量应该会创建相同的内容。
    • 您好,能否请您给我您的库的 url,您用于让 FFMPEG 库为您的项目工作?
    • github.com/chrisballinger/FFmpeg-iOS这个我用过,不过你也可以用这个脚本自己编译:github.com/kewlbear/FFmpeg-iOS-build-script
    【解决方案2】:

    部分解决方案和背景说明可以在这里找到: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()

    【讨论】:

    • 感谢您的回答,我完全忘记了这一点。我尝试了这个解决方案,但它不起作用(在同一行上仍然存在相同的错误)。我稍后会在这里发布对我有用的解决方案。
    猜你喜欢
    • 2020-01-17
    • 2012-04-19
    • 2022-08-03
    • 1970-01-01
    • 2017-11-03
    • 1970-01-01
    • 2022-10-06
    • 2015-09-08
    • 1970-01-01
    相关资源
    最近更新 更多