2014-02-18 97 views
8

我有錄製的視頻與聲音。將其添加到AVMutableComposition並將其導出後,視頻將在播放聲音時凍結。我錯過了什麼?AVFoundation AVMutableComposition - 視頻與音頻 - 凍結視頻

我有我的兩個軌道:

_compositionVideoTrack = [self.composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; 
_compositionAudioTrack = [self.composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; 

然後創建曲目:

AVAssetTrack *clipVideoTrack = [[sourceAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; 
AVAssetTrack *clipAudioTrack = [[sourceAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]; 

然後我插入軌道:

[_compositionVideoTrack insertTimeRange:editRange ofTrack:clipVideoTrack atTime:self.composition.duration error:&editError]; 
[_compositionAudioTrack insertTimeRange:editRange ofTrack:clipAudioTrack atTime:self.composition.duration error:&editError]; 

最後,我將其導出:

AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:urlAsset presetName:AVAssetExportPresetPassthrough]; 
exportSession.outputFileType = AVFileTypeQuickTimeMovie; 

而該文件是.mp4。它可能是文件格式?

+0

視頻是否每次都在特定的幀上凍結?這可能是視頻軌道的時間範圍不正確。您正在將{clipVideoTrack}和{clipAudioTrack}的時間範圍插入到合成持續時間開始的合成軌道。 – jlw

+0

聲音開始時視頻開始凍結。音頻和視頻不能同時播放。是的,我希望所有添加到比賽的片段都在之前的片段之後,這就是爲什麼我將它添加到比賽的持續時間。 – Mikael

+0

沒有看到更多的代碼,很難說。我的猜測是AVMutableVideoCompositionInstruction中AVMutableVideoCompositionLayerInstructions在聲音開始時的順序有問題。 – jlw

回答

0

試試以下

AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:self.inputAsset presetName:AVAssetExportPresetLowQuality]; 
    exportSession.outputFileType = AVFileTypeQuickTimeMovie; 
+0

我試着將AVAssetExportPresetPassthrough改爲AVAssetExportPresetMediumQuality。也沒有工作。 – Mikael

+0

你保存了你的視頻嗎?或者你剛剛輸出? – iDeveloper

+0

它被保存,只是沒有顯示整個代碼 – Mikael

0

導出我想你應該添加AVMutableVideoCompositionInstruction

+0

我應該添加到該指令? – Mikael

0

您CA試着這樣做:

AVMutableComposition *mutableComposition = [AVMutableComposition composition]; 
AVMutableCompositionTrack *videoCompositionTrack = [mutableComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; 
[videoCompositionTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration) ofTrack:clipVideoTrack atTime:kCMTimeZero error:nil]; 
AVMutableCompositionTrack *audioCompositionTrack = [mutableComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; 
[audioCompositionTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration) ofTrack:[[asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:kCMTimeZero error:nil]; 

,然後初始化AVAssetExportSession與可變組成

AVAssetExportSession* exporter = [[AVAssetExportSession alloc] initWithAsset:mutableComposition presetName:AVAssetExportPresetHighestQuality]; 

希望這會幫助你