PQStuckPointEditerController.swift 107 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064
  1. //
  2. // PQStuckPointEditerController.swift
  3. // PQSpeed
  4. //
  5. // Created by ak on 2021/4/26.
  6. // Copyright © 2021 BytesFlow. All rights reserved.
  7. // 功能:卡点音乐编辑界面
  8. // 创建不同玩法的类型 (1:跳跃卡点,2:快慢速,3:仅配乐)
  9. public enum createStickersModel: Int {
  10. case createStickersModelPoint = 1 // 跳跃卡点
  11. case createStickersModelSpeed = 2 // 快慢速
  12. case createStickersModelOnlyMusic = 3 // 仅配乐
  13. }
  14. import BFCommonKit
  15. import Foundation
  16. import ObjectMapper
  17. import Photos
  18. import RealmSwift
  19. import UIKit
  20. class PQStuckPointEditerController: PQBaseViewController {
  21. // 是否导出视频成功
  22. var isExportVideosSuccess: Bool = false
  23. // 是否请求卡点数据成功
  24. var isStuckPointDataSuccess: Bool = false
  25. // 是否同步音乐成功
  26. var isSynchroMusicInfoSuccess: Bool = false
  27. /// 当前所有的filter
  28. var filters: Array = Array<ImageProcessingOperation>.init()
  29. // 选中所有素材的的总时长 再进入编辑界面时已经不包括图片的时长
  30. var selectedTotalDuration: Float64 = 0
  31. // 选择的总数
  32. var selectedDataCount: Int = 0
  33. // 选择的图片总数
  34. var selectedImageDataCount: Int = 0
  35. // 选中的素材数据
  36. var selectedPhotoData: [PHAsset]? {
  37. didSet {
  38. if selectedPhotoData != nil, (selectedPhotoData?.count ?? 0) > 0 {
  39. selectedMetarialData = Array<PQEditVisionTrackMaterialsModel>.init()
  40. selectedPhotoData?.forEach { phAsset in
  41. let metarialData = PQEditVisionTrackMaterialsModel()
  42. metarialData.asset = phAsset
  43. metarialData.width = Float(phAsset.pixelWidth)
  44. metarialData.itemWidth = Float(phAsset.pixelWidth)
  45. metarialData.height = Float(phAsset.pixelHeight)
  46. metarialData.itemHeight = Float(phAsset.pixelHeight)
  47. if phAsset.mediaType == .image {
  48. metarialData.type = "image"
  49. } else if phAsset.mediaType == .video {
  50. metarialData.type = "video"
  51. metarialData.duration = Float64(phAsset.duration)
  52. }
  53. metarialData.canvasFillType = phAsset.canvasFillType ?? ""
  54. metarialData.locationPath = phAsset.localPath ?? ""
  55. metarialData.selectedIndex = phAsset.selectedIndex ?? 1
  56. metarialData.originalData = phAsset.originalData
  57. // metarialData.coverImageUI = phAsset.image
  58. selectedMetarialData?.append(metarialData)
  59. }
  60. }
  61. }
  62. }
  63. var selectedMetarialData: [PQEditVisionTrackMaterialsModel]?
  64. // 选中的音乐数据
  65. var stuckPointMusicData: PQVoiceModel?
  66. // 保存所有段的所有贴纸,音频信息,用于播放器的渲染使用
  67. var projectModel: PQEditProjectModel = PQEditProjectModel()
  68. // 从草稿箱进入的项目数据
  69. var draftProjectModel: PQEditProjectModel?
  70. var mStickers: [PQEditVisionTrackMaterialsModel]?
  71. // 播放器的开始和结束时间,1,刚进界面使用推荐的开始结束时间,2,用户修改起结点时修改
  72. var playeTimeRange: CMTimeRange = CMTimeRange()
  73. // 首帧图片
  74. var firstFrameImage: UIImage?{
  75. didSet{
  76. if firstFrameImage != nil {
  77. self.playerView.layer.contents = firstFrameImage?.cgImage
  78. }
  79. }
  80. }
  81. // add by ak 是否是再创作模式
  82. var isReCreate: Bool = false
  83. public var reCreateVideoData: PQReCreateModel? // 再创作数据
  84. // 最后一个选择的模式 BTN 用于还原选中状态
  85. var lastEditModelBtn: UIButton?
  86. // add by ak 最大、最小速度 有固定值和自定义,当快慢速下两个值都有效,当跳跃卡点只有maxSpeed有效
  87. // 快慢速模式的 速度设置,快/慢速
  88. var modelSpeed_maxSpeed: Float = 1.0
  89. var modelSpeed_minSpeed: Float = 1.0
  90. //跳跃模式的速度
  91. var modelPoint_speed:Float = 1.0
  92. // 快慢速最后一次选择的速度位置
  93. var lastSpeedSelectIndex: Int = 0
  94. // 跳跃卡点最后一次选择的速度位置
  95. var lastJumpSpeedSelectIndex: Int = 0
  96. // 循环次数设置最后一次选择的位置
  97. var lastCyclesSelectIndex: Int = -1
  98. // 当前选择的玩法模式
  99. var currentCreateStickersModel: createStickersModel = .createStickersModelSpeed
  100. // 最终使用的卡点数据
  101. var finallyStuckPoints: Array = Array<Float>.init()
  102. var finallyStuckPointsInt64: Array = Array<Int64>.init()
  103. // 最终使用的音频时长
  104. var finallyUserAudioTime: Float = 0.0
  105. // 注意推荐时间位置和后面最近的卡点时间与0.3的关系
  106. // 保存丢卡点处理后的卡点信息推荐开始到最后倒数第二个
  107. // 经过档位处理后的卡点信息
  108. var stuckPointsTemp: Array = Array<Float>.init()
  109. var stuckPointsTempInt64: Array = Array<Int64>.init()
  110. // 下一步
  111. lazy var nextBtn: UIButton = {
  112. let nextBtn = UIButton(type: .custom)
  113. nextBtn.frame = CGRect(x: cScreenWidth - 16 - cDefaultMargin * 6, y: cDevice_iPhoneStatusBarHei + (cDevice_iPhoneNavBarHei - cDefaultMargin * 3) / 2, width: cDefaultMargin * 6, height: cDefaultMargin * 3)
  114. nextBtn.setTitle("合成", for: .normal)
  115. nextBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15, weight: .medium)
  116. nextBtn.addTarget(self, action: #selector(nextBtnClick(sender:)), for: .touchUpInside)
  117. nextBtn.backgroundColor = UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)
  118. nextBtn.setTitleColor(UIColor.hexColor(hexadecimal: "#FFFFFF"), for: .normal)
  119. nextBtn.uxy_acceptEventInterval = 0.5
  120. nextBtn.addCorner(corner: 3)
  121. return nextBtn
  122. }()
  123. // 播放器显示 view
  124. lazy var playerView: PQGPUImagePlayerView = {
  125. let playerView = PQGPUImagePlayerView(frame: .zero)
  126. playerView.backgroundColor = PQBFConfig.shared.styleBackGroundColor
  127. playerView.isShowLine = false
  128. playerView.showGaussianBlur = true
  129. playerView.pause()
  130. playerView.renderViewOnClickHandle = { [weak self] in
  131. self?.musicEditBGView.pausePlayer()
  132. }
  133. playerView.playerEmptyView.isHidden = true
  134. return playerView
  135. }()
  136. /// 节奏选择视图
  137. lazy var sustomSwitchView: PQCustomSwitchView = {
  138. let sustomSwitchView = PQCustomSwitchView(frame: CGRect(x: 16, y: 0, width: 180, height: 30), titles: ["慢节奏", "适中", "快节奏"], defaultIndex: stuckPointMusicData?.speed ?? 2)
  139. sustomSwitchView.switchChangeHandle = { [weak self] sender in
  140. // 改变速率,.只有快慢速且非只有图片素材时自动+1处理
  141. self?.stuckPointMusicData?.speed = sender.tag
  142. self?.musicEditBGView.pausePlayer()
  143. self?.projectModel.sData?.getBGMSession()?.sectionTimeline?.audioTrack?.audioTrackMaterials.first?.bgmInfo?.rhythmMusicSpeed = sender.tag
  144. // 播放前先暂停
  145. // self?.playerView.stop()
  146. // 开始播放
  147. self?.settingPlayerView()
  148. // 下面都是统计
  149. if self?.currentCreateStickersModel == .createStickersModelPoint {
  150. PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_shanyinApp_musicVideoPreview_selectMusicVideoRhythm, pageSource: .sp_stuck_previewSyncedUp, extParams: nil, remindmsg: "点击上报:选择节奏")
  151. } else if self?.currentCreateStickersModel == .createStickersModelSpeed {
  152. PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_shanyinApp_musicVideoPreview_selectSpeedRhythm, pageSource: .sp_stuck_previewSyncedUp, extParams: nil, remindmsg: "点击上报:选择节奏")
  153. } else if self?.currentCreateStickersModel == .createStickersModelOnlyMusic {
  154. PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_shanyinApp_musicVideoPreview_selectMusicVideoRepeatRhythm, pageSource: .sp_stuck_previewSyncedUp, extParams: nil, remindmsg: "点击上报:选择节奏")
  155. }
  156. }
  157. return sustomSwitchView
  158. }()
  159. /// 裁剪视图
  160. lazy var stuckPointCuttingView: PQStuckPointCuttingView = {
  161. let stuckPointCuttingView = PQStuckPointCuttingView(frame: CGRect(x: 0, y: optionlineView.frame.minY - 85 - 28, width: view.frame.width, height: 80), duration: CGFloat(Float(stuckPointMusicData?.duration ?? "0") ?? 0), suggestRhythmStartTime: CGFloat(stuckPointMusicData?.suggestRhythmStartTime ?? 0))
  162. /// 裁剪进度回调
  163. stuckPointCuttingView.videoDidBeginDrag = { [weak self] in
  164. BFLog(message: "开始划动")
  165. self?.playerView.pause()
  166. }
  167. /// 播放进度回调
  168. stuckPointCuttingView.videoProgressDidChanged = { [weak self] progress in
  169. BFLog(message: "进度更新返回--progress = \(progress) \(String(describing: self?.playerView.mPlayeTimeRange))")
  170. }
  171. /// 拖缀结束的回调 type - 1-拖动左边裁剪结束 2--拖动右边裁剪结束 3-进度条拖动结束 4-滑动结束
  172. stuckPointCuttingView.videoDidEndDragging = { [weak self] type, startTime, endTime, progress in
  173. BFLog(1, message: "拖拽结束返回--type = \(type),startTime = \(startTime),endTime = \(endTime),progress = \(progress)")
  174. self?.playerView.pause()
  175. self?.musicEditBGView.pausePlayer()
  176. // 修改最新值
  177. self?.stuckPointMusicData?.startTime = Float64(startTime)
  178. self?.stuckPointMusicData?.endTime = Float64(endTime)
  179. // 红的指针完成
  180. if type == 3 {
  181. if CMTimeGetSeconds(self?.playerView.mPlayeTimeRange?.end ?? .zero) == 0 {
  182. BFLog(message: "mPlayeTimeRange is error")
  183. return
  184. }
  185. let newBeginSconds = (Double(startTime) + (Double(endTime) - Double(startTime)) * Double(progress)) * 600
  186. BFLog(message: " newBeginSconds is \(newBeginSconds)")
  187. let seekTimeRange: CMTimeRange = CMTimeRange(start: CMTime(value: CMTimeValue(Int64(newBeginSconds)), timescale: 600), end:
  188. CMTime(value: CMTimeValue(Int64(endTime * 600)), timescale: 600))
  189. BFLog(message: "修改的开始 \(CMTimeGetSeconds(seekTimeRange.start)) 结束 \(CMTimeGetSeconds(seekTimeRange.end))")
  190. // 重新设置有效缓存
  191. self?.playerView.configCache(beginTime: CMTimeGetSeconds(seekTimeRange.start))
  192. self?.playerView.play(pauseFirstFrame: false, playeTimeRange: seekTimeRange)
  193. } else {
  194. // 更改素材开始时间及结束时间
  195. self?.projectModel.sData?.getBGMSession()?.sectionTimeline?.audioTrack?.audioTrackMaterials.first?.out = Float64(endTime)
  196. self?.projectModel.sData?.getBGMSession()?.sectionTimeline?.audioTrack?.audioTrackMaterials.first?.model_in = Float64(startTime)
  197. self?.projectModel.sData?.getBGMSession()?.sectionTimeline?.audioTrack?.audioTrackMaterials.first?.timelineIn = Float64(startTime)
  198. self?.projectModel.sData?.getBGMSession()?.sectionTimeline?.audioTrack?.audioTrackMaterials.first?.timelineOut = Float64(endTime)
  199. BFLog(message: "调整后总时长: \(endTime - startTime) startTime:\(startTime) endTime:\(endTime)")
  200. // 初始化音频的开始和结束时间
  201. self?.playeTimeRange = CMTimeRange(start: CMTimeMakeWithSeconds(Float64(startTime), preferredTimescale: BASE_FILTER_TIMESCALE), end: CMTimeMakeWithSeconds(Float64(endTime), preferredTimescale: BASE_FILTER_TIMESCALE))
  202. self?.dealParameter(model: self?.currentCreateStickersModel ?? .createStickersModelSpeed)
  203. if((self?.finallyStuckPoints.count ?? 0) < 1){
  204. BFLog(message: "finallyStuckPoints data is error!!!!")
  205. return
  206. }
  207. DispatchQueue.global().async { // 并行、异步
  208. let beginTime = Date()
  209. 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)), model: self?.currentCreateStickersModel ?? .createStickersModelSpeed)
  210. DispatchQueue.main.async { // 串行、异步
  211. self?.playerView.mStickers = self?.mStickers
  212. BFLog(message: "endTime is endTimeendTime \(Date().timeIntervalSince(beginTime))")
  213. self?.playerView.play(pauseFirstFrame: false, playeTimeRange: self!.playeTimeRange)
  214. // 更新一下时间条的UI总时间 及数据
  215. self?.stuckPointCuttingView.videoDuration = CGFloat(self?.finallyUserAudioTime ?? 0)
  216. self?.stuckPointCuttingView.stuckPointStartTime = CGFloat(CMTimeGetSeconds(self?.playeTimeRange.start ?? .zero))
  217. self?.stuckPointCuttingView.stuckPointEndTime = CGFloat(CMTimeGetSeconds(self?.playeTimeRange.end ?? .zero))
  218. self?.stuckPointCuttingView.tatalTimeLabel.text = "\(Float64(CMTimeGetSeconds(self?.playeTimeRange.end ?? .zero) - CMTimeGetSeconds(self?.playeTimeRange.start ?? .zero)).formatDurationToHMS())"
  219. }
  220. }
  221. }
  222. }
  223. return stuckPointCuttingView
  224. }()
  225. /// 卡点模式标题
  226. lazy var pointEditRemindLab: UILabel = {
  227. let pointEditRemindLab = UILabel()
  228. pointEditRemindLab.backgroundColor = .clear
  229. pointEditRemindLab.textAlignment = .left
  230. pointEditRemindLab.font = UIFont.boldSystemFont(ofSize: 14)
  231. pointEditRemindLab.textColor = .black
  232. pointEditRemindLab.text = "卡点模式"
  233. return pointEditRemindLab
  234. }()
  235. /// 卡点模式标题
  236. lazy var speedTitleLab: UILabel = {
  237. let speedTitleLab = UILabel()
  238. speedTitleLab.backgroundColor = .clear
  239. speedTitleLab.textAlignment = .left
  240. speedTitleLab.font = UIFont.boldSystemFont(ofSize: 14)
  241. speedTitleLab.textColor = .black
  242. speedTitleLab.text = "节奏变化"
  243. return speedTitleLab
  244. }()
  245. /// 卡点模式下方操作区背景
  246. lazy var pointEditBGView: UIView = {
  247. let pointEditBGView = UIView()
  248. pointEditBGView.backgroundColor = .clear
  249. return pointEditBGView
  250. }()
  251. /// 下方音乐编辑操作区背景
  252. lazy var musicEditBGView: PQSelecteMusicView = {
  253. let musicEditBGView = PQSelecteMusicView()
  254. musicEditBGView.backgroundColor = .clear
  255. musicEditBGView.isUserInteractionEnabled = true
  256. musicEditBGView.isHidden = true
  257. musicEditBGView.musicSearchBtn.addTarget(self, action: #selector(musicSearchBtnClick(sender:)), for: .touchUpInside)
  258. musicEditBGView.didSelectItemHandle = { [weak self] status in
  259. if status == .isSelected{
  260. if self?.playerView.status != .playing{
  261. self?.playerView.RenderViewOnclick()
  262. }
  263. }else{
  264. self?.playerView.pause()
  265. }
  266. }
  267. musicEditBGView.btnClickHandle = { [weak self] _, bgmData in
  268. // 使用音乐
  269. self?.userstuckPointMusic(musicData: bgmData as? PQVoiceModel)
  270. }
  271. return musicEditBGView
  272. }()
  273. // 卡点编辑 btn
  274. lazy var pointEditerBtn: UIButton = {
  275. let pointEdterBtn = UIButton(type: .custom)
  276. pointEdterBtn.setImage(UIImage().BF_Image(named: "pointEditerBtn_n"), for: .normal)
  277. pointEdterBtn.setImage(UIImage().BF_Image(named: "pointEditerBtn_h").withRenderingMode(.alwaysTemplate), for: .selected)
  278. pointEdterBtn.tintColor = UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)
  279. pointEdterBtn.addTarget(self, action: #selector(pointEditerBtnClick(sender:)), for: .touchUpInside)
  280. pointEdterBtn.isSelected = true
  281. pointEdterBtn.adjustsImageWhenHighlighted = false
  282. return pointEdterBtn
  283. }()
  284. // 音乐编辑 btn
  285. lazy var musicEditerBtn: UIButton = {
  286. let musicEditerBtn = UIButton(type: .custom)
  287. musicEditerBtn.setImage(UIImage().BF_Image(named: "musicEditerBtn_n"), for: .normal)
  288. musicEditerBtn.setImage(UIImage().BF_Image(named: "musicEditerBtn_h").withRenderingMode(.alwaysTemplate), for: .selected)
  289. musicEditerBtn.tintColor = UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)
  290. musicEditerBtn.addTarget(self, action: #selector(musicEditerBtnClick(sender:)), for: .touchUpInside)
  291. musicEditerBtn.adjustsImageWhenHighlighted = false
  292. return musicEditerBtn
  293. }()
  294. // 快慢速卡点模式 btn
  295. lazy var speedStuckBtn: UIButton = {
  296. let speedStuckBtn = UIButton(type: .custom)
  297. speedStuckBtn.addTarget(self, action: #selector(editModelClick1(sender:)), for: .touchUpInside)
  298. speedStuckBtn.setTitle("快慢速卡点", for: .normal)
  299. speedStuckBtn.titleLabel?.font = UIFont.systemFont(ofSize: 13, weight: .regular)
  300. jumpPointBtn.backgroundColor = PQBFConfig.shared.pointEditNamalBackgroundColor
  301. speedStuckBtn.setTitleColor(UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue), for: .selected)
  302. speedStuckBtn.setTitleColor(UIColor.hexColor(hexadecimal: "#959595"), for: .normal)
  303. speedStuckBtn.addCorner(corner: 5)
  304. speedStuckBtn.imagePosition(at: .top, space: 8)
  305. speedStuckBtn.tag = 1
  306. speedStuckBtn.adjustsImageWhenHighlighted = false
  307. speedStuckBtn.setImage(UIImage.moduleImage(named: PQBFConfig.shared.speedStuckBtnImage_N, moduleName: "BFFramework", isAssets: false), for: .normal)
  308. speedStuckBtn.setImage(UIImage.moduleImage(named: PQBFConfig.shared.speedStuckBtnImage_H, moduleName: "BFFramework", isAssets: false), for: .selected)
  309. return speedStuckBtn
  310. }()
  311. // //
  312. // lazy var speedStuckBtnGif: UIImageView = {
  313. // let speedStuckBtnGif = UIImageView()
  314. // speedStuckBtnGif.kf.setImage(with: URL(fileURLWithPath: currentBundlePath()!.path(forResource: "speedstuck_h", ofType: "gif")!))
  315. // speedStuckBtnGif.isHidden = true
  316. // return speedStuckBtnGif
  317. //
  318. // }()
  319. // 跳转卡点模式 btn
  320. lazy var jumpPointBtn: UIButton = {
  321. let jumpPointBtn = UIButton(type: .custom)
  322. jumpPointBtn.setTitle("跳跃卡点", for: .normal)
  323. jumpPointBtn.titleLabel?.font = UIFont.systemFont(ofSize: 13, weight: .regular)
  324. jumpPointBtn.backgroundColor = PQBFConfig.shared.pointEditNamalBackgroundColor
  325. jumpPointBtn.setTitleColor(UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue), for: .selected)
  326. jumpPointBtn.setTitleColor(UIColor.hexColor(hexadecimal: "#959595"), for: .normal)
  327. jumpPointBtn.imagePosition(at: .top, space: 8)
  328. jumpPointBtn.addCorner(corner: 5)
  329. jumpPointBtn.tag = 2
  330. jumpPointBtn.addTarget(self, action: #selector(editModelClick1(sender:)), for: .touchUpInside)
  331. jumpPointBtn.adjustsImageWhenHighlighted = false
  332. jumpPointBtn.setImage(UIImage.moduleImage(named: PQBFConfig.shared.jumpPointBtnImage_N, moduleName: "BFFramework", isAssets: false), for: .normal)
  333. jumpPointBtn.setImage(UIImage.moduleImage(named: PQBFConfig.shared.jumpPointBtnImage_H, moduleName: "BFFramework", isAssets: false), for: .selected)
  334. return jumpPointBtn
  335. }()
  336. // lazy var jumpPointBtnGif: UIImageView = {
  337. // let jumpPointBtnGif = UIImageView()
  338. // jumpPointBtnGif.kf.setImage(with: URL(fileURLWithPath: currentBundlePath()!.path(forResource: "jumpPoint_n", ofType: "gif")!))
  339. // jumpPointBtnGif.isHidden = true
  340. // return jumpPointBtnGif
  341. //
  342. // }()
  343. // 仅配乐模式 btn
  344. lazy var onlyMusicBtn: UIButton = {
  345. let onlyMusicBtn = UIButton(type: .custom)
  346. onlyMusicBtn.setTitle("仅配乐", for: .normal)
  347. onlyMusicBtn.titleLabel?.font = UIFont.systemFont(ofSize: 13, weight: .regular)
  348. onlyMusicBtn.backgroundColor = PQBFConfig.shared.pointEditNamalBackgroundColor
  349. onlyMusicBtn.setTitleColor(UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue), for: .selected)
  350. onlyMusicBtn.setTitleColor(UIColor.hexColor(hexadecimal: "#959595"), for: .normal)
  351. onlyMusicBtn.addCorner(corner: 5)
  352. onlyMusicBtn.tag = 3
  353. onlyMusicBtn.addTarget(self, action: #selector(editModelClick1(sender:)), for: .touchUpInside)
  354. onlyMusicBtn.adjustsImageWhenHighlighted = false
  355. onlyMusicBtn.setImage(UIImage.moduleImage(named: PQBFConfig.shared.onlyMusicBtnImage_N, moduleName: "BFFramework", isAssets: false), for: .normal)
  356. onlyMusicBtn.setImage(UIImage.moduleImage(named: PQBFConfig.shared.onlyMusicBtnImage_H, moduleName: "BFFramework", isAssets: false), for: .selected)
  357. return onlyMusicBtn
  358. }()
  359. // 操作面板上的分割线
  360. lazy var optionlineView: UIView = {
  361. let optionlineView = UIView()
  362. optionlineView.backgroundColor = PQBFConfig.shared.pointEditNamalBackgroundColor
  363. return optionlineView
  364. }()
  365. // 固定速度 UI
  366. lazy var speedSettingView: PQSpeedSettingView = {
  367. let speedSetView = PQSpeedSettingView()
  368. speedSetView.backgroundColor = .clear
  369. speedSetView.selectSpeedCallBack = { [weak self] maxSpeed, minSpeed, selectIndex, isSettingPlayer in
  370. BFLog(message: "固定maxSpeed is\(maxSpeed) minSpeed \(minSpeed)")
  371. self?.musicEditBGView.pausePlayer()
  372. if maxSpeed == -1.0 && minSpeed == -1.0 {
  373. self?.customSpeedSettingView.isHidden = false
  374. self?.customSpeedSettingView.viewType = self?.speedSettingView.viewType ?? 2
  375. } else {
  376. if maxSpeed != 0.0 {
  377. // 更新最后一次选择的位置恢复时使用
  378. if self?.speedSettingView.viewType == 1 {
  379. self?.lastSpeedSelectIndex = selectIndex
  380. self?.modelSpeed_maxSpeed = maxSpeed
  381. self?.modelSpeed_minSpeed = minSpeed
  382. } else if self?.speedSettingView.viewType == 2 {
  383. self?.lastJumpSpeedSelectIndex = selectIndex
  384. self?.modelPoint_speed = maxSpeed
  385. } else {
  386. self?.lastCyclesSelectIndex = selectIndex
  387. }
  388. } else {
  389. BFLog(message: "设置速度无效")
  390. }
  391. }
  392. if isSettingPlayer {
  393. self?.settingPlayerView()
  394. }
  395. }
  396. return speedSetView
  397. }()
  398. // 自定义速度
  399. lazy var customSpeedSettingView: PQCustomSpeedSettingView = {
  400. let customSpeedSetView = PQCustomSpeedSettingView(frame: CGRect(x: 0, y: cScreenHeigth - 354, width: cScreenWidth, height: 354))
  401. customSpeedSetView.isHidden = true
  402. customSpeedSetView.selectSpeedCallBack = { [weak self, weak customSpeedSetView] maxSpeed, minSpeed, isJumpSpeedModel, isCancle in
  403. if !isCancle {
  404. BFLog(message: "自定义速度maxSpeed is\(maxSpeed) minSpeed \(minSpeed) \(isJumpSpeedModel)")
  405. self?.musicEditBGView.pausePlayer()
  406. // 自定定义的更新一下最后的选择位置
  407. if self?.speedSettingView.viewType == 1 {
  408. self?.lastSpeedSelectIndex = -1
  409. self?.modelSpeed_maxSpeed = maxSpeed
  410. self?.modelSpeed_minSpeed = minSpeed
  411. } else if self?.speedSettingView.viewType == 2 {
  412. self?.lastJumpSpeedSelectIndex = -1
  413. self?.modelPoint_speed = maxSpeed
  414. } else {
  415. self?.lastCyclesSelectIndex = Int(maxSpeed - 1)
  416. }
  417. self?.settingPlayerView()
  418. // 确认后 选中自定义
  419. self?.speedSettingView.selectCustom()
  420. } else {
  421. // 取消后恢复上一次选择的位置
  422. if self?.speedSettingView.viewType == 1 {
  423. self?.speedSettingView.setSelectItem(index: self?.lastSpeedSelectIndex ?? 0, isSettingPlayer: false)
  424. } else if self?.speedSettingView.viewType == 2 {
  425. self?.speedSettingView.setSelectItem(index: self?.lastJumpSpeedSelectIndex ?? 0, isSettingPlayer: false)
  426. } else {
  427. self?.speedSettingView.setSelectItem(index: self?.lastCyclesSelectIndex ?? 0, isSettingPlayer: false)
  428. }
  429. customSpeedSetView?.isHidden = true
  430. }
  431. }
  432. return customSpeedSetView
  433. }()
  434. /// 音乐标题
  435. lazy var musicNameView: UIView = {
  436. let musicNameView = UIView()
  437. musicNameView.addSubview(musicNameLab)
  438. let nameWidth: CGFloat = musicNameLab.frame.width + (25 + cDefaultMargin * 3)
  439. musicNameView.frame = CGRect(x: (view.frame.width - nameWidth) / 2, y: cDevice_iPhoneStatusBarHei + (cDevice_iPhoneNavBarHei - cDefaultMargin * 3) / 2, width: nameWidth, height: cDefaultMargin * 3)
  440. // musicNameView.backgroundColor = UIColor.hexColor(hexadecimal: "#333333")
  441. musicNameView.addCorner(corner: musicNameView.frame.height / 2)
  442. let musicImageView = UIImageView()
  443. musicImageView.tintColor = PQBFConfig.shared.styleTitleColor
  444. musicImageView.image = UIImage.moduleImage(named: "stuckPoint_reCreate_music", moduleName: "BFFramework", isAssets: false)?.withRenderingMode(.alwaysTemplate)
  445. musicImageView.frame = CGRect(x: musicNameView.frame.height / 2 - 5, y: (musicNameView.frame.height - 22) / 2, width: 22, height: 22)
  446. musicNameView.addSubview(musicImageView)
  447. musicNameLab.frame.origin.x = musicImageView.frame.maxX + 5
  448. return musicNameView
  449. }()
  450. /// 音乐歌曲名称
  451. lazy var musicNameLab: LMJHorizontalScrollText = {
  452. 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
  453. let musicNameLab = LMJHorizontalScrollText(frame: CGRect(x: 0, y: 0, width: nameWidth < cDefaultMargin * 4 ? cDefaultMargin * 4 : nameWidth, height: cDefaultMargin * 3))
  454. musicNameLab.textColor = PQBFConfig.shared.styleTitleColor
  455. musicNameLab.textFont = UIFont.systemFont(ofSize: 13)
  456. musicNameLab.speed = 0.03
  457. musicNameLab.moveDirection = LMJTextScrollMoveLeft
  458. musicNameLab.moveMode = LMJTextScrollContinuous
  459. if nameWidth < cDefaultMargin * 4 {
  460. musicNameLab.text = " \(stuckPointMusicData?.musicName ?? "") "
  461. } else {
  462. musicNameLab.text = " \(stuckPointMusicData?.musicName ?? "") "
  463. }
  464. return musicNameLab
  465. }()
  466. /// 同步进度显示
  467. lazy var synchroMarskView: PQStuckPointLoadingView = {
  468. var synchroMarskView = PQStuckPointLoadingView(frame: CGRect(x: 0, y: 0, width: cScreenWidth, height: cScreenHeigth))
  469. synchroMarskView.cancelHandle = { [weak self] _ in
  470. self?.navigationController?.popViewController(animated: true)
  471. }
  472. return synchroMarskView
  473. }()
  474. @objc func willEnterForeground() {
  475. BFLog(message: "进入到前台")
  476. if(self.navigationController?.topViewController == self){
  477. if projectModel.sData!.sections.count > 0 {
  478. settingPlayerView()
  479. }else {
  480. prepareMeta()
  481. }
  482. }
  483. }
  484. @objc func enterBackground() {
  485. BFLog(message: "进入到后台")
  486. // 取消导出
  487. if(self.navigationController?.topViewController == self){
  488. playerView.pause()
  489. }
  490. }
  491. override func backBtnClick() {
  492. super.backBtnClick()
  493. // playerView.pause()
  494. // 点击上报:返回按钮
  495. PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_click_back, pageSource: .sp_stuck_previewSyncedUp, extParams: nil, remindmsg: "卡点视频数据上报-(点击上报:返回按钮)")
  496. }
  497. // 使用选择音乐 调用情况:1,操作面板直接选择 ,2 搜索界面点击使用
  498. func userstuckPointMusic(musicData: PQVoiceModel?) {
  499. // 1,音乐面板点击了使用
  500. stuckPointMusicData = musicData
  501. // 2,同步最新音乐数据
  502. synchroMusicInfoData(resetSelectIndex: false)
  503. // 3,更新音乐标题UI
  504. 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
  505. if nameWidth < cDefaultMargin * 4 {
  506. musicNameLab.text = " \(stuckPointMusicData?.musicName ?? "") "
  507. } else {
  508. musicNameLab.text = " \(stuckPointMusicData?.musicName ?? "") "
  509. }
  510. // 更新一下节奏的 UI
  511. sustomSwitchView.selectOneBtn(Index: (stuckPointMusicData?.speed ?? 2))
  512. }
  513. // 点击搜索音乐
  514. @objc func musicSearchBtnClick(sender _: UIButton) {
  515. let searchVC = PQEditMusicSearchController()
  516. searchVC.btnClickHandle = { [weak self] _, bgmData in
  517. // 使用音乐
  518. BFLog(message: "搜索音乐点击了使用")
  519. self?.musicEditBGView.insertSearchMusic(model: bgmData as! PQVoiceModel)
  520. self?.userstuckPointMusic(musicData: bgmData as? PQVoiceModel)
  521. }
  522. let navigationController: UINavigationController = UINavigationController(rootViewController: searchVC)
  523. navigationController.modalPresentationStyle = .fullScreen
  524. present(navigationController, animated: true, completion: nil)
  525. }
  526. // 卡点编辑
  527. @objc func pointEditerBtnClick(sender: UIButton) {
  528. if sender.isSelected { return }
  529. sender.isSelected = !sender.isSelected
  530. musicEditerBtn.isSelected = false
  531. pointEditBGView.isHidden = false
  532. musicEditBGView.isHidden = true
  533. PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_shanyinApp_musicVideoPreview_videoTab, pageSource: .sp_shanyinApp_main, extParams: nil, remindmsg: "")
  534. }
  535. // 音乐编辑
  536. @objc func musicEditerBtnClick(sender: UIButton) {
  537. if sender.isSelected { return }
  538. sender.isSelected = !sender.isSelected
  539. pointEditerBtn.isSelected = false
  540. pointEditBGView.isHidden = true
  541. musicEditBGView.showData()
  542. PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_shanyinApp_musicVideoPreview_musicTab, pageSource: .sp_shanyinApp_main, extParams: nil, remindmsg: "")
  543. }
  544. @objc func editModelClick1(sender: UIButton) {
  545. editModelClick(sender: sender)
  546. }
  547. // 三种模式修改
  548. @objc func editModelClick(sender: UIButton, reportLog: Bool = true) {
  549. // if sender.isSelected {
  550. // BFLog(message: "已经是选中状态")
  551. // return “”
  552. // }
  553. self.musicEditBGView.pausePlayer()
  554. sharedImageProcessingContext.framebufferCache.purgeAllUnassignedFramebuffers()
  555. if sender == jumpPointBtn && selectedTotalDuration < 6 && selectedDataCount != selectedImageDataCount && reCreateVideoData == nil{
  556. cShowHUB(superView: view, msg: "素材时长需要大于6秒才\n可选择“跳跃卡点”模式")
  557. return
  558. }
  559. lastEditModelBtn?.isSelected = false
  560. //设置取消选中的背景色
  561. lastEditModelBtn?.backgroundColor = PQBFConfig.shared.pointEditNamalBackgroundColor
  562. sender.isSelected = !sender.isSelected
  563. lastEditModelBtn = sender
  564. //设置选中的背景色
  565. let styleColor = UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)
  566. lastEditModelBtn?.backgroundColor = UIColor(red: styleColor.rgbaf[0], green: styleColor.rgbaf[1], blue: styleColor.rgbaf[2], alpha: 0.15)
  567. BFLog(message: "sender tag is \(sender.tag)")
  568. // 1 ui 调整
  569. if sender.tag == 1 || sender.tag == 2 {
  570. speedSettingView.viewType = sender.tag
  571. customSpeedSettingView.viewType = speedSettingView.viewType
  572. speedSettingView.snp.remakeConstraints { make in
  573. make.left.equalToSuperview().offset(16)
  574. make.right.equalToSuperview()
  575. make.top.equalTo(onlyMusicBtn.snp.bottom).offset(10)
  576. make.height.equalTo(sender.tag == 1 ? 44 : 30)
  577. }
  578. speedSettingView.isHidden = false
  579. speedTitleLab.isHidden = false
  580. sustomSwitchView.isHidden = false
  581. } else {
  582. speedTitleLab.isHidden = true
  583. speedSettingView.isHidden = true
  584. sustomSwitchView.isHidden = true
  585. }
  586. // 2素材全是图片的时候三个模式都显示循环设置 UI
  587. if selectedDataCount == selectedImageDataCount {
  588. speedSettingView.viewType = 3
  589. customSpeedSettingView.viewType = speedSettingView.viewType
  590. speedSettingView.snp.remakeConstraints { make in
  591. make.left.equalToSuperview().offset(16)
  592. make.right.equalToSuperview()
  593. make.top.equalTo(onlyMusicBtn.snp.bottom).offset(10)
  594. make.height.equalTo(30)
  595. }
  596. speedSettingView.isHidden = false
  597. speedTitleLab.isHidden = false
  598. sustomSwitchView.isHidden = false
  599. if lastCyclesSelectIndex != -1 {
  600. speedSettingView.setSelectItem(index: lastCyclesSelectIndex, isSettingPlayer: false)
  601. }
  602. } else {
  603. if sender.tag == 1 { // 快慢速
  604. speedSettingView.setSelectItem(index: lastSpeedSelectIndex, isSettingPlayer: false, setDisable: (selectedTotalDuration < 6 && selectedDataCount != selectedImageDataCount) ? true : false)
  605. } else if sender.tag == 2 { // 跳跃卡点
  606. speedSettingView.setSelectItem(index: lastJumpSpeedSelectIndex, isSettingPlayer: false)
  607. } else if sender.tag == 3 { // 仅音乐
  608. // customSpeedSettingView.isJumpSpeedModel = false
  609. }
  610. }
  611. // 3 设置 btn 不同显示状态
  612. var speedStuckBtnGifName = ""
  613. var jumpPointBtnGifName = ""
  614. if sender.tag == 1 { // 快慢速
  615. speedStuckBtnGifName = "speedstuck_h_pq"
  616. jumpPointBtnGifName = "jumpPoint_n_pq"
  617. currentCreateStickersModel = .createStickersModelSpeed
  618. if reportLog {
  619. PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_shanyinApp_musicVideoPreview_selectPatternSpeed, pageSource: .sp_shanyinApp_main, extParams: nil, remindmsg: "")
  620. }
  621. } else if sender.tag == 2 { // 跳跃卡点
  622. speedStuckBtnGifName = "speedstuck_n_pq"
  623. jumpPointBtnGifName = "jumpPoint_h_pq"
  624. currentCreateStickersModel = .createStickersModelPoint
  625. if reportLog {
  626. PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_shanyinApp_musicVideoPreview_selectPatternMusicVideo, pageSource: .sp_shanyinApp_main, extParams: nil, remindmsg: "")
  627. }
  628. } else if sender.tag == 3 { // 仅音乐
  629. speedStuckBtnGifName = "speedstuck_n_pq"
  630. jumpPointBtnGifName = "jumpPoint_n_pq"
  631. currentCreateStickersModel = .createStickersModelOnlyMusic
  632. if reportLog {
  633. PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_shanyinApp_musicVideoPreview_selectPatternBgm, pageSource: .sp_shanyinApp_main, extParams: nil, remindmsg: "")
  634. }
  635. }
  636. settingPlayerView()
  637. }
  638. override func viewWillAppear(_ animated: Bool) {
  639. super.viewWillAppear(animated)
  640. lineView?.isHidden = true
  641. DispatchQueue.main.async {
  642. UIApplication.shared.isIdleTimerDisabled = true
  643. }
  644. musicNameLab.move()
  645. PQNotification.addObserver(self, selector: #selector(enterBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
  646. PQNotification.addObserver(self, selector: #selector(willEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)
  647. }
  648. override func viewWillDisappear(_ animated: Bool) {
  649. super.viewWillDisappear(animated)
  650. DispatchQueue.main.async {
  651. UIApplication.shared.isIdleTimerDisabled = false
  652. }
  653. musicNameLab.stop()
  654. playerView.pause()
  655. musicEditBGView.pausePlayer()
  656. PQNotification.removeObserver(self)
  657. self.synchroMarskView.removeMarskView()
  658. }
  659. override func viewDidLoad() {
  660. super.viewDidLoad()
  661. leftButton(image: nil, tintColor: PQBFConfig.shared.styleTitleColor)
  662. navHeadImageView?.addSubview(nextBtn)
  663. navHeadImageView?.addSubview(musicNameView)
  664. // 添加子视图
  665. addSubViews()
  666. prepareMeta()
  667. // 曝光上报:预览页面曝光上报
  668. PQEventTrackViewModel.baseReportUpload(businessType: .bt_windowView, objectType: .ot_view_previewSyncedUp, pageSource: .sp_stuck_previewSyncedUp, extParams: nil, remindmsg: "卡点视频数据上报-(曝光上报:预览页面曝光上报)")
  669. //从选择的素材中 第一个素材设置封面
  670. if selectedPhotoData != nil && selectedPhotoData!.count > 0 {
  671. let photo = selectedPhotoData!.first!
  672. let option = PHImageRequestOptions()
  673. option.isNetworkAccessAllowed = true //允许下载iCloud的图片
  674. option.resizeMode = .none
  675. option.deliveryMode = .highQualityFormat
  676. PHImageManager.default().requestImage(for: photo,
  677. targetSize: CGSize(width: 1920, height: 1920),
  678. contentMode: .aspectFit,
  679. options: option)
  680. { [weak self] (image, nil) in
  681. //image就是图片
  682. if image != nil {
  683. self?.firstFrameImage = image
  684. }
  685. }
  686. }
  687. }
  688. override func viewDidLayoutSubviews() {
  689. super.viewDidLayoutSubviews()
  690. playerView.resetCanvasFrame(frame: coculationPlayViewRect())
  691. }
  692. func prepareMeta(){
  693. // 导出相册视频
  694. exportPhotoData()
  695. // 同步音乐数据
  696. synchroMusicInfoData()
  697. // 插入选择的音乐信息
  698. musicEditBGView.firstInsertVoiceModel = stuckPointMusicData!
  699. }
  700. /// 添加子视图
  701. /// - Returns: <#description#>
  702. func addSubViews() {
  703. if (stuckPointMusicData?.rhythmSdata.count ?? 0) <= 0 {
  704. return
  705. }
  706. view.addSubview(playerView)
  707. view.addSubview(pointEditBGView)
  708. view.addSubview(musicEditBGView)
  709. view.addSubview(optionlineView)
  710. view.addSubview(pointEditerBtn)
  711. view.addSubview(musicEditerBtn)
  712. view.addSubview(customSpeedSettingView)
  713. // 卡点
  714. pointEditBGView.addSubview(pointEditRemindLab)
  715. pointEditBGView.addSubview(speedTitleLab)
  716. pointEditBGView.addSubview(speedStuckBtn)
  717. pointEditBGView.addSubview(jumpPointBtn)
  718. pointEditBGView.addSubview(onlyMusicBtn)
  719. pointEditBGView.addSubview(speedSettingView)
  720. pointEditBGView.addSubview(sustomSwitchView)
  721. // 音乐
  722. musicEditBGView.addSubview(stuckPointCuttingView)
  723. pointEditerBtn.snp.makeConstraints { make in
  724. make.left.equalToSuperview().offset(100)
  725. make.bottom.equalToSuperview().offset(-(14 + cAKSafeAreaHeight))
  726. make.height.equalTo(24)
  727. make.width.equalTo(24)
  728. }
  729. musicEditerBtn.snp.makeConstraints { make in
  730. make.right.equalToSuperview().offset(-100)
  731. make.top.equalTo(pointEditerBtn.snp.top)
  732. make.height.equalTo(24)
  733. make.width.equalTo(24)
  734. }
  735. pointEditBGView.snp.makeConstraints { make in
  736. make.left.right.equalTo(view)
  737. make.bottom.equalTo(pointEditerBtn.snp.top).offset(-7)
  738. make.height.equalTo(290)
  739. }
  740. musicEditBGView.snp.makeConstraints { make in
  741. make.left.right.equalToSuperview()
  742. make.bottom.equalTo(pointEditerBtn.snp.top).offset(-7)
  743. make.height.equalTo(290)
  744. }
  745. optionlineView.snp.makeConstraints { make in
  746. make.left.right.equalToSuperview()
  747. make.bottom.equalTo(pointEditBGView.snp.bottom).offset(-1)
  748. make.height.equalTo(1)
  749. }
  750. stuckPointCuttingView.snp.makeConstraints { make in
  751. make.left.right.equalToSuperview()
  752. make.bottom.equalTo(musicEditBGView.snp.bottom).offset(-1)
  753. make.height.equalTo(85)
  754. }
  755. pointEditRemindLab.snp.makeConstraints { make in
  756. make.left.equalToSuperview().offset(16)
  757. make.top.equalToSuperview().offset(16)
  758. make.height.equalTo(20)
  759. make.width.equalTo(80)
  760. }
  761. speedStuckBtn.snp.makeConstraints { make in
  762. make.left.equalToSuperview().offset(16)
  763. make.top.equalTo(pointEditRemindLab.snp.bottom).offset(8)
  764. make.height.equalTo(80)
  765. make.width.equalTo(80)
  766. }
  767. jumpPointBtn.snp.makeConstraints { make in
  768. make.left.equalTo(speedStuckBtn.snp.right).offset(10)
  769. make.top.equalTo(speedStuckBtn.snp.top)
  770. make.height.equalTo(80)
  771. make.width.equalTo(80)
  772. }
  773. onlyMusicBtn.snp.makeConstraints { make in
  774. make.left.equalTo(jumpPointBtn.snp.right).offset(10)
  775. make.top.equalTo(speedStuckBtn.snp.top)
  776. make.height.equalTo(80)
  777. make.width.equalTo(64)
  778. }
  779. //重新设置三个模式 btn 图片和title的位置
  780. speedStuckBtn.titleEdgeInsets = UIEdgeInsets(top: 0, left: -(speedStuckBtn.imageView?.frame.size.width ?? 0) , bottom: -(speedStuckBtn.imageView?.frame.size.height ?? 0), right: 0);
  781. speedStuckBtn.imageEdgeInsets = UIEdgeInsets(top: -(speedStuckBtn.titleLabel?.intrinsicContentSize.height ?? 0), left: 0, bottom: 0, right: -(speedStuckBtn.titleLabel?.intrinsicContentSize.width ?? 0));
  782. jumpPointBtn.titleEdgeInsets = UIEdgeInsets(top: 0, left: -(jumpPointBtn.imageView?.frame.size.width ?? 0) , bottom: -(jumpPointBtn.imageView?.frame.size.height ?? 0), right: 0);
  783. jumpPointBtn.imageEdgeInsets = UIEdgeInsets(top: -(jumpPointBtn.titleLabel?.intrinsicContentSize.height ?? 0), left: 0, bottom: 0, right: -(jumpPointBtn.titleLabel?.intrinsicContentSize.width ?? 0));
  784. onlyMusicBtn.titleEdgeInsets = UIEdgeInsets(top: 0, left: -(onlyMusicBtn.imageView?.frame.size.width ?? 0) , bottom: -(onlyMusicBtn.imageView?.frame.size.height ?? 0), right: 0);
  785. onlyMusicBtn.imageEdgeInsets = UIEdgeInsets(top: -(onlyMusicBtn.titleLabel?.intrinsicContentSize.height ?? 0), left: 0, bottom: 0, right: -(onlyMusicBtn.titleLabel?.intrinsicContentSize.width ?? 0));
  786. speedSettingView.snp.makeConstraints { make in
  787. make.left.equalToSuperview().offset(16)
  788. make.right.equalToSuperview()
  789. make.top.equalTo(onlyMusicBtn.snp.bottom).offset(10)
  790. make.height.equalTo(44)
  791. }
  792. speedTitleLab.snp.makeConstraints { make in
  793. make.left.equalToSuperview().offset(16)
  794. make.top.equalToSuperview().offset(190)
  795. make.height.equalTo(20)
  796. make.width.equalTo(80)
  797. }
  798. sustomSwitchView.snp.makeConstraints { make in
  799. make.left.equalToSuperview().offset(16)
  800. make.top.equalTo(speedTitleLab.snp.bottom).offset(8)
  801. make.height.equalTo(30)
  802. make.width.equalTo(180)
  803. }
  804. }
  805. @objc func nextBtnClick(sender _: UIButton) {
  806. BFLog(message: "去发布")
  807. playerView.pause()
  808. // 使用深 copy
  809. let json = projectModel.toJSONString(prettyPrint: false)
  810. if json == nil {
  811. BFLog(message: "数据转换有问题 跳转")
  812. return
  813. }
  814. let tempModel: PQEditProjectModel? = Mapper<PQEditProjectModel>().map(JSONString: json!)
  815. let materialVC: PQStuckPointMaterialController? = navigationController?.viewControllers.first(where: { (vc) -> Bool in
  816. vc is PQStuckPointMaterialController
  817. }) as? PQStuckPointMaterialController
  818. if materialVC != nil, materialVC?.isToPublicHandle != nil {
  819. materialVC?.isToPublicHandle!(isReCreate, selectedTotalDuration, selectedDataCount, selectedImageDataCount, mStickers, stuckPointMusicData, tempModel)
  820. } else {
  821. if finallyStuckPoints.count == 0 {
  822. cShowHUB(superView: nil, msg: "无卡点信息,返回重新选择音乐")
  823. return
  824. }
  825. let videoExporter = PQStuckPointPublicController()
  826. videoExporter.rhythmMode = currentCreateStickersModel
  827. videoExporter.syncedUpVideoSpeedMin = modelSpeed_maxSpeed
  828. videoExporter.syncedUpVideoSpeedMax = modelSpeed_minSpeed
  829. videoExporter.isReCreate = isReCreate
  830. videoExporter.selectedTotalDuration = selectedTotalDuration
  831. videoExporter.selectedDataCount = selectedDataCount
  832. videoExporter.selectedImageDataCount = selectedImageDataCount
  833. videoExporter.finallyUserAudioTime = Float(finallyStuckPoints.last ?? 0) - Float(finallyStuckPoints.first ?? 0)
  834. videoExporter.clipAudioRange = getClipAudioRange()
  835. videoExporter.playeTimeRange = playeTimeRange
  836. videoExporter.mStickers = mStickers
  837. videoExporter.audioMixModel = stuckPointMusicData
  838. videoExporter.editProjectModel = tempModel
  839. navigationController?.pushViewController(videoExporter, animated: true)
  840. }
  841. // 点击上报:去合成
  842. 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: "点击上报:去合成")
  843. }
  844. override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  845. super.touchesBegan(touches, with: event)
  846. if touches.first?.view != self.customSpeedSettingView {
  847. if !customSpeedSettingView.isHidden {
  848. customSpeedSettingView.isHidden = true
  849. }
  850. }
  851. }
  852. // MARK: - 播放器相关操作
  853. /// seek 播放器
  854. /// - Parameter playeTimeRange: 开始和结束时间
  855. func seekPlayer(playeTimeRange: CMTimeRange) {
  856. playerView.setEnableSeek(isSeek: true)
  857. playerView.play(pauseFirstFrame: false, playeTimeRange: playeTimeRange)
  858. }
  859. /// 通过传入的 selectedPhotoData 、 stuckPointMusicData 创建 projectModel 模型 后面都使用 projectModel 参数
  860. func createPorjectData() {
  861. // 1,添加选择的视觉素材
  862. if projectModel.sData?.sections.count == 0 {
  863. let section: PQEditSectionModel = PQEditSectionModel()
  864. selectedMetarialData?.forEach { model in
  865. let json = model.toJSONString(prettyPrint: false)
  866. if json == nil {
  867. BFLog(message: "数据转换有问题 跳转")
  868. return
  869. }
  870. let tempModel: PQEditVisionTrackMaterialsModel = Mapper<PQEditVisionTrackMaterialsModel>().map(JSONString: json!)!
  871. section.sectionTimeline?.visionTrack?.visionTrackMaterials.append(tempModel)
  872. }
  873. projectModel.sData?.sections.append(section)
  874. }
  875. // 2,添加背景音乐
  876. projectModel.sData?.addBGM(audioMix: stuckPointMusicData!)
  877. }
  878. // 计算拼接音乐的开始和结束点
  879. func getClipAudioRange() -> CMTimeRange {
  880. // 设置音乐的拼接范围,开始:推荐的卡点 结束:点到的倒数第二位
  881. if stuckPointMusicData!.rhythmSdata.count > 0 && stuckPointMusicData?.rhythmSdata[0].pointTimes.count ?? 0 > 2 {
  882. let lastSecondPoint = Float((stuckPointMusicData!.rhythmSdata[0].pointTimes[stuckPointMusicData!.rhythmSdata[0].pointTimes.count - 2])) / Float(BASE_FILTER_TIMESCALE)
  883. let clipAudioRange =
  884. CMTimeRange(start: CMTime(value: CMTimeValue(Float(stuckPointMusicData?.startTime ?? 0) * Float(BASE_FILTER_TIMESCALE)), timescale: BASE_FILTER_TIMESCALE), end: CMTime(value: CMTimeValue((Float(lastSecondPoint)) * Float(BASE_FILTER_TIMESCALE)), timescale: BASE_FILTER_TIMESCALE))
  885. return clipAudioRange
  886. }
  887. return CMTimeRange(start: CMTime(value: 0, timescale: 1), duration: CMTime(value: 1, timescale: 1))
  888. }
  889. // 设置播放器
  890. func coculationPlayViewRect() -> CGRect {
  891. let playerShowHeight = pointEditBGView.frame.minY - (navHeadImageView?.frame.maxY ?? 0)
  892. var showRect: CGRect = CGRect(x: (cScreenWidth - playerShowHeight) / 2, y: 0, width: playerShowHeight, height: playerShowHeight)
  893. if self.firstFrameImage != nil {
  894. let w = self.firstFrameImage!.size.width
  895. let h = self.firstFrameImage!.size.height
  896. let ratioMaterial: Float = Float(w / max(h, 1))
  897. if ratioMaterial > 1 {
  898. // 横屏
  899. let tempPlayerHeight = min(cScreenWidth * CGFloat(h / w), playerShowHeight)
  900. showRect = CGRect(x: (cScreenWidth - tempPlayerHeight * CGFloat(ratioMaterial)) / 2, y: (playerShowHeight - tempPlayerHeight) / 2, width: tempPlayerHeight * CGFloat(ratioMaterial), height: tempPlayerHeight)
  901. } else {
  902. // 竖屏
  903. let playerViewWidth = (CGFloat(w) / CGFloat(h)) * playerShowHeight
  904. showRect = CGRect(x: (cScreenWidth - playerViewWidth) / 2, y: 0, width: playerViewWidth, height: playerShowHeight)
  905. }
  906. }
  907. if showRect.size.width == showRect.size.height {
  908. if cScreenWidth > playerShowHeight {
  909. showRect.origin.x = (cScreenWidth - playerShowHeight) / 2
  910. showRect.size.width = playerShowHeight
  911. showRect.size.height = playerShowHeight
  912. } else {
  913. showRect.origin.x = 0
  914. showRect.size.width = cScreenWidth
  915. showRect.size.height = cScreenWidth
  916. }
  917. }
  918. if showRect.size.width != 0, showRect.size.height != 0 {
  919. if(Int(showRect.height) % 2 != 0){
  920. showRect.size.height = showRect.size.height + 1.0
  921. }
  922. if(Int(showRect.width) % 2 != 0){
  923. showRect.size.width = showRect.size.width + 1.0
  924. }
  925. }
  926. showRect.origin.y = (playerShowHeight - showRect.size.height) / 2.0 + (navHeadImageView?.frame.maxY ?? 0)
  927. return showRect
  928. }
  929. func settingPlayerView() {
  930. stuckPointCuttingView.resetDefaultsColor()
  931. synchroMarskView.show()
  932. // 1,设置播放器的显示区域 和画布大小
  933. // - 按第一个素材尺寸自适应
  934. playerView.pause()
  935. var firstModel: PQEditVisionTrackMaterialsModel?
  936. for part in projectModel.sData!.sections {
  937. if part.sectionTimeline?.visionTrack?.getEnableVisionTrackMaterials().count ?? 0 > 0 {
  938. firstModel = part.sectionTimeline?.visionTrack?.getEnableVisionTrackMaterials().first
  939. break
  940. }
  941. }
  942. var videoSize: CGSize = CGSize(width: Int(firstModel?.width ?? 0), height: Int(firstModel?.height ?? 0))
  943. var minSlider = min(videoSize.width, videoSize.height)
  944. var maxSlider = max(videoSize.width, videoSize.height)
  945. let ration = 1080 / minSlider
  946. minSlider = minSlider * ration
  947. maxSlider = maxSlider * ration
  948. if videoSize.width > videoSize.height { // 宽屏
  949. videoSize = CGSize(width: maxSlider, height: minSlider)
  950. } else {
  951. videoSize = CGSize(width: minSlider, height: maxSlider)
  952. }
  953. if videoSize.width.isNaN || videoSize.height.isNaN {
  954. BFLog(1, message: "宽高无效NaN")
  955. return
  956. }
  957. let maxValue = max(videoSize.width, videoSize.height ?? 0)
  958. if maxValue > 1920 {
  959. let maxRation = 1920 / maxValue
  960. videoSize = CGSize(width: videoSize.width * CGFloat(maxRation), height: videoSize.height * CGFloat(maxRation))
  961. BFLog(message: "最长边已经超过 1920 要等比缩小 缩放后\(videoSize)")
  962. }
  963. if (Int(videoSize.width) % 2) != 0 {
  964. videoSize.width = videoSize.width - 1
  965. }
  966. if (Int(videoSize.height) % 2) != 0 {
  967. videoSize.height = videoSize.height - 1
  968. }
  969. projectModel.sData?.videoMetaData?.videoWidth = Int(videoSize.width)
  970. projectModel.sData?.videoMetaData?.videoHeight = Int(videoSize.height)
  971. let beginTime = Date()
  972. dealParameter(model: currentCreateStickersModel)
  973. // 更新裁剪时间条的的ui数据
  974. stuckPointCuttingView.videoDuration = max(CGFloat(finallyUserAudioTime), CGFloat(finallyStuckPoints.last!))
  975. let counn = (stuckPointMusicData?.rhythmSdata[0].pointTimes.count)! - 2
  976. let suggestRhythmStartTime = CGFloat(stuckPointMusicData?.suggestRhythmStartTime ?? 0)
  977. let suggestRhythmEndTime = max(suggestRhythmStartTime, CGFloat(stuckPointMusicData?.rhythmSdata[0].pointTimes[max(counn, 0)] ?? 0)/CGFloat(BASE_FILTER_TIMESCALE))
  978. stuckPointCuttingView.updateEndTime(
  979. startTime: CGFloat(CMTimeGetSeconds(playeTimeRange.start)),
  980. endTime: CGFloat(CMTimeGetSeconds(playeTimeRange.end)),
  981. suggestRhythmStartTime: suggestRhythmStartTime,
  982. suggestRhythmEndTime: suggestRhythmEndTime)
  983. // 2,创建滤镜
  984. DispatchQueue.global().async {
  985. 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)), model: self.currentCreateStickersModel)
  986. DispatchQueue.main.async { // 串行、异步
  987. self.playerView.mStickers = self.mStickers
  988. BFLog(message: "createStickers tiskskskskme \(Date().timeIntervalSince(beginTime))")
  989. // 3,设置音频
  990. let audioPath = self.stuckPointMusicData?.localPath ?? ""
  991. BFLog(message: "初始化音频播放器的音频地址为:\(audioPath)")
  992. self.playerView.stop()
  993. // 这里的测试这个音乐播放有问题
  994. // self.playerView.updateAsset(URL(fileURLWithPath: "63930549652d74e477141e3b79c8d29a9ef8af81625053214516.mp3", relativeTo:Bundle.main.resourceURL!), videoComposition: nil, audioMixModel: nil)
  995. self.playerView.updateAsset(URL(fileURLWithPath: documensDirectory + audioPath), videoComposition: nil, audioMixModel: nil, originMusicDuration: self.finallyUserAudioTime, clipAudioRange: self.getClipAudioRange())
  996. // 4, 设置播放器的输出画布大小
  997. self.playerView.movie?.mShowVidoSize = CGSize(width: CGFloat(self.projectModel.sData?.videoMetaData?.videoWidth ?? 0), height: CGFloat(self.projectModel.sData?.videoMetaData?.videoHeight ?? 0))
  998. // 传给movie 音频的原始卡点
  999. let fir = Int64(self.stuckPointsTemp.first ?? 0)
  1000. let endd = Int64(self.stuckPointsTemp.last ?? 0)
  1001. self.playerView.movie?.orginStuckRange = CMTimeRange(start: CMTime(value: Int64(fir) * Int64(BASE_FILTER_TIMESCALE), timescale: BASE_FILTER_TIMESCALE), end: CMTime(value: Int64(endd) * Int64(BASE_FILTER_TIMESCALE), timescale: BASE_FILTER_TIMESCALE))
  1002. // 5,开始播放
  1003. self.playerView.isLoop = false
  1004. self.playerView.showProgressLab = true
  1005. // 初始化音频的开始和结束时间
  1006. BFLog(message: "播放的器 开始\(String(describing: CMTimeGetSeconds(self.playeTimeRange.start))) 结束 \(String(describing: CMTimeGetSeconds(self.playeTimeRange.end)))")
  1007. let end3: TimeInterval = Date().timeIntervalSince1970
  1008. self.playerView.play(pauseFirstFrame: false, playeTimeRange: CMTimeRange(start: self.playeTimeRange.start, end: self.playeTimeRange.end))
  1009. self.stuckPointCuttingView.updateProgress(progress: 0)
  1010. self.synchroMarskView.removeMarskView()
  1011. let end4: TimeInterval = Date().timeIntervalSince1970
  1012. BFLog(message: " playerView.play tiskskskskme \(end4 - end3)")
  1013. // 6,进度回调
  1014. self.playerView.progress = { [weak self] currentTime, tatolTime, percent in
  1015. if percent == 1 {
  1016. self?.stuckPointCuttingView.resetDefaultsColor(clearData: false)
  1017. sharedImageProcessingContext.framebufferCache.purgeAllUnassignedFramebuffers()
  1018. return
  1019. }
  1020. if(CMTimeGetSeconds(self?.playeTimeRange.duration ?? .zero) <= 0.0){
  1021. BFLog(message: "时长错误!!!!")
  1022. return
  1023. }
  1024. // 更新进度
  1025. let progress = (currentTime - CMTimeGetSeconds(self?.playeTimeRange.start ?? .zero)) / CMTimeGetSeconds(self?.playeTimeRange.duration ?? .zero)
  1026. BFLog(message: "\(currentTime) \(tatolTime) 显示播放器进度为: \(progress)")
  1027. self?.stuckPointCuttingView.updateProgress(progress: CGFloat(progress))
  1028. }
  1029. }
  1030. }
  1031. }
  1032. deinit {
  1033. musicNameLab.stop()
  1034. // playerView.pause()
  1035. // 取消所有的导出
  1036. PQSingletoMemoryUtil.shared.allExportSession.forEach { _, exportSession in
  1037. exportSession.cancelExport()
  1038. }
  1039. self.synchroMarskView.removeMarskView()
  1040. sharedImageProcessingContext.framebufferCache.purgeAllUnassignedFramebuffers()
  1041. BFLog(1, message: "卡点视频预览界面release")
  1042. }
  1043. }
  1044. // MARK: - 视频渲染相关逻辑方法
  1045. extension PQStuckPointEditerController {
  1046. /// 分割视频 这里只设置视频类型的 in 和 out 并不设置显示的开始和结束时间 mp4 ,png ,png ,mp4
  1047. /// - Parameter section: 当前段
  1048. /// - Parameter stuckPoints: 用户选择的,或推荐的卡点数
  1049. /// - Returns: 返回分割后的所有 stickers 和卡点数是一致的
  1050. func clipVideoMerage(section: PQEditSectionModel, stuckPoints: [Float]) -> [PQEditVisionTrackMaterialsModel] {
  1051. var stickers: Array = Array<PQEditVisionTrackMaterialsModel>.init()
  1052. // 第二种情况:有视频要进行分割
  1053. /*
  1054. 1, 确定每个视频素材需要切的段数p
  1055. 2, 将所有视频时长相加,得到总视频素材时长L = l1 + l2 + ... + ln
  1056. 3, 视频素材a1需要切分的个数clipNum = max (round (kongduan * a1 / L) , 1)
  1057. */
  1058. // 要补的空位数
  1059. let kongduan: Int = Int(stuckPoints.count - 1) - selectedImageDataCount
  1060. // 所有视频总时长
  1061. var videoTotalDuration: Float64 = 0.0
  1062. for video in section.sectionTimeline!.visionTrack!.getEnableVisionTrackMaterials(type: "video") {
  1063. let asset: AVURLAsset = AVURLAsset(url: URL(fileURLWithPath: documensDirectory + video.locationPath), options: nil)
  1064. videoTotalDuration = videoTotalDuration + Float64(CMTimeGetSeconds(asset.duration))
  1065. }
  1066. if videoTotalDuration == 0 {
  1067. BFLog(message: "视频总时长出现错误!!!!这里应该有视频素材的")
  1068. return stickers
  1069. }
  1070. for sticker in section.sectionTimeline!.visionTrack!.getEnableVisionTrackMaterials() {
  1071. if sticker.type == StickerType.VIDEO.rawValue {
  1072. let asset: AVURLAsset = AVURLAsset(url: URL(fileURLWithPath: documensDirectory + sticker.locationPath), options: nil)
  1073. // 要分割的段落
  1074. let clipNum = Int(max(round(Double(kongduan) * CMTimeGetSeconds(asset.duration) / videoTotalDuration), 1))
  1075. sticker.duration = CMTimeGetSeconds(asset.duration)
  1076. BFLog(message: "单个视频\(sticker.locationPath)时长::\(CMTimeGetSeconds(asset.duration)) ,clipNum is:\(clipNum)")
  1077. for clipindex in 0 ..< clipNum {
  1078. // deep copy sticker model 防止只有一个对象
  1079. let deepCopySticker:PQEditVisionTrackMaterialsModel? = sticker.copy() as? PQEditVisionTrackMaterialsModel
  1080. // 设置循环模式和适配模式
  1081. deepCopySticker?.generateDefaultValues()
  1082. deepCopySticker?.model_in = clipindex == 0 ? 0 : CMTimeGetSeconds(asset.duration) / Double(clipNum) * Double(clipindex)
  1083. deepCopySticker?.out = (deepCopySticker?.model_in ?? 0) + CMTimeGetSeconds(asset.duration) / Double(clipNum)
  1084. if (deepCopySticker?.model_in ?? 0) >= CMTimeGetSeconds(asset.duration) || (deepCopySticker?.out ?? 0) >= CMTimeGetSeconds(asset.duration) {
  1085. deepCopySticker?.model_in = CMTimeGetSeconds(asset.duration) - CMTimeGetSeconds(asset.duration) / Double(clipNum)
  1086. deepCopySticker?.out = CMTimeGetSeconds(asset.duration)
  1087. }
  1088. BFLog(message: " crilp is in \(deepCopySticker?.model_in ?? 0) out \(deepCopySticker?.out ?? 0) 总时长\(CMTimeGetSeconds(asset.duration))")
  1089. if deepCopySticker != nil {
  1090. stickers.append(deepCopySticker!)
  1091. }
  1092. }
  1093. } else if sticker.type == StickerType.IMAGE.rawValue {
  1094. sticker.generateDefaultValues()
  1095. stickers.append(sticker)
  1096. }
  1097. }
  1098. // kongduan = clipNumTep
  1099. return stickers
  1100. }
  1101. //更新 playeTimeRange & finallyUserAudioTime
  1102. func updateTimeInfomation(){
  1103. // 四,背景音乐时长处理)计算最后使用的音频时长, 如果不用拼接音频时长度是卡点的倒数第二位时间
  1104. let asset = AVURLAsset(url: URL(fileURLWithPath: documensDirectory + (stuckPointMusicData?.localPath ?? "")), options: nil)
  1105. // 原推荐卡点的倒数第二位时间
  1106. let lastSecondPoint = Float((stuckPointMusicData!.rhythmSdata[0].pointTimes[stuckPointMusicData!.rhythmSdata[0].pointTimes.count - 2])) / Float(BASE_FILTER_TIMESCALE)
  1107. finallyUserAudioTime = Float(lastSecondPoint)
  1108. if (finallyStuckPoints.last ?? 0) > Float(CMTimeGetSeconds(asset.duration)) {
  1109. finallyUserAudioTime = Float(finallyStuckPoints.last ?? 0) + (Float(CMTimeGetSeconds(asset.duration)) - Float(lastSecondPoint))
  1110. }
  1111. playeTimeRange = CMTimeRange(start: CMTime(value: CMTimeValue(Float64(finallyStuckPoints.first ?? 0) * Float64(BASE_FILTER_TIMESCALE)), timescale: BASE_FILTER_TIMESCALE), end: CMTime(value: CMTimeValue((Float64(finallyStuckPoints.last ?? 0)) * Float64(BASE_FILTER_TIMESCALE)), timescale: BASE_FILTER_TIMESCALE))
  1112. for (index, usePoint) in finallyStuckPoints.enumerated() {
  1113. BFLog(message: "测试人员最后使用的卡点信息 \(index) : \(usePoint)")
  1114. }
  1115. BFLog(message: "计算后给播放器使用的开始:\(CMTimeGetSeconds(playeTimeRange.start)) 结束时间\(CMTimeGetSeconds(playeTimeRange.end)) 播放总时长:\(CMTimeGetSeconds(playeTimeRange.end) - CMTimeGetSeconds(playeTimeRange.start))")
  1116. }
  1117. /// 创建sticker
  1118. /// - Parameters:
  1119. /// - sections: 项目所有段落数据信息
  1120. /// - inputSize: 画布大小
  1121. /// - Returns: filters 数据 播放器可直接使用
  1122. func createStickers(sections: List<PQEditSectionModel>, inputSize _: CGSize = .zero, model: createStickersModel = .createStickersModelPoint) -> [PQEditVisionTrackMaterialsModel] {
  1123. // 推荐卡点数
  1124. let beginDecoderTime: TimeInterval = Date().timeIntervalSince1970
  1125. // 保存滤镜对象数据
  1126. var stickers: Array = Array<PQEditVisionTrackMaterialsModel>.init()
  1127. for section in sections {
  1128. if section.sectionType == "normal" {
  1129. // 第一种情况:全是图片,三种模式都进行图片回环播放
  1130. if section.sectionTimeline!.visionTrack!.getEnableVisionTrackMaterials(type: "video").count == 0, section.sectionTimeline!.visionTrack!.getEnableVisionTrackMaterials(type: "image").count > 0 {
  1131. for (index, _) in finallyStuckPoints.enumerated() {
  1132. let sticker: PQEditVisionTrackMaterialsModel = section.sectionTimeline!.visionTrack!.getEnableVisionTrackMaterials()[index % section.sectionTimeline!.visionTrack!.getEnableVisionTrackMaterials().count]
  1133. BFLog(message: "stickerlocationPath sticker : \(sticker.locationPath)")
  1134. //
  1135. let deepCopySticker:PQEditVisionTrackMaterialsModel? = sticker.copy() as? PQEditVisionTrackMaterialsModel
  1136. if deepCopySticker!.type == StickerType.IMAGE.rawValue {
  1137. if index + 1 < finallyStuckPoints.count {
  1138. deepCopySticker!.timelineIn = Float64(String(format: "%.6f",finallyStuckPoints[index])) ?? 0.0
  1139. deepCopySticker!.timelineOut = Float64(String(format: "%.6f",finallyStuckPoints[index + 1])) ?? 0.0
  1140. if deepCopySticker != nil {
  1141. deepCopySticker?.generateDefaultValues()
  1142. stickers.append(deepCopySticker!)
  1143. BFLog(1, message: "测试人员index is 纯图片 timelineOut:\(deepCopySticker!.timelineIn) timelineOut :\(deepCopySticker!.timelineOut)")
  1144. }
  1145. }
  1146. }
  1147. }
  1148. } else { // 第二种情况:视频 + 图片情况、视频要分段+图片按卡点时长创建
  1149. if model == .createStickersModelPoint { // 跳跃卡点
  1150. // 第二种情况:有视频要进行分割
  1151. let clipFilters = clipVideoMerage(section: section, stuckPoints: finallyStuckPoints)
  1152. //数据不一致时要对数据进行二次处理,不是最好方案
  1153. if(clipFilters.count > finallyStuckPoints.count - 1){
  1154. clipPoint(clipNum: clipFilters.count - finallyStuckPoints.count, oldPoints: finallyStuckPoints)
  1155. }else if(clipFilters.count < finallyStuckPoints.count - 1){
  1156. while (clipFilters.count < finallyStuckPoints.count - 1) {
  1157. finallyStuckPoints.removeLast()
  1158. }
  1159. }
  1160. //更新最终使用值
  1161. updateTimeInfomation()
  1162. //stikcer段数比clipFilters 数 大于 1才是正确的
  1163. BFLog(message: "stikcer count is\(clipFilters.count) finallyStuckPoints count is\(finallyStuckPoints.count)")
  1164. for (index, point) in finallyStuckPoints.enumerated() {
  1165. BFLog(message: "aaaaaindexindeindexxindexindexindex \(index) \(point)")
  1166. if index + 1 < finallyStuckPoints.count, index < clipFilters.count {
  1167. BFLog(message: "bbbbbindexindeindexxindexindexindex \(index) \(point)")
  1168. let sticker: PQEditVisionTrackMaterialsModel = clipFilters[index]
  1169. if(sticker.type == StickerType.IMAGE.rawValue){
  1170. BFLog(message: "当前是image filter !!!!!")
  1171. }
  1172. sticker.timelineIn = Float64(String(format: "%.6f",finallyStuckPoints[index])) ?? 0.0
  1173. sticker.timelineOut = Float64(String(format: "%.6f",finallyStuckPoints[index + 1])) ?? 0.0
  1174. // 卡点的时间 > in out 值 这里就会出现鬼畜效果
  1175. let timelineInterval = sticker.timelineOut - sticker.timelineIn
  1176. let inOutInterval = sticker.out - sticker.model_in
  1177. if timelineInterval > inOutInterval {
  1178. BFLog(message: "实际要显示卡点时长\(timelineInterval) 素材裁剪时长:\(inOutInterval)")
  1179. sticker.out = sticker.model_in + timelineInterval
  1180. // 下面只是 LOG 方便查问题
  1181. let stickerInOut = sticker.out - sticker.model_in
  1182. let stickerTimelineInOut = sticker.timelineOut - sticker.timelineIn
  1183. if stickerInOut != stickerTimelineInOut {
  1184. BFLog(message: "sticker.timelineIn \(sticker.timelineIn) stickerTimelineInOut is\(stickerTimelineInOut) stickerInOut is\(stickerInOut) 相差\(stickerTimelineInOut - stickerInOut)")
  1185. }
  1186. }
  1187. // out > 素材的总时长in out 进行前移操作
  1188. let offsetAssetDuration = sticker.out - sticker.duration
  1189. if offsetAssetDuration > 0 {
  1190. sticker.model_in = sticker.model_in - offsetAssetDuration
  1191. sticker.out = sticker.out - offsetAssetDuration
  1192. }
  1193. print("跳跃卡点测试人员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)")
  1194. stickers.append(sticker)
  1195. }
  1196. }
  1197. } else if model == .createStickersModelOnlyMusic || model == .createStickersModelSpeed { // 仅音乐 和 快慢速卡点
  1198. BFLog(message: "stuckPoints count is \(finallyStuckPoints.count)")
  1199. for sticker in section.sectionTimeline!.visionTrack!.getEnableVisionTrackMaterials() {
  1200. if sticker.type == StickerType.VIDEO.rawValue {
  1201. let asset: AVURLAsset = AVURLAsset(url: URL(fileURLWithPath: documensDirectory + sticker.locationPath), options: nil)
  1202. BFLog(message: "单个视频\(sticker.locationPath)时长::\(CMTimeGetSeconds(asset.duration)) ,clipNum is:\(sticker.clipCount)")
  1203. var lastOutTime: Float64 = 0.0
  1204. for _ in 1 ... sticker.clipCount {
  1205. // deep copy sticker model 防止只有一个对象
  1206. let deepCopyStickerDecoderTime: TimeInterval = Date().timeIntervalSince1970
  1207. let deepCopySticker:PQEditVisionTrackMaterialsModel? = sticker.copy() as? PQEditVisionTrackMaterialsModel
  1208. BFLog(message: "生成stickers 总时长为 aaa\(Date().timeIntervalSince1970 - deepCopyStickerDecoderTime)")
  1209. // 设置循环模式和适配模式
  1210. deepCopySticker?.generateDefaultValues()
  1211. deepCopySticker?.materialDurationFit?.fitType = adapterMode.staticFrame.rawValue
  1212. // 当前分段的速度
  1213. var tempSpeed: Float = 1.0
  1214. if model == .createStickersModelSpeed {
  1215. tempSpeed = (stickers.count % 2) == 0 ? modelSpeed_maxSpeed : modelSpeed_minSpeed
  1216. }
  1217. if stickers.count + 1 < finallyStuckPoints.count {
  1218. deepCopySticker?.speedRate = tempSpeed
  1219. // 定义临时使用的变量
  1220. // 素材显示的开始时间和结束时间
  1221. let tempTimelineIn:Float64 = Float64(String(format: "%.6f",finallyStuckPoints[stickers.count])) ?? 0.0
  1222. let timelineOut:Float64 = Float64(String(format: "%.6f",finallyStuckPoints[stickers.count + 1])) ?? 0.0
  1223. // 素材分割的开始时间和结束时间
  1224. let tempModel_In = lastOutTime
  1225. var tempOut = lastOutTime + Float64(tempSpeed) * (timelineOut - tempTimelineIn)
  1226. // 处理最后一点视频素材不够卡点时长 e.g. 0.3 卡点时长0.5
  1227. if tempOut > CMTimeGetSeconds(asset.duration) {
  1228. // 最后一点素材时长
  1229. let lastAssetDuration = CMTimeGetSeconds(asset.duration) - lastOutTime
  1230. let pointDuration = timelineOut - tempTimelineIn
  1231. // 要适应到卡点内要使用的C速度
  1232. let needSpeed = lastAssetDuration / pointDuration
  1233. // 当前卡点段为快速 快速都用 C 速处理
  1234. BFLog(message: "最后一点视频素材不够卡点时长要做变速C处理 差\(tempOut - CMTimeGetSeconds(asset.duration)) \(needSpeed)")
  1235. deepCopySticker?.speedRate = Float(needSpeed)
  1236. tempOut = CMTimeGetSeconds(asset.duration)
  1237. if(needSpeed == 0){
  1238. BFLog(message: "needSpeed is 0 出现在时长和卡点正好相等")
  1239. continue
  1240. }
  1241. }
  1242. deepCopySticker?.model_in = tempModel_In
  1243. deepCopySticker?.out = tempOut
  1244. deepCopySticker?.timelineIn = tempTimelineIn
  1245. deepCopySticker?.timelineOut = timelineOut
  1246. lastOutTime = deepCopySticker?.out ?? 0
  1247. }
  1248. BFLog(message: "测试人员创建 sticker crilp is in 视频 \(String(format: "%.6f", deepCopySticker?.model_in ?? 0)) out \(String(format: "%.6f", deepCopySticker?.out ?? 0)) ,分段素材时长:\(String(format: "%.6f", (deepCopySticker?.out ?? 0) - (deepCopySticker?.model_in ?? 0))) ,分段显示时长:\(String(format: "%.6f", (deepCopySticker?.timelineOut ?? 0) - (deepCopySticker?.timelineIn ?? 0))), 视频素材原时长\(CMTimeGetSeconds(asset.duration)) clipNum \(sticker.clipCount) timelineIN: \(String(format: "%.6f", deepCopySticker?.timelineIn ?? 0)) timelineOUT:\(String(format: "%.6f", deepCopySticker?.timelineOut ?? 0)) speedRate:\(deepCopySticker?.speedRate ?? 0.0)")
  1249. if deepCopySticker != nil {
  1250. if(deepCopySticker?.timelineIn == 0){
  1251. BFLog(message: "timelineIn data is error!!!")
  1252. }
  1253. stickers.append(deepCopySticker!)
  1254. }
  1255. }
  1256. } else if sticker.type == StickerType.IMAGE.rawValue {
  1257. if stickers.count + 1 >= finallyStuckPoints.count {
  1258. BFLog(message: "数据出现错误!!!查正") // 155.318253
  1259. break
  1260. }
  1261. sticker.generateDefaultValues()
  1262. sticker.timelineIn = Float64(String(format: "%.6f",finallyStuckPoints[stickers.count])) ?? 0.0
  1263. sticker.timelineOut = Float64(String(format: "%.6f",finallyStuckPoints[stickers.count + 1])) ?? 0.0
  1264. stickers.append(sticker)
  1265. BFLog(message: "测试人员创建 sticker crilp is in 图片 \(String(format: "%.6f", sticker.model_in)) out \(String(format: "%.6f", sticker.out)) ,分段素材时长:\(String(format: "%.6f", (sticker.out) - (sticker.model_in))) ,分段显示时长:\(String(format: "%.6f", (sticker.timelineOut) - (sticker.timelineIn))), timelineIN: \(String(format: "%.6f", sticker.timelineIn)) timelineOUT:\(String(format: "%.6f", sticker.timelineOut)) speedRate:\(sticker.speedRate)")
  1266. }
  1267. }
  1268. }
  1269. }
  1270. }
  1271. }
  1272. BFLog(message: "生成stickers 总时长为:\(Date().timeIntervalSince1970 - beginDecoderTime)")
  1273. return stickers
  1274. }
  1275. /// 根据档位取最后使用的卡点数据
  1276. /// - Parameter seed: 档位速度
  1277. /// - Returns: 最后使用的卡点
  1278. func getUsedStuckPoint(seed: Int) -> Array<Float> {
  1279. if !(stuckPointMusicData!.rhythmSdata.count > 0 && stuckPointMusicData!.rhythmSdata[0].pointTimes.count > 1){
  1280. return []
  1281. }
  1282. // 推荐卡点数
  1283. var stuckPoints: Array = Array<Float>.init()
  1284. var pointsTemp = Array<Float>.init()
  1285. //
  1286. // 最后一个卡点时间(原推荐卡点的倒数第二位时间)
  1287. let lastPoint = stuckPointMusicData!.rhythmSdata[0].pointTimes[stuckPointMusicData!.rhythmSdata[0].pointTimes.count - 2]
  1288. for (index, dunshu) in stuckPointMusicData!.rhythmSdata[0].pointTimes.enumerated() {
  1289. if dunshu >= Int64((stuckPointMusicData?.startTime ?? 0) * Float64(BASE_FILTER_TIMESCALE)) && dunshu < lastPoint {
  1290. let savePointStr = String(format: "%.6f", Float(dunshu) / Float(BASE_FILTER_TIMESCALE))
  1291. BFLog(message: "原所有卡点数:\(index) \(savePointStr)")
  1292. pointsTemp.append(Float(savePointStr) ?? 0.0)
  1293. }
  1294. }
  1295. /*
  1296. 一,快慢速模式下取卡点 2 3 4
  1297. 二,跳跃卡点模式下根据不同速度 取卡点 1,2,3
  1298. 快节奏为选中区域的所有点位,即0,1,2,3,4 5 6 7 8 9 10 ……
  1299. 适中为每两个点位取一个,即0,2,4,6……
  1300. 慢节奏为每三个点位取一个,即0,3,6,9……
  1301. */
  1302. // 不丢
  1303. if seed == 1 {
  1304. stuckPoints = pointsTemp
  1305. } else {
  1306. // 根据档位要丢
  1307. for (index, point) in pointsTemp.enumerated() {
  1308. if index % seed == 0 {
  1309. stuckPoints.append(point)
  1310. }
  1311. }
  1312. }
  1313. for point in stuckPoints {
  1314. BFLog(message: "没有 start end 计算后的卡点数\(point)")
  1315. }
  1316. // 若音乐起点至第一个卡点点位之间时长t0<0.3时,此段时长与下一个点位时长合并,故第一段卡点部分时长为t0+d
  1317. if Float(stuckPointMusicData?.startTime ?? 0) - (stuckPoints.first ?? 0.0) < 0.3 {
  1318. if stuckPoints.first != nil {
  1319. stuckPoints.removeFirst()
  1320. }
  1321. }
  1322. stuckPoints.insert(Float(stuckPointMusicData?.startTime ?? 0), at: 0)
  1323. for point in stuckPoints {
  1324. BFLog(message: "有 start end 计算后的卡点数\(point)")
  1325. }
  1326. BFLog(message: "处理节奏后 stuckPoints count is \(stuckPoints.count) seed \(seed), start time:\(stuckPoints.first ?? 0.0),end time:\(stuckPoints.last ?? 0.0) 总时长为:\((stuckPoints.last ?? 0.0) - (stuckPoints.first ?? 0.0))")
  1327. return stuckPoints
  1328. }
  1329. func clipPoint(clipNum: Int, oldPoints: Array<Float>) {
  1330. BFLog(message: "拼接卡点数:\(clipNum)")
  1331. if(clipNum < 0 || oldPoints.count < 2){
  1332. BFLog(message: "clipNum is error!!!! \(clipNum)")
  1333. return
  1334. }
  1335. // 如果是第一次拼接先补第0位
  1336. if finallyStuckPoints.count == 0 {
  1337. finallyStuckPoints.append(stuckPointsTemp.first ?? 0.0)
  1338. }
  1339. for i in finallyStuckPoints.count ... clipNum + finallyStuckPoints.count {
  1340. if (i % (oldPoints.count - 1)) != 1 {
  1341. let value = String(format: "%.6f", finallyStuckPoints[i - 1] + oldPoints[((i-1) % (oldPoints.count - 1)) + 1] - oldPoints[((i - 2) % (oldPoints.count - 1)) + 1])
  1342. finallyStuckPoints.append(Float(value) ?? 0.0)
  1343. } else {
  1344. let value = String(format: "%.6f", finallyStuckPoints[i - 1] + oldPoints[1] - oldPoints[0])
  1345. finallyStuckPoints.append(Float(value) ?? 0.0)
  1346. }
  1347. }
  1348. }
  1349. /// 根据不同模式model, maxSpeed ,minSpeed, self?.stuckPointMusicData?.speed 档位,生成音乐时长和最终使用的卡点信息
  1350. func dealParameter(model: createStickersModel) {
  1351. // 清空上一次使用的卡点数据
  1352. finallyStuckPoints.removeAll()
  1353. // 已经取到的视频素材总长度,用于和原视频素材时长做对比,不够多加一个点
  1354. var useAssestDuration: Float = 0.0
  1355. switch model {
  1356. case .createStickersModelPoint: // 跳跃卡点
  1357. stuckPointsTemp = getUsedStuckPoint(seed: stuckPointMusicData?.speed ?? 0)
  1358. // 要拼接的段数
  1359. var clipNum: Int = 0
  1360. var i: Int = 0
  1361. // L/(n+1) L -原视觉素材总时长 n-抛留倍数 lastJumpSpeedSelectIndex 是位置 对应的值要+1
  1362. // 根据公式计划出的总时长
  1363. let jumpTime = Float(selectedTotalDuration) / Float(modelPoint_speed + 1)
  1364. // 只有图片素材时会为0
  1365. if jumpTime > 0 {
  1366. while useAssestDuration < Float(jumpTime) {
  1367. // 回环从头取\
  1368. if i + 1 >= stuckPointsTemp.count {
  1369. i = 0
  1370. }
  1371. // 快速段
  1372. let LA = (stuckPointsTemp[i + 1] - stuckPointsTemp[i])
  1373. useAssestDuration = useAssestDuration + Float(LA)
  1374. i = i + 1
  1375. clipNum = clipNum + 1
  1376. }
  1377. // 拼接要使用的卡点信息
  1378. clipPoint(clipNum: clipNum, oldPoints: stuckPointsTemp)
  1379. }
  1380. break
  1381. case .createStickersModelSpeed, .createStickersModelOnlyMusic: // 快慢速
  1382. // 快慢速 (2:快节奏,3:适中,4:慢节奏)
  1383. var tempMaxSpeed: Float = 1
  1384. var tempMinSpeed: Float = 1
  1385. if model == .createStickersModelSpeed {
  1386. // 改变速率,.只有快慢速且非只有图片素材时自动+1处理
  1387. if model == .createStickersModelSpeed && selectedDataCount != selectedImageDataCount {
  1388. stuckPointsTemp = getUsedStuckPoint(seed: (stuckPointMusicData?.speed ?? 0) + 1)
  1389. } else {
  1390. stuckPointsTemp = getUsedStuckPoint(seed: (stuckPointMusicData?.speed ?? 0))
  1391. }
  1392. tempMaxSpeed = modelSpeed_maxSpeed
  1393. tempMinSpeed = modelSpeed_minSpeed
  1394. } else {
  1395. stuckPointsTemp = getUsedStuckPoint(seed: (stuckPointMusicData?.speed ?? 0))
  1396. }
  1397. /*
  1398. - A-视频中的快速片段
  1399. - B-视频中的慢速片段
  1400. - d-在一档下音乐每个点位时长
  1401. - n-不同音乐档位对应的d倍数,快节奏时,n=1;适中时,n=3;慢节奏时,n=5
  1402. - L-原视觉素材时长
  1403. - x-视频在A片段的播放倍速
  1404. - y-视频在B片段的播放倍速
  1405. */
  1406. // LA=x*n*d,LB=y*n*d (n=1/3/5) 注:视频经过快慢速处理后的总时长约=L*2/(x+y)
  1407. BFLog(message: "Ax快速为:\(tempMaxSpeed) By慢速为:\(tempMinSpeed) 档位 N为:\(stuckPointMusicData?.speed ?? 0) 使用的卡点总数:\(stuckPointsTemp.count)")
  1408. // 使用新方法取使用的卡点数据
  1409. for section in projectModel.sData?.sections ?? List() {
  1410. if section.sectionType == "normal" {
  1411. for sticker in section.sectionTimeline!.visionTrack!.getEnableVisionTrackMaterials() {
  1412. if sticker.type == StickerType.VIDEO.rawValue {
  1413. let asset: AVURLAsset = AVURLAsset(url: URL(fileURLWithPath: documensDirectory + sticker.locationPath), options: nil)
  1414. let assetDuration = Float(CMTimeGetSeconds(asset.duration))
  1415. BFLog(message: "输入素材时长 \(assetDuration)")
  1416. if(finallyStuckPoints.count == 0){
  1417. finallyStuckPoints.append(stuckPointsTemp[0])
  1418. }
  1419. var j = finallyStuckPoints.count
  1420. // 添加卡点的段数 用于判断当前卡点是快/慢速
  1421. var pointCount:Int = 1
  1422. // 已经取到的视频素材总长度,用于和原视频素材时长做对比,不够多加一个卡点
  1423. var useAssestDurationTemp: Float = 0.0
  1424. while useAssestDurationTemp < assetDuration {
  1425. //当前段的应该使用的速度 A/B
  1426. let useSpeed = (pointCount % 2 != 0 ) ? tempMaxSpeed : tempMinSpeed
  1427. // 计算卡点
  1428. //要添加的卡点数值
  1429. var sub:Float = 0.0
  1430. if stuckPointsTemp.count > 2 {
  1431. if ((j - 1) % (stuckPointsTemp.count - 1)) != 0{
  1432. sub = stuckPointsTemp[((j - 1) % (stuckPointsTemp.count-1)) + 1] - stuckPointsTemp[(j - 1) % (stuckPointsTemp.count - 1)]
  1433. }else {
  1434. sub = stuckPointsTemp[1] - stuckPointsTemp[0]
  1435. }
  1436. finallyStuckPoints.append(finallyStuckPoints[j - 1] + sub)
  1437. j += 1
  1438. }
  1439. useAssestDurationTemp += sub * useSpeed
  1440. if(useAssestDurationTemp > assetDuration){
  1441. useAssestDurationTemp -= sub * useSpeed
  1442. break
  1443. }
  1444. pointCount += 1
  1445. BFLog(2, message: "finallyStuckPoints last ;\((finallyStuckPoints.last ?? 0.0) - (finallyStuckPoints.first ?? 0.0)), tmp:\(useAssestDurationTemp)")
  1446. }
  1447. // 2拼接要使用的卡点信息
  1448. sticker.clipCount = pointCount
  1449. if stuckPointsTemp.count < 1 {
  1450. //todo 和产品沟通提示
  1451. BFLog(message: "卡点数据有错误!!!")
  1452. return
  1453. }
  1454. BFLog(message: "finallyStuckPoints\(finallyStuckPoints)")
  1455. // 3,多补一个卡点 做 C级 速处理,要根据条件不满足 要删除最后一位,
  1456. if useAssestDurationTemp < assetDuration {
  1457. /*
  1458. // 下一个卡的的速度性质快、慢,e.g. sticker.clipCount = 5时 tempSpeed 应该是慢速。下面计算正确。
  1459. var tempSpeed: Float = 1.0
  1460. if model == .createStickersModelSpeed {
  1461. tempSpeed = (sticker.clipCount) % 2 != 0 ? modelSpeed_maxSpeed : modelSpeed_minSpeed
  1462. }
  1463. // 最后一点素材时长
  1464. let lastAssetDuration = Float(CMTimeGetSeconds(asset.duration)) - useAssestDurationTemp
  1465. let lastPointIndex = (sticker.clipCount % stuckPointsTemp.count)
  1466. // 两个卡点
  1467. let a: Float = stuckPointsTemp[lastPointIndex]
  1468. var b: Float = 0.0
  1469. if lastPointIndex + 1 < stuckPointsTemp.count {
  1470. b = stuckPointsTemp[lastPointIndex + 1]
  1471. let pointDuration = b - a
  1472. // 要适应到卡点内要使用的C速度
  1473. let needSpeed = lastAssetDuration / pointDuration
  1474. // 当前卡点段为快速
  1475. if tempSpeed >= 1 {
  1476. // if needSpeed < 0.4 * tempSpeed {
  1477. // BFLog(message: "条件不满足不用补位 删除多加的一位")
  1478. // finallyStuckPoints.removeLast()
  1479. // }
  1480. } else { // 当前卡点段为慢速
  1481. if needSpeed >= 0.4 * tempSpeed && needSpeed >= 0.2 {
  1482. // 查找使用的最后一个卡点在原数组中的位置
  1483. } else {
  1484. BFLog(message: "条件不满足不用补位 删除多加的一位")
  1485. finallyStuckPoints.removeLast()
  1486. }
  1487. }
  1488. }
  1489. */
  1490. }else {
  1491. //出现在第一个卡点X 倍速 > 原素材
  1492. finallyStuckPoints.removeLast()
  1493. }
  1494. }
  1495. }
  1496. }
  1497. }
  1498. break
  1499. }
  1500. // 拼接图片所使用的时长.选择一组图片 按图片数量计算卡点的总时长
  1501. if selectedImageDataCount > 0 {
  1502. clipPoint(clipNum: selectedImageDataCount - 1, oldPoints: stuckPointsTemp)
  1503. }
  1504. // 全是图片时数组里放着的一定都是图片的使用的卡点
  1505. // 定义一次循环的总时长
  1506. var oneSelectImageDuration: Float = 0.0
  1507. if selectedDataCount == selectedImageDataCount {
  1508. oneSelectImageDuration = (finallyStuckPoints.last ?? 0) - (finallyStuckPoints.first ?? 0)
  1509. }
  1510. // 3)素材全是图片处理
  1511. if selectedDataCount == selectedImageDataCount{
  1512. // lastCyclesSelectIndex != -1 已经设置过循环次数 应该是手动设置的值
  1513. if lastCyclesSelectIndex != -1 {
  1514. // 纯图片时 已经默认添加一次循环 所以要用lastCyclesSelectIndex - 1
  1515. if(lastCyclesSelectIndex != 0){
  1516. clipPoint(clipNum: selectedImageDataCount * lastCyclesSelectIndex - 1, oldPoints: stuckPointsTemp)
  1517. }
  1518. } else {
  1519. if(oneSelectImageDuration < 10 ){
  1520. lastCyclesSelectIndex = 0
  1521. while oneSelectImageDuration < 10 {
  1522. // 不够10S 时 一次加图片数量的卡点数
  1523. clipPoint(clipNum: selectedImageDataCount - 1, oldPoints: stuckPointsTemp)
  1524. oneSelectImageDuration = Float((finallyStuckPoints.last ?? 0) - (finallyStuckPoints.first ?? 0))
  1525. lastCyclesSelectIndex = lastCyclesSelectIndex + 1
  1526. }
  1527. speedSettingView.setSelectItem(index: lastCyclesSelectIndex, isSettingPlayer: false,enableInsert: true)
  1528. }else{
  1529. lastCyclesSelectIndex = 0
  1530. }
  1531. }
  1532. }
  1533. // 设置速度选择的位置
  1534. if speedSettingView.viewType == 1 {
  1535. speedSettingView.setSelectItem(index: lastSpeedSelectIndex, isSettingPlayer: false)
  1536. } else if speedSettingView.viewType == 2 {
  1537. speedSettingView.setSelectItem(index: lastJumpSpeedSelectIndex, isSettingPlayer: false)
  1538. } else if speedSettingView.viewType == 3 {
  1539. if lastCyclesSelectIndex != -1 {
  1540. speedSettingView.setSelectItem(index: lastCyclesSelectIndex, isSettingPlayer: false)
  1541. } else {
  1542. speedSettingView.setSelectItem(index: 0, isSettingPlayer: false)
  1543. }
  1544. }
  1545. updateTimeInfomation()
  1546. }
  1547. }
  1548. // MARK: - 同步/下载素材相关
  1549. /// 同步/下载素材相关
  1550. extension PQStuckPointEditerController {
  1551. /// 同步音乐相关数据
  1552. /// - Returns: <#description#>
  1553. func synchroMusicInfoData(resetSelectIndex:Bool = true) {
  1554. if (stuckPointMusicData?.rhythmSdata.count ?? 0) <= 0 {
  1555. synchroMarskView.show()
  1556. PQStuckPointViewModel.stuckPointMusicDetailData(musicId: stuckPointMusicData?.musicId ?? "", originType: stuckPointMusicData?.originType ?? 1) { [weak self] newMusicData, _ in
  1557. if newMusicData != nil, (newMusicData?.rhythmSdata.count ?? 0) > 0 {
  1558. self?.isStuckPointDataSuccess = true
  1559. self?.stuckPointMusicData?.rhythmSdata = newMusicData?.rhythmSdata ?? []
  1560. self?.stuckPointMusicData?.startTime = newMusicData?.startTime ?? 0
  1561. self?.stuckPointMusicData?.endTime = newMusicData?.endTime ?? 0
  1562. if newMusicData?.speed != nil {
  1563. self?.stuckPointMusicData?.speed = newMusicData?.speed ?? 2
  1564. }
  1565. if self?.stuckPointMusicData?.localPath == nil || (self?.stuckPointMusicData?.localPath?.count ?? 0) > 0 {
  1566. PQDownloadManager.downLoadFile(url: self?.stuckPointMusicData?.musicPath ?? "") { [weak self] filePath, error in
  1567. if error == nil, filePath != nil {
  1568. self?.isSynchroMusicInfoSuccess = true
  1569. self?.stuckPointMusicData?.localPath = filePath?.replacingOccurrences(of: documensDirectory, with: "")
  1570. // 处理所有数据完成
  1571. self?.dealWithDataSuccess(resetSelectIndex: resetSelectIndex)
  1572. }
  1573. self?.synchroMarskView.removeMarskView()
  1574. }
  1575. } else {
  1576. self?.isSynchroMusicInfoSuccess = true
  1577. // 处理所有数据完成
  1578. self?.dealWithDataSuccess(resetSelectIndex: resetSelectIndex)
  1579. }
  1580. // 添加子视图
  1581. self?.addSubViews()
  1582. } else {
  1583. self?.synchroMarskView.removeMarskView()
  1584. cShowHUB(superView: nil, msg: "音乐信息加载失败,请重新选择音乐")
  1585. self?.navigationController?.popViewController(animated: true)
  1586. }
  1587. }
  1588. } else if stuckPointMusicData?.localPath == nil || (stuckPointMusicData?.localPath?.count ?? 0) > 0 {
  1589. synchroMarskView.show()
  1590. isStuckPointDataSuccess = true
  1591. PQDownloadManager.downLoadFile(url: stuckPointMusicData?.musicPath ?? "") { [weak self] filePath, error in
  1592. if error == nil, filePath != nil {
  1593. self?.isSynchroMusicInfoSuccess = true
  1594. self?.stuckPointMusicData?.localPath = filePath?.replacingOccurrences(of: documensDirectory, with: "")
  1595. // 处理所有数据完成
  1596. self?.dealWithDataSuccess(resetSelectIndex: resetSelectIndex)
  1597. } else {
  1598. self?.synchroMarskView.removeMarskView()
  1599. // PQUploadRemindView.showUploadRemindView(title: nil, attributedTitle: NSAttributedString(string: "加载音乐失败,请重新选择音乐"), summary: "", confirmTitle: nil) { [weak self] _, _ in
  1600. // self?.navigationController?.popViewController(animated: true)
  1601. // }
  1602. }
  1603. }
  1604. } else {
  1605. isStuckPointDataSuccess = true
  1606. // 处理所有数据完成
  1607. dealWithDataSuccess(resetSelectIndex: resetSelectIndex)
  1608. }
  1609. }
  1610. /// 导出相册数据
  1611. /// - Returns: <#description#>
  1612. func exportPhotoData() {
  1613. // 取消所有的导出
  1614. PQSingletoMemoryUtil.shared.allExportSession.forEach { _, exportSession in
  1615. exportSession.cancelExport()
  1616. }
  1617. var isHaveVideo: Bool = false
  1618. if selectedMetarialData != nil, (selectedMetarialData?.count ?? 0) > 0 {
  1619. if synchroMarskView.superview == nil {
  1620. UIApplication.shared.keyWindow?.addSubview(synchroMarskView)
  1621. }
  1622. let dispatchGroup = DispatchGroup()
  1623. for photo in selectedMetarialData! {
  1624. if photo.asset != nil, photo.asset?.mediaType == .video {
  1625. if !isHaveVideo {
  1626. isHaveVideo = true
  1627. }
  1628. dispatchGroup.enter()
  1629. PQPHAssetVideoParaseUtil.parasToAVAsset(phAsset: photo.asset!) { avAsset, _, _, _ in
  1630. if avAsset is AVURLAsset {
  1631. // 创建目录
  1632. let fileName = (avAsset as! AVURLAsset).url.absoluteString
  1633. BFLog(message: "video fileName is\(fileName)")
  1634. let tempPhoto = self.selectedMetarialData?.first(where: { material in
  1635. material.asset == photo.asset
  1636. })
  1637. if fileName.count > 0 {
  1638. createDirectory(path: photoLibraryDirectory)
  1639. let outFilePath = photoLibraryDirectory + fileName.md5 + ".mp4"
  1640. // 文件存在先删除老文件
  1641. if FileManager.default.fileExists(atPath: outFilePath) {
  1642. do {
  1643. try FileManager.default.removeItem(at: NSURL.fileURL(withPath: outFilePath))
  1644. } catch {
  1645. BFLog(message: "导出相册视频-error == \(error)")
  1646. }
  1647. }
  1648. do {
  1649. try FileManager.default.copyItem(atPath: fileName.replacingOccurrences(of: "file:///", with: ""), toPath: outFilePath)
  1650. print("Success to copy file.")
  1651. } catch {
  1652. print("Failed to copy file.")
  1653. }
  1654. tempPhoto?.locationPath = outFilePath.replacingOccurrences(of: documensDirectory, with: "")
  1655. BFLog(message: "导出视频相册地址为 \(String(describing: tempPhoto?.locationPath))")
  1656. dispatchGroup.leave()
  1657. }
  1658. }else {
  1659. // 结束loading动画
  1660. DispatchQueue.main.async {
  1661. self.synchroMarskView.removeMarskView()
  1662. }
  1663. }
  1664. }
  1665. }
  1666. }
  1667. dispatchGroup.notify(queue: DispatchQueue.main) { [weak self] in
  1668. self?.isExportVideosSuccess = true
  1669. BFLog(message: "所有相册视频导出成功")
  1670. // 处理所有数据完成
  1671. if isHaveVideo {
  1672. self?.dealWithDataSuccess()
  1673. }
  1674. }
  1675. // 只有图片
  1676. if !isHaveVideo {
  1677. isExportVideosSuccess = true
  1678. BFLog(message: "所有相册视频导出成功")
  1679. dealWithDataSuccess()
  1680. }
  1681. }
  1682. }
  1683. /// 处理所有数据完成
  1684. /// - Returns: <#description#>
  1685. /// resetSelectIndex : 是否重置用户选择的速度设置,在编辑界面切换音乐时不重置
  1686. func dealWithDataSuccess(resetSelectIndex:Bool = true) {
  1687. BFLog(message: "三组参数:\(isSynchroMusicInfoSuccess) \(isStuckPointDataSuccess) \(isExportVideosSuccess)")
  1688. if !isSynchroMusicInfoSuccess || !isStuckPointDataSuccess || !isExportVideosSuccess {
  1689. return
  1690. }
  1691. createPorjectData()
  1692. BFLog(1, message: "界面编辑界面时参数 选择素材时长:\(selectedTotalDuration) 选择素材总数:\(selectedDataCount) 选择图片总数\(selectedImageDataCount) 再创建类型:\(String(describing: reCreateVideoData?.rhythmMode))")
  1693. if(resetSelectIndex){
  1694. // 1 生成默认参数值
  1695. /*
  1696. - 当素材总时长∈[0-6)s 时,提示推荐仅配乐模式 or 快慢速模式
  1697. - 当素材总时长∈[6-80)s 时,卡点抛留倍数为1x
  1698. - 当素材总时长∈[80,120)s 时,卡点抛留倍数为2x
  1699. - 当素材总时长∈[120,160)s 时,卡点抛留倍数为3x
  1700. - 当素材总时长∈[160,200)s 时,卡点抛留倍数为4x
  1701. - 当素材总时长∈[200,240)s 时,卡点抛留倍数为5x
  1702. - 当素材总时长∈[240,∞)s 时,卡点抛留倍数为5x
  1703. */
  1704. // 1.1) 生成跳跃卡点的默认值
  1705. if selectedTotalDuration >= 6 && selectedTotalDuration < 80 {
  1706. lastJumpSpeedSelectIndex = 0
  1707. } else if selectedTotalDuration >= 80 && selectedTotalDuration < 120 {
  1708. lastJumpSpeedSelectIndex = 1
  1709. } else if selectedTotalDuration >= 120 && selectedTotalDuration < 160 {
  1710. lastJumpSpeedSelectIndex = 2
  1711. } else if selectedTotalDuration >= 160 && selectedTotalDuration < 200 {
  1712. lastJumpSpeedSelectIndex = 3
  1713. } else if (selectedTotalDuration >= 200 && selectedTotalDuration < 240) || selectedTotalDuration >= 240 {
  1714. lastJumpSpeedSelectIndex = 4
  1715. }
  1716. /* 默认进入快慢速模式
  1717. - 当素材总时长∈[120,144]s 时,快慢速处理方式:快速为 6x,慢速为 1.2x,效果是快&快
  1718. - 当素材总时长∈[70,120)s 时,快慢速处理方式:快速为5x,慢速为 1x,效果是快&正常
  1719. - 当素材总时长∈[56,70)s 时,快慢速处理方式:快速为3x,慢速为 0.5x,效果是快&慢
  1720. - 当素材总时长∈[17.5,56)s 时,快慢速处理方式:快速为 2.4x,慢速为 0.4x,效果是快&慢
  1721. - 当素材总时长∈[10.5,17.5)s 时,快慢速处理方式:快速为 1.8x,慢速为 0.3x,效果是快&慢
  1722. - 当素材总时长∈(0,10.5)s 时,快慢速处理方式:快速为 1x,慢速为 0.2x,效果是正常&慢
  1723. */
  1724. // 1.2)生成快慢速的默认值
  1725. if selectedTotalDuration >= 120 && selectedTotalDuration <= 144 || selectedTotalDuration > 144 {
  1726. lastSpeedSelectIndex = 5
  1727. } else if selectedTotalDuration >= 70 && selectedTotalDuration < 120 {
  1728. lastSpeedSelectIndex = 4
  1729. } else if selectedTotalDuration >= 56 && selectedTotalDuration < 70 {
  1730. lastSpeedSelectIndex = 3
  1731. } else if selectedTotalDuration >= 17.5 && selectedTotalDuration < 56 {
  1732. lastSpeedSelectIndex = 2
  1733. } else if selectedTotalDuration >= 10.5 && selectedTotalDuration < 17.5 {
  1734. lastSpeedSelectIndex = 1
  1735. } else if selectedTotalDuration > 0 && selectedTotalDuration < 10.5 {
  1736. lastSpeedSelectIndex = 0
  1737. }
  1738. // 如果是再创作进来的安原视频的模式
  1739. if reCreateVideoData != nil {
  1740. BFLog(message: "是再创作进来的 \(reCreateVideoData!.rhythmMode)")
  1741. switch reCreateVideoData!.rhythmMode {
  1742. case 1:
  1743. editModelClick(sender: jumpPointBtn, reportLog: false)
  1744. break
  1745. case 2:
  1746. editModelClick(sender: speedStuckBtn, reportLog: false)
  1747. break
  1748. case 3:
  1749. editModelClick(sender: onlyMusicBtn, reportLog: false)
  1750. break
  1751. default: break
  1752. }
  1753. return
  1754. }
  1755. // 跳跃卡点不可用
  1756. if selectedTotalDuration < 6 && selectedDataCount != selectedImageDataCount {
  1757. let styleColor = UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)
  1758. jumpPointBtn.setTitleColor(UIColor.init(red: styleColor.rgbaf[0], green: styleColor.rgbaf[1], blue: styleColor.rgbaf[2], alpha: 0.3), for: .selected)
  1759. let pointEditNamalBackgroundColor = PQBFConfig.shared.pointEditNamalBackgroundColor
  1760. jumpPointBtn.backgroundColor = UIColor.init(red: pointEditNamalBackgroundColor.rgbaf[0], green: pointEditNamalBackgroundColor.rgbaf[1], blue: pointEditNamalBackgroundColor.rgbaf[2], alpha: 0.3)
  1761. }
  1762. /*
  1763. 文档规则 https://w42nne6hzg.feishu.cn/docs/doccnQZm1uCfkU4QtJb5fLxYk4d#
  1764. */
  1765. // 2,根据所选择所有素材时长进入默认模式
  1766. // 全是图片
  1767. if selectedDataCount == selectedImageDataCount {
  1768. BFLog(message: "全是图片 \(selectedDataCount) \(selectedImageDataCount)")
  1769. // 默认进入跳跃卡点模式
  1770. editModelClick(sender: jumpPointBtn, reportLog: false)
  1771. } else {
  1772. // 默认进入快慢速模式
  1773. if selectedTotalDuration > 0 && selectedTotalDuration <= 144 {
  1774. editModelClick(sender: speedStuckBtn, reportLog: false)
  1775. } else {
  1776. // 默认进入卡点模式
  1777. editModelClick(sender: jumpPointBtn, reportLog: false)
  1778. }
  1779. }
  1780. }else{
  1781. editModelClick(sender: lastEditModelBtn ?? jumpPointBtn, reportLog: false)
  1782. }
  1783. }
  1784. }