ios 为什么通过网络发送视频时不修剪(使用AVAssetExportSession)?

h22fl7wq  于 11个月前  发布在  iOS
关注(0)|答案(1)|浏览(149)

我想使用Network框架将视频发送到另一个iOS设备。在发送视频之前,我添加了一个逻辑,以修剪到视频的特定开始和结束时间。逻辑如下所示:

func export(_ asset: AVAsset, to outputMovieURL: URL, startTime: CMTime, endTime: CMTime, composition: AVVideoComposition, completion: @escaping (Bool) -> Void) {

    //Create trim range
    let timeRange = CMTimeRangeFromTimeToTime(start: startTime, end: endTime)
    print("startTime \(startTime.seconds) endTime \(endTime.seconds)")

    //delete any old file
    do {
        try FileManager.default.removeItem(at: outputMovieURL)
    } catch {
        print("Could not remove file \(error.localizedDescription)")
    }

    var exporter = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetHighestQuality)

    //configure exporter
    exporter?.videoComposition = composition
    exporter?.outputURL = outputMovieURL
    exporter?.outputFileType = .mov
    exporter?.timeRange = timeRange
    exporter?.shouldOptimizeForNetworkUse = true

    //export
    exporter?.exportAsynchronously(completionHandler: { [weak exporter] in
        DispatchQueue.main.async {
            if let error = exporter?.error {
                print("Failed \(error.localizedDescription)")
                completion(false)
            } else {
                print("Video saved to \(outputMovieURL)")
                completion(true)
            }
        }
    })
}

字符串
但不幸的是,在发送后的另一端收到的视频没有修剪.我试图保存在发送方使用PHAssetChangeRequest的视频和它保存修剪的视频.我错过了我的逻辑在这里的东西?任何帮助将不胜感激.谢谢!
编辑
修剪视频的逻辑:

func autoTrimBeforeSending(completion: @escaping () -> Void) {
        let options: [String : Any] = [AVURLAssetPreferPreciseDurationAndTimingKey: false]
        let videoURLAsset = AVURLAsset(url: theVideoURL, options: options)
        let videoDurationInSeconds = videoURLAsset.duration.seconds
        var startTimeSec = 0.0
        let startCMTime = CMTimeMakeWithSeconds(startTimeSec, preferredTimescale: 600)
        let composition = AVMutableVideoComposition(propertiesOf: videoURLAsset)
        let endTime = CMTimeMakeWithSeconds(videoDurationInSeconds, preferredTimescale: 600)
        
        startTimeSec = videoDurationInSeconds - VideoConstants.autoTrimSec // The value is 4, so basically I want just the 
        
        VideoUtils.export(videoURLAsset, to: theVideoURL, startTime: startCMTime, endTime: endTime, composition: composition, completion: { _ in
            completion()
        })
    }


调用它并发送视频:

autoTrimBeforeSending(completion: {
            networkSession.sendVideo(from: theVideoURL)
        })

z9smfwbn

z9smfwbn1#

我想是出口商的问题。
试试这个然后告诉我。

exporter?.exportAsynchronously(completionHandler: { [weak exporter] in
                DispatchQueue.main.async {
                    switch exporter.status{
                    case .failed:
                        print("failed \(String(describing: exporter.error))")
                    case .cancelled:
                        print("cancelled \(String(describing: exporter.error))")
                    case .unknown:
                        print("unknown\(String(describing: exporter.error))")
                    case .waiting:
                        print("waiting\(String(describing: exporter.error))")
                    case .exporting:
                        print("exporting\(String(describing: exporter.error))")
                    default:
                        print("complete")
                        //Do what you want
                    }
                }
            })

字符串

相关问题