Swift:方形视频合成 [英] Swift: Square video composition

查看:82
本文介绍了Swift:方形视频合成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在按照以下代码进行方形视频合成

I am following the below code for square video composition

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()函数中,选择长度 max 值g>在 track.naturalSize.width track.naturalSize.height ,因为我不想裁剪视频的任何部分。如果我取最小值,则对于纵向视频,它会裁剪上方的& ;;视频的下部,对于风景视频,它裁剪了一些左&视频的右侧部分。

From the squareVideoCompositionForAsset() function I take the max value for length between track.naturalSize.width & track.naturalSize.height cause I don't want to crop any partial part of the video. If I take min value, for portrait video it cropped the upper & lower portion of the video and for landscape video it cropped some left & right portion of the video.


  • 对于风景视频,输出是可以的


  • 但对于纵向视频,输出类似于以下图像

视频左移双面。是否可以将视频居中?任何帮助将是巨大的,抱歉,请长解释。

the video gets left sided. Is it possible to center the video? Any assistance would be great and sorry for long explanation.

推荐答案

而不是此行


let scale:CGFloat =(transform.a == -1&& transform.b == 0&
transform.c == 0& ;& transform.d == -1)吗? -1:1

let scale: 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
    }


它就像一个魅力

这篇关于Swift:方形视频合成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆