gpt4 book ai didi

ios - 调整视频大小并保持 1 :1 scale

转载 作者:可可西里 更新时间:2023-10-31 23:49:03 29 4
gpt4 key购买 nike

我有一个 800x800 的视频,我想将此视频转换为 375x375。

它应该是这样的: enter image description here

但是最后的结果是这样的: enter image description here

我做错了什么?

我的代码:

  let mixComposition = AVMutableComposition()

// 2 - Create video tracks
let compositionVideoTrack = mixComposition.addMutableTrackWithMediaType(AVMediaTypeVideo, preferredTrackID: Int32(kCMPersistentTrackID_Invalid))
let clipVideoTrack = videoAsset.tracksWithMediaType(AVMediaTypeVideo)[0]

do {
try compositionVideoTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, videoAsset.duration), ofTrack: clipVideoTrack, atTime: kCMTimeZero)
} catch _ {

}
clipVideoTrack.preferredTransform

// Video size
let videoSize = clipVideoTrack.naturalSize
let scale = imageLayer.frame.size.width / videoSize.width
let scaledSize = CGSizeMake(videoSize.width * scale, videoSize.height * scale)
let transform = CGAffineTransformMakeScale(scale, scale)

// sorts the layer in proper order and add title layer
let parentLayer = CALayer()
let videoLayer = CALayer()
parentLayer.frame = CGRectMake(0, 0, imageLayer.frame.size.width, imageLayer.frame.size.height)
videoLayer.frame = CGRectMake(0, 0, imageLayer.frame.size.width, imageLayer.frame.size.height)
parentLayer.addSublayer(videoLayer)
parentLayer.addSublayer(imageLayer)

let videoComp = AVMutableVideoComposition()
videoComp.renderSize = scaledSize
videoComp.frameDuration = CMTimeMake(1, 30)
videoComp.animationTool = AVVideoCompositionCoreAnimationTool(postProcessingAsVideoLayer: videoLayer, inLayer: parentLayer)

/// instruction
let instruction = AVMutableVideoCompositionInstruction()
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, mixComposition.duration)
//let videoTrack = mixComposition.tracksWithMediaType(AVMediaTypeVideo)[0]

//let layerInstruction = self.videoCompositionInstructionForTrack(compositionVideoTrack, asset: videoAsset)

let layerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: clipVideoTrack)
layerInstruction.setTransform(transform, atTime: kCMTimeZero)

instruction.layerInstructions = [layerInstruction]
videoComp.instructions = [instruction]

最佳答案

这是由于之间的微小计算差异造成的

let scaledSize = CGSizeMake(videoSize.width * scale, videoSize.height * scale)
let transform = CGAffineTransformMakeScale(scale, scale)

解决方案是将缩放后的尺寸减小 2 px

let scaledSize = CGSizeMake(videoSize.width * scale - 2, videoSize.height * scale - 2)
let transform = CGAffineTransformMakeScale(scale, scale)

关于ios - 调整视频大小并保持 1 :1 scale,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37128739/

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