出口VideoAsset後:AVMutableComposition - 導出錯誤的視頻轉換
的問題:
- 視頻取向不是原來的變換
- 導出視頻的層似乎總是景觀。
努力:
- 變換視頻層取向 - 旋轉至原來的方向
- 視頻層尺寸 - 使其全屏大小(按原來的方向)
- videoAsset的CGRect與開頭相反。
AVURLAsset*videoAsset = [[AVURLAsset alloc]initWithURL:url options:nil];
AVMutableComposition* mixComposition = [AVMixerBase compositionVideoTrackAssetUrl:videoAsset];
AVMutableVideoComposition *videoComposition=[AVMutableVideoComposition videoComposition];
videoComposition.frameDuration=CMTimeMake(1, 30); //frames per seconds
videoComposition.renderSize = videoAsset.naturalSize;
//videoComposition.renderScale = 1.0;
videoComposition.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];
AVMutableCompositionTrack *videoTrack = [[mixComposition tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; //was AVAssetTrack *videoTrack
AVMutableVideoCompositionLayerInstruction *layerInstruction = [self layerInstructionAfterFixingOrientationForAsset:videoAsset
forTrack:videoTrack atTime:videoAsset.duration];
AVMutableVideoCompositionLayerInstruction *layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack];
[layerInstruction setTransform:videoTrack.preferredTransform atTime:kCMTimeZero];
[layerInstruction setOpacity:0.0 atTime:videoAsset.duration];
AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, [videoAsset duration]);
instruction.layerInstructions = [NSArray arrayWithObject:layerInstruction];
videoComposition.instructions = [NSArray arrayWithObject:instruction];
旋轉設置軌道
+(AVMutableComposition *)compositionVideoTrackAssetUrl:(AVURLAsset*)assetUrl{
AVMutableComposition* mixComposition = [AVMutableComposition composition];
AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
AVMutableCompositionTrack *compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
AVAssetTrack *clipVideoTrack = [[assetUrl tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
AVAssetTrack *clipAudioTrack = [[assetUrl tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
[compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, assetUrl.duration) ofTrack:clipVideoTrack atTime:kCMTimeZero error:nil];
[compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, assetUrl.duration) ofTrack:clipAudioTrack atTime:kCMTimeZero error:nil];
[compositionVideoTrack setPreferredTransform:[[[assetUrl tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] preferredTransform]];
return mixComposition;
}
so after researching all stack overflow discussions about export rotation,
I'm still having video rotation to size issue...
你解決呢? –
似乎stackoverflow上的解決方案已過時。 –
@RoiMulia,請看我的回答 –