|
@@ -4,16 +4,16 @@
|
|
|
//
|
|
|
// Created by ak on 2021/12/2.
|
|
|
// 功能:录制头像 ,生成多个 MP4 文件。
|
|
|
-
|
|
|
+
|
|
|
import Foundation
|
|
|
import BFFramework
|
|
|
|
|
|
|
|
|
//录制完成回调
|
|
|
-typealias recordEndCallBack = (_ material: PQEditVisionTrackMaterialsModel) -> Void
|
|
|
+typealias recordEndCallBack = (_ isSucess: Bool,_ material: PQEditVisionTrackMaterialsModel?) -> Void
|
|
|
|
|
|
class BFRecordAvatarView: UIView {
|
|
|
-
|
|
|
+
|
|
|
//画布
|
|
|
var renderView: RenderView!
|
|
|
//摄像机
|
|
@@ -24,7 +24,11 @@ class BFRecordAvatarView: UIView {
|
|
|
var movieOutput:MovieOutput? = nil
|
|
|
//录制完成回调
|
|
|
var recordEndCallBack: recordEndCallBack?
|
|
|
-
|
|
|
+ //录制完成生成的视频文件地址
|
|
|
+ var recorderFilePath:String?
|
|
|
+ //开始录制时间
|
|
|
+ var startRecordeTime:CMTime = .zero
|
|
|
+
|
|
|
//打开摄像头
|
|
|
lazy var closedBtn:UIButton = {
|
|
|
let btn = UIButton(type: .custom)
|
|
@@ -46,17 +50,23 @@ class BFRecordAvatarView: UIView {
|
|
|
self.addSubview(closedBtn)
|
|
|
closedBtn.frame = CGRect.init(x: frame.maxX - 68, y: frame.maxY - 68, width: 68, height: 68)
|
|
|
do {
|
|
|
- camera = try Camera(sessionPreset:.vga640x480,location: .frontFacing)
|
|
|
- camera.runBenchmark = true
|
|
|
+ camera = try Camera(sessionPreset:.high,location: .frontFacing,captureAsYUV: true)
|
|
|
+ // camera.runBenchmark = true
|
|
|
+
|
|
|
+ let conertFilter = PQCornerFilter.init()
|
|
|
let cropFilter = Crop.init()
|
|
|
-
|
|
|
cropFilter.cropSizeInPixels = videoPixelsSize
|
|
|
- cropFilter.locationOfCropInPixels = Position.init(0, 0)
|
|
|
- camera --> cropFilter --> renderView
|
|
|
+ cropFilter.cropSizeInPixels = Size(width: 1080, height:1080)
|
|
|
+ cropFilter.locationOfCropInPixels = Position.init(0, (1920 - 1080) / 2)
|
|
|
+
|
|
|
+// camera --> cropFilter --> conertFilter --> renderView
|
|
|
+ camera --> cropFilter --> conertFilter --> renderView
|
|
|
} catch {
|
|
|
fatalError("Could not initialize rendering pipeline: \(error)")
|
|
|
}
|
|
|
|
|
|
+ let Drag = UIPanGestureRecognizer(target: self, action: #selector(onDrag(gesture:)))
|
|
|
+ addGestureRecognizer(Drag)
|
|
|
}
|
|
|
|
|
|
required init?(coder _: NSCoder) {
|
|
@@ -89,21 +99,31 @@ class BFRecordAvatarView: UIView {
|
|
|
BFLog(message: "摄像机不可用")
|
|
|
return
|
|
|
}
|
|
|
+ startRecordeTime = startTime
|
|
|
+ BFLog(message: "开始录制时间:\(CMTimeGetSeconds(startRecordeTime))")
|
|
|
|
|
|
do {
|
|
|
- var recorderFilePath = recordVideosDirectory
|
|
|
- if !directoryIsExists(dicPath: recorderFilePath) {
|
|
|
- BFLog(message: "文件夹不存在 \(recorderFilePath)")
|
|
|
- createDirectory(path: recorderFilePath)
|
|
|
+ recorderFilePath = recordVideosDirectory
|
|
|
+ if !directoryIsExists(dicPath: recorderFilePath ?? "") {
|
|
|
+ BFLog(message: "文件夹不存在 \(recorderFilePath ?? "")")
|
|
|
+ createDirectory(path: recorderFilePath ?? "")
|
|
|
}
|
|
|
- recorderFilePath.append("recordAvatar_\(Date().timeIntervalSince1970).mp4")
|
|
|
- BFLog(message: "开始录制视频到: \(recorderFilePath)")
|
|
|
+ recorderFilePath!.append("recordAvatar_\(Date().timeIntervalSince1970).mp4")
|
|
|
+ BFLog(message: "开始录制视频到: \(recorderFilePath ?? "")")
|
|
|
+
|
|
|
+ movieOutput = try MovieOutput(URL:URL(fileURLWithPath: recorderFilePath ?? ""), size:videoPixelsSize, liveVideo:true)
|
|
|
+
|
|
|
+ let cropFilter = Crop.init()
|
|
|
+ let conertFilter = PQCornerFilter.init()
|
|
|
+
|
|
|
+ cropFilter.cropSizeInPixels = videoPixelsSize
|
|
|
+ cropFilter.locationOfCropInPixels = Position.init(0, 0)
|
|
|
+
|
|
|
+ camera --> cropFilter --> conertFilter --> movieOutput!
|
|
|
|
|
|
- movieOutput = try MovieOutput(URL:URL(fileURLWithPath: recorderFilePath), size:videoPixelsSize, liveVideo:true)
|
|
|
- camera --> movieOutput!
|
|
|
camera.audioEncodingTarget = movieOutput
|
|
|
movieOutput!.startRecording()
|
|
|
-
|
|
|
+
|
|
|
} catch {
|
|
|
fatalError("Couldn't initialize movie, error: \(error)")
|
|
|
}
|
|
@@ -117,8 +137,37 @@ class BFRecordAvatarView: UIView {
|
|
|
self.camera.audioEncodingTarget = nil
|
|
|
self.movieOutput = nil
|
|
|
|
|
|
-// .replacingOccurrences(of: documensDirectory, with: "").
|
|
|
-// if(recordEndCallBack != nil){
|
|
|
+ if(self.recorderFilePath != nil && (self.recorderFilePath?.count ?? 0) > 0){
|
|
|
+ BFLog(message: "录制成功:\(self.recorderFilePath ?? "")")
|
|
|
+ self.recorderFilePath = self.recorderFilePath?.replacingOccurrences(of: documensDirectory, with: "")
|
|
|
+
|
|
|
+ //拼接回调 model
|
|
|
+ let movieAsset = AVURLAsset(url: URL(fileURLWithPath: self.recorderFilePath!), options: avAssertOptions)
|
|
|
+
|
|
|
+ let movieInfo: PQEditVisionTrackMaterialsModel = PQEditVisionTrackMaterialsModel()
|
|
|
+ movieInfo.type = StickerType.VIDEO.rawValue
|
|
|
+ movieInfo.locationPath = self.recorderFilePath ?? ""
|
|
|
+ movieInfo.timelineIn = Float64(CMTimeGetSeconds(self.startRecordeTime))
|
|
|
+ movieInfo.timelineOut = CMTimeGetSeconds(movieAsset.duration)
|
|
|
+ movieInfo.model_in = 0
|
|
|
+ movieInfo.out = movieInfo.timelineOut
|
|
|
+ movieInfo.canvasFillType = stickerContentMode.aspectFitStr.rawValue
|
|
|
+
|
|
|
+ if(self.recordEndCallBack != nil){
|
|
|
+ self.recordEndCallBack!(false,movieInfo)
|
|
|
+ }
|
|
|
+
|
|
|
+ }else{
|
|
|
+ BFLog(message: "录制失败:\(self.recorderFilePath ?? "")")
|
|
|
+ if(self.recordEndCallBack != nil){
|
|
|
+ self.recordEndCallBack!(false,nil)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
//
|
|
|
// let movieAsset = AVURLAsset(url: URL(fileURLWithPath: fileURL), options: avAssertOptions)
|
|
|
//
|
|
@@ -133,7 +182,20 @@ class BFRecordAvatarView: UIView {
|
|
|
// recordEndCallBack!(movieInfo)
|
|
|
// }
|
|
|
|
|
|
+
|
|
|
+ //拖拽手势方法具体实现
|
|
|
+ @objc func onDrag(gesture: UIPanGestureRecognizer) {
|
|
|
+ let translation = gesture.translation(in: self.superview)
|
|
|
+ var center = gesture.view!.center
|
|
|
+ BFLog(message: "translationtranslationtranslation\(center.x) \(center.y)")
|
|
|
+ center.x += translation.x
|
|
|
+ center.y += translation.y
|
|
|
+ if(center.x > self.bounds.width / 2 && center.x < cScreenWidth - self.bounds.width / 2 && center.y > self.bounds.height / 2 && center.y < cScreenHeigth - self.bounds.height / 2 ){
|
|
|
+ gesture.view!.center = center
|
|
|
+ gesture.setTranslation(CGPoint.zero, in: self.superview)
|
|
|
}
|
|
|
+
|
|
|
|
|
|
}
|
|
|
+
|
|
|
}
|