我正在将视频的帧频更改为快进和快退视频,但是在迅速更改帧频后如何将视频保存到照片库中。
答案 0 :(得分:0)
所以我通过以下代码解决了这个问题:-
let videoAsset = AVAsset(url: inputURL as URL) as AVAsset
let clipVideoTrack = videoAsset.tracks(withMediaType: AVMediaType.video).first! as AVAssetTrack
let composition = AVMutableComposition()
let videoCompositions = composition.addMutableTrack(withMediaType: AVMediaType.video, preferredTrackID: CMPersistentTrackID())
do {
try videoCompositions?.insertTimeRange(CMTimeRangeMake(kCMTimeZero, videoAsset.duration), of: (videoAsset.tracks(withMediaType: AVMediaType.video).first)!, at: kCMTimeZero)
} catch {
print("handle insert error")
return
}
let videoDuration = videoAsset.duration
let finalTimeScale:Int64 = (Int64(float_t(videoDuration.value) / player.rate))
print("player rate is",player.rate)
videoCompositions?.scaleTimeRange(CMTimeRangeMake(kCMTimeZero, videoDuration), toDuration: CMTimeMake(finalTimeScale, videoDuration.timescale))
let fileManager = FileManager.default
let documentDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
var outputURL = documentDirectory.appendingPathComponent("output")
do {
try fileManager.createDirectory(at: outputURL, withIntermediateDirectories: true, attributes: nil)
outputURL = outputURL.appendingPathComponent("\(String(describing: inputURL.lastPathComponent)).mp4")
}catch let error {
print(error)
}
try? fileManager.removeItem(at: outputURL)
guard let exporter = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetHighestQuality) else { return }
exporter.outputURL = outputURL
exporter.outputFileType = .mp4
exporter.exportAsynchronously {
switch exporter.status {
case .completed:
print("exported at \(outputURL)")
completion?(outputURL)
UISaveVideoAtPathToSavedPhotosAlbum(outputURL.path,nil, nil, nil)
case .failed:
print("failed \(exporter.error.debugDescription)")
case .cancelled:
print("cancelled \(exporter.error.debugDescription)")
default: break
}
}