更新时间:2022-01-17 08:21:32
Swift 3对我来说就像一个魅力!
Swift 3 Worked like a charm for me!
func exportVideoAsset(_ asset: PHAsset) {
let filename = UUID().uuidString.appending(".mp4") // setting random file name
let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
do {
var fileurl = try documentsUrl.absoluteString.appending(filename).asURL()
print("exporting video to ", fileurl)
fileurl = fileurl.standardizedFileURL
let options = PHVideoRequestOptions()
options.deliveryMode = .highQualityFormat
options.isNetworkAccessAllowed = true
// remove any existing file at that location
do {
try FileManager.default.removeItem(at: fileurl)
}
catch {
// most likely, the file didn't exist. Don't sweat it
}
PHImageManager.default().requestExportSession(forVideo: asset, options: options, exportPreset: AVAssetExportPresetHighestQuality) {
(exportSession: AVAssetExportSession?, _) in
if exportSession == nil {
print("COULD NOT CREATE EXPORT SESSION")
return
}
exportSession!.outputURL = fileurl
exportSession!.outputFileType = AVFileTypeMPEG4 //file type encode goes here, you can change it for other types
print("GOT EXPORT SESSION")
exportSession!.exportAsynchronously() {
print("EXPORT DONE")
}
print("progress: \(exportSession!.progress)")
print("error: \(exportSession!.error)")
print("status: \(exportSession!.status.rawValue)")
}
}
catch {
// something may happend here, like no disk space
}
}