使用图像(宽高比填充)和视频制作 AVMutableComposition

问题描述 投票:0回答:1

我正在尝试使用尺寸始终为 CGSize(375, 667) 的图像制作新视频,但视频尺寸不同,内容模式为“aspectFit”。问题是我无法弄清楚如何使整个视频组合具有正确的尺寸(即图像尺寸),而是视频的自然尺寸和一堆奇怪的结果。 (编辑说明:视频应该在视图中居中,就像普通的aspectFit 对 UIImageView 所做的那样......)

这是我想要实现的目标的一个例子...请注意,我已经有了图像和视频,所有我需要做的就是用它们制作新视频。这就是它的样子(如图所示):

desired result image here --

这是我当前正在尝试的代码,其中包含“背景”的占位符图像(资产中随机的 375、667 图像..):我认为我可能在评论“重要的东西”周围做的事情不正确......但我目前无法弄清楚:/

  func makeVideo(fromVideoAt videoURL: URL, forName name: String, onComplete: @escaping (URL?) -> Void) {
    let asset = AVURLAsset(url: videoURL)
    let composition = AVMutableComposition()
    
    guard
      let compositionTrack = composition.addMutableTrack(
        withMediaType: .video, preferredTrackID: kCMPersistentTrackID_Invalid),
      let assetTrack = asset.tracks(withMediaType: .video).first
      else {
        print("Something is wrong with the asset.")
        onComplete(nil)
        return
    }
    
    do {
      let timeRange = CMTimeRange(start: .zero, duration: asset.duration)
      try compositionTrack.insertTimeRange(timeRange, of: assetTrack, at: .zero)
      
      if let audioAssetTrack = asset.tracks(withMediaType: .audio).first,
        let compositionAudioTrack = composition.addMutableTrack(withMediaType: .audio, preferredTrackID: kCMPersistentTrackID_Invalid) {
        try compositionAudioTrack.insertTimeRange(timeRange, of: audioAssetTrack, at: .zero)
      }
    } catch {
      print(error)
      onComplete(nil)
      return
    }
    
    compositionTrack.preferredTransform = assetTrack.preferredTransform
    let videoInfo = orientation(from: assetTrack.preferredTransform)
    
    
    
    //Important stuff potentially? general below:
    
    let videoSize: CGSize
    if videoInfo.isPortrait {
      videoSize = CGSize(width: 720, height: 1280)
    } else {
      videoSize = CGSize(width: 720, height: 1280) //720.0, 1280 tiktok default..?
    }
    
    
    
    //the Background image:
    let backgroundLayer = CALayer()
    backgroundLayer.frame = CGRect(origin: .zero, size: videoSize) //videosize
    
    backgroundLayer.contents = UIImage(named: "background")?.cgImage
    backgroundLayer.contentsGravity = .resizeAspectFill
    backgroundLayer.backgroundColor = UIColor.red.cgColor

    //Video layer:
    let videoLayer = CALayer()
//    videoLayer.frame = CGRect(origin: .zero, size: CGSize(width: composition.naturalSize.width, height: composition.naturalSize.height)) //videosize
    videoLayer.backgroundColor = UIColor.yellow.cgColor
        
    print(composition.naturalSize, "<-- composition.naturalSize")
    videoLayer.frame = CGRect(origin: .zero, size: CGSize(width: videoSize.width, height: composition.naturalSize.height))//CGRect(x: 0, y: 0, width: videoSize.width, height: composition.naturalSize.height)

    
    //OutPutlayer putting the together?
    let outputLayer = CALayer()
    outputLayer.frame = CGRect(origin: .zero, size: CGSize(width: 720, height: 1280)) //videosize
    outputLayer.backgroundColor = UIColor.white.cgColor
    outputLayer.addSublayer(backgroundLayer)
    outputLayer.addSublayer(videoLayer)
//    outputLayer.addSublayer(overlayLayer)
    
    let videoComposition = AVMutableVideoComposition()
    videoComposition.renderSize = videoSize
    videoComposition.frameDuration = CMTime(value: 1, timescale: 30)
    videoComposition.animationTool = AVVideoCompositionCoreAnimationTool(postProcessingAsVideoLayer: videoLayer, in: outputLayer)
    
    
    //Setting Up Instructions
    let instruction = AVMutableVideoCompositionInstruction()
    instruction.timeRange = CMTimeRange(start: .zero, duration: composition.duration)
    videoComposition.instructions = [instruction]
    let layerInstruction = compositionLayerInstruction(for: compositionTrack, assetTrack: assetTrack)
    instruction.layerInstructions = [layerInstruction]
    
    
    
    //EXPORTING
    guard let export = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetHighestQuality) else {
        print("Cannot create export session.")
        onComplete(nil)
        return
    }
    
    let videoName = UUID().uuidString
    let exportURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(videoName).appendingPathExtension("mp4")
    
    export.videoComposition = videoComposition
    export.outputFileType = .mov
    export.outputURL = exportURL
    
    export.exportAsynchronously {
      DispatchQueue.main.async {
        switch export.status {
        case .completed:
          onComplete(exportURL)
        default:
          print("Something went wrong during export.")
          print(export.error ?? "unknown error")
          onComplete(nil)
          break
        }
      }
    }
  }
ios swift avfoundation avmutablecomposition
1个回答
-2
投票

尝试使用此代码https://github.com/vabe1337/VBVideoEditor。它像 TikTok、Instagram 一样渲染视频。

© www.soinside.com 2019 - 2024. All rights reserved.