FFMPeg integration with xcode

一曲冷凌霜 提交于 2020-05-28 04:53:53

问题


I have downloaded the ffmpeg library source code using the command: git clone git://git.videolan.org/ffmpeg.git ffmpeg

the source code contains only ".c" and ".h" files, now how to integrate ffmpeg library with my xcode project with cocoa.


回答1:


Apple has a very detailed documentation on the general subject of porting Linux apps to Mac OS X. It covers the compilation issues.




回答2:


I was building a render farm and got tired of installing and updating ffmpeg separately on each render node machine. If you want to use ffmpeg in an OS X app, you can live with the precompiled options, and you're OK with using NSTask, the easiest way I found is this:

1. Download the free app The Unarchiver if you don't already have it (available in the App Store).

2. Go to the ffmpeg site > Downloads > (Apple icon) > click "Static builds for OS X Intel 64-bit", download whichever bundle you need. This will be a file like "ffmpeg-3.0.2-2.7z".

3. Open it with The Unarchiver, will yield a UNIX file simply named "ffmpeg".

4. Drag that file into your Xcode project. When you compile, it will be in your app's main bundle.

5. Set up your NSTask, something like this (example is in Swift):

guard let launchPath = NSBundle.mainBundle().pathForResource("ffmpeg", ofType: "") else { return }
dispatch_async(dispatch_get_main_queue()) {
    let compressTask: NSTask = NSTask()
    compressTask.launchPath = launchPath
    compressTask.arguments = [
        "-y",
        "-i", myUncompressedInputFilePath,
        "-vcodec", "libx264",
        "-b:v", "1500k",
        "-c:a", "aac",
        "-pix_fmt", "yuv420p", // Necessary to allow playback in OS X Finder and QT Player
        myCompressedOutputFilePath
    ]
    compressTask.standardInput = NSFileHandle.fileHandleWithNullDevice()
    compressTask.launch()
    compressTask.waitUntilExit()

    // Do cleanup work here if necessary.
}

That's it, super easy and works great. The async dispatch is optional, remove that if you want it to run synchronously, but it'll block your program's execution. You can set any options you normally can on the command line including which codecs to use, bitrates, scaling, etc. The output will show up in Xcode's debug pane as well (control how much with the "-loglevel" option).

Doing 2-pass encoding is also very easy (and makes a big difference in quality):

guard let launchPath = NSBundle.mainBundle().pathForResource("ffmpeg", ofType: "") else { return }
dispatch_async(dispatch_get_main_queue()) {
    // Make this a unique name to avoid conflicts if you have simultaneous encodings.
    let passLogFilePath = "\(NSTemporaryDirectory())\(NSUUID().UUIDString)_passlog"

    let compressPass1: NSTask = NSTask()
    compressPass1.launchPath = launchPath
    compressPass1.arguments = [
        "-y",
        "-i", myUncompressedInputFilePath,
        "-pass", "1",
        "-passlogfile", passLogFilePath,
        "-vcodec", "libx264",
        "-b:v", "1500k",
        "-c:a", "aac",
        "-f", "mov",
        "/dev/null"
    ]
    compressPass1.standardInput = NSFileHandle.fileHandleWithNullDevice()
    compressPass1.launch()
    compressPass1.waitUntilExit()

    let compressPass2: NSTask = NSTask()
    compressPass2.launchPath = launchPath
    compressPass2.arguments = [
        "-y",
        "-i", myUncompressedInputFilePath,
        "-pass", "2",
        "-passlogfile", passLogFilePath,
        "-vcodec", "libx264",
        "-b:v", "1500k",
        "-c:a", "aac",
        "-pix_fmt", "yuv420p", // Necessary to allow playback in OS X Finder and QT Player
        myCompressedOutputFilePath
    ]

    compressPass2.standardInput = NSFileHandle.fileHandleWithNullDevice()
    compressPass2.launch()
    compressPass2.waitUntilExit()

    // Clean up ffmpeg pass log files, there should be two of them.
    let fm = NSFileManager.defaultManager()
    if fm.fileExistsAtPath("\(passLogFilePath)-0.log") {
        do {
            try fm.removeItemAtPath("\(passLogFilePath)-0.log")
        } catch {
            print("Failed to remove '-0.log' file.")
        }
    }
    if fm.fileExistsAtPath("\(passLogFilePath)-0.log.mbtree") {
        do {
            try fm.removeItemAtPath("\(passLogFilePath)-0.log.mbtree")
        } catch {
            print("Failed to remove '-0.log.mbtree' file.")
        }
    }
}



回答3:


I suggest to install ffmpeg with MacPorts(the easiest way to use linux apps on mac)
sudo port install ffmpeg
and then set up header and library search path in xcode project When you install it with MacPorts then it is:
/opt/local/include* for header files
/opt/local/lib* for lib files

After all you just add includes in your code:

#include <avcodec.h>
#include <avformat.h>


来源:https://stackoverflow.com/questions/5416701/ffmpeg-integration-with-xcode

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!