2013-11-05 149 views
2

我有一段視頻長度爲5秒。我有一個35秒長的音頻文件。當我使用此代碼將音頻添加到視頻時,它會生成35秒長的視頻。修剪音頻到視頻長度

如何修改此代碼以便音頻播放視頻的長度並停止產生僅5秒長的視頻?

我把這個代碼從這個職位

problem using UIImage and caf to create video file

-(void) addAudioToFileAtPath:(NSString *)vidoPath andAudioPath:(NSString *)audioPath{ 
    AVMutableComposition* mixComposition = [AVMutableComposition composition]; 

    NSURL* audio_inputFileUrl = [NSURL fileURLWithPath:audioPath]; 
    NSURL* video_inputFileUrl = [NSURL fileURLWithPath:vidoPath]; 

    NSString *outputFilePath = FinalVideoPath; 
    NSURL* outputFileUrl = [NSURL fileURLWithPath:outputFilePath]; 

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

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


    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:kCMTimeZero error:nil]; 

    //nextClipStartTime = CMTimeAdd(nextClipStartTime, a_timeRange.duration); 
    [audioAsset release];audioAsset = nil; 
    [videoAsset release];videoAsset = nil; 

    AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality]; 
    _assetExport.outputFileType = AVFileTypeQuickTimeMovie; 
    _assetExport.outputURL = outputFileUrl; 

    [_assetExport exportAsynchronouslyWithCompletionHandler: 
    ^(void) { 
     switch (_assetExport.status) 
     { 
      case AVAssetExportSessionStatusCompleted: 
       //export complete 
       NSLog(@"Export Complete"); 
       break; 
      case AVAssetExportSessionStatusFailed: 
       NSLog(@"Export Failed"); 
       NSLog(@"ExportSessionError: %@", [_assetExport.error localizedDescription]); 
      //export error (see exportSession.error) 
       break; 
      case AVAssetExportSessionStatusCancelled: 
       NSLog(@"Export Failed"); 
       NSLog(@"ExportSessionError: %@", [_assetExport.error localizedDescription]); 
       //export cancelled 
       break; 
     } 
     }];  
} 

回答

2
_assetExport.timeRange = CMTimeRangeMake(kCMTimeZero, videoAsset.duration); 
+0

我,只要我張貼的問題,意識到這點。但你打敗了我。只要我被允許,我會將其標記爲正確的 –