ios – 在Swift 3中为视频添加叠加层

ios – 在Swift 3中为视频添加叠加层,第1张

概述我正在学习AVFoundation,我在尝试使用 Swift 3中的叠加图像保存视频时遇到问题.使用AVMutableComposition我可以将图像添加到视频中但是视频放大而不是将自己约束到视频拍摄的肖像大小.我试过: >通过AVAssetTrack设置自然大小. >在AVMutableVideoComposition renderFrame中将视频约束为纵向大小. >将新视频边界锁定到录制的 我正在学习AVFoundation,我在尝试使用 Swift 3中的叠加图像保存视频时遇到问题.使用AVMutableComposition我可以将图像添加到视频中但是视频放大而不是将自己约束到视频拍摄的肖像大小.我试过:

>通过AVAssetTrack设置自然大小.
>在AVMutableVIDeoComposition renderFrame中将视频约束为纵向大小.
>将新视频边界锁定到录制的视频宽度和高度.

下面的代码与我需要帮助的问题不同.我正在尝试添加的图像覆盖了整个纵向视图,并且边缘周围都有边框.该应用程序也只允许肖像.

func processVIDeoWithWatermark(vIDeo: AVURLAsset,watermark: UIImage,completion: @escaPing (Bool) -> VoID) {    let composition = AVMutableComposition()    let asset = AVURLAsset(url: vIDeo.url,options: nil)    let track =  asset.tracks(withMediaType: AVMediaTypeVIDeo)    let vIDeoTrack:AVAssetTrack = track[0] as AVAssetTrack    let timerange = CMTimeRangeMake(kCMTimeZero,asset.duration)    let compositionVIDeoTrack:AVMutableCompositionTrack = composition.addMutableTrack(withMediaType: AVMediaTypeVIDeo,preferredTrackID: CMPersistentTrackID())    do {        try compositionVIDeoTrack.insertTimeRange(timerange,of: vIDeoTrack,at: kCMTimeZero)        compositionVIDeoTrack.preferredtransform = vIDeoTrack.preferredtransform    } catch {        print(error)    }//      let compositionAudioTrack:AVMutableCompositionTrack = composition.addMutableTrack(withMediaType: AVMediaTypeAudio,preferredTrackID: CMPersistentTrackID())//      //      for audioTrack in asset.tracks(withMediaType: AVMediaTypeAudio) {//          do {//              try compositionAudioTrack.insertTimeRange(audioTrack.timeRange,of: audioTrack,at: kCMTimeZero)//          } catch {//              print(error)//          }//          //      }//          let size = vIDeoTrack.naturalSize    let watermark = watermark.cgImage    let watermarklayer = CALayer()    watermarklayer.contents = watermark    watermarklayer.frame = CGRect(x: 0,y: 0,wIDth: screenWIDth,height: screenHeight)    watermarklayer.opacity = 1    let vIDeolayer = CALayer()    vIDeolayer.frame = CGRect(x: 0,height: screenHeight)    let parentlayer = CALayer()    parentlayer.frame = CGRect(x: 0,wIDth: size.wIDth,height: size.height)    parentlayer.addSublayer(vIDeolayer)    parentlayer.addSublayer(watermarklayer)    let layercomposition = AVMutableVIDeoComposition()    layercomposition.frameDuration = CMTimeMake(1,30)    layercomposition.renderSize = CGSize(wIDth: screenWIDth,height: screenHeight)    layercomposition.renderScale = 1.0    layercomposition.animationTool = AVVIDeoCompositionCoreAnimationTool(postProcessingAsVIDeolayer: vIDeolayer,in: parentlayer)    let instruction = AVMutableVIDeoCompositionInstruction()    instruction.timeRange = CMTimeRangeMake(kCMTimeZero,composition.duration)    let vIDeotrack = composition.tracks(withMediaType: AVMediaTypeVIDeo)[0] as AVAssetTrack    let layerinstruction = AVMutableVIDeoCompositionLayerInstruction(assetTrack: vIDeotrack)    layerinstruction.settransform(vIDeoTrack.preferredtransform,at: kCMTimeZero)    instruction.layerInstructions = [layerinstruction]    layercomposition.instructions = [instruction]    let filePath = NstemporaryDirectory() + self.filename()    let movIEUrl = URL(fileURLWithPath: filePath)    guard let assetExport = AVAssetExportSession(asset: composition,presetname:AVAssetExportPresetHighestQuality) else {return}    assetExport.vIDeoComposition = layercomposition    assetExport.outputfileType = AVfileTypeMPEG4    assetExport.outputURL = movIEUrl    assetExport.exportAsynchronously(completionHandler: {        switch assetExport.status {        case .completed:            print("success")            print(vIDeo.url)            self.saveVIDeoToUserlibrary(fileURL: movIEUrl,completion: { (success,error) in                if success {                    completion(true)                } else {                    completion(false)                }            })            break        case .cancelled:            print("cancelled")            break        case .exporting:            print("exporting")            break        case .Failed:            print(vIDeo.url)            print("Failed: \(assetExport.error!)")            break        case .unkNown:            print("unkNown")            break        case .waiting:            print("waiting")            break        }    })}
解决方法 如果视频图层应填充父图层,则您的vIDeolayer的帧不正确.您需要将大小设置为size而不是screenSize. 总结

以上是内存溢出为你收集整理的ios – 在Swift 3中为视频添加叠加层全部内容,希望文章能够帮你解决ios – 在Swift 3中为视频添加叠加层所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1001411.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存