Home > Software engineering >  Ffmpeg for use in iOS application coded in Swift
Ffmpeg for use in iOS application coded in Swift

Time:06-19

I have been browsing the web, even this website... bu cannot find a good option to implement ffmpeg functionality in an iOS application made in swift.

Options looked at and reasons why they are not solutions:

SwiftFFmpeg - I am not sure it can run on iOS, plus I don't see an option to run my desired Ffmpeg command.

MobileFFmpeg - Not maintained anymore, and a new project by the same founders created a "better altenrative" called ffmpeg-kit.

ffmpeg-kit - looks amazing, but their API only allows for interaction in the Objective-C language.

Any solutions anyone can give me?

CodePudding user response:

First...

Make sure you understand what "packages" are available. You can build it yourself, but to be honest, unless you have a very specific reason, I'd just use the pre-build packages. See "8. Packages" of the enter image description here

Forth

For this experiment, I opened the ViewController class (which was automatically created by Xcode) and simply added...

func syncCommand() {
    guard let session = FFmpegKit.execute("-i file1.mp4 -c:v file1.mp4") else {
        print("!! Failed to create session")
        return
    }
    let returnCode = session.getReturnCode()
    if ReturnCode.isSuccess(returnCode) {
        
    } else if ReturnCode.isCancel(returnCode) {
        
    } else {
        print("Command failed with state \(FFmpegKitConfig.sessionState(toString: session.getState()) ?? "Unknown") and rc \(returnCode?.description ?? "Unknown").\(session.getFailStackTrace() ?? "Unknown")")
    }
}

func asyncCommand() {
    FFmpegKit.executeAsync("-i file1.mp4 -c:v file2.mp4") { session in
        guard let session = session else {
            print("!! Invalid session")
            return
        }
        guard let returnCode = session.getReturnCode() else {
            print("!! Invalid return code")
            return
        }
        print("FFmpeg process exited with state \(FFmpegKitConfig.sessionState(toString: session.getState()) ?? "Unknown") and rc \(returnCode).\(session.getFailStackTrace() ?? "Unknown")")
    } withLogCallback: { logs in
        guard let logs = logs else { return }
        // CALLED WHEN SESSION PRINTS LOGS
    } withStatisticsCallback: { stats in
        guard let stats = stats else { return }
        // CALLED WHEN SESSION GENERATES STATISTICS
    }

}

The code above is basically the "2. Execute synchronous FFmpeg commands." and "4. Execute asynchronous FFmpeg commands by providing session specific execute/log/session callbacks." examples from the ffmpeg-kit/apple documentation

!! Important !! - don't forget to add import ffmpegkit to the start of the file!

At this point, this should now compile (you'll get a couple of warnings about logs and stats not been used, you can ignore those).

After thoughts...

You should realise by now that the code I've provided won't actually run, for two reasons.

  1. I've not actually called either func from anywhere (I tested it by placing it in the viewDidLoad func of the ViewController class)
  2. The input file, used in the execute command, doesn't exist. You will need to provide an actual reference to an actual file, preferably with an absolute path. This, how ever, may require you to change the "App Sandbox" settings under the targets "Signing and Capabilities"
  3. Xcodes auto code suggestions aren't bad and I mostly filled out the above using it, and the Obj-c code as a starting point.

Also, beware, SO is not a "tutorial" site, Xcode is a complex beast and you may need to spend some time exploring other resources to overcome issues you encounter

  • Related