这是一个快速而肮脏的 Swift 5 示例,说明如何从UIImagePickerController
extension ViewController: UIImagePickerControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let mediaType = info[.mediaType] as! String
dismiss(animated: true) { [weak self] in
// Handle a movie capture
if mediaType == kUTTypeMovie as String {
guard let videoURL = info[.mediaURL] as? URL else {
SwiftyBeaver.error("Could not get URL for movie")
return
}
let editingEnd = UIImagePickerController.InfoKey(rawValue: "_UIImagePickerControllerVideoEditingEnd")
let editingStart = UIImagePickerController.InfoKey(rawValue: "_UIImagePickerControllerVideoEditingStart")
let startMilliseconds: Double?
let endMilliseconds: Double?
if let start = info[editingStart] as? Double, let end = info[editingEnd] as? Double {
startMilliseconds = start
endMilliseconds = end
} else {
startMilliseconds = nil
endMilliseconds = nil
}
let alert = UIAlertController(title: "Creating", message: "File is being processed", preferredStyle: .alert)
self?.present(alert, animated: true)
self?.process(srcVideoURL: videoURL, startSeconds: startMilliseconds, endSeconds: endMilliseconds) { (error) in
DispatchQueue.main.async {
if let error = error {
alert.title = "Whoops"
alert.message = "\(error)"
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { _ in
self?.dismiss(animated: true, completion: nil)
}))
return
}
self?.dismiss(animated: true, completion: nil)
}
}
}
}
}
}
enum VideoError: Error {
case error(message: String)
}
extension ViewController {
func process(srcVideoURL: URL, startSeconds: Double?, endSeconds: Double?, completed: @escaping (_ error: Error?) -> ()) {
DispatchQueue.global(qos: .userInitiated).async {
let dstVideoURL: URL // some URL for the destination
do {
try self.handleNewVideo(srcVideoURL: srcVideoURL, dstVideoURL: dstVideoURL, startSeconds: startSeconds, endSeconds: endSeconds)
completed(nil)
} catch {
completed(error)
}
}
}
func handleNewVideo(srcVideoURL: URL, dstVideoURL: URL, startSeconds: Double?, endSeconds: Double?) throws {
guard let start = startSeconds, let end = endSeconds else {
print("No video editing information. Copying file.")
try FileManager.default.moveItem(at: srcVideoURL, to: dstVideoURL)
return
}
print("Video editing information. Processing start \(start) end \(end).")
let videoAsset = AVURLAsset(url: srcVideoURL)
let exportSession = AVAssetExportSession(asset: videoAsset, presetName: AVAssetExportPresetHighestQuality)!
exportSession.outputURL = dstVideoURL
exportSession.outputFileType = AVFileType.mov
let timeRange = CMTimeRange(start: CMTime(seconds: start, preferredTimescale: 1000), duration: CMTime(seconds: end - start, preferredTimescale: 1000))
exportSession.timeRange = timeRange
var error: Error? = nil
let dispatchGroup = DispatchGroup()
dispatchGroup.enter()
exportSession.exportAsynchronously(completionHandler: {
switch exportSession.status {
case .completed:
break
case .failed:
error = exportSession.error ?? VideoError.error(message: "Unknown failed error")
case .cancelled:
error = exportSession.error ?? VideoError.error(message: "Video Cancelled")
case .exporting:
error = exportSession.error ?? VideoError.error(message: "Video still exporting")
case .unknown:
error = exportSession.error ?? VideoError.error(message: "Unknown unknown error")
case .waiting:
error = exportSession.error ?? VideoError.error(message: "Waiting error")
@unknown default:
error = exportSession.error ?? VideoError.error(message: "Future error")
}
dispatchGroup.leave()
})
dispatchGroup.wait()
if let error = error {
throw error
}
}
}