2012-09-13 50 views
8

我試圖用AVAssetExportSession旋轉到其正確方向的視頻,我總是得到以下錯誤的AVAsset:旋轉與AVAssetExportSession

Error Domain=AVFoundationErrorDomain Code=-11841 "The operation couldn’t be completed. (AVFoundationErrorDomain error -11841.)" 

這相當於AVErrorInvalidVideoComposition,但我看不出什麼毛病我視頻組成。代碼如下:

AVAssetTrack *sourceVideo = [[avAsset tracksWithMediaType:AVMediaTypeVideo] lastObject]; 
AVAssetTrack *sourceAudio = [[avAsset tracksWithMediaType:AVMediaTypeAudio] lastObject]; 
CGAffineTransform preferredTransform = [sourceVideo preferredTransform]; 

AVMutableComposition *composition = [[AVMutableComposition alloc] init]; 

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

AVAssetExportSession *exporter = [[[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetMediumQuality] autorelease]; 

[compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, avAsset.duration) 
           ofTrack:sourceVideo 
           atTime:kCMTimeZero 
           error:nil]; 

if(!CGAffineTransformIsIdentity(preferredTransform)) { 

    AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition]; 
    videoComposition.renderSize = CGSizeMake([avAsset naturalSize].height, [avAsset naturalSize].width); 
    videoComposition.frameDuration = CMTimeMake(1, compositionVideoTrack.naturalTimeScale); 

    AVMutableVideoCompositionLayerInstruction *instruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:sourceVideo]; 
    [instruction setTransform:preferredTransform atTime:kCMTimeZero]; 

    AVMutableVideoCompositionInstruction *videoTrackInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction]; 
    videoTrackInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, avAsset.duration); 
    videoTrackInstruction.layerInstructions = [NSArray arrayWithObject:instruction]; 

    [videoComposition setInstructions:[NSArray arrayWithObject:videoTrackInstruction]]; 

    exporter.videoComposition = videoComposition; 
} 

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

[compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, avAsset.duration) 
           ofTrack:sourceAudio 
           atTime:kCMTimeZero 
           error:nil]; 

exporter.outputURL = tempPathUrl; 
exporter.outputFileType = AVFileTypeQuickTimeMovie; 
[exporter exportAsynchronouslyWithCompletionHandler:^{ }]; 

構圖有什麼問題?我已經完成了文檔,目前看不出有什麼問題。

+1

你有沒有發現你爲什麼得到這個錯誤? – shabbirv

+0

沒問題,所以看起來有一半的賞金去了GingerBreadMane,因爲這個答案不起作用。似乎在StackOverflow上並沒有真正的iOS開發人員社區。這是一個恥辱。 –

+0

患有iOS 7.0.3上的類似問題...我的代碼在使用剛錄製的視頻時非常類似,但在從庫中選擇視頻時失敗。這對我來說毫無意義。當我找到解決方案時,會通知您。可能不得不使用AVExportSession。 – DogpatchTech

回答

0

它可能與你的幀持續時間有關。您正在使用 CMTimeMake(1, naturalTimeScale)
您應該檢查naturalTimeScale,因爲它並不總是等於fps。請參閱AVFoundation Programming Guide'時間表示'瞭解更多信息。

+0

你知道如何獲得視頻資產的FPS嗎? – Crashalot

0

我認爲這是因爲寬度和高度參數的順序不正確。 您有:

videoComposition.renderSize = CGSizeMake([avAsset naturalSize].height, [avAsset naturalSize].width); 

它不應該是

videoComposition.renderSize = CGSizeMake([avAsset naturalSize].width, [avAsset naturalSize].height); 

呢?

0

錯誤在於你如何設置instruction

... videoCompositionLayerInstructionWithAssetTrack:sourceVideo] 

sourceVideo不是作曲家的成員。在你的情況下,這應該是compositionVideoTrack