PQStuckPointEditerController.swift 110 KB

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