Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FFMpeg on iOS Swift

I'm trying to learn FFMpeg through this tutorial: http://dranger.com/ffmpeg/tutorial01.html

I was hoping that just translating the C code to swift should get me up and running but I guess I was mistaken

I tried converting the following code:

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

to:

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

This code breaks at: if avformat_open_input(pFormatCtx, path, nil, opaque) != 0 {} With an EXC_BAD_ACCESS error

can anyone guess whats wrong here??

By the way I have the FFMpeg library compiling without an issue so I don't think there might be an issue with the way I compiled or imported it. I'm probably passing wrong arguments I think :/ Any guesses??

like image 516
Umer Hassam Avatar asked Sep 26 '22 12:09

Umer Hassam


1 Answers

First off I'm using Swift 2 with xCode 7.2 ...

The solution was to create the format Context as an "UnsafeMutablePointer< AVFormatContext >" and then pass its address through the avformat_open_input method. Here's the code that worked for me:

var formatContext = UnsafeMutablePointer<AVFormatContext>()

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

Hope this helps.

like image 197
Umer Hassam Avatar answered Nov 05 '22 04:11

Umer Hassam