我一直在尝试让肖像视频转换为可以在播放器中心放置的方面,同时保持屏幕的其余部分为空。
这里是代码: 几件事: self.videoRenderSize = CGSize(width: 1920.0, height: 1080.0) videoTrackSize 可能是 (960,720), (1280,720), (1920,1080) (虽然全是纵向,所以宽度是高度和反之亦然)
let instruction = AVMutableVideoCompositionInstruction()
instruction.timeRange = passThroughTimeRanges[i]
let layerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: currentTrack)
let assetObject = self.assetsList[i]["assetVideo"] as! Asset
let asset = assetObject.movieAsset as AVAsset
let videoTrack:AVAssetTrack = asset.tracksWithMediaType(AVMediaTypeVideo)[0]
let videoTrackSize = videoTrack.naturalSize
let preferredTransform = videoTrack.preferredTransform
let videoOrientationMode = self.isVideoPortrait(asset)
print(preferredTransform)
print(videoTrackSize)
switch videoOrientationMode {
case "Portrait" :
if self.videoRenderSize.height > videoTrackSize.height {
}else if self.videoRenderSize.height == videoTrackSize.height {
}else {
}
break
case "PortraitUpsideDown":
break
case "LandscapeRight", "LandscapeLeft":
let heightScale = CGFloat(self.videoRenderSize.height / videoTrackSize.height)
let widthScale = CGFloat(self.videoRenderSize.width / videoTrackSize.width)
layerInstruction.setTransform(CGAffineTransformMakeScale(widthScale,heightScale), atTime: instruction.timeRange.start)
break
default:
print("Unknown Orientation")
}
instruction.layerInstructions = [layerInstruction]
compositionInstructions.append(instruction)
}
videoComposition.instructions = compositionInstructions
videoComposition.renderSize = self.videoRenderSize
videoComposition.frameDuration = CMTimeMake(1, 30)
videoComposition.renderScale = 1.0 // This is a iPhone only option.
isVideoPortrait 函数的代码
func isVideoPortrait(asset : AVAsset) -> String{
var videoOrientation : String = ""
let tracks : NSArray = asset.tracksWithMediaType(AVMediaTypeVideo)
if(tracks.count > 0) {
let videoTrack : AVAssetTrack = tracks[0] as! AVAssetTrack
let t = videoTrack.preferredTransform
// Portrait
if(t.a == 0 && t.b == 1.0 && t.c == -1.0 && t.d == 0 || t.a == 0 && t.b == -1.0 && t.c == 1.0 && t.d == 0)
{
videoOrientation = "Portrait"
}
// PortraitUpsideDown
if(t.a == 0 && t.b == -1.0 && t.c == 1.0 && t.d == 0) {
videoOrientation = "PortraitUpsideDown"
}
// LandscapeRight
if(t.a == 1.0 && t.b == 0 && t.c == 0 && t.d == 1.0)
{
videoOrientation = "LandscapeRight"
}
// LandscapeLeft
if(t.a == -1.0 && t.b == 0 && t.c == 0 && t.d == -1.0)
{
videoOrientation = "LandscapeLeft"
}
}
return videoOrientation
}
什么是转换层指令,用于确保视频将在中心转换并可以像上面附加的图像一样放置。