Я работаю над модулем загрузки видео в моем приложении. Перед загрузкой на сервер я сжимаю видео, которое работает нормально, и размер видео уменьшается после сжатия для файла типа mp4 & .mov, но когда я загружаю видео в формате .3gp, оно не сжимается. Предположим, я загружаю 20 МБ видео после сжатия, оно сжимается в 20 МБ или иногда в 27 МБ. Вот что я попробовал: -
import UIKit
import AVFoundation
class ViewController: UIViewController {
let picker = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
picker.delegate = self
}
@IBAction func selectVideo(_ sender: UIButton) {
picker.sourceType = .photoLibrary
picker.delegate = self
picker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .savedPhotosAlbum)!
picker.mediaTypes = ["public.movie"]
present(picker, animated: true, completion: nil)
}
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 = AVFileType.mov
exportSession.shouldOptimizeForNetworkUse = true
exportSession.exportAsynchronously { () -> Void in
handler(exportSession)
}
}
}
extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if info[UIImagePickerController.InfoKey.mediaType] as? String == "public.movie" {
// here your video capture code
let videoURL = info[UIImagePickerController.InfoKey.mediaURL] as! NSURL!
let data = NSData(contentsOf: videoURL! as URL)!
print("File size before compression: \(Double(data.length / 1048576)) mb")
let compressedURL = NSURL.fileURL(withPath: NSTemporaryDirectory() + NSUUID().uuidString + ".m4v")
compressVideo(inputURL: videoURL! 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
}
let b = compressedData.length;
let k = compressedData.length / 1024;
let m = ((compressedData.length / 1024) / 1024);
let g = (((compressedData.length / 1024) / 1024) / 1024);
let t = ((((compressedData.length / 1024) / 1024) / 1024) / 1024);
if(t>1) {
let fileSizeStr = String("\(t) TB");
print(fileSizeStr)
} else if(g>1){
let fileSizeStr = String("\(g) GB");
print(fileSizeStr)
} else if(m>1){
let fileSizeStr = String(" \(m) MB");
print(fileSizeStr)
} else if(k>1){
let fileSizeStr = String(" \(k) KB");
print(fileSizeStr)
} else {
let fileSizeStr = String(" \(b) bytes");
print(fileSizeStr)
}
print("File size after compression: \(Double(compressedData.length / 1048576)) mb")
case .failed:
break
case .cancelled:
break
}
}
}
self.dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
}