AVFoundation -Videos merge but only last video plays

瘦欲@ 提交于 2020-01-03 17:28:00

问题


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.

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.

Where am I going wrong at?

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

回答1:


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.

The problem was here:

func videoFileLocation() -> String {
    return NSTemporaryDirectory().appending("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")
}


来源:https://stackoverflow.com/questions/58323683/avfoundation-videos-merge-but-only-last-video-plays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!