PQStuckPointEditerController.swift 58 KB

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