2012-09-13 33 views
8

Tôi đang cố gắng để xoay một đoạn video để định hướng đúng của nó sử dụng một AVAssetExportSession và tôi luôn luôn nhận được lỗi sau:Xoay một AVAsset với AVAssetExportSession

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

Đó dịch để AVErrorInvalidVideoComposition nhưng tôi không thể nhìn thấy bất cứ điều gì sai trái với tôi thành phần video. Đây là mã:

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:^{ }]; 

Điều gì có thể xảy ra với bố cục? Tôi đã được thông qua các tài liệu và không thể nhìn thấy bất cứ điều gì sai trái với nó cho đến nay.

+1

Bạn đã bao giờ tìm hiểu lý do tại sao bạn gặp phải lỗi đó? – shabbirv

+0

ok, do đó, có vẻ như một nửa số tiền thưởng được chuyển đến GingerBreadMane, để có câu trả lời không hiệu quả. Dường như không có cộng đồng các nhà phát triển iOS trên StackOverflow. Đó là một sự xấu hổ. –

+0

Đau khổ từ một sự cố tương tự trên iOS 7.0.3 ... mã của tôi tương tự khi tôi sử dụng một video vừa được ghi, nhưng không thành công khi tôi chọn một video từ thư viện. Nó có ý nghĩa rất ít đối với tôi. Sẽ cho bạn biết khi tôi tìm một giải pháp. Có thể không sử dụng AVExportSession. – DogpatchTech

Trả lời

0

Nó có thể phải làm với thời lượng khung của bạn. Bạn đang sử dụng CMTimeMake(1, naturalTimeScale)
Bạn nên kiểm tra NaturalTimeScale vì nó không phải lúc nào cũng bằng khung hình/giây. Xem AVFoundation Programming Guide 'Đại diện thời gian' để biết thêm thông tin.

+0

bạn có biết cách tải FPS cho nội dung video không? – Crashalot

0

Tôi nghĩ rằng đó là do thứ tự các đối số chiều rộng và chiều cao không chính xác. Bạn có:

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

nên không đó là

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

để thay thế?

0

Lỗi là từ cách bạn đặt instruction.

... videoCompositionLayerInstructionWithAssetTrack:sourceVideo] 

sourceVideo không phải là thành viên của sáng tác. Trong trường hợp của bạn, điều này phải là compositionVideoTrack.

Các vấn đề liên quan