2012-02-01 28 views
7

此問題與AVMutableComposition - Blank/Black frame between videos assets完全相關,但由於我沒有使用AVAssetExportSession,所以答案不適合我的問題。AVMutableComposition中的黑色框架

我正在使用AVMutableComposition來創建視頻合成,我正在使用AVAssetReader(我需要有幀數據,我不能使用AVPlayer)讀取它,但我經常會在我的視頻之間出現黑框大塊(音頻中沒有明顯的毛刺)。

創建我組成

AVMutableCompositionTrack *compositionVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; 
AVMutableCompositionTrack *compositionAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; 

NSMutableArray* durationList = [NSMutableArray array]; 
NSMutableArray* videoList= [NSMutableArray array]; 
NSMutableArray* audioList= [NSMutableArray array]; 

for (NSInteger i = 0; i < [clips count]; i++) 
{ 
    AVURLAsset *myasset = [clips objectAtIndex:i]; 
    AVAssetTrack *clipVideoTrack = [[myasset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; 
    [videoList addObject:clipVideoTrack]; 

    AVAssetTrack *clipAudioTrack = [[myasset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]; 
    [audioList addObject:clipAudioTrack]; 

    CMTime clipDuration = [myasset duration]; 
    CMTimeRange clipRange = CMTimeRangeMake(kCMTimeZero, clipDuration); 
    [durationList addObject:[NSValue valueWithCMTimeRange:clipRange]]; 
} 

[compositionVideoTrack insertTimeRanges:durationList ofTracks:videoList atTime:kCMTimeZero error:nil]; 
[compositionAudioTrack insertTimeRanges:durationList ofTracks:audioList atTime:kCMTimeZero error:nil]; 

我也試圖在我的作文手動插入每個軌道,但我也有同樣的現象。

由於

+0

您是否曾經解決過這個問題?我遇到了類似的問題。 – elprl 2013-08-06 21:48:19

+0

@elprl我看了一下我的代碼,它保持幾乎相同。我認爲我們通過調整(關鍵幀間隔,幀率,...)視頻格式輸入(我們可以控制它)來「解決」問題。那是很久以前的事了,我不能肯定地說,參數改善了這種情況。 – chub 2013-10-10 08:21:22

回答

0

添加剪輯時嘗試建立兩個視頻軌道,然後在兩者之間交替。

19

經過數小時的測試,我設法解決了這個問題。有兩件事我需要改變。 1)創建資產時添加'精確'選項。

NSDictionary *options = @{AVURLAssetPreferPreciseDurationAndTimingKey:@YES}; 
AVURLAsset *videoAsset3 = [AVURLAsset URLAssetWithURL:clip3URL options:options]; 

2)不使用

CMTime duration3 = [videoAsset3 duration]; 

改用

AVAssetTrack *videoAsset3Track = [[videoAsset3 tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; 
CMTime duration3 = videoAsset3Track.timeRange.duration; 

我發現了這一點後,我設置AVPlayer背景色爲藍,這時我才發現出現藍色框架,所以問題與時間有關。一旦我改變爲以上設置,不同視頻在使用時對齊:

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

[videoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, duration3) 
         ofTrack:videoAsset3Track 
         atTime:kCMTimeZero error:&error]; 
+2

謝謝@elprl ..這個作品完美 – MAMN84 2013-11-22 17:58:37