前言
上一篇文章音視頻開發(fā)的概念(音視頻開發(fā)概念),這篇介紹音視頻的采集的四種方式 (采集實(shí)現(xiàn)Demo)
系統(tǒng)封裝:
UIImagePickerController
AVFoundation框架實(shí)現(xiàn)的兩種方式:
AVCaptureSession+AVCaptureMovieFileOutput
AVCaptureSession+AVAssetWriter
第三方框架實(shí)現(xiàn):
GpuImage
UIImagePickerController
UIImagePickerController 類是系統(tǒng)獲取選擇圖片和視頻的接口,這種方式只能設(shè)置一些簡(jiǎn)單參數(shù)仑撞,自定義程度不高,只能自定義界面上的操作按鈕私爷,還有視頻的畫質(zhì)等
- 懶加載初始化UIImagePickerController
fileprivate lazy var imagePickerViewController : UIImagePickerController = {
let imagePickerViewController = UIImagePickerController()
imagePickerViewController.delegate = self
imagePickerViewController.allowsEditing = true
return imagePickerViewController
}()
- 打開相冊(cè)
self.imagePickerViewController.sourceType = .photoLibrary
self.present(self.imagePickerViewController, animated: true, completion: nil)
- 拍照和攝像
self.imagePickerViewController.sourceType = .camera
self.imagePickerViewController.videoMaximumDuration = 10.0
self.imagePickerViewController.mediaTypes = [kUTTypeImage as String, kUTTypeMovie as String]
self.present(self.imagePickerViewController, animated: true, completion: nil)
注意:先導(dǎo)入MobileCoreServices框架扫尖,mediaTypes 為數(shù)組類型伐坏,拍照對(duì)應(yīng)kUTTypeImage,錄制為kUTTypeMovie击胜,設(shè)置其他不生效
- 實(shí)現(xiàn)UIImagePickerControllerDelegate的方法,獲取到選擇的資源亏狰,做一系列的操作
extension RecordingSystemViewController : UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
self.backImageView.image = image;
}
if let videoUrl = info[UIImagePickerController.InfoKey.mediaURL] {
let videoVC = VideoDetailViewController()
videoVC.url = (videoUrl as! URL)
self.navigationController?.pushViewController(videoVC, animated: true)
}
self.dismiss(animated: true, completion: nil);
}
}
UIImagePickerController屬性解釋可以看這篇博客:UIImagePickerController類
AVCaptureSession+AVCaptureMovieFileOutput
這種實(shí)現(xiàn)能夠自定義處理錄制頁面,實(shí)現(xiàn)相比AVCaptureSession+AVAssetWriter更加簡(jiǎn)單偶摔,但是這種方案無法實(shí)現(xiàn)濾鏡渲染暇唾,是沒有進(jìn)行編碼的源視頻,所以只能錄制完成后進(jìn)行壓縮處理
流程
1. 創(chuàng)建捕捉會(huì)話
2. 設(shè)置視頻的輸入
3. 設(shè)置音頻的輸入
4. 輸出源設(shè)置,這里視頻辰斋,音頻數(shù)據(jù)會(huì)合并到一起輸出策州,在代理方法中也可以單獨(dú)拿到視頻或者音頻數(shù)據(jù),給AVCaptureMovieFileOutput指定路徑宫仗,開始錄制之后就會(huì)向這個(gè)路徑寫入數(shù)據(jù)
5. 添加視頻預(yù)覽層
6. 開始采集數(shù)據(jù)够挂,這個(gè)時(shí)候還沒有寫入數(shù)據(jù),用戶點(diǎn)擊錄制后就可以開始寫入數(shù)據(jù)
- 創(chuàng)建捕捉會(huì)話
fileprivate lazy var session : AVCaptureSession = {
let session = AVCaptureSession()
session.sessionPreset = .high
return session
}()
- 設(shè)置視頻的輸入(AVCaptureDevice屬性藕夫、AVCaptureInput 屬性)
// 視頻的輸入
@available(iOS 10.2, *)
func setUpVideo(position: AVCaptureDevice.Position) {
let videoCaptureDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: position)
//在修改AVCaptureDevice相關(guān)屬性之前孽糖,必須調(diào)用下述方法上鎖(設(shè)置isVideoHDREnabled崩潰,還沒弄明白)
// do {
// try videoCaptureDevice?.lockForConfiguration()
// videoCaptureDevice?.activeFormat = (videoCaptureDevice?.formats[0])!
// videoCaptureDevice?.automaticallyAdjustsVideoHDREnabled = false
// videoCaptureDevice?.isVideoHDREnabled = true
// } catch {
//
// }
// 視頻輸入源
do {
videoInput = try AVCaptureDeviceInput.init(device: videoCaptureDevice!)
} catch {
}
if self.session.canAddInput(videoInput) {
self.session.addInput(videoInput)
}
}
- 設(shè)置音頻的輸入
// 音頻的輸入
func setUpAudio() {
let audioCaptureDevice = AVCaptureDevice.default(for: .audio)
do {
audioInput = try AVCaptureDeviceInput(device: audioCaptureDevice!)
} catch {
}
if session.canAddInput(audioInput) {
self.session.addInput(audioInput)
}
}
- 輸出源設(shè)置 (AVCaptureFileOutput屬性毅贮、AVCaptureConnection屬性)
func setUpFileOut() {
fileOutput = AVCaptureMovieFileOutput()
// let captureConnection = fileOutput.connection(with: .video)
//
// if ((captureConnection?.isVideoStabilizationSupported)!) {
// // 設(shè)置防抖(崩潰也沒弄明白)
// captureConnection?.preferredVideoStabilizationMode = .auto
// }
if session.canAddOutput(fileOutput) {
self.session.addOutput(fileOutput)
}
}
- 視頻預(yù)覽 (AVCaptureVideoPreviewLayer屬性)
func setUpLayerView() {
view.addSubview(playerView)
previewLayer = AVCaptureVideoPreviewLayer(session: self.session)
previewLayer.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_WIDTH)
self.playerView.layer.addSublayer(previewLayer)
}
這時(shí)候我們就完成了視頻的錄制頁面办悟,所以我們可以添加錄制采集按鈕、相機(jī)轉(zhuǎn)換按鈕等自定義視圖滩褥,
fileprivate func setUpUI() {
videoCustomView.delegate = self
// UIView子視圖與Layer層級(jí)關(guān)系(相互覆蓋),通過zPosition設(shè)置優(yōu)選級(jí)
videoCustomView.layer.zPosition = 3
playerView.addSubview(videoCustomView)
videoCustomView.snp.makeConstraints { (make) in
make.edges.equalTo(UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0))
}
}
實(shí)現(xiàn)自定義視圖代理的方法
extension MovieFileOutputRecordingViewController : VideoCustomViewControlDelegate {
// 點(diǎn)擊錄制按鈕
func clickPlayButton(isPlay: Bool) {
if isPlay {
writeDataTofile()
} else {
self.session.stopRunning()
self.fileOutput.stopRecording()
}
}
// 錄制完成
func recordingEnd() {
self.session.stopRunning()
self.fileOutput.stopRecording()
}
// 相機(jī)轉(zhuǎn)換
func switchCamera() {
session.removeInput(videoInput)
if videoInput.device.position == .back {
setUpVideo(position: .front)
} else {
setUpVideo(position: .back)
}
session.startRunning()
}
}
問題: 相機(jī)轉(zhuǎn)換為前置病蛉,相機(jī)類型為builtInDualCamera會(huì)崩潰,待查證原因
- 寫入數(shù)據(jù)
func writeDataTofile() {
if FileManager.default.fileExists(atPath: self.videoStoragePath()) {
do {
try FileManager.default.removeItem(atPath: self.videoStoragePath())
} catch {
}
}
let path = self.videoStoragePath()
videoUrl = URL.init(fileURLWithPath: path)
self.fileOutput.startRecording(to: videoUrl, recordingDelegate: self)
}
func videoStoragePath() -> String {
let ducment = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, .userDomainMask, true).first
return ducment! + "/video.mp4"
}
注意: 寫入數(shù)據(jù)時(shí)需要先移除之前路徑的數(shù)據(jù)瑰煎,無法自動(dòng)替換铺然,否則一直都是為首次錄制的數(shù)據(jù),錄制10s的視頻數(shù)據(jù)信息如下圖所示
如果錄制一分鐘的視頻丢间,內(nèi)存大約為50MB探熔,上傳到服務(wù)器壓力過大驹针,因此需要在錄制完成后進(jìn)行壓縮
func fileOutput(_ output:AVCaptureFileOutput,didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?) {
let exportSession = AVAssetExportSession.init(asset: AVAsset.init(url: videoUrl), presetName: AVAssetExportPresetMediumQuality)
let ducment = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, .userDomainMask, true).first
let newVideoUrl = URL.init(fileURLWithPath: ducment! + "/newVideo.mp4")
do {
try FileManager.default.removeItem(atPath: ducment! + "/newVideo.mp4")
} catch {
}
// 輸出URL
exportSession?.outputURL = newVideoUrl
// 轉(zhuǎn)換后的格式
exportSession?.outputFileType = AVFileType.mp4
// 優(yōu)化網(wǎng)絡(luò)
exportSession?.shouldOptimizeForNetworkUse = true
// 異步導(dǎo)出
exportSession?.exportAsynchronously(completionHandler: {
Alamofire.upload(newVideoUrl, to: "http://m.toysplanet.cn/gateway/file/upload", method: .post, headers: nil)
let videoVC = VideoDetailViewController()
videoVC.url = newVideoUrl
self.navigationController?.pushViewController(videoVC, animated: true)
})
}
這時(shí)我們?cè)倏纯磯嚎s后的視頻信息
視頻大小已經(jīng)壓縮到1MB
擴(kuò)展: 錄制進(jìn)度動(dòng)畫為CADisplayLink + UIBezierPath實(shí)現(xiàn)烘挫,具體實(shí)現(xiàn)參照Demo
由于篇幅問題,簡(jiǎn)單總結(jié)一下柬甥,以上兩種方式系統(tǒng)都處理好了數(shù)據(jù)的存儲(chǔ)和顯示饮六,簡(jiǎn)單實(shí)現(xiàn)視頻的錄制,不支持實(shí)時(shí)濾鏡采集苛蒲,在下一篇文章卤橄,會(huì)介紹AVCaptureSession+AVAssetWriter和GpuImage分別實(shí)現(xiàn)實(shí)時(shí)濾鏡的采集
特別感謝以上朋友的技術(shù)分享