AVFoundation-视频合并,但仅最后一个视频播放 [英] AVFoundation -Videos merge but only last video plays

查看:651
本文介绍了AVFoundation-视频合并,但仅最后一个视频播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个[AVAsset]()数组.每当我以不同的时长录制不同的视频时,下面的代码都会将所有时长合并为1个视频,但它只会循环播放最后一个视频.

I have an array of [AVAsset](). Whenever I record different videos at different durations the below code merges all the durations into 1 video but it will only play the last video in a loop.

例如video1是1分钟,表示正在walking狗; video2是1分钟,表示正在飞鸟; video3是1分钟,表示正在奔马.该视频将合并并播放3分钟,但只会显示该马连续3次每分钟跑1分钟.

For eg. video1 is 1 minute and shows a dog walking, video2 is 1 minute and shows a bird flying, video3 is 1 minute and shows a horse running. The video will merge and play for 3 minutes but it will only show the horse running for 1 minute each three consecutive times.

我要去哪里错了?

var movieFileOutput = AVCaptureMovieFileOutput()
var arrayVideos = [AVAsset]()
var videoFileUrl: URL?

// button to record video
@objc func recordButtonTapped() {

    // Stop recording
    if movieFileOutput.isRecording {

        movieFileOutput.stopRecording()

        print("Stop Recording")

     } else {

         // Start recording
         movieFileOutput.connection(with: AVMediaType.video)?.videoOrientation = videoOrientation()

         movieFileOutput.maxRecordedDuration = maxRecordDuration()

         videoFileUrl = URL(fileURLWithPath: videoFileLocation())

         if let videoFileUrlFromCamera = videoFileUrl {
            movieFileOutput.startRecording(to: videoFileUrlFromCamera, recordingDelegate: self)
         }
     }
}

func videoFileLocation() -> String {
    return NSTemporaryDirectory().appending("videoFile.mov")
}

// button to save the merged video
@objc func saveButtonTapped() {

      mergeVids()
}

// function to merge and save videos
func mergeVids() {

    let mixComposition = AVMutableComposition()

    let compositionVideoTrack = mixComposition.addMutableTrack(withMediaType: .video,
                                                               preferredTrackID: Int32(kCMPersistentTrackID_Invalid))

    compositionVideoTrack?.preferredTransform = CGAffineTransform(rotationAngle: .pi / 2)

    let soundtrackTrack = mixComposition.addMutableTrack(withMediaType: .audio,
                                                         preferredTrackID: Int32(kCMPersistentTrackID_Invalid))

    var insertTime = CMTime.zero

    for videoAsset in arrayVideos {

        do {

            try compositionVideoTrack?.insertTimeRange(CMTimeRangeMake(start: CMTime.zero,
                                                                    duration: videoAsset.duration),
                                                    of: videoAsset.tracks(withMediaType: .video)[0],
                                                    at: insertTime)

            try soundtrackTrack?.insertTimeRange(CMTimeRangeMake(start: CMTime.zero,
                                                              duration: videoAsset.duration),
                                              of: videoAsset.tracks(withMediaType: .audio)[0],
                                              at: insertTime)

            insertTime = CMTimeAdd(insertTime, videoAsset.duration)

        } catch let error as NSError {
            print("\(error.localizedDescription)")
        } 
    }

    let outputFileURL = URL(fileURLWithPath: NSTemporaryDirectory() + "merge.mp4")
    let path = outputFileURL.path
    if FileManager.default.fileExists(atPath: path) {
        try! FileManager.default.removeItem(atPath: path)
    }

    let exporter = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality)

    exporter!.outputURL = outputFileURL
    exporter!.outputFileType = AVFileType.mp4
    exporter!.shouldOptimizeForNetworkUse = true
    exporter!.exportAsynchronously { [weak self] in

        let cameraVideoURL = exporter!.outputURL!

        PHPhotoLibrary.shared().performChanges({
            PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: cameraVideoURL)
        }) { (saved, error) in

            if let error = error { return }

            if !saved { return }

            // url is saved

            self?.videoFileUrl = nil
            self?.arrayVideos.removeAll()
        }
    }
}

// AVCaptureFileOutputRecording Delegates
func fileOutput(_ output: AVCaptureFileOutput, didStartRecordingTo fileURL: URL, from connections: [AVCaptureConnection]) {

    print("+++++++++++++++Started")
    print("*****Started recording: \(fileURL)\n")
}

func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?) {

    if error == nil {

        let asset = AVAsset(url: outputFileURL)

        arrayVideos.append(asset)
        print(arrayVideos.count)

    } else {

        print("Error recording movie: \(error!.localizedDescription)")
    }

    func cleanUp() {

        let path = outputFileURL.path

        if FileManager.default.fileExists(atPath: path) {
            do {
                try FileManager.default.removeItem(atPath: path)
            } catch {
                print("Could not remove file at url: \(outputFileURL)")
            }
        }
    }
}

func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
    print("++++++Frame Drop: \(connection.description)")
}

推荐答案

感谢@alxlives测试了合并功能,并指出由于在他的计算机上正常,问题肯定出在其他地方.

Thanks to @alxlives for testing out the merge function and pointing out that since it was fine on his machine the problem must've been somewhere else.

问题出在这里

func videoFileLocation() -> String {
    return NSTemporaryDirectory().appending("videoFile.mov")
}

在使用上面的代码的recordButtonTapped中,它使用相同的"videoFile.mov"扩展名进行保存:

In the recordButtonTapped when it used the above code it kept using the same "videoFile.mov" extension:

videoFileUrl = URL(fileURLWithPath: videoFileLocation()) // <<< it gets called here every time a new video runs

if let videoFileUrlFromCamera = videoFileUrl {
    movieFileOutput.startRecording(to: videoFileUrlFromCamera, recordingDelegate: self)
}

要修复此问题,我需要使每个扩展名都唯一:

To fix it I needed to make each extension unique:

func videoFileLocation() -> String {
    let uuid = UUID().uuidString
    return NSTemporaryDirectory().appending("videoFile_\(uuid).mov")
}

这篇关于AVFoundation-视频合并,但仅最后一个视频播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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