PQStuckPointEditerController.swift 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. //
  2. // PQStuckPointEditerController.swift
  3. // PQSpeed
  4. //
  5. // Created by ak on 2021/4/26.
  6. // Copyright © 2021 BytesFlow. All rights reserved.
  7. // 功能:卡点音乐编辑界面
  8. import Foundation
  9. import ObjectMapper
  10. import RealmSwift
  11. import UIKit
  12. class PQStuckPointEditerController: PQBaseViewController {
  13. // 是否导出视频成功
  14. var isExportVideosSuccess: Bool = false
  15. // 是否请求卡点数据成功
  16. var isStuckPointDataSuccess: Bool = false
  17. // 是否同步音乐成功
  18. var isSynchroMusicInfoSuccess: Bool = false
  19. /// 当前所有的filter
  20. var filters: Array = Array<ImageProcessingOperation>.init()
  21. // 选中的总时长
  22. var selectedTotalDuration: Float64 = 0
  23. // 选择的总数
  24. var selectedDataCount: Int = 0
  25. // 选择的图片总数
  26. var selectedImageDataCount: Int = 0
  27. // 选中的素材数据
  28. var selectedPhotoData: [PQEditVisionTrackMaterialsModel]?
  29. // 选中的音乐数据
  30. var stuckPointMusicData: PQVoiceModel?
  31. // 保存所有段的所有贴纸,音频信息,用于播放器的渲染使用
  32. var projectModel: PQEditProjectModel = PQEditProjectModel()
  33. // 从草稿箱进入的项目数据
  34. var draftProjectModel: PQEditProjectModel?
  35. var mStickers: [PQEditVisionTrackMaterialsModel]?
  36. // 播放器的开始和结束时间,1,刚进界面使用推荐的开始结束时间,2,用户修改起结点时修改
  37. var playeTimeRange: CMTimeRange = CMTimeRange()
  38. // 下一步
  39. lazy var nextBtn: UIButton = {
  40. let nextBtn = UIButton(type: .custom)
  41. nextBtn.frame = CGRect(x: cScreenWidth - 16 - cDefaultMargin * 6, y: cDevice_iPhoneStatusBarHei + (cDevice_iPhoneNavBarHei - cDefaultMargin * 3) / 2, width: cDefaultMargin * 6, height: cDefaultMargin * 3)
  42. nextBtn.setTitle("合成", for: .normal)
  43. nextBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15, weight: .medium)
  44. nextBtn.addTarget(self, action: #selector(nextBtnClick(sender:)), for: .touchUpInside)
  45. nextBtn.backgroundColor = UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)
  46. nextBtn.setTitleColor(UIColor.hexColor(hexadecimal: "#FFFFFF"), for: .normal)
  47. nextBtn.uxy_acceptEventInterval = 0.5
  48. nextBtn.addCorner(corner: 3)
  49. return nextBtn
  50. }()
  51. // 播放器显示 view
  52. lazy var playerView: PQGPUImagePlayerView = {
  53. let playerHeight = cScreenWidth
  54. let playerView = PQGPUImagePlayerView(frame: CGRect(x: 0, y: navHeadImageView?.frame.maxY ?? 0, width: playerHeight, height: playerHeight))
  55. playerView.backgroundColor = PQBFConfig.shared.styleBackGroundColor
  56. playerView.isShowLine = false
  57. // playerView.showGaussianBlur = true
  58. return playerView
  59. }()
  60. /// 节奏选择视图
  61. lazy var sustomSwitchView: PQCustomSwitchView = {
  62. let sustomSwitchView = PQCustomSwitchView(frame: CGRect(x: (view.frame.width - cDefaultMargin * 28) / 2, y: view.frame.height - cSafeAreaHeight - cDefaultMargin * 3 - cDefaultMargin * 3, width: cDefaultMargin * 28, height: 35), titles: ["快节奏", "适中", "慢节奏"], defaultIndex: stuckPointMusicData?.speed ?? 2)
  63. sustomSwitchView.switchChangeHandle = { [weak self] sender in
  64. // 改变速率
  65. self?.stuckPointMusicData?.speed = sender.tag
  66. self?.projectModel.sData?.getBGMSession()?.sectionTimeline?.audioTrack?.audioTrackMaterials.first?.bgmInfo?.rhythmMusicSpeed = sender.tag
  67. // 播放前先暂停
  68. // self?.playerView.stop()
  69. // 开始播放
  70. self?.settingPlayerView()
  71. // 点击上报:选择节奏
  72. PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_click_selectRhythm, pageSource: .sp_stuck_previewSyncedUp, extParams: ["rhythmNumber": sender.tag], remindmsg: "点击上报:选择节奏")
  73. }
  74. return sustomSwitchView
  75. }()
  76. /// 裁剪视图
  77. lazy var stuckPointCuttingView: PQStuckPointCuttingView = {
  78. let stuckPointCuttingView = PQStuckPointCuttingView(frame: CGRect(x: 0, y: sustomSwitchView.frame.minY - cDefaultMargin * 14 - cDefaultMargin * 2, width: view.frame.width, height: cDefaultMargin * 14), duration: CGFloat(Float(stuckPointMusicData?.duration ?? "0") ?? 0), startTime: CGFloat(stuckPointMusicData?.startTime ?? 0), endTime: CGFloat(stuckPointMusicData?.endTime ?? 0))
  79. /// 裁剪进度回调
  80. stuckPointCuttingView.videoRangeDidChanged = { [weak self] startTime, endTime in
  81. BFLog(message: "裁剪返回--startTime = \(startTime),endTime = \(endTime)")
  82. }
  83. /// 播放进度回调
  84. stuckPointCuttingView.videoProgressDidChanged = { [weak self] progress in
  85. BFLog(message: "进度更新返回--progress = \(progress) \(String(describing: self?.playerView.mPlayeTimeRange))")
  86. }
  87. /// 拖缀结束的回调 type - 1-拖动左边裁剪结束 2--拖动右边裁剪结束 3-进度条拖动结束 4-滑动结束
  88. stuckPointCuttingView.videoDidEndDragging = { [weak self] type, startTime, endTime, progress in
  89. BFLog(message: "拖拽结束返回--type = \(type),startTime = \(startTime),endTime = \(endTime),progress = \(progress)")
  90. self?.playerView.pause()
  91. // 修改最新值
  92. self?.stuckPointMusicData?.startTime = Float64(startTime)
  93. self?.stuckPointMusicData?.endTime = Float64(endTime)
  94. // 红的指针完成
  95. if type == 3 {
  96. if CMTimeGetSeconds(self?.playerView.mPlayeTimeRange?.end ?? .zero) == 0 {
  97. BFLog(message: "mPlayeTimeRange is error")
  98. return
  99. }
  100. let newBeginSconds = (Double(startTime) + (Double(endTime) - Double(startTime)) * Double(progress)) * 600
  101. BFLog(message: " newBeginSconds is \(newBeginSconds)")
  102. let seekTimeRange: CMTimeRange = CMTimeRange(start: CMTime(value: CMTimeValue(Int64(newBeginSconds)), timescale: 600), end:
  103. CMTime(value: CMTimeValue(Int64(endTime * 600)), timescale: 600))
  104. BFLog(message: "修改的开始 \(CMTimeGetSeconds(seekTimeRange.start)) 结束 \(CMTimeGetSeconds(seekTimeRange.end))")
  105. self?.playerView.play(pauseFirstFrame: false, playeTimeRange: seekTimeRange)
  106. } else {
  107. // 更改素材开始时间及结束时间
  108. self?.projectModel.sData?.getBGMSession()?.sectionTimeline?.audioTrack?.audioTrackMaterials.first?.out = Float64(endTime)
  109. self?.projectModel.sData?.getBGMSession()?.sectionTimeline?.audioTrack?.audioTrackMaterials.first?.model_in = Float64(startTime)
  110. self?.projectModel.sData?.getBGMSession()?.sectionTimeline?.audioTrack?.audioTrackMaterials.first?.timelineIn = Float64(startTime)
  111. self?.projectModel.sData?.getBGMSession()?.sectionTimeline?.audioTrack?.audioTrackMaterials.first?.timelineOut = Float64(endTime)
  112. // 初始化音频的开始和结束时间
  113. self?.playeTimeRange = CMTimeRange(start: CMTime(value: CMTimeValue(Int64(startTime * 600)), timescale: 600), end: CMTime(value: CMTimeValue(Int64(endTime * 600)), timescale: 600))
  114. DispatchQueue.global().async { // 并行、异步
  115. let beginTime: TimeInterval = Date().timeIntervalSince1970
  116. self?.mStickers = self?.createStickers(sections: self?.projectModel.sData?.sections ?? List(), inputSize: CGSize(width: CGFloat(self?.projectModel.sData?.videoMetaData?.videoWidth ?? 0), height: CGFloat(self?.projectModel.sData?.videoMetaData?.videoHeight ?? 0)))
  117. self?.playerView.mStickers = self?.mStickers
  118. DispatchQueue.main.async { // 串行、异步
  119. var endTime: TimeInterval = Date().timeIntervalSince1970
  120. BFLog(message: "endTime is endTimeendTime \(endTime = beginTime)")
  121. self?.playerView.play(pauseFirstFrame: false, playeTimeRange: self!.playeTimeRange)
  122. }
  123. }
  124. }
  125. // 埋点上报
  126. if type == 1 || type == 2 {
  127. // 点击上报:拖动拖拽条(左/右部分)
  128. PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonClick, objectType: type == 1 ? .ot_click_dragFront : .ot_click_dragBehind, pageSource: .sp_stuck_previewSyncedUp, extParams: ["targetTime": type == 1 ? startTime * 1000 : endTime * 1000], remindmsg: "点击上报:拖动拖拽条(左/右部分)")
  129. }
  130. }
  131. return stuckPointCuttingView
  132. }()
  133. /// 卡点时长显示视图
  134. lazy var timeRemindLab: UILabel = {
  135. let timeRemindLab = UILabel(frame: CGRect(x: 0, y: stuckPointCuttingView.frame.minY - cDefaultMargin * 4 - cDefaultMargin * 3, width: view.frame.width, height: cDefaultMargin * 4))
  136. timeRemindLab.backgroundColor = UIColor.hexColor(hexadecimal: "#262626")
  137. timeRemindLab.textAlignment = .center
  138. timeRemindLab.font = UIFont.systemFont(ofSize: 12)
  139. timeRemindLab.textColor = UIColor.hexColor(hexadecimal: "#999999")
  140. let total: Float64 = ((stuckPointMusicData?.endTime ?? Float64(stuckPointMusicData?.duration ?? "0") ?? 0) - (stuckPointMusicData?.startTime ?? 0))
  141. timeRemindLab.text = "现卡点时长\(total.formatDurationToHMS()) / 原视频总时长\(selectedTotalDuration.formatDurationToHMS())"
  142. return timeRemindLab
  143. }()
  144. /// 音乐标题
  145. lazy var musicNameView: UIView = {
  146. let musicNameView = UIView()
  147. musicNameView.addSubview(musicNameLab)
  148. let nameWidth: CGFloat = musicNameLab.frame.width + (25 + cDefaultMargin * 3)
  149. musicNameView.frame = CGRect(x: (view.frame.width - nameWidth) / 2, y: cDevice_iPhoneStatusBarHei + (cDevice_iPhoneNavBarHei - cDefaultMargin * 3) / 2, width: nameWidth, height: cDefaultMargin * 3)
  150. // musicNameView.backgroundColor = UIColor.hexColor(hexadecimal: "#333333")
  151. musicNameView.addCorner(corner: musicNameView.frame.height / 2)
  152. let musicImageView = UIImageView()
  153. musicImageView.tintColor = PQBFConfig.shared.styleTitleColor
  154. musicImageView.image = UIImage().BF_Image(named: "stuckPoint_reCreate_music").withRenderingMode(.alwaysTemplate)
  155. musicImageView.frame = CGRect(x: musicNameView.frame.height / 2 - 5, y: (musicNameView.frame.height - 22) / 2, width: 22, height: 22)
  156. musicNameView.addSubview(musicImageView)
  157. musicNameLab.frame.origin.x = musicImageView.frame.maxX + 5
  158. let ges = UITapGestureRecognizer(target: self, action: #selector(musicNameClick))
  159. musicNameView.addGestureRecognizer(ges)
  160. return musicNameView
  161. }()
  162. /// 音乐歌曲名称
  163. lazy var musicNameLab: LMJHorizontalScrollText = {
  164. let nameWidth: CGFloat = sizeWithText(text: "\(stuckPointMusicData?.musicName ?? "")", font: UIFont.systemFont(ofSize: 13), size: CGSize(width: view.frame.width - ((cDefaultMargin * 6 + 16 * 2) * 2) - (25 + cDefaultMargin * 3), height: cDefaultMargin * 3)).width
  165. let musicNameLab = LMJHorizontalScrollText(frame: CGRect(x: 0, y: 0, width: nameWidth < cDefaultMargin * 4 ? cDefaultMargin * 4 : nameWidth, height: cDefaultMargin * 3))
  166. musicNameLab.textColor = PQBFConfig.shared.styleTitleColor
  167. musicNameLab.textFont = UIFont.systemFont(ofSize: 13)
  168. musicNameLab.speed = 0.03
  169. musicNameLab.moveDirection = LMJTextScrollMoveLeft
  170. musicNameLab.moveMode = LMJTextScrollContinuous
  171. if nameWidth < cDefaultMargin * 4 {
  172. musicNameLab.text = " \(stuckPointMusicData?.musicName ?? "") "
  173. } else {
  174. musicNameLab.text = " \(stuckPointMusicData?.musicName ?? "") "
  175. }
  176. return musicNameLab
  177. }()
  178. /// 同步进度显示
  179. lazy var synchroMarskView: PQStuckPointLoadingView = {
  180. var synchroMarskView = PQStuckPointLoadingView(frame: CGRect(x: 0, y: 0, width: cScreenWidth, height: cScreenHeigth))
  181. synchroMarskView.cancelHandle = { [weak self] _ in
  182. self?.navigationController?.popViewController(animated: true)
  183. }
  184. return synchroMarskView
  185. }()
  186. override func viewWillAppear(_ animated: Bool) {
  187. super.viewDidAppear(animated)
  188. lineView?.isHidden = true
  189. UIApplication.shared.isIdleTimerDisabled = true
  190. musicNameLab.move()
  191. PQNotification.addObserver(self, selector: #selector(enterBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
  192. }
  193. @objc func enterBackground() {
  194. BFLog(message: "进入到后台")
  195. // 取消导出
  196. playerView.pause()
  197. }
  198. override func backBtnClick() {
  199. super.backBtnClick()
  200. playerView.pause()
  201. // 点击上报:返回按钮
  202. PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_click_back, pageSource: .sp_stuck_previewSyncedUp, extParams: nil, remindmsg: "卡点视频数据上报-(点击上报:返回按钮)")
  203. }
  204. @objc func musicNameClick() {
  205. // let musicVc = navigationController?.viewControllers.first(where: { vc in
  206. // vc is PQStuckPointMusicController
  207. // })
  208. // if musicVc != nil {
  209. // navigationController?.popToViewController(musicVc!, animated: true)
  210. // }
  211. }
  212. override func viewWillDisappear(_ animated: Bool) {
  213. super.viewWillDisappear(animated)
  214. UIApplication.shared.isIdleTimerDisabled = false
  215. musicNameLab.stop()
  216. playerView.pause()
  217. }
  218. override func viewDidLoad() {
  219. super.viewDidLoad()
  220. leftButton(image: "icon_detail_back", tintColor: PQBFConfig.shared.styleTitleColor)
  221. navHeadImageView?.addSubview(nextBtn)
  222. navHeadImageView?.addSubview(musicNameView)
  223. // 添加子视图
  224. addSubViews()
  225. // 导出相册视频
  226. exportPhotoData()
  227. // 同步音乐数据
  228. synchroMusicInfoData()
  229. // 曝光上报:预览页面曝光上报
  230. PQEventTrackViewModel.baseReportUpload(businessType: .bt_windowView, objectType: .ot_view_previewSyncedUp, pageSource: .sp_stuck_previewSyncedUp, extParams: nil, remindmsg: "卡点视频数据上报-(曝光上报:预览页面曝光上报)")
  231. }
  232. /// 添加子视图
  233. /// - Returns: <#description#>
  234. func addSubViews() {
  235. if (stuckPointMusicData?.rhythmSdata.count ?? 0) <= 0 {
  236. return
  237. }
  238. view.addSubview(playerView)
  239. view.addSubview(sustomSwitchView)
  240. view.addSubview(stuckPointCuttingView)
  241. // view.addSubview(timeRemindLab)
  242. // 添加一个背景区分不同色值
  243. let backView: UIView = UIView()
  244. backView.backgroundColor = PQBFConfig.shared.styleBackGroundColor
  245. view.insertSubview(backView, aboveSubview: navHeadImageView!)
  246. backView.frame = CGRect(x: 0, y: navHeadImageView?.frame.height ?? 0, width: cScreenWidth, height: (stuckPointCuttingView.frame.minY - cDefaultMargin * 3) - (navHeadImageView?.frame.height ?? 0))
  247. }
  248. @objc func nextBtnClick(sender _: UIButton) {
  249. BFLog(message: "去发布")
  250. playerView.pause()
  251. let videoExporter = PQStuckPointPublicController()
  252. videoExporter.selectedTotalDuration = selectedTotalDuration
  253. videoExporter.selectedDataCount = selectedDataCount
  254. videoExporter.selectedImageDataCount = selectedImageDataCount
  255. // 使用深 copy
  256. let json = projectModel.toJSONString(prettyPrint: false)
  257. if json == nil {
  258. BFLog(message: "数据转换有问题 跳转")
  259. return
  260. }
  261. let tempModel: PQEditProjectModel? = Mapper<PQEditProjectModel>().map(JSONString: json!)
  262. videoExporter.mStickers = mStickers
  263. videoExporter.audioMixModel = stuckPointMusicData
  264. videoExporter.editProjectModel = tempModel
  265. navigationController?.pushViewController(videoExporter, animated: true)
  266. // 点击上报:去合成
  267. PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_click_commit, pageSource: .sp_stuck_previewSyncedUp, extParams: ["musicName": stuckPointMusicData?.musicName ?? "", "musicId": stuckPointMusicData?.musicId ?? "", "rhythmNumber": stuckPointMusicData?.speed ?? 2, "duration": ((stuckPointMusicData?.endTime ?? 0) - (stuckPointMusicData?.startTime ?? 0)) * 1000], remindmsg: "点击上报:去合成")
  268. }
  269. // MARK: - 播放器相关操作
  270. /// seek 播放器
  271. /// - Parameter playeTimeRange: 开始和结束时间
  272. func seekPlayer(playeTimeRange: CMTimeRange) {
  273. playerView.setEnableSeek(isSeek: true)
  274. playerView.play(pauseFirstFrame: false, playeTimeRange: playeTimeRange)
  275. }
  276. /// 通过传入的 selectedPhotoData 、 stuckPointMusicData 创建 projectModel 模型 后面都使用 projectModel 参数
  277. func createPorjectData() {
  278. // 1,添加选择的视觉素材
  279. let section: PQEditSectionModel = PQEditSectionModel()
  280. selectedPhotoData?.forEach { model in
  281. let json = model.toJSONString(prettyPrint: false)
  282. if json == nil {
  283. BFLog(message: "数据转换有问题 跳转")
  284. return
  285. }
  286. let tempModel: PQEditVisionTrackMaterialsModel = Mapper<PQEditVisionTrackMaterialsModel>().map(JSONString: json!)!
  287. section.sectionTimeline?.visionTrack?.visionTrackMaterials.append(tempModel)
  288. }
  289. projectModel.sData?.sections.append(section)
  290. // 2,添加背景音乐
  291. projectModel.sData?.addBGM(audioMix: stuckPointMusicData!)
  292. }
  293. // 设置播放器
  294. func settingPlayerView() {
  295. // 1,设置播放器的显示区域 和画布大小
  296. // - 按第一个素材尺寸自适应
  297. let playerShowHeight = (stuckPointCuttingView.frame.minY - cDefaultMargin * 3) - (navHeadImageView?.frame.maxY ?? 0)
  298. var showRect: CGRect = PQPlayerViewModel.getShowCanvasRect(editProjectModel: projectModel, showType: 1, playerViewHeight: playerShowHeight)
  299. if showRect.size.width == showRect.size.height {
  300. if cScreenWidth > playerShowHeight {
  301. showRect.origin.x = (cScreenWidth - playerShowHeight) / 2
  302. showRect.size.width = playerShowHeight
  303. showRect.size.height = playerShowHeight
  304. } else {
  305. showRect.origin.x = 0
  306. showRect.size.width = cScreenWidth
  307. showRect.size.height = cScreenWidth
  308. }
  309. }
  310. showRect.origin.y = (playerShowHeight - showRect.size.height) / 2.0 + (navHeadImageView?.frame.maxY ?? 0)
  311. if showRect.size.width != 0, showRect.size.height != 0 {
  312. playerView.resetCanvasFrame(frame: showRect)
  313. }
  314. var firstModel: PQEditVisionTrackMaterialsModel?
  315. for part in projectModel.sData!.sections {
  316. if part.sectionTimeline?.visionTrack?.getEnableVisionTrackMaterials().count ?? 0 > 0 {
  317. firstModel = part.sectionTimeline?.visionTrack?.getEnableVisionTrackMaterials().first
  318. break
  319. }
  320. }
  321. var videoSize: CGSize = CGSize(width: Int(firstModel?.width ?? 0), height: Int(firstModel?.height ?? 0))
  322. var minSlider = min(videoSize.width, videoSize.height)
  323. var maxSlider = max(videoSize.width, videoSize.height)
  324. let ration = 1080 / minSlider
  325. minSlider = minSlider * ration
  326. maxSlider = maxSlider * ration
  327. if videoSize.width > videoSize.height { // 宽屏
  328. videoSize = CGSize(width: maxSlider, height: minSlider)
  329. } else {
  330. videoSize = CGSize(width: minSlider, height: maxSlider)
  331. }
  332. let maxValue = max(videoSize.width ?? 0, videoSize.height ?? 0)
  333. if maxValue > 1920 {
  334. let maxRation = 1920 / maxValue
  335. videoSize = CGSize(width: videoSize.width * CGFloat(maxRation), height: videoSize.height * CGFloat(maxRation))
  336. BFLog(message: "最长边已经超过 1920 要等比缩小 缩放后\(videoSize)")
  337. }
  338. if (Int(videoSize.width) % 2) != 0 {
  339. videoSize.width = videoSize.width - 1
  340. }
  341. if (Int(videoSize.height) % 2) != 0 {
  342. videoSize.height = videoSize.height - 1
  343. }
  344. projectModel.sData?.videoMetaData?.videoWidth = Int(videoSize.width)
  345. projectModel.sData?.videoMetaData?.videoHeight = Int(videoSize.height)
  346. // 2,创建滤镜
  347. DispatchQueue.global().async { // 并行、异步
  348. let beginTime: TimeInterval = Date().timeIntervalSince1970
  349. self.mStickers = self.createStickers(sections: self.projectModel.sData?.sections ?? List(), inputSize: CGSize(width: CGFloat(self.projectModel.sData?.videoMetaData?.videoWidth ?? 0), height: CGFloat(self.projectModel.sData?.videoMetaData?.videoHeight ?? 0)))
  350. self.playerView.mStickers = self.mStickers
  351. let end: TimeInterval = Date().timeIntervalSince1970
  352. BFLog(message: "createStickers tiskskskskme \(end - beginTime)")
  353. DispatchQueue.main.async { // 串行、异步
  354. // 3,设置音频
  355. let audioPath = self.stuckPointMusicData?.localPath ?? ""
  356. BFLog(message: "初始化音频播放器的音频地址为:\(audioPath)")
  357. self.playerView.stop()
  358. self.playerView.updateAsset(URL(fileURLWithPath: documensDirectory + audioPath), videoComposition: nil, audioMixModel: nil)
  359. let end2: TimeInterval = Date().timeIntervalSince1970
  360. BFLog(message: "updateAsset tiskskskskme \(end2 - end)")
  361. // 4, 设置播放器的输出画布大小
  362. self.playerView.movie?.mShowVidoSize = CGSize(width: CGFloat(self.projectModel.sData?.videoMetaData?.videoWidth ?? 0), height: CGFloat(self.projectModel.sData?.videoMetaData?.videoHeight ?? 0))
  363. // 5,开始播放
  364. self.playerView.isLoop = false
  365. self.playerView.showProgressLab = false
  366. // 初始化音频的开始和结束时间
  367. BFLog(message: "播放的器 开始\(String(describing: CMTimeGetSeconds(self.playeTimeRange.start))) 结束 \(String(describing: CMTimeGetSeconds(self.playeTimeRange.end)))")
  368. let end3: TimeInterval = Date().timeIntervalSince1970
  369. self.playerView.play(pauseFirstFrame: false, playeTimeRange: CMTimeRange(start: self.playeTimeRange.start, end: self.playeTimeRange.end))
  370. let end4: TimeInterval = Date().timeIntervalSince1970
  371. BFLog(message: " playerView.play tiskskskskme \(end4 - end3)")
  372. // 6,进度回调
  373. self.playerView.progress = { [weak self] currentTime, tatolTime, _ in
  374. // 更新进度
  375. let progress = (currentTime - CMTimeGetSeconds(self?.playeTimeRange.start ?? .zero
  376. )) / CMTimeGetSeconds(self?.playeTimeRange.duration ?? .zero
  377. )
  378. BFLog(message: "\(currentTime) \(tatolTime) 显示播放器进度为: \(progress)")
  379. self?.stuckPointCuttingView.videoCropView.updateProgress(progress: CGFloat(progress))
  380. if self?.synchroMarskView.superview != nil {
  381. self?.synchroMarskView.removeMarskView()
  382. }
  383. }
  384. }
  385. }
  386. }
  387. deinit {
  388. BFLog(message: "卡点视频预览界面销毁")
  389. musicNameLab.stop()
  390. playerView.pause()
  391. // 取消所有的导出
  392. PQSingletoMemoryUtil.shared.allExportSession.forEach { _, exportSession in
  393. exportSession.cancelExport()
  394. }
  395. }
  396. }
  397. // MARK: - 视频渲染相关逻辑方法
  398. extension PQStuckPointEditerController {
  399. /// 分割视频 这里只设置视频类型的 in 和 out 并不设置显示的开始和结束时间 mp4 ,png ,png ,mp4
  400. /// - Parameter section: 当前段
  401. /// - Parameter stuckPoints: 用户选择的,或推荐的卡点数
  402. /// - Returns: 返回分割后的所有 stickers 和卡点数是一致的
  403. func clipVideoMerage(section: PQEditSectionModel, stuckPoints: [Float]) -> [PQEditVisionTrackMaterialsModel] {
  404. var stickers: Array = Array<PQEditVisionTrackMaterialsModel>.init()
  405. // 第二种情况:有视频要进行分割
  406. /*
  407. 1, 确定每个视频素材需要切的段数p
  408. 2, 将所有视频时长相加,得到总视频素材时长L = l1 + l2 + ... + ln
  409. 3, 视频素材a1需要切分的个数clipNum = max (round (kongduan * a1 / L) , 1)
  410. */
  411. // 要补的空位数
  412. let kongduan: Int = Int(stuckPoints.count) - 1 - Int(section.sectionTimeline!.visionTrack?.getEnableVisionTrackMaterials().count ?? 0)
  413. // 所有视频总时长
  414. var videoTotalDuration: Float64 = 0.0
  415. for video in section.sectionTimeline!.visionTrack!.getEnableVisionTrackMaterials(type: "video") {
  416. let asset: AVURLAsset = AVURLAsset(url: URL(fileURLWithPath: documensDirectory + video.locationPath), options: nil)
  417. videoTotalDuration = videoTotalDuration + Float64(CMTimeGetSeconds(asset.duration))
  418. }
  419. if videoTotalDuration == 0 {
  420. BFLog(message: "视频总时长出现错误!!!!这里应该有视频素材的")
  421. return stickers
  422. }
  423. for sticker in section.sectionTimeline!.visionTrack!.getEnableVisionTrackMaterials() {
  424. if sticker.type == StickerType.VIDEO.rawValue {
  425. let asset: AVURLAsset = AVURLAsset(url: URL(fileURLWithPath: documensDirectory + sticker.locationPath), options: nil)
  426. // 要分割的段落
  427. let clipNum = Int(max(round(Double(kongduan) * CMTimeGetSeconds(asset.duration) / videoTotalDuration), 1))
  428. sticker.duration = CMTimeGetSeconds(asset.duration)
  429. BFLog(message: "单个视频\(sticker.locationPath)时长::\(CMTimeGetSeconds(asset.duration)) ,clipNum is:\(clipNum)")
  430. for clipindex in 0 ... clipNum {
  431. // deep copy sticker model 防止只有一个对象
  432. let stickerjson = sticker.toJSONString(prettyPrint: false)
  433. let deepCopySticker = Mapper<PQEditVisionTrackMaterialsModel>().map(JSONString: stickerjson!)
  434. // 设置循环模式和适配模式
  435. deepCopySticker?.generateDefaultValues()
  436. deepCopySticker?.model_in = clipindex == 0 ? 0 : CMTimeGetSeconds(asset.duration) / Double(clipNum) * Double(clipindex)
  437. deepCopySticker?.out = (deepCopySticker?.model_in ?? 0) + CMTimeGetSeconds(asset.duration) / Double(clipNum)
  438. if (deepCopySticker?.model_in ?? 0) >= CMTimeGetSeconds(asset.duration) || (deepCopySticker?.out ?? 0) >= CMTimeGetSeconds(asset.duration) {
  439. deepCopySticker?.model_in = CMTimeGetSeconds(asset.duration) - CMTimeGetSeconds(asset.duration) / Double(clipNum)
  440. deepCopySticker?.out = CMTimeGetSeconds(asset.duration)
  441. }
  442. BFLog(message: " crilp is in \(deepCopySticker?.model_in ?? 0) out \(deepCopySticker?.out ?? 0) 总时长\(CMTimeGetSeconds(asset.duration))")
  443. if deepCopySticker != nil {
  444. stickers.append(deepCopySticker!)
  445. }
  446. }
  447. } else if sticker.type == StickerType.IMAGE.rawValue {
  448. sticker.generateDefaultValues()
  449. stickers.append(sticker)
  450. }
  451. }
  452. return stickers
  453. }
  454. /// 创建sticker
  455. /// - Parameters:
  456. /// - sections: 项目所有段落数据信息
  457. /// - inputSize: 画布大小
  458. /// - Returns: filters 数据 播放器可直接使用
  459. func createStickers(sections: List<PQEditSectionModel>, inputSize _: CGSize = .zero) -> [PQEditVisionTrackMaterialsModel] {
  460. // 保存滤镜对象数据
  461. var stickers: Array = Array<PQEditVisionTrackMaterialsModel>.init()
  462. for section in sections {
  463. if section.sectionType == "normal" {
  464. // 推荐卡点数
  465. var stuckPoints: Array = Array<Float>.init()
  466. var stuckPointsTemp = Array<Float>.init()
  467. for (index, dunshu) in stuckPointMusicData!.rhythmSdata[0].pointTimes.enumerated() {
  468. BFLog(message: "所有卡点数:\(Float64(dunshu) / 1_000_000.0)")
  469. if Float64(dunshu) / 1_000_000.0 > CMTimeGetSeconds(playeTimeRange.start), Float64(dunshu) / 1_000_000.0 < CMTimeGetSeconds(playeTimeRange.end) {
  470. stuckPointsTemp.append(Float(dunshu) / 1_000_000.0)
  471. }
  472. }
  473. // 根据不同速度 取卡点 1,2,3
  474. /*
  475. - 快节奏为选中区域的所有点位,即0,1,2,3,4……
  476. - 适中为每两个点位取一个,即0,2,4,6……
  477. - 慢节奏为每三个点位取一个,即0,3,6,9……
  478. */
  479. BFLog(message: "stuckPointMusicData?.speed is \(String(describing: stuckPointMusicData?.speed))")
  480. for (index, point) in stuckPointsTemp.enumerated() {
  481. if stuckPointMusicData?.speed == 1 {
  482. stuckPoints.append(point)
  483. } else if stuckPointMusicData?.speed == 2 {
  484. if index % 2 == 0 {
  485. stuckPoints.append(point)
  486. }
  487. } else if stuckPointMusicData?.speed == 3 {
  488. if index % 3 == 0 {
  489. stuckPoints.append(point)
  490. }
  491. }
  492. }
  493. for point in stuckPoints {
  494. BFLog(message: "有 start end 计算后的卡点数\(point)")
  495. }
  496. if stuckPoints.first != nil {
  497. stuckPoints.removeFirst()
  498. }
  499. if stuckPoints.last != nil {
  500. stuckPoints.removeLast()
  501. }
  502. stuckPoints.insert(Float(CMTimeGetSeconds(playeTimeRange.start)), at: 0)
  503. stuckPoints.insert(Float(CMTimeGetSeconds(playeTimeRange.end)), at: stuckPoints.count)
  504. BFLog(message: "stuckPoints count is \(stuckPoints.count)")
  505. // 当用户上传视觉素材个数大于等于音乐选择区域节拍分割个数时,无需进行视频分割,只显示卡点数-1 个素材
  506. if section.sectionTimeline!.visionTrack!.getEnableVisionTrackMaterials().count >= stuckPointMusicData!.rhythmSdata[0].pointTimes.count {
  507. for (index, sticker) in section.sectionTimeline!.visionTrack!.getEnableVisionTrackMaterials().enumerated() {
  508. if index == stuckPointMusicData!.rhythmSdata[0].pointTimes.count {
  509. BFLog(message: "到达卡点数量")
  510. break
  511. }
  512. BFLog(message: "创建 filter start :\(sticker.timelineIn) end :\(sticker.timelineOut) type is \(sticker.type) \(sticker.locationPath)")
  513. sticker.timelineIn = Float64(stuckPoints[index])
  514. sticker.timelineOut = Float64(stuckPoints[index + 1])
  515. BFLog(message: "卡点 间隔 \(sticker.timelineIn - sticker.timelineOut)")
  516. sticker.generateDefaultValues()
  517. stickers.append(sticker)
  518. }
  519. } else {
  520. // 卡点数 > 选择素材数
  521. // 第一种情况:全是图片,图片回环播放
  522. if section.sectionTimeline!.visionTrack!.getEnableVisionTrackMaterials(type: "video").count == 0, section.sectionTimeline!.visionTrack!.getEnableVisionTrackMaterials(type: "image").count > 0 {
  523. for (index, point) in stuckPoints.enumerated() {
  524. let sticker: PQEditVisionTrackMaterialsModel = section.sectionTimeline!.visionTrack!.getEnableVisionTrackMaterials()[index % section.sectionTimeline!.visionTrack!.getEnableVisionTrackMaterials().count]
  525. BFLog(message: "stickerlocationPath sticker : \(sticker.locationPath)")
  526. let stickerjson = sticker.toJSONString(prettyPrint: false)
  527. let deepCopySticker = Mapper<PQEditVisionTrackMaterialsModel>().map(JSONString: stickerjson!)
  528. if deepCopySticker!.type == StickerType.IMAGE.rawValue {
  529. if index + 1 < stuckPoints.count {
  530. deepCopySticker!.timelineIn = Float64(stuckPoints[index])
  531. deepCopySticker!.timelineOut = Float64(stuckPoints[index + 1])
  532. if deepCopySticker != nil {
  533. deepCopySticker?.generateDefaultValues()
  534. stickers.append(deepCopySticker!)
  535. }
  536. }
  537. }
  538. }
  539. } else {
  540. // 第二种情况:有视频要进行分割
  541. let clipFilters = clipVideoMerage(section: section, stuckPoints: stuckPoints)
  542. for (index, point) in stuckPoints.enumerated() {
  543. if index + 1 < stuckPoints.count, index < clipFilters.count {
  544. let sticker: PQEditVisionTrackMaterialsModel = clipFilters[index]
  545. sticker.timelineIn = Float64(stuckPoints[index])
  546. sticker.timelineOut = Float64(stuckPoints[index + 1])
  547. // 卡点的时间 > in out 值
  548. let timelineInterval = sticker.timelineOut - sticker.timelineIn
  549. let inOutInterval = sticker.out - sticker.model_in
  550. if timelineInterval > inOutInterval {
  551. sticker.out = sticker.model_in + timelineInterval
  552. }
  553. // out > 素材的总时长in out 进行前移操作
  554. let offsetAssetDuration = sticker.out - sticker.duration
  555. if offsetAssetDuration > 0 {
  556. sticker.model_in = sticker.model_in - offsetAssetDuration
  557. sticker.out = sticker.out - offsetAssetDuration
  558. }
  559. BFLog(message: "分割后 创建 filter start :\(sticker.timelineIn) end :\(sticker.timelineOut) type is \(sticker.type)")
  560. stickers.append(sticker)
  561. }
  562. }
  563. }
  564. }
  565. }
  566. }
  567. return stickers
  568. }
  569. }
  570. // MARK: - 同步/下载素材相关
  571. /// 同步/下载素材相关
  572. extension PQStuckPointEditerController {
  573. /// 同步音乐相关数据
  574. /// - Returns: <#description#>
  575. func synchroMusicInfoData() {
  576. if (stuckPointMusicData?.rhythmSdata.count ?? 0) <= 0 {
  577. if synchroMarskView.superview == nil {
  578. UIApplication.shared.keyWindow?.addSubview(synchroMarskView)
  579. }
  580. PQStuckPointViewModel.stuckPointMusicDetailData(musicId: stuckPointMusicData?.musicId ?? "", originType: stuckPointMusicData?.originType ?? 1) { [weak self] newMusicData, _ in
  581. if newMusicData != nil, (newMusicData?.rhythmSdata.count ?? 0) > 0 {
  582. self?.isStuckPointDataSuccess = true
  583. self?.stuckPointMusicData?.rhythmSdata = newMusicData?.rhythmSdata ?? []
  584. self?.stuckPointMusicData?.startTime = newMusicData?.startTime ?? 0
  585. self?.stuckPointMusicData?.endTime = newMusicData?.endTime ?? 0
  586. if newMusicData?.speed != nil {
  587. self?.stuckPointMusicData?.speed = newMusicData?.speed ?? 2
  588. }
  589. if (self?.stuckPointMusicData?.rhythmSdata.count ?? 0) > 0 && (((self?.selectedDataCount ?? 0) - (self?.selectedImageDataCount ?? 0)) > 0 || (self?.selectedImageDataCount ?? 0) > 0 || (self?.selectedTotalDuration ?? 0) > 0) {
  590. self?.stuckPointMusicData?.endTime = (self?.stuckPointMusicData?.startTime ?? 0) + (self?.stuckPointMusicData?.stuckPointCuttingTime(videoCount: (self?.selectedDataCount ?? 0) - (self?.selectedImageDataCount ?? 0), imageCount: self?.selectedImageDataCount ?? 0, totalDuration: self?.selectedTotalDuration ?? 0) ?? 0)
  591. }
  592. self?.stuckPointCuttingView.updateEndTime(startTime: CGFloat(self?.stuckPointMusicData?.startTime ?? 0), endTime: CGFloat(self?.stuckPointMusicData?.endTime ?? 0))
  593. if self?.stuckPointMusicData?.localPath == nil || (self?.stuckPointMusicData?.localPath?.count ?? 0) > 0 {
  594. PQDownloadManager.downLoadFile(url: self?.stuckPointMusicData?.musicPath ?? "") { [weak self] filePath, error in
  595. if error == nil, filePath != nil {
  596. self?.isSynchroMusicInfoSuccess = true
  597. self?.stuckPointMusicData?.localPath = filePath?.replacingOccurrences(of: documensDirectory, with: "")
  598. // 处理所有数据完成
  599. self?.dealWithDataSuccess()
  600. } else {
  601. if self?.synchroMarskView.superview != nil {
  602. self?.synchroMarskView.removeMarskView()
  603. }
  604. // PQUploadRemindView.showUploadRemindView(title: nil, attributedTitle: NSAttributedString(string: "加载音乐失败,请重新选择音乐"), summary: "", confirmTitle: nil) { [weak self] _, _ in
  605. // self?.navigationController?.popViewController(animated: true)
  606. // }
  607. }
  608. }
  609. } else {
  610. self?.isSynchroMusicInfoSuccess = true
  611. // 处理所有数据完成
  612. self?.dealWithDataSuccess()
  613. }
  614. // 添加子视图
  615. self?.addSubViews()
  616. } else {
  617. if self?.synchroMarskView.superview != nil {
  618. self?.synchroMarskView.removeMarskView()
  619. }
  620. // PQUploadRemindView.showUploadRemindView(title: nil, attributedTitle: NSAttributedString(string: "加载音乐失败,请重新选择音乐"), summary: "", confirmTitle: nil) { [weak self] _, _ in
  621. // self?.navigationController?.popViewController(animated: true)
  622. // }
  623. }
  624. }
  625. } else if stuckPointMusicData?.localPath == nil || (stuckPointMusicData?.localPath?.count ?? 0) > 0 {
  626. if synchroMarskView.superview == nil {
  627. UIApplication.shared.keyWindow?.addSubview(synchroMarskView)
  628. }
  629. isStuckPointDataSuccess = true
  630. PQDownloadManager.downLoadFile(url: stuckPointMusicData?.musicPath ?? "") { [weak self] filePath, error in
  631. if error == nil, filePath != nil {
  632. self?.isSynchroMusicInfoSuccess = true
  633. self?.stuckPointMusicData?.localPath = filePath?.replacingOccurrences(of: documensDirectory, with: "")
  634. // 处理所有数据完成
  635. self?.dealWithDataSuccess()
  636. } else {
  637. if self?.synchroMarskView.superview != nil {
  638. self?.synchroMarskView.removeMarskView()
  639. }
  640. // PQUploadRemindView.showUploadRemindView(title: nil, attributedTitle: NSAttributedString(string: "加载音乐失败,请重新选择音乐"), summary: "", confirmTitle: nil) { [weak self] _, _ in
  641. // self?.navigationController?.popViewController(animated: true)
  642. // }
  643. }
  644. }
  645. } else {
  646. isStuckPointDataSuccess = true
  647. // 处理所有数据完成
  648. dealWithDataSuccess()
  649. }
  650. }
  651. /// 导出相册数据
  652. /// - Returns: <#description#>
  653. func exportPhotoData() {
  654. // 取消所有的导出
  655. PQSingletoMemoryUtil.shared.allExportSession.forEach { _, exportSession in
  656. exportSession.cancelExport()
  657. }
  658. var isHaveVideo: Bool = false
  659. if selectedPhotoData != nil, (selectedPhotoData?.count ?? 0) > 0 {
  660. if synchroMarskView.superview == nil {
  661. UIApplication.shared.keyWindow?.addSubview(synchroMarskView)
  662. }
  663. let dispatchGroup = DispatchGroup()
  664. for photo in selectedPhotoData! {
  665. if photo.asset != nil, photo.asset?.mediaType == .video{
  666. if !isHaveVideo {
  667. isHaveVideo = true
  668. }
  669. dispatchGroup.enter()
  670. PQPHAssetVideoParaseUtil.parasToAVAsset(phAsset: photo.asset!) { avAsset, fileSize, _, _ in
  671. if avAsset is AVURLAsset {
  672. // 创建目录
  673. let fileName = (avAsset as! AVURLAsset).url.absoluteString
  674. BFLog(message: "video fileName is\(fileName)")
  675. let tempPhoto = self.selectedPhotoData?.first(where: { material in
  676. material.asset == photo.asset
  677. })
  678. if(fileName.count ) > 0 {
  679. createDirectory(path: photoLibraryDirectory)
  680. let outFilePath = photoLibraryDirectory + fileName.md5 + ".mp4"
  681. // 文件存在先删除老文件
  682. if FileManager.default.fileExists(atPath: outFilePath) {
  683. do {
  684. try FileManager.default.removeItem(at: NSURL.fileURL(withPath: outFilePath))
  685. } catch {
  686. BFLog(message: "导出相册视频-error == \(error)")
  687. }
  688. }
  689. do{
  690. try FileManager.default.copyItem(atPath: fileName.replacingOccurrences(of: "file:///", with: ""), toPath: outFilePath)
  691. print("Success to copy file.")
  692. }catch{
  693. print("Failed to copy file.")
  694. }
  695. tempPhoto?.locationPath = outFilePath.replacingOccurrences(of: documensDirectory, with: "")
  696. BFLog(message: "导出视频相册地址为 \(String(describing: tempPhoto?.locationPath))")
  697. dispatchGroup.leave()
  698. }
  699. }
  700. }
  701. }
  702. }
  703. dispatchGroup.notify(queue: DispatchQueue.main) { [weak self] in
  704. self?.isExportVideosSuccess = true
  705. BFLog(message: "所有相册视频导出成功")
  706. // 处理所有数据完成
  707. if isHaveVideo {
  708. self?.dealWithDataSuccess()
  709. }
  710. }
  711. //只有图片
  712. if(!isHaveVideo){
  713. self.isExportVideosSuccess = true
  714. BFLog(message: "所有相册视频导出成功")
  715. self.dealWithDataSuccess()
  716. }
  717. }
  718. }
  719. /// 处理所有数据完成
  720. /// - Returns: <#description#>
  721. func dealWithDataSuccess() {
  722. BFLog(message: "三组参数:\(isSynchroMusicInfoSuccess) \(isStuckPointDataSuccess) \(isExportVideosSuccess)")
  723. if !isSynchroMusicInfoSuccess || !isStuckPointDataSuccess || !isExportVideosSuccess {
  724. return
  725. }
  726. playeTimeRange = CMTimeRange(start: CMTime(value: CMTimeValue(Int64((stuckPointMusicData?.startTime ?? 0) * 600)), timescale: 600), end: CMTime(value: CMTimeValue(Int64((stuckPointMusicData?.endTime ?? 0) * 600)), timescale: 600))
  727. createPorjectData()
  728. settingPlayerView()
  729. }
  730. }