2014-03-28 127 views

回答

5

現場編輯音頻就像現場編輯視頻。返回到每部電影並獲取音軌並將其粘貼到可變的作品中。

在這個例子中,我抓住視頻的前五秒,並從電影視頻的最後五秒鐘,並把它們一前一後在新視頻:

NSString* type = AVMediaTypeVideo; 
NSArray* arr = [oldAsset tracksWithMediaType:type]; 
AVAssetTrack* track = [arr lastObject]; 
CMTime duration = track.timeRange.duration; 
AVMutableComposition* comp = [AVMutableComposition composition]; 
AVMutableCompositionTrack* comptrack = [comp addMutableTrackWithMediaType:type preferredTrackID:kCMPersistentTrackID_Invalid]; 
[comptrack insertTimeRange:CMTimeRangeMake(CMTimeMakeWithSeconds(0,600), CMTimeMakeWithSeconds(5,600)) ofTrack:track atTime:CMTimeMakeWithSeconds(0,600) error:nil]; 
[comptrack insertTimeRange:CMTimeRangeMake(CMTimeSubtract(duration, CMTimeMakeWithSeconds(5,600)), CMTimeMakeWithSeconds(5,600)) ofTrack:track atTime:CMTimeMakeWithSeconds(5,600) error:nil]; 

但由此產生的視頻會安靜。所以我也回去拿對應的音頻:

type = AVMediaTypeAudio; 
arr = [oldAsset tracksWithMediaType:type]; 
track = [arr lastObject]; 
comptrack = [comp addMutableTrackWithMediaType:type preferredTrackID:kCMPersistentTrackID_Invalid]; 
[comptrack insertTimeRange:CMTimeRangeMake(CMTimeMakeWithSeconds(0,600), CMTimeMakeWithSeconds(5,600)) ofTrack:track atTime:CMTimeMakeWithSeconds(0,600) error:nil]; 
[comptrack insertTimeRange:CMTimeRangeMake(CMTimeSubtract(duration, CMTimeMakeWithSeconds(5,600)), CMTimeMakeWithSeconds(5,600)) ofTrack:track atTime:CMTimeMakeWithSeconds(5,600) error:nil]; 
+0

非常感謝很多明確的解釋馬特..這對我工作! :) – BrianChristo

+0

'AVMutableCompositionTrack * AudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];' '[AudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero,firstAsset.duration)ofTrack:[[firstAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:kCMTimeZero error:nil];' 'AVMutableCompositionTrack * secondTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; //爲另一首曲目做同樣的事情,如下所示:** [secondTrack insertTimeRange:.... ** –