objective-c - 如何在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");
}];
解决方案
推荐阅读
- codesmith - CodeSmith 生成器获取视图模式
- android - Realm db 在一个活动中返回数据,而不是在其他活动中返回数据
- php - Laravel Multiauth:尝试获取非对象的属性“标题”
- python - 在数据框中解包字典
- visual-studio - VS2017 - 构建返回 D8050:无法将命令行放入调试记录
- php - 加载 XML 文件 -> 添加项目 -> 重新排序 -> 写入 XML 文件 [PHP & SimpleXML]
- javascript - 大屏幕上的不同 FlatList
- angular - 如何将 HTML 的属性转换为打字稿
- python - Python 和 Oracle DB -“错误 DPI-1050:Oracle 客户端库必须为 11.2 或更高版本”
- python - 我用 Python 编写了一个简单的聚类算法,但没有得到预期的结果。我的算法正确吗?