Iphone 如何录制视频并使其慢动作

Iphone 如何录制视频并使其慢动作,iphone,ios4,video-capture,Iphone,Ios4,Video Capture,我正在为学校开发一款iPhone应用程序,需要一些帮助。该应用程序应该录制视频,使其慢动作(约2倍),然后将其保存到照片库。到目前为止,除了如何使视频慢动作,我什么都有。我知道这是可以做到的,因为应用商店中已经有一个应用程序可以做到这一点 如何将已保存的视频拍摄到临时url并在保存到照片库之前调整速度?如果需要导出视频,则需要使用 然后将视频作为AVAsset添加到AVMutableComposition中,并使用以下内容对其进行缩放: - (void)scaleTimeRange:(CMTim

我正在为学校开发一款iPhone应用程序,需要一些帮助。该应用程序应该录制视频,使其慢动作(约2倍),然后将其保存到照片库。到目前为止,除了如何使视频慢动作,我什么都有。我知道这是可以做到的,因为应用商店中已经有一个应用程序可以做到这一点


如何将已保存的视频拍摄到临时url并在保存到照片库之前调整速度?

如果需要导出视频,则需要使用

然后将视频作为
AVAsset
添加到
AVMutableComposition
中,并使用以下内容对其进行缩放:

- (void)scaleTimeRange:(CMTimeRange)timeRange toDuration:(CMTime)duration
最后,您可以使用

导出它,这是一个OSS项目,它似乎可以很好地实现这一点,尽管我不知道它是否可以在iPhone上运行

它不只是让你的视频以0.01倍的速度播放。你可以 流畅地减速和加速您的画面,可以选择运动 变得模糊不清慢动作是如何工作的?斯洛姆莫维迪奥试图找出在哪里 像素在视频中移动(该信息称为光流), 然后使用此信息计算附加帧


我写了一个代码,使您的视频在“慢动作”,并保存在照片库。“此代码在Swift 5中工作的主要内容”。在iOS swift中创建“慢动作”视频并不容易,因为我遇到了许多“慢动作”视频,这些视频不起作用,或者其中的一些代码被贬低了。所以我终于想出了一个用Swift制作慢动作的方法。 此代码可用于120fps,但也大于此值。只需添加视频的url并使其变慢即可

下面是“我为实现慢动作而创建的代码片段”

如果这个代码有效,请给我一票

        func slowMotion(pathUrl: URL) {

    let videoAsset = AVURLAsset.init(url: pathUrl, options: nil)
    let currentAsset = AVAsset.init(url: pathUrl)

    let vdoTrack = currentAsset.tracks(withMediaType: .video)[0]
    let mixComposition = AVMutableComposition()

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

    let videoInsertError: Error? = nil
    var videoInsertResult = false
    do {
        try compositionVideoTrack?.insertTimeRange(
            CMTimeRangeMake(start: .zero, duration: videoAsset.duration),
            of: videoAsset.tracks(withMediaType: .video)[0],
            at: .zero)
        videoInsertResult = true
    } catch let videoInsertError {
    }

    if !videoInsertResult || videoInsertError != nil {
        //handle error
        return
    }


    var duration: CMTime = .zero
    duration = CMTimeAdd(duration, currentAsset.duration)
    
    
    //MARK: You see this constant (videoScaleFactor) this helps in achieving the slow motion that you wanted. This increases the time scale of the video that makes slow motion
    // just increase the videoScaleFactor value in order to play video in higher frames rates(more slowly)
    let videoScaleFactor = 2.0
    let videoDuration = videoAsset.duration
    
    compositionVideoTrack?.scaleTimeRange(
        CMTimeRangeMake(start: .zero, duration: videoDuration),
        toDuration: CMTimeMake(value: videoDuration.value * Int64(videoScaleFactor), timescale: videoDuration.timescale))
    compositionVideoTrack?.preferredTransform = vdoTrack.preferredTransform
    
    let dirPaths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).map(\.path)
    let docsDir = dirPaths[0]
    let outputFilePath = URL(fileURLWithPath: docsDir).appendingPathComponent("slowMotion\(UUID().uuidString).mp4").path
    
    if FileManager.default.fileExists(atPath: outputFilePath) {
        do {
            try FileManager.default.removeItem(atPath: outputFilePath)
        } catch {
        }
    }
    let filePath = URL(fileURLWithPath: outputFilePath)
    
    let assetExport = AVAssetExportSession(
        asset: mixComposition,
        presetName: AVAssetExportPresetHighestQuality)
    assetExport?.outputURL = filePath
    assetExport?.outputFileType = .mp4
    
    assetExport?.exportAsynchronously(completionHandler: {
        switch assetExport?.status {
        case .failed:
            print("asset output media url = \(String(describing: assetExport?.outputURL))")
            print("Export session faiied with error: \(String(describing: assetExport?.error))")
            DispatchQueue.main.async(execute: {
                // completion(nil);
            })
        case .completed:
            print("Successful")
            let outputURL = assetExport!.outputURL
            print("url path = \(String(describing: outputURL))")
            
            PHPhotoLibrary.shared().performChanges({
                PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: outputURL!)
            }) { saved, error in
                if saved {
                    print("video successfully saved in photos gallery view video in photos gallery")
                }
                if (error != nil) {
                    print("error in saing video \(String(describing: error?.localizedDescription))")
                }
            }
            DispatchQueue.main.async(execute: {
                //      completion(_filePath);
            })
        case .none:
            break
        case .unknown:
            break
        case .waiting:
            break
        case .exporting:
            break
        case .cancelled:
            break
        case .some(_):
            break
        }
    })
}

我不知道如何用它来制作视频慢动作。。。检查此链接,这可能会对您有所帮助