Related to this question
Loading slow-mo videos with PHPicker via NSItemProvider.loadFileRepresentation
seems to ignore
PHPickerConfiguration.preferredAssetRepresentationMode
and always reencodes (presumably to bake the slow-mo time segments into the video file). This reencode appears to be ~realtime so this can take a minute for a 1 minute video. Normal videos are near instant.
Is there a faster way to import slow-mo videos than this API?
One nuance - If I use loadInPlaceFileRepresentation
I get no error, inPlace
is set to true, but the file is 0 bytes/nonexistent. This seems perfect for the case mentioned in the docs where it would set inPlace to false and take time to make a local copy, so this seems like a bug.
Interestingly on a slow-mo that I have previously used loadFileRepresentation
that I then use loadInPlaceFileRepresentation
it will work instantly but I think it's just using a cached version
var configuration = PHPickerConfiguration(photoLibrary: PHPhotoLibrary.shared())
configuration.filter = .videos
configuration.selectionLimit = 1
configuration.preferredAssetRepresentationMode = .current
let photoPicker = PHPickerViewController(configuration: configuration)
photoPicker.delegate = self
present(photoPicker, animated: true, completion: nil)
...
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
guard let provider = results.first?.itemProvider, provider.hasItemConformingToTypeIdentifier(UTType.movie.identifier) else {
print("Failed to get photo asset")
picker.dismiss(animated: true, completion: nil)
return
}
provider.loadFileRepresentation(forTypeIdentifier: UTType.movie.identifier) { url, error in
DispatchQueue.main.async {
picker.dismiss(animated: true, completion: nil)
}
guard error == nil, let url = url, let copyTo = DocumentsHelper.getCachedVideoDirectory()?.appendingPathComponent(url.lastPathComponent) else {
print("Failed to load video")
return
}
do {
if FileManager.default.fileExists(atPath: copyTo.path) {
try FileManager.default.removeItem(at: copyTo)
}
try FileManager.default.copyItem(at: url, to: copyTo)
} catch let error {
print("error")
return
}
// load video into a player
}