我想使用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)
})
型
1条答案
按热度按时间z9smfwbn1#
我想是出口商的问题。
试试这个然后告诉我。
字符串