Compression is too slow
JayMehta97 opened this issue · comments
I tried to compress a 6 seconds video of 2.5 MB and it took 2.5 minutes. I also tried to compress a video of 14.5 minutes and it took more than 2 hours. I followed the same process as given in readme. It does compress the video but the amount of time it takes is too high. Please help. Thanks!
Hey, I am having the same issue, obviously, this is too long for a mobile app. Let me know if you found a way to speed it up or found another library to do this?
Hi @bolak2 , I have found a solution to this but its not very customisable and very straight forward.
Here is the code :-
extension PreviewVideoViewController: AVCaptureFileOutputRecordingDelegate {
func capture(_ captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAt outputFileURL: URL!, fromConnections connections: [Any]!, error: Error!) {
guard let data = NSData(contentsOf: outputFileURL as URL) else {
return
}
print("File size before compression: \(Double(data.length / 1048576)) mb")
let compressedURL = NSURL.fileURL(withPath: NSTemporaryDirectory() + NSUUID().uuidString + ".m4v")
compressVideo(inputURL: outputFileURL as URL, outputURL: compressedURL) { (exportSession) in
guard let session = exportSession else {
return
}
switch session.status {
case .unknown:
break
case .waiting:
break
case .exporting:
break
case .completed:
guard let compressedData = NSData(contentsOf: compressedURL) else {
return
}
print("File size after compression: \(Double(compressedData.length / 1048576)) mb")
case .failed:
break
case .cancelled:
break
}
}
}
func compressVideo(inputURL: URL, outputURL: URL, handler:@escaping (_ exportSession: AVAssetExportSession?)-> Void) {
let urlAsset = AVURLAsset(url: inputURL, options: nil)
guard let exportSession = AVAssetExportSession(asset: urlAsset, presetName: AVAssetExportPresetMediumQuality) else {
handler(nil)
return
}
exportSession.outputURL = outputURL
exportSession.outputFileType = AVFileTypeQuickTimeMovie
exportSession.shouldOptimizeForNetworkUse = true
exportSession.exportAsynchronously { () -> Void in
handler(exportSession)
}
}
}
I found this code from this link :-
https://stackoverflow.com/questions/40470637/swift-compressing-video-files
The only thing you can modify here is the presetName which controls the quality.
Hope it helps.
Hi everyone, maintainer here. Sorry to hear that compression takes longer than expected. Without compiling an encoder such as ffmpeg from its C source with latest optimizations, this will be an hard issue to solve. If it is not too much trouble, can I ask you to provide the configurations you have tried (bitrate, other params etc) and the corresponding encoding durations?
@JayMehta97, @bolak2 try to configure compressQueue like this:
fileprivate let compressQueue = DispatchQueue(label: "compressQueue", qos: .userInitiated, attributes: [.concurrent])