Skip to content
Sue N. Cooper edited this page Aug 27, 2023 · 8 revisions
  1. Add FFmpegKit dependency to your Podfile in ffmpeg-kit-<platform>-<package name> pattern. Use one of the FFmpegKit package names given in the project README.

    pod 'ffmpeg-kit-macos-full', '~> 6.0'
  2. Execute synchronous FFmpeg commands.

    #include <ffmpegkit/FFmpegKit.h>
    
    FFmpegSession *session = [FFmpegKit execute:@"-i file1.mp4 -c:v mpeg4 file2.mp4"];
    ReturnCode *returnCode = [session getReturnCode];
    if ([ReturnCode isSuccess:returnCode]) {
    
        // SUCCESS
    
    } else if ([ReturnCode isCancel:returnCode]) {
    
        // CANCEL
    
    } else {
    
        // FAILURE
        NSLog(@"Command failed with state %@ and rc %@.%@", [FFmpegKitConfig sessionStateToString:[session getState]], returnCode, [session getFailStackTrace]);
    
    }
  3. Each execute call (sync or async) creates a new session. Access every detail about your execution from the session created.

    FFmpegSession *session = [FFmpegKit execute:@"-i file1.mp4 -c:v mpeg4 file2.mp4"];
    
    // Unique session id created for this execution
    long sessionId = [session getSessionId];
    
    // Command arguments as a single string
    NSString *command = [session getCommand];
    
    // Command arguments
    NSArray *arguments = [session getArguments];
    
    // State of the execution. Shows whether it is still running or completed
    SessionState state = [session getState];
    
    // Return code for completed sessions. Will be null if session is still running or ends with a failure
    ReturnCode *returnCode = [session getReturnCode];
    
    NSDate *startTime =[session getStartTime];
    NSDate *endTime =[session getEndTime];
    long duration =[session getDuration];
    
    // Console output generated for this execution
    NSString *output = [session getOutput];
    
    // The stack trace if FFmpegKit fails to run a command
    NSString *failStackTrace = [session getFailStackTrace];
    
    // The list of logs generated for this execution
    NSArray *logs = [session getLogs];
    
    // The list of statistics generated for this execution
    NSArray *statistics = [session getStatistics];
  4. Execute asynchronous FFmpeg commands by providing session specific execute/log/session callbacks.

    FFmpegSession* session = [FFmpegKit executeAsync:@"-i file1.mp4 -c:v mpeg4 file2.mp4" withCompleteCallback:^(FFmpegSession* session){
        SessionState state = [session getState];
        ReturnCode *returnCode = [session getReturnCode];
    
        // CALLED WHEN SESSION IS EXECUTED
    
        NSLog(@"FFmpeg process exited with state %@ and rc %@.%@", [FFmpegKitConfig sessionStateToString:state], returnCode, [session getFailStackTrace]);
    
    } withLogCallback:^(Log *log) {
    
        // CALLED WHEN SESSION PRINTS LOGS
    
    } withStatisticsCallback:^(Statistics *statistics) {
    
        // CALLED WHEN SESSION GENERATES STATISTICS
    
    }];
  5. Execute FFprobe commands.

    • Synchronous
    FFprobeSession *session = [FFprobeKit execute:ffprobeCommand];
    
    if ([ReturnCode isSuccess:[session getReturnCode]]) {
        NSLog(@"Command failed. Please check output for the details.");
    }
    • Asynchronous
    [FFprobeKit executeAsync:ffmpegCommand withCompleteCallback:^(FFprobeSession* session) {
    
        CALLED WHEN SESSION IS EXECUTED
    
    }];
  6. Get media information for a file.

    MediaInformationSession *mediaInformation = [FFprobeKit getMediaInformation:"<file path or uri>"];
    MediaInformation *mediaInformation =[mediaInformation getMediaInformation];
  7. Stop ongoing FFmpeg operations.

    • Stop all executions
      [FFmpegKit cancel];
    • Stop a specific session
      [FFmpegKit cancel:sessionId];
  8. Get previous FFmpeg and FFprobe sessions from session history.

    NSArray* sessions = [FFmpegKitConfig getSessions];
    for (int i = 0; i < [sessions count]; i++) {
        id<Session> session = [sessions objectAtIndex:i];
        NSLog(@"Session %d = id: %ld, startTime: %@, duration: %ld, state:%@, returnCode:%@.\n",
            i,
            [session getSessionId],
            [session getStartTime],
            [session getDuration],
            [FFmpegKitConfig sessionStateToString:[session getState]],
            [session getReturnCode]);
    }
  9. Enable global callbacks.

    • Session type specific Complete Callbacks, called when an async session has been completed

      [FFmpegKitConfig enableFFmpegSessionCompleteCallback:^(FFmpegSession* session) {
          ...
      }];
      
      [FFmpegKitConfig enableFFprobeSessionCompleteCallback:^(FFprobeSession* session) {
          ...
      }];
      
      [FFmpegKitConfig enableMediaInformationSessionCompleteCallback:^(MediaInformationSession* session) {
          ...
      }];
    • Log Callback, called when a session generates logs

      [FFmpegKitConfig enableLogCallback:^(Log *log) {
          ...
      }];
    • Statistics Callback, called when a session generates statistics

      [FFmpegKitConfig enableStatisticsCallback:^(Statistics *statistics) {
          ...
      }];
  10. Ignore the handling of a signal. Required by Mono and frameworks that use Mono, e.g. Unity and Xamarin.

    [FFmpegKitConfig ignoreSignal:SIGXCPU];
  11. Register system fonts and custom font directories.

    [FFmpegKitConfig setFontDirectoryList:[NSArray arrayWithObjects:@"/System/Library/Fonts", @"<folder with fonts>", nil] with:nil];
Clone this wiki locally