1

我使用图像数组创建了一个视频。

它成功创建了视频,然后我将音频添加到创建的视频文件中。

我创建 AVMuatableComposition 对象,通过创建 AVAssetTracks 添加视频和音频,最后在 AVAssetsExportSession 的帮助下导出到单个视频文件。

假设第一个没有音频的视频是vdo.mp4,最终(添加音频后)是final.mp4,所以我的final.mp4的大小和分辨率低于vdo.mp4

这是我的代码,它结合了这两个文件,

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSError * error = nil;

AVMutableComposition * composition = [AVMutableComposition composition];

NSURL *url = [NSURL fileURLWithPath:filePath];

AVURLAsset * videoAsset = [AVURLAsset URLAssetWithURL:url options:nil];

AVAssetTrack * videoAssetTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];

AVMutableCompositionTrack *compositionVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo
                                                                            preferredTrackID: kCMPersistentTrackID_Invalid];

[compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero,videoAsset.duration) ofTrack:videoAssetTrack atTime:kCMTimeZero error:&error];

CMTime audioStartTime = kCMTimeZero;
for (   NSInteger i = 0;i< [mArrAudioFileNames count];i++ )
{
    NSString *audioFileName = nil;
    NSString *docsDir = nil;
    if ( [mArrAudioFileNames objectAtIndex:i] != [NSNull null]) {
        audioFileName = [mArrAudioFileNames objectAtIndex:i];
        docsDir = [[self dataFolderPathForAudio] stringByAppendingPathComponent:audioFileName];

    }else{
        //audioFileName = @" ";
        docsDir = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"mp3"];
    }

    // NSString *docsDir = [[self dataFolderPathForAudio] stringByAppendingPathComponent:audioFileName];
    AVURLAsset * urlAsset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:docsDir] options:nil];

    AVAssetTrack * audioAssetTrack = [[urlAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
    AVMutableCompositionTrack *compositionAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio
                                                                                preferredTrackID: kCMPersistentTrackID_Invalid];

    [compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero,urlAsset.duration) ofTrack:audioAssetTrack atTime:audioStartTime error:&error];

    Float64 duration = CMTimeGetSeconds(urlAsset.duration);

    audioStartTime = CMTimeAdd(audioStartTime, CMTimeMake((int) ((duration * kRecordingFPS) + 0.5), kRecordingFPS));
}

AVAssetExportSession* assetExport = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetMediumQuality];
//assetExport.videoComposition = compositionVideoTrack;

 assetExport.outputFileType = AVFileTypeQuickTimeMovie;// @"com.apple.quicktime-movie";
assetExport.outputURL = [NSURL fileURLWithPath:outFilePath];

[assetExport exportAsynchronouslyWithCompletionHandler:
 ^(void ) {
     switch (assetExport.status)
     {
         case AVAssetExportSessionStatusCompleted:
             //                export complete
             NSLog(@"Export Complete");
             [self performSelectorOnMainThread:@selector(creatingVideoDone:)
                                    withObject:outFilePath waitUntilDone:NO];
             [assetExport release];
             break;
         case AVAssetExportSessionStatusFailed:
             NSLog(@"Export Failed");
             NSLog(@"ExportSessionError: %@", [assetExport.error localizedDescription]);
             // Set delegate to move to view
             if ( mDelegate!= nil && [mDelegate respondsToSelector:@selector(errorAlert:)])
             {
                 [self performSelectorOnMainThread:@selector(errorOccured:)
                                        withObject:[assetExport.error
                                                    localizedDescription]
                                     waitUntilDone:NO];
             }
             [assetExport release];
             break;
         case AVAssetExportSessionStatusCancelled:
             NSLog(@"Export Failed");
             NSLog(@"ExportSessionError: %@", [assetExport.error localizedDescription]);
             // Set delegate to move to view
             if ( mDelegate!= nil && [mDelegate respondsToSelector:@selector(errorAlert:)])
             {
                 [self performSelectorOnMainThread:@selector(errorOccured:)
                                        withObject:[assetExport.error
                                                    localizedDescription]
                                     waitUntilDone:NO];
             }
             [assetExport release];
             break;
     }
 }];

任何帮助表示赞赏。

谢谢。

4

0 回答 0