【问题标题】:Overlap Multiple Videos in swift快速重叠多个视频
【发布时间】:2018-06-27 15:04:30
【问题描述】:

我尝试将目标 C 代码转换为 swift 4,该代码在另一个视频上添加视频 http://www.theappguruz.com/blog/ios-overlap-multiple-videos 这是目标 C 代码 我收到了这个错误: "(Error Domain=AVFoundationErrorDomain Code=-11841 "Operation Stopped" UserInfo={NSLocalizedFailureReason=无法合成视频。, NSLocalizedDescription=Operation Stopped, NSUnderlyingError=0x604000646cf0 {Error Domain=NSOSStatusErrorDomain Code=-17390 "(null)"} }) "

class func mergeVideos(firestUrl : URL , SecondUrl : URL ){

    let firstAssets = AVAsset(url: firestUrl)
    let secondAssets = AVAsset(url: SecondUrl)

    let mixComposition = AVMutableComposition()

    let firstTrack = mixComposition.addMutableTrack(withMediaType: .video, preferredTrackID: kCMPersistentTrackID_Invalid)
    let seconTrack = mixComposition.addMutableTrack(withMediaType: .video, preferredTrackID: kCMPersistentTrackID_Invalid)

    do{
  try   firstTrack?.insertTimeRange(CMTimeRangeMake(kCMTimeZero, firstAssets.duration), of: firstAssets.tracks(withMediaType: .video)[0], at: kCMTimeZero)
    try seconTrack?.insertTimeRange(CMTimeRangeMake(kCMTimeZero, secondAssets.duration), of: secondAssets.tracks(withMediaType: .video)[0], at: kCMTimeZero)


    }catch{


    }

    let mainInstraction = AVMutableVideoCompositionInstruction()

    mainInstraction.timeRange  = CMTimeRangeMake(kCMTimeZero, firstAssets.duration)

    let firstLayerInstraction = AVMutableVideoCompositionLayerInstruction(assetTrack: firstTrack!)
    let scale = CGAffineTransform(scaleX: 0.6, y: 0.6)
    let move = CGAffineTransform(translationX: 0, y: 0)
    let transform = scale.concatenating(move)
    firstLayerInstraction.setTransform(transform, at: kCMTimeZero)


    let secondLayerInstraction = AVMutableVideoCompositionLayerInstruction(assetTrack: seconTrack!)
    let secondScale = CGAffineTransform(scaleX: 0.9, y: 0.9)
    let secondMove = CGAffineTransform(translationX: 0, y: 0)
    let secondTransform = secondScale.concatenating(secondMove)
    secondLayerInstraction.setTransform(secondTransform, at: kCMTimeZero)

 let  mainCompositonInst = AVMutableVideoComposition()
    mainCompositonInst.instructions = [mainInstraction]
    mainCompositonInst.frameDuration = CMTimeMake(1, 30)
    mainCompositonInst.renderSize = CGSize(width:(firstTrack?.naturalSize.width)!, height:(firstTrack?.naturalSize.height)! )


  let saveUrl = URL(fileURLWithPath: NSHomeDirectory() + "/Documents/1.mp4")






   guard let assetExport = AVAssetExportSession(asset: mixComposition, presetName:AVAssetExportPresetHighestQuality) else {return}
    assetExport.videoComposition = mainCompositonInst
    assetExport.outputFileType = .mp4
    assetExport.outputURL = saveUrl
    assetExport.exportAsynchronously(completionHandler: {
        switch assetExport.status{
        case  AVAssetExportSessionStatus.failed:
            print("failed \(assetExport.error)")
        case AVAssetExportSessionStatus.cancelled:
            print("cancelled \(assetExport.error)")
        case AVAssetExportSessionStatus.completed:
            print("Completed")
        default:
            print("unknown")
        }
    })

}

【问题讨论】:

    标签: iphone avfoundation swift4 ios11


    【解决方案1】:

    我有解决问题的办法。 我只需要将 layerInstractions 添加到 AVMutableVideoCompositionInstruction 属性,代码就可以正常工作。 这行代码将解决问题:

        mainInstraction.layerInstructions = [firstLayerInstraction , secondLayerInstraction]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多