Created
March 7, 2019 14:02
-
-
Save santosgabriel/6ee6d66c659dfa71ac54e3512bc0900c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// VideoCompression | |
// | |
// Created by Gabriel Lima on 02/03/19. | |
// Copyright © 2019 Gabriel Lima. All rights reserved. | |
// | |
import UIKit | |
import AVFoundation | |
import NextLevelSessionExporter | |
class ViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
imagePicker() | |
} | |
func imagePicker() { | |
let imagePickerController = UIImagePickerController() | |
imagePickerController.delegate = self | |
imagePickerController.sourceType = .photoLibrary | |
imagePickerController.mediaTypes = ["public.image", "public.movie"] | |
imagePickerController.videoExportPreset = AVAssetExportPresetPassthrough | |
present(imagePickerController, animated: true, completion: nil) | |
} | |
} | |
extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate { | |
func imagePickerController(_ picker: UIImagePickerController, | |
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { | |
let videoUrl = info[UIImagePickerController.InfoKey.mediaURL] as! URL | |
let tmpDirectory = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true) | |
.appendingPathComponent(ProcessInfo().globallyUniqueString) | |
.appendingPathExtension("MOV") | |
let asset = AVAsset(url: videoUrl) | |
let exporter = NextLevelSessionExporter(withAsset: asset) | |
exporter.outputURL = tmpDirectory | |
let compressionDict: [String: Any] = [ | |
AVVideoAverageBitRateKey: NSNumber(integerLiteral: 1_000_000), | |
AVVideoProfileLevelKey: AVVideoProfileLevelH264HighAutoLevel as String, | |
] | |
let videoOutputConfig: [String: Any] = [ | |
AVVideoCodecKey: AVVideoCodecType.h264, | |
AVVideoWidthKey: NSNumber(integerLiteral: 1280), | |
AVVideoHeightKey: NSNumber(integerLiteral: 720), | |
AVVideoScalingModeKey: AVVideoScalingModeResizeAspectFill, | |
AVVideoCompressionPropertiesKey: compressionDict | |
] | |
let audioOutputConfig: [String: Any] = [ | |
AVFormatIDKey: kAudioFormatMPEG4AAC, | |
AVEncoderBitRateKey: NSNumber(integerLiteral: 128000), | |
AVNumberOfChannelsKey: NSNumber(integerLiteral: 2), | |
AVSampleRateKey: NSNumber(value: Float(44100)) | |
] | |
asset.nextlevel_export(outputURL: tmpDirectory, videoOutputConfiguration: videoOutputConfig, audioOutputConfiguration: audioOutputConfig) | |
dismiss(animated: true, completion: nil) | |
} | |
} | |
extension URL { | |
var attributes: [FileAttributeKey : Any]? { | |
do { | |
return try FileManager.default.attributesOfItem(atPath: path) | |
} catch let error as NSError { | |
print("FileAttribute error: \(error)") | |
} | |
return nil | |
} | |
var fileSize: UInt64 { | |
return attributes?[.size] as? UInt64 ?? UInt64(0) | |
} | |
var fileSizeString: String { | |
return ByteCountFormatter.string(fromByteCount: Int64(fileSize), countStyle: .file) | |
} | |
var creationDate: Date? { | |
return attributes?[.creationDate] as? Date | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment