2013-04-26 9 views
7

AVMutableCompositionを使用してオーディオとビデオファイルをミキシングしています。以下は、私のコードはそのためである:私は直面しています音声の長さが音声の長さより長い場合、オーディオトラックを繰り返す方法

enter code here 
AVMutableComposition* mixComposition = [AVMutableComposition composition]; 

NSString *bundleDirectory = [[NSBundle mainBundle] bundlePath]; 

NSString *audio_inputFilePath = [bundleDirectory stringByAppendingPathComponent:@"xyz.mp3"];//audio of 35 seconds 

NSURL *audio_inputFileUrl = [NSURL fileURLWithPath:audio_inputFilePath]; 

    NSURL *video_inputFileUrl = [NSURL fileURLWithPath:videoOutputPath]; 

    NSString *outputFilePath = [documentsDirectory stringByAppendingPathComponent:@"video.mp4"];//video of 60 seconds 

NSURL *outputFileUrl = [NSURL fileURLWithPath:outputFilePath]; 

if ([[NSFileManager defaultManager] fileExistsAtPath:outputFilePath]) 
     [[NSFileManager defaultManager] removeItemAtPath:outputFilePath error:nil]; 

    CMTime nextClipStartTime = kCMTimeZero; 

    AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:video_inputFileUrl options:nil]; 

CMTimeRange video_timeRange = CMTimeRangeMake(kCMTimeZero,videoAsset.duration); 

    AVMutableCompositionTrack *a_compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; 

[a_compositionVideoTrack insertTimeRange:video_timeRange ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:nextClipStartTime error:nil]; 

    AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:audio_inputFileUrl options:nil]; 
    enter code here 
    CMTimeRange audio_timeRange = CMTimeRangeMake(kCMTimeZero, audioAsset.duration); 
    enter code here 
    AVMutableCompositionTrack *b_compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; 
    [b_compositionAudioTrack insertTimeRange:audio_timeRange ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:nextClipStartTime error:nil]; 
    enter code here 
    AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPreset640x480]; 

問題は、私のオーディオファイルのデュレーションがビデオ再生時間よりも短くなっています。だから私は、ビデオが終了するまで、オーディオファイルをループすることをやりたい。私のビデオは60秒、オーディオは35秒なので、オーディオは25秒間繰り返す必要があります。

どのようにすればいいのですか?

答えて

4

ソリューション1:テストしたが、それは動作するはずはない。注AVMutableCompositionTrack

if (videoAsset.duration>audioAsset.duration) 
{ 
    //new time range 
    CMTime duration = CMTimeMakeWithSeconds(CMTimeGetSeconds(videoAsset.duration)-CMTimeGetSeconds(audioAsset.duration), audioAsset.duration.timescale); 
    if (CMTIME_IS_VALID(duration)) 
    { 
    CMTimeRange video_timeRange2 = CMTimeRangeMake(audioAsset.duration,duration); 
    //start from where left 
    CMTime nextClipStartTime2 = audioAsset.duration; 
    //add in AVMutableCompositionTrack 
    [b_compositionVideoTrack insertTimeRange:video_timeRange2 ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:nextClipStartTime2 error:nil]; 
    } 
    else 
     NSLog(@"time is invalid"); 
} 

に新しいCMTimeRangeinsertTimeRangeを作成します。

EDIT

ソリューション2:これを試してみてください。私のコードを使用せず、下にこの行を置き換えてください

[b_compositionAudioTrack insertTimeRange:audio_timeRange ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:kCMTimeInvalid error:nil]; 
+0

は機能しません。あなたのロジックは正しいようですが、まだオーディオは35秒後に終了します – coder1010

+0

私はすでにb_compositionで試しました。私はあなたのロジックを使用しました。同じ行ではありません。ミックスコンポジションに2つのオーディオトラックを追加することは可能ですか? – coder1010

+0

もう一度私の答えを読んで、もう一度お試しください –

関連する問題