First look at what is AVAssetExportPresetPassthrough
// The media of all tracks will be passed to output exactly as stored in the source resource, except for tracks that cannot be passed by channels, usually due to constraints on the container format indicated by the specified outputFileType. This option is not included in allExportPresets and - exportPresetsCompatibleWithAsset the returned array. public let AVAssetExportPresetPassthrough: StringCopy the code
1. Export with AVAssetExportSession:
If not necessary, use presetName AVAssetExportPresetPassthrough can, this name will not perform other operations, transcoding, for example, when I test, When choosing the name for AVAssetExportPresetHighestQuality, video for two minutes, about 30 s export. When choosing AVAssetExportPresetPassthrough, about 7 or 8 s. That’s a big difference.
2 simply copy the video sandbox
This requirement doesn’t really need AVAssetExportSession to export. In the above tests, a two-minute video can run as fast as 7-8s with good quality. This is obviously not fast enough, because we don’t need other requirements like cropping or transcoding. At this time, you can first convert the video into data data, and then write the data to the sandbox file. The two-minute video is about 1s.
let fileUrl = createVideoFilePath(fileName: "videoCache") do { let data = try Data(contentsOf: Write (to: fileUrl) // Other operations (upload, etc.)} catch {}} catch {} func createVideoFilePath(fileName: URL String) - > {/ / set the video saved address let documentDirectory = NSSearchPathForDirectoriesInDomains (. CachesDirectory, .userDomainMask, true).first! + "/\(fileName)" if ! FileManager.default.fileExists(atPath: documentDirectory) { do { try FileManager.default.createDirectory(atPath: documentDirectory, withIntermediateDirectories: true) } catch let error { logDebug("\(error)") } } let filePath: String = "\(documentDirectory)/\((Date().timeIntervalSince1970*100).int).mp4" let fileUrl: URL = URL(fileURLWithPath: filePath) return fileUrl }Copy the code