How to use FFmpeg in IOS

Here i am going to show complete integration of FFmpeg in IOS

Follow these steps to use FFmpeg in IOS

iOS / tvOS

Add MobileFFmpeg dependency to your Podfile in mobile-ffmpeg- pattern

  • iOS
pod 'mobile-ffmpeg-full', '~> 4.4'
  • tvOS
pod 'mobile-ffmpeg-tvos-full', '~> 4.4'

Execute synchronous FFmpeg commands

#import <mobileffmpeg/MobileFFmpegConfig.h>
#import <mobileffmpeg/MobileFFmpeg.h>

int rc = [MobileFFmpeg execute: @"-i file1.mp4 -c:v mpeg4 file2.mp4"];

if (rc == RETURN_CODE_SUCCESS) {
    NSLog(@"Command execution completed successfully.\n");
} else if (rc == RETURN_CODE_CANCEL) {
    NSLog(@"Command execution cancelled by user.\n");
} else {
    NSLog(@"Command execution failed with rc=%d and output=%@.\n", rc, [MobileFFmpegConfig getLastCommandOutput]);
}

Execute asynchronous FFmpeg commands

#import <mobileffmpeg/MobileFFmpegConfig.h>
#import <mobileffmpeg/MobileFFmpeg.h>

long executionId = [MobileFFmpeg executeAsync:@"-i file1.mp4 -c:v mpeg4 file2.mp4" withCallback:self];

- (void)executeCallback:(long)executionId :(int)returnCode {
    if (rc == RETURN_CODE_SUCCESS) {
        NSLog(@"Async command execution completed successfully.\n");
    } else if (rc == RETURN_CODE_CANCEL) {
        NSLog(@"Async command execution cancelled by user.\n");
    } else {
        NSLog(@"Async command execution failed with rc=%d.\n", rc);
    }
}

Execute FFprobe commands

#import <mobileffmpeg/MobileFFmpegConfig.h>
#import <mobileffmpeg/MobileFFprobe.h>

int rc = [MobileFFprobe execute: @"-i file1.mp4"];

if (rc == RETURN_CODE_SUCCESS) {
    NSLog(@"Command execution completed successfully.\n");
} else if (rc == RETURN_CODE_CANCEL) {
    NSLog(@"Command execution cancelled by user.\n");
} else {
    NSLog(@"Command execution failed with rc=%d and output=%@.\n", rc, [MobileFFmpegConfig getLastCommandOutput]);
}

Check execution output later

int rc = [MobileFFmpegConfig getLastReturnCode];
NSString *output = [MobileFFmpegConfig getLastCommandOutput];

if (rc == RETURN_CODE_SUCCESS) {
    NSLog(@"Command execution completed successfully.\n");
} else if (rc == RETURN_CODE_CANCEL) {
    NSLog(@"Command execution cancelled by user.\n");
} else {
    NSLog(@"Command execution failed with rc=%d and output=%@.\n", rc, output);
}

Stop ongoing FFmpeg operations

  • Stop all executions
[MobileFFmpeg cancel];
  • Stop a specific execution
[MobileFFmpeg cancel:executionId];

Get media information for a file

MediaInformation *mediaInformation = [MobileFFprobe getMediaInformation:@"<file path or uri>"];

Record video and audio using iOS camera

[MobileFFmpeg execute: @"-f avfoundation -r 30 -video_size 1280x720 -pixel_format bgr0 -i 0:0 -vcodec h264_videotoolbox -vsync 2 -f h264 -t 00:00:05 %@", recordFilePath];

Enable log callback

[MobileFFmpegConfig setLogDelegate:self];

- (void)logCallback:(long)executionId :(int)level :(NSString*)message {
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"%@", message);
    });
}

Enable statistics callback

[MobileFFmpegConfig setStatisticsDelegate:self];

- (void)statisticsCallback:(Statistics *)newStatistics {
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"frame: %d, time: %d\n", newStatistics.getVideoFrameNumber, newStatistics.getTime);
    });
}

Ignore the handling of a signal

[MobileFFmpegConfig ignoreSignal:SIGXCPU];

List ongoing executions

NSArray* ffmpegExecutions = [MobileFFmpeg listExecutions];
for (int i = 0; i < [ffmpegExecutions count]; i++) {
    FFmpegExecution* execution = [ffmpegExecutions objectAtIndex:i];
    NSLog(@"Execution %d = id: %ld, startTime: %@, command: %@.\n", i, [execution getExecutionId], [execution getStartTime], [execution getCommand]);
}

Set default log level

[MobileFFmpegConfig setLogLevel:AV_LOG_FATAL];

Register custom fonts directory

[MobileFFmpegConfig setFontDirectory:@"<folder with fonts>" with:nil];

Leave a Comment