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

问题描述 投票:0回答:1

我想使用

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)
        })
ios swift avfoundation
1个回答
0
投票

我认为出口商身份有问题。

尝试一下并告诉我。

exporter?.exportAsynchronously(completionHandler: { [weak exporter] in
                DispatchQueue.main.async {
                    switch exporter.status{
                    case .failed:
                        print("failed \(String(describing: assetExport?.error))")
                    case .cancelled:
                        print("cancelled \(String(describing: assetExport?.error))")
                    case .unknown:
                        print("unknown\(String(describing: assetExport?.error))")
                    case .waiting:
                        print("waiting\(String(describing: assetExport?.error))")
                    case .exporting:
                        print("exporting\(String(describing: assetExport?.error))")
                    default:
                        print("complete")
                        //Do what you want
                    }
                }
            })
© www.soinside.com 2019 - 2024. All rights reserved.