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