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

时间:2011-05-06 02:50:09

标签: cocoa-touch ios video audio avfoundation

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

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

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

任何人都可以告诉我,在我的情况下是否有用,因为我的音频和图像/视频的长度不同?

提前致谢。

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

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

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];
    if (avPlayer.duration >0.00000)
    {
        NSLog(@"SOMEDATA     IS THERE ");
        AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:audioUrl options:nil];
        AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:videoUrl options:nil];

        AVMutableComposition* mixComposition = [AVMutableComposition composition];

        NSLog(@"audio =%@",audioAsset);
        AVMutableCompositionTrack *compositionCommentaryTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
        [compositionCommentaryTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset.duration) ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:kCMTimeZero error:nil];

        AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
        [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) ofTrack:[[videoAsset tracksWithMediaType: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(@"file type %@",_assetExport.outputFileType);
        _assetExport.outputURL = exportUrl;
        _assetExport.shouldOptimizeForNetworkUse = YES;



        [_assetExport exportAsynchronouslyWithCompletionHandler:
         ^(void ) 
        {      

            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];
             [audioAsset release];
             [videoAsset release];
             [_assetExport release];
         }       
         ];