I’m making an attempt to trim a video from pictures app. Under is my code. The error appears to be on exportSession.exportAsynchronously
which i’m not capable of perceive why
import UIKit
import AVFoundation
import AVKit
import Photographs
import PhotosUI
class ViewController: UIViewController, PHPickerViewControllerDelegate {
func loadVideo(url: URL) -> AVAsset? {
return AVAsset(url: url)
}
func trimVideo(asset: AVAsset, startTime: CMTime, endTime: CMTime, completion: @escaping (URL?, Error?) -> Void) {
// Create an export session with the specified output URL and preset.
guard let exportSession = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetHighestQuality) else {
completion(nil, NSError(area: "com.yourapp.trimVideo", code: 1, userInfo: [NSLocalizedDescriptionKey: "Failed to create AVAssetExportSession."]))
return
}
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let outputURL = documentsDirectory.appendingPathComponent("trimmedVideo.mp4")
// Take away the prevailing file if it exists
strive? FileManager.default.removeItem(at: outputURL)
exportSession.outputURL = outputURL
exportSession.outputFileType = AVFileType.mp4
exportSession.shouldOptimizeForNetworkUse = true
// Set the time vary for trimming
let timeRange = CMTimeRangeFromTimeToTime(begin: startTime, finish: endTime)
exportSession.timeRange = timeRange
// Carry out the export
exportSession.exportAsynchronously {
swap exportSession.standing {
case .accomplished:
completion(exportSession.outputURL, nil)
case .failed:
completion(nil, exportSession.error)
case .cancelled:
completion(nil, NSError(area: "com.yourapp.trimVideo", code: 2, userInfo: [NSLocalizedDescriptionKey: "Export cancelled"]))
default:
break
}
}
}
override func viewDidLoad() {
tremendous.viewDidLoad()
// Do any extra setup after loading the view.
}
func picker(_ picker: PHPickerViewController, didFinishPicking outcomes: [PHPickerResult]) {
debugPrint("1")
picker.dismiss(animated: true,completion: nil)
debugPrint("2")
guard let supplier = outcomes.first?.itemProvider else { return }
debugPrint("3")
if supplier.hasItemConformingToTypeIdentifier(UTType.film.identifier) {
debugPrint("4")
supplier.loadItem(forTypeIdentifier: UTType.film.identifier, choices: [:]) { [self] (videoURL, error) in
debugPrint("5")
cropVideo(sourceURL1: videoURL as! URL, statTime: 10, endTime: 20)
}
}
}
@IBAction func btnClicked(_ sender: Any) {
var config = PHPickerConfiguration(photoLibrary: .shared())
config.selectionLimit = 1
config.filter = .movies
let vc = PHPickerViewController(configuration: config)
vc.delegate = self
current(vc, animated: true)
}
func cropVideo(sourceURL1: URL, statTime:Float, endTime:Float)
{
debugPrint("21")
let supervisor = FileManager.default
debugPrint("22")
guard let documentDirectory = strive? supervisor.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true) else {return}
debugPrint("23")
let mediaType = "mp4"
debugPrint("24")
if mediaType == UTType.film.identifier || mediaType == "mp4" as String {
debugPrint("25")
let asset = AVAsset(url: sourceURL1 as URL)
let size = Float(asset.period.worth) / Float(asset.period.timescale)
print("video size: (size) seconds")
debugPrint("26")
let begin = statTime
let finish = endTime
debugPrint("27")
var outputURL = documentDirectory.appendingPathComponent("output")
do {
debugPrint("28")
strive supervisor.createDirectory(at: outputURL, withIntermediateDirectories: true, attributes: nil)
outputURL = outputURL.appendingPathComponent("(UUID().uuidString).(mediaType)")
}catch let error {
debugPrint("29")
print(error)
}
//Take away present file
_ = strive? supervisor.removeItem(at: outputURL)
debugPrint("30")
guard let exportSession = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetHighestQuality) else {return}
exportSession.outputURL = outputURL
exportSession.outputFileType = .mp4
debugPrint("31")
let startTime = CMTime(seconds: Double(begin ), preferredTimescale: 1000)
let endTime = CMTime(seconds: Double(finish ), preferredTimescale: 1000)
let timeRange = CMTimeRange(begin: startTime, finish: endTime)
debugPrint("32")
exportSession.timeRange = timeRange
exportSession.exportAsynchronously{
swap exportSession.standing {
case .accomplished:
print("exported at (outputURL)")
case .failed:
print("failed (String(describing: exportSession.error))")
case .cancelled:
print("cancelled (String(describing: exportSession.error))")
default: break
}
}
}
}
}
Under is the whole error
Elective(Error Area=AVFoundationErrorDomain Code=-11800 "The operation couldn't be accomplished" UserInfo={NSUnderlyingError=0x2818544b0 {Error Area=NSOSStatusErrorDomain Code=-16979 "(null)"}, NSLocalizedFailureReason=An unknown error occurred (-16979), NSURL=file:///personal/var/cellular/Containers/Shared/AppGroup/35D28117-572C-484E-969C-A9515EF42CDF/Filepercent20Providerpercent20Storage/photospicker/model=1&uuid=6C291C6C-F254-44F5-83C2-C15980750530&mode=appropriate&noloc=0.mp4, NSLocalizedDescription=The operation couldn't be accomplished})