AVMutableComposition减少了最终视频的大小和分辨率

时间:2013-06-06 13:41:24

标签: ios avmutablecomposition

我使用图像阵列创建了一个视频。

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

我创建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;
     }
 }];

感谢任何帮助。

感谢。

0 个答案:

没有答案