gpt4 book ai didi

ios - 视频不使用 AVMutableVideoCompositionLayerInstruction 旋转

转载 作者:搜寻专家 更新时间:2023-10-30 22:35:53 26 4
gpt4 key购买 nike

我正在尝试合并使用相机作为 UIImagePickerController 录制后获得的两个视频。我已成功将视频合并为一个,但视频的方向存在一些问题。

正如我对 UIImagePickerController 的理解,所有视频都是横向拍摄的,这意味着纵向录制的视频会旋转 90°。

每次录制后,我都会将新视频添加到数组中

func imagePickerController(picker: UIImagePickerController!, didFinishPickingMediaWithInfo info:NSDictionary) {
let tempImage = info[UIImagePickerControllerMediaURL] as NSURL
videos.append(tempImage)
let pathString = tempImage.relativePath
self.dismissViewControllerAnimated(true, completion: {})
}

然后当我想合并时,我浏览每个视频并创建一条指令并将指令添加到另一个数组

var composition = AVMutableComposition()
let trackVideo:AVMutableCompositionTrack = composition.addMutableTrackWithMediaType(AVMediaTypeVideo, preferredTrackID: CMPersistentTrackID())
let trackAudio:AVMutableCompositionTrack = composition.addMutableTrackWithMediaType(AVMediaTypeAudio, preferredTrackID: CMPersistentTrackID())
var insertTime = kCMTimeZero

for i in 0...(videos.count-1){
let moviePathUrl = videos[i]
let sourceAsset = AVURLAsset(URL: moviePathUrl, options: nil)

let tracks = sourceAsset.tracksWithMediaType(AVMediaTypeVideo)
let audios = sourceAsset.tracksWithMediaType(AVMediaTypeAudio)

if tracks.count > 0{
var videoDuration = CMTimeRangeMake(kCMTimeZero, sourceAsset.duration);

let assetTrack:AVAssetTrack = tracks[0] as AVAssetTrack
let assetTrackAudio:AVAssetTrack = audios[0] as AVAssetTrack

trackVideo.insertTimeRange(videoDuration, ofTrack: assetTrack, atTime: insertTime, error: nil)
trackAudio.insertTimeRange(videoDuration, ofTrack: assetTrackAudio, atTime: insertTime, error: nil)

//Rotate
var rotater = AVMutableVideoCompositionLayerInstruction(assetTrack: assetTrack)
rotater.setTransform(assetTrack.preferredTransform, atTime: insertTime)
rotater.setOpacity(0.0, atTime: CMTimeAdd(insertTime, sourceAsset.duration))
instructions.append(rotater)

//Resize
var resizer = AVMutableVideoCompositionLayerInstruction(assetTrack: assetTrack)
resizer.setCropRectangle(CGRectMake(0, 0, 300, 300), atTime: insertTime)
instructions.append(resizer)

insertTime = CMTimeAdd(insertTime, sourceAsset.duration)

}
}

当我创建所有指令后,我将它们添加到主指令并创建导出 session 。

var instruction = AVMutableVideoCompositionInstruction();
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, insertTime);

instruction.layerInstructions = instructions;
var mainCompositionInst = AVMutableVideoComposition()
mainCompositionInst.instructions = NSArray(object: instruction)

mainCompositionInst.frameDuration = CMTimeMake(1, 60);
mainCompositionInst.renderSize = CGSizeMake(300, 300);

var exporter = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetHighestQuality)
exporter.videoComposition = mainCompositionInst;

我错过了什么?

最佳答案

尝试在层指令的初始化程序中使用 trackVideo,这样它将使用 AVMutableCompositionTracktrackID 而不是源 Assets 的 trackID

更新:

你只需要一个AVMutableVideoCompositionLayerInstruction,所以在循环之前用AVMutableCompositionTrack作为参数声明它。然后在循环的每次迭代中,为您正在使用的当前视频 Assets 设置图层指令的必要属性(转换、裁剪矩形)。您正在控制合成轨道中的视频内容在每次插入时的显示方式。

最后,将单层指令放在指令数组中,并在视频合成中使用它。

var layerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: trackVideo)
for i in 0...(videos.count-1){
//...your other code here

layerInstruction.setTransform(assetTrack.preferredTransform, atTime: insertTime)
layerInstruction.setCropRectangle(CGRectMake(0, 0, 300, 300), atTime: insertTime)
}

var instruction = AVMutableVideoCompositionInstruction();
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, insertTime);

instruction.layerInstructions = NSArray(object: layerInstruction);
var mainCompositionInst = AVMutableVideoComposition()
mainCompositionInst.instructions = NSArray(object: instruction)

关于ios - 视频不使用 AVMutableVideoCompositionLayerInstruction 旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27627610/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com