合并音频和视频/图像以创建电影文件

Posted

技术标签:

【中文标题】合并音频和视频/图像以创建电影文件【英文标题】:Merge audio and video/image to create a movie file 【发布时间】:2011-05-06 02:50:09 【问题描述】:

我想合并 Audion CAF 文件和视频/图像UIImage 以创建电影文件(.mov 格式)。

假设我的音频有 30 秒长,我有一个 UIImage;我想创建一个 .mov 文件,以便在整个音频播放期间显示 UIImage

我找到了这个参考: How to add audio to video file on iphone SDK

谁能告诉我,这对我有帮助吗,因为我的音频和图像/视频的长度不同?

提前致谢。

【问题讨论】:

【参考方案1】:

是的,您应该使用 AVMutableComposition。要从 UIImage 创建视频轨道,请使用 AVAssetWriter。

【讨论】:

你能提供关于这个的示例演示应用程序吗......教程也足够了......请帮助我......【参考方案2】:

Quicktime Pro 可以做到这一点。为我自己的应用程序做到了。 您从图像创建电影。 Quicktime 提供读取一系列图像并从中创建电影的功能。他也在导入期间要求提供 FPS。

然后可以简单地将音轨合并、粘贴到某处或缩放到电影的选定范围。

【讨论】:

将使用哪个类/框架?如果您有,请提供一些教程或参考链接。谢谢。 Quicktime 安装在每台 Mac 上。 Quicktime Pro 是 Quicktime 的商业升级。您可以在 Apples Online Shop 中获取密钥。我认为可以使用 quicktime 框架手动完成所有你想到的编辑,但我没有任何例子。由于 Quicktime 是 Apple 技术的一部分,因此您将在其开发者网站上提供示例。【参考方案3】:

使用这个我在网上的某个地方找到了这个,我不记得了,....

NSString *fileNamePath = @"audio.caf"; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *oldappSettingsPath = [documentsDirectory stringByAppendingPathComponent:fileNamePath]; NSURL *audioUrl = [NSURL fileURLWithPath:oldappSettingsPath]; NSString *fileNamePath1 = @"output.mp4"; NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); NSString *documentsDirectory1 = [paths1 objectAtIndex:0]; NSString *oldappSettingsPath1 = [documentsDirectory1 stringByAppendingPathComponent:fileNamePath1]; NSLog(@"oldpath=%@",oldappSettingsPath); NSURL *videoUrl = [NSURL fileURLWithPath:oldappSettingsPath1]; 如果(avPlayer.duration > 0.00000) NSLog(@"有一些数据"); AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:audioUrl options:nil]; AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:videoUrl options:nil]; AVMutableComposition* mixComposition = [AVMutableComposition 组合]; NSLog(@"audio =%@",audioAsset); AVMutableCompositionTrack *compositionCommentaryTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; [compositionCommentaryTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset.duration) ofTrack:[[audioAsset trackingWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:kCMTimeZero error:nil]; AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) ofTrack:[[videoAsset trackingWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:nil]; AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetPassthrough]; NSString* videoName = @"export.mov"; NSString *exportPath = [NSTemporaryDirectory() stringByAppendingPathComponent:videoName]; NSURL *exportUrl = [NSURL fileURLWithPath:exportPath]; if ([[NSFileManager defaultManager] fileExistsAtPath:exportPath]) [[NSFileManager defaultManager] removeItemAtPath:exportPath error:nil]; _assetExport.outputFileType = @"com.apple.quicktime-movie"; NSLog(@"文件类型%@",_assetExport.outputFileType); _assetExport.outputURL = 出口网址; _assetExport.shouldOptimizeForNetworkUse = YES; [_assetExport exportAsynchronouslyWithCompletionHandler: ^(无效) NSString *fileNamePath = @"sound_record.mov"; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *oldappSettingsPath = [documentsDirectory stringByAppendingPathComponent:fileNamePath]; // if ([[NSFileManager defaultManager] fileExistsAtPath:oldappSettingsPath]) // // NSFileManager *fileManager = [NSFileManager defaultManager]; // [fileManager removeItemAtPath: oldappSettingsPath error:NULL]; // // NSURL *documentDirectoryURL = [NSURL fileURLWithPath:oldappSettingsPath]; [[NSFileManager defaultManager] copyItemAtURL:exportUrl toURL:documentDirectoryURL error:nil]; [音频资产发布]; [视频资产发布]; [_assetExport 发布]; ];

【讨论】:

以上是关于合并音频和视频/图像以创建电影文件的主要内容,如果未能解决你的问题,请参考以下文章

ffmpeg:连接图像和视频

如何将音频文件与新的视频文件合并?在 android 中可以吗?

如何将视频文件与音频文件合并并保持创建时间?

将音频和视频文件合并为一个 [C++] [关闭]

从中间或特定时间将视频合并到音频文件

aforge.video.ffmpeg 支持音频吗