我正在按照以下代码进行方形视频合成
func completeWithVideoAtURL(input: NSURL) {
let asset = AVAsset(url: input as URL)
let output = NSURL(fileURLWithPath: NSHomeDirectory() + "/Documents/Video.mp4")
let session = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetMediumQuality)!
session.videoComposition = self.squareVideoCompositionForAsset(asset: asset)
session.outputURL = output as URL
session.outputFileType = AVFileTypeMPEG4
session.shouldOptimizeForNetworkUse = true
session.exportAsynchronously(completionHandler: { () -> Void in
DispatchQueue.main.async(execute: { () -> Void in
// do something with the output
print("\(output)")
PHPhotoLibrary.shared().performChanges({
PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: output as URL)
}) { saved, error in
if saved {
print("saved to gallery")
}
}
})
})
}
func squareVideoCompositionForAsset(asset: AVAsset) -> AVVideoComposition {
let track = asset.tracks(withMediaType: AVMediaTypeVideo)[0]
let length = max(track.naturalSize.width, track.naturalSize.height)
var transform = track.preferredTransform
let size = track.naturalSize
let scale: CGFloat = (transform.a == -1 && transform.b == 0 && transform.c == 0 && transform.d == -1) ? -1 : 1 // check for inversion
transform = transform.translatedBy(x: scale * -(size.width - length) / 2, y: scale * -(size.height - length) / 2)
let transformer = AVMutableVideoCompositionLayerInstruction(assetTrack: track)
transformer.setTransform(transform, at: kCMTimeZero)
let instruction = AVMutableVideoCompositionInstruction()
instruction.timeRange = CMTimeRange(start: kCMTimeZero, duration: kCMTimePositiveInfinity)
instruction.layerInstructions = [transformer]
let composition = AVMutableVideoComposition()
composition.frameDuration = CMTime(value: 1, timescale: 30)
composition.renderSize = CGSize(width: length, height: length)
composition.instructions = [instruction]
return composition
}
从 squareVideoCompositionForAsset()函数中,我在 track.naturalSize.width 之间取长度的最大值&安培; track.naturalSize.height 因为我不想裁剪视频的任何部分内容。如果我拿最小值,对于肖像视频它会裁剪上限和下限。视频的下半部分和风景视频中的一些左右播放。视频的右侧部分。
视频左侧。可以将视频居中吗?对于长时间的解释,任何帮助都会很棒并且很抱歉。
答案 0 :(得分:3)
而不是这一行
让比例:CGFloat =(transform.a == -1&& transform.b == 0&& transform.c == 0&& transform.d == -1)? -1:1
我刚用过这个
var scale = CGFloat() if (transform.a == 0 && transform.b == 1 && transform.c == -1 && transform.d == 0) { scale = -1 } else if (transform.a == 0 && transform.b == -1 && transform.c == 1 && transform.d == 0) { scale = -1 } else if (transform.a == 1 && transform.b == 0 && transform.c == 0 && transform.d == 1) { scale = 1 } else if (transform.a == -1 && transform.b == 0 && transform.c == 0 && transform.d == -1) { scale = 1 }
它就像一个魅力
答案 1 :(得分:0)
Swift 4.2
import random
my_dict = {
"choice a" : 1, # will in this case be chosen 1/3 of the time
"choice b" : 2, # will in this case be chosen 2/3 of the time
}
choice = random.choices(*zip(*my_dict.items()))[0]