Перекрыть несколько видео в Swift - PullRequest
0 голосов
/ 27 июня 2018

Я пытался преобразовать объективный код C в swift 4. Код добавляет видео поверх другого видео http://www.theappguruz.com/blog/ios-overlap-multiple-videos этот код объективного c Я получил эту ошибку: "(Ошибка Домена = AVFoundationErrorDomain Code = -11841" Операция остановлена ​​"UserInfo = {NSLocalizedFailureReason = Видео не может быть скомпоновано., NSLocalizedDescription = Операция остановлена, NSUnderlyingError = 0x604000646cf0 {Ошибка Домена = NSOSStatusErrorDomain) Code = nu1717 () }) «

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")
        }
    })

}

1 Ответ

0 голосов
/ 28 июня 2018

У меня есть решение моей проблемы. Мне просто нужно добавить layerInstractions в свойства AVMutableVideoCompositionInstruction, и код будет работать нормально. эта строка кода решит проблему:

    mainInstraction.layerInstructions = [firstLayerInstraction , secondLayerInstraction]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...