首页 > 解决方案 > 如何在Objective c中使用FFMPEG在视频上叠加图像

问题描述

我使用了许多 FFMPEG 命令,但没有用。

我想在视频上叠加图像。图像和视频大小相同。

下载完整的演示源代码。

https://www.dropbox.com/s/gqcbuynnkpbcufi/FFmpegDemo.zip?dl=0

我在我的演示中使用下面的 POD。

pod 'ffmpeg-kit-ios-full', '~> 4.4.LTS'

这是我在视频上叠加图像的源代码。

NSString *video_path = [[NSBundle mainBundle] pathForResource:@"xyz" ofType:@"mp4"];

NSString *image_path = [[NSBundle mainBundle] pathForResource:@"frame" ofType:@"png"];

NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];

NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"Videos"];
// New Folder is your folder name
NSError *error = nil;
if (![[NSFileManager defaultManager] fileExistsAtPath:stringPath])
    [[NSFileManager defaultManager] createDirectoryAtPath:stringPath withIntermediateDirectories:NO attributes:nil error:&error];

NSString *saveFilePath = [NSString stringWithFormat:@"%@",[self generateFileNameWithExtension:@".mp4"]];
NSURL *exportUrl =  [documentsDirectoryURL URLByAppendingPathComponent:saveFilePath];

NSString *output_path = [NSString stringWithFormat:@"%@/%@",stringPath,saveFilePath];

if ([[NSFileManager defaultManager] fileExistsAtPath:exportUrl.path])
{
    [[NSFileManager defaultManager] removeItemAtPath:exportUrl.path error:nil];
}

NSString *command = [NSString stringWithFormat:@"-i %@ -i %@ -filter_complex [0:v]scale=1080:1080:force_original_aspect_ratio=decrease,pad=1080:1080:-1:-1:color=black[a];[a][1:v]overlay[video] -map [video] %@",video_path,image_path,output_path];

id<Session> session = [FFmpegKit executeAsync:command withExecuteCallback:^(id<Session> 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
  //  NSLog(@"CALLED WHEN SESSION PRINTS LOGS");


} withStatisticsCallback:^(Statistics *statistics) {

    // CALLED WHEN SESSION GENERATES STATISTICS
    
    NSLog(@"CALLED WHEN SESSION GENERATES STATISTICS");

}];

标签: objective-cxcodeffmpegmobile-ffmpeg

解决方案


推荐阅读