1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342 |
- //
- // PQStuckPointPublicController.swift
- // PQSpeed
- //
- // Created by SanW on 2021/5/6.
- // Copyright © 2021 BytesFlow. All rights reserved.
- //
- import ObjectMapper
- import Photos
- import UIKit
- import WechatOpenSDK
- class PQStuckPointPublicController: PQBaseViewController {
- private var isShared: Bool = false // 是否在分享
- private var isExportSuccess: Bool = false // 是否导出完成
- private var isSaveDraftSuccess: Bool = false // 是否保存草稿完成
- private var isSaveProjectSuccess: Bool = false // 是否保存项目完成
- private var isUploadSuccess: Bool = false // 是否上传完成
- private var isPublicSuccess: Bool = false // 是否发布完成
- private var exportLocalURL: URL? // 导出的地址
- // 再创作数据
- private var reCreateData: PQReCreateModel?
- // 确定上传的数据
- private var uploadData: PQUploadModel?
- // 发布成功的视频数据
- private var videoData: PQVideoListModel?
- // 视频创作埋点数据
- private var eventTrackData: PQVideoMakeEventTrackModel?
- // 选中的总时长-统计使用
- var selectedTotalDuration: Float64 = 0
- // 选择的总数-统计使用
- var selectedDataCount: Int = 0
- // 选择的图片总数-统计使用
- var selectedImageDataCount: Int = 0
- // 最大的宽度
- private var maxWidth: CGFloat = cScreenWidth
- // 最大的高度
- private var maxHeight: CGFloat = 385
- // 开始导出的时间
- private let startExportDate: Float64 = Date().timeIntervalSince1970
- // 导出结束的时间
- private var exportEndDate: Float64 = Date().timeIntervalSince1970
- // 取到的封面 给发布界面使用
- private var coverImage: UIImage?
- // 导出视频工具类
- private var exporter: PQCompositionExporter!
- // 导出进度
- private var exportProgrss = 0
- var mStickers: [PQEditVisionTrackMaterialsModel]?
- var remindView: PQRemindView?
- //已经选择标题内容,加一个属性接收 使用有不在主线不能直接使用 titleLabel text
- var selectTitle:String = ""
- // 预览大小
- private var preViewSize: CGSize {
- switch aspectRatio {
- case let .origin(width, height):
- var tempHeight: CGFloat = 0
- var tempWidth: CGFloat = 0
- if width > height {
- tempWidth = maxWidth
- tempHeight = (maxWidth * height / width)
- if tempHeight > maxHeight {
- tempHeight = maxHeight
- tempWidth = (maxHeight * width / height)
- }
- } else {
- tempHeight = maxHeight
- tempWidth = (maxHeight * width / height)
- if tempWidth > maxWidth {
- tempWidth = maxWidth
- tempHeight = (maxWidth * height / width)
- }
- }
- if tempHeight.isNaN || tempWidth.isNaN {
- return CGSize.zero
- } else {
- return CGSize(width: tempWidth, height: tempHeight)
- }
- case .oneToOne:
- if maxWidth > maxHeight {
- return CGSize(width: maxHeight, height: maxHeight)
- } else {
- return CGSize(width: maxWidth, height: maxWidth)
- }
- case .sixteenToNine:
- return CGSize(width: maxWidth, height: maxWidth * 9.0 / 16.0)
- case .nineToSixteen:
- return CGSize(width: maxHeight * 9.0 / 16.0, height: maxHeight)
- default:
- break
- }
- return CGSize(width: maxHeight, height: maxHeight)
- }
- // 背景音乐
- var audioMixModel: PQVoiceModel?
- // 画面比例
- var aspectRatio: aspectRatio?
- // 导出的项目数据
- var editProjectModel: PQEditProjectModel? {
- didSet {
- aspectRatio = PQPlayerViewModel.videoCanvasTypeToAspectRatio(projectModel: editProjectModel)
- var totalDuration: Float64 = 0
- if editProjectModel?.sData?.sections.count ?? 0 > 0 {
- for section in (editProjectModel?.sData?.sections)! {
- totalDuration = totalDuration + section.sectionDuration
- }
- }
- editProjectModel?.sData?.videoMetaData?.duration = totalDuration
- if editProjectModel?.sData?.sections != nil, (editProjectModel?.sData?.sections.count ?? 0) > 0 {
- // 查找出背景图并设置
- var coverImageMaterialsModel: PQEditVisionTrackMaterialsModel?
- for section in (editProjectModel?.sData?.sections)! {
- if coverImageMaterialsModel != nil {
- break
- }
- coverImageMaterialsModel = section.sectionTimeline?.visionTrack?.getEnableVisionTrackMaterials().first
- }
- if coverImageMaterialsModel != nil {
- coverImage = coverImageMaterialsModel?.getCoverImage()
- playerHeaderView.image = coverImage
- playerHeaderView.contentMode = coverImageMaterialsModel!.canvasFillType == stickerContentMode.aspectFitStr.rawValue ? .scaleAspectFill : .scaleAspectFit
- }
- }
- }
- }
- /// 所有需要导出的filter
- var filters: Array = Array<ImageProcessingOperation>.init()
- /// 预览背景页
- lazy var bgTopView: UIView = {
- let bgTopView = UIView(frame: CGRect(x: 0, y: cDevice_iPhoneNavBarAndStatusBarHei, width: cScreenWidth, height: maxHeight))
- bgTopView.backgroundColor = PQBFConfig.shared.styleBackGroundColor
- return bgTopView
- }()
- // 预览界面
- var playerHeaderView: UIImageView = {
- let playerHeaderView = UIImageView(frame: CGRect(x: 0, y: cDevice_iPhoneNavBarAndStatusBarHei, width: cScreenWidth, height: 0))
- playerHeaderView.isUserInteractionEnabled = true
- playerHeaderView.contentMode = .scaleAspectFit
- playerHeaderView.clipsToBounds = true
- return playerHeaderView
- }()
-
- // add by ak 播放器的封面 为了不和原有的播放器层级单独添加一个 view
- lazy var playerHeaderCoverImageView: UIImageView = {
- let playerHeaderCoverImageView = UIImageView.init()
- playerHeaderCoverImageView.isUserInteractionEnabled = true
- playerHeaderCoverImageView.contentMode = .scaleAspectFit
- playerHeaderCoverImageView.clipsToBounds = true
-
- let playBtn = UIButton(type: .custom)
- playBtn.setImage(UIImage().BF_Image(named: "icon_video_play"), for: .normal)
- playBtn.tag = 4
- playBtn.isUserInteractionEnabled = false
- playerHeaderCoverImageView.addSubview(playBtn)
- playerHeaderCoverImageView.isHidden = true
- return playerHeaderCoverImageView
- }()
- /// 播放器
- lazy var avPlayer: AVPlayer = {
- let avPlayer = AVPlayer()
- NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: avPlayer.currentItem, queue: .main) { [weak self] notify in
- BFLog(message: "AVPlayerItemDidPlayToEndTime = \(notify)")
- avPlayer.seek(to: CMTime.zero)
- if(self?.playerHeaderCoverImageView.image != nil){
- self?.playerHeaderCoverImageView.isHidden = false
- }
- self?.playBtn.isHidden = false
- }
- NotificationCenter.default.addObserver(forName: .AVPlayerItemNewErrorLogEntry, object: avPlayer.currentItem, queue: .main) { notify in
- BFLog(message: "AVPlayerItemNewErrorLogEntry = \(notify)")
- }
- NotificationCenter.default.addObserver(forName: .AVPlayerItemFailedToPlayToEndTime, object: avPlayer.currentItem, queue: .main) { notify in
- BFLog(message: "AVPlayerItemFailedToPlayToEndTime = \(notify)")
- }
- NotificationCenter.default.addObserver(forName: .AVPlayerItemPlaybackStalled, object: avPlayer.currentItem, queue: .main) { notify in
- BFLog(message: "AVPlayerItemPlaybackStalled = \(notify)")
- }
- avPlayer.addPeriodicTimeObserver(forInterval: CMTime(value: 1, timescale: 1000), queue: .main) { [weak self] _ in
- let progress = CMTimeGetSeconds(avPlayer.currentItem?.currentTime() ?? CMTime.zero) / CMTimeGetSeconds(avPlayer.currentItem?.duration ?? CMTime.zero)
- if progress >= 1 {
- self?.playBtn.isHidden = false
- }
- }
- return avPlayer
- }()
- /// 预览layer
- lazy var playerLayer: AVPlayerLayer = {
- let playerLayer = AVPlayerLayer(player: avPlayer)
- playerLayer.frame = playerHeaderView.bounds
- return playerLayer
- }()
- /// 播放按钮
- lazy var playBtn: UIButton = {
- let playBtn = UIButton(type: .custom)
- playBtn.frame = CGRect(x: (preViewSize.width - cDefaultMargin * 5) / 2, y: (preViewSize.height - cDefaultMargin * 5) / 2, width: cDefaultMargin * 5, height: cDefaultMargin * 5)
- playBtn.setImage(UIImage().BF_Image(named: "icon_video_play"), for: .normal)
- playBtn.tag = 4
- playBtn.isHidden = true
- playBtn.isUserInteractionEnabled = false
- return playBtn
- }()
- // progressTipsLab
- lazy var progressTipsLab: UILabel = {
- let progressTipsLab = UILabel()
- progressTipsLab.textAlignment = .center
- progressTipsLab.font = UIFont.systemFont(ofSize: 16, weight: .medium)
- progressTipsLab.numberOfLines = 2
- progressTipsLab.textColor = UIColor.white
- let attributedText = NSMutableAttributedString(string: "0%\n视频正在处理中,请勿离开")
- attributedText.addAttributes([.font: UIFont.systemFont(ofSize: 34)], range: NSRange(location: 0, length: 2))
- progressTipsLab.attributedText = attributedText
- progressTipsLab.addShadow()
- return progressTipsLab
- }()
- // 进度条
- lazy var progressView: UIProgressView = {
- let progressView = UIProgressView(progressViewStyle: .default)
- progressView.trackTintColor = UIColor(white: 0, alpha: 0.5)
- progressView.progressTintColor = UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)
- progressView.transform = CGAffineTransform(scaleX: 1.0, y: playerHeaderView.frame.height / 3.0)
- return progressView
- }()
- //提示
- lazy var remindLab: UILabel = {
- let remindLab = UILabel()
- remindLab.font = UIFont.boldSystemFont(ofSize: 18)
- remindLab.textColor = PQBFConfig.shared.styleTitleColor
- remindLab.textAlignment = .center
- remindLab.numberOfLines = 2
- remindLab.backgroundColor = .clear
- remindLab.text = "为你的大作起个响亮的标题\n分享秀一下🎉"
- return remindLab
- }()
-
- //输入框背景
- lazy var inputBackView: UIView = {
- let inputBackView = UIView()
- inputBackView.backgroundColor = .clear
- inputBackView.layer.cornerRadius = 7
- inputBackView.layer.borderWidth = 2
- inputBackView.layer.borderColor = UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue).cgColor
- return inputBackView
- }()
- // 手势提示
- lazy var pinView: UIImageView = {
- let pinView = UIImageView()
- pinView.kf.setImage(with: URL(fileURLWithPath: Bundle().BF_mainbundle().path(forResource: "editCoverPin", ofType: ".gif")!))
- return pinView
- }()
- //封面
- lazy var coverImageView: UIImageView = {
- let coverImageView = UIImageView()
- coverImageView.isUserInteractionEnabled = true
- coverImageView.backgroundColor = .clear
- coverImageView.contentMode = .scaleToFill
- return coverImageView
- }()
- //封面标题
- lazy var coverImageTitle: UILabel = {
- let coverImageTitle = UILabel()
- coverImageTitle.text = "换封面"
- coverImageTitle.textAlignment = .center
- coverImageTitle.backgroundColor = UIColor(red: 0.22, green: 0.26, blue: 0.35, alpha: 0.5)
- coverImageTitle.textColor = .white
- coverImageTitle.font = UIFont.boldSystemFont(ofSize: 12)
- return coverImageTitle
-
- }()
-
- //标题
- lazy var titleLabel: UILabel = {
- let titleLabel = UILabel()
- titleLabel.numberOfLines = 2
- titleLabel.isUserInteractionEnabled = true
- titleLabel.textColor = UIColor.hexColor(hexadecimal: "#ABABAB")
- titleLabel.textAlignment = .left
- titleLabel.font = UIFont.systemFont(ofSize: 17)
- let ges = UITapGestureRecognizer(target: self, action: #selector(titleLabelClick))
- titleLabel.addGestureRecognizer(ges)
- return titleLabel
- }()
-
- //编辑发布标题
- lazy var publicTitleView: PQEditPublicTitleView = {
- let publicTitleView = PQEditPublicTitleView()
- publicTitleView.isHidden = true
- publicTitleView.confirmBtnClock = { [weak self] title in
- BFLog(message: "传出的 title is :\(String(describing: title))")
- self?.changPlayerIsPause(isPause: false)
- self?.setTitleText(text: title ?? "",textColor: .black)
- //更新数据
- self?.videoData?.title = title
- self?.updateCoverImagegOrTitle()
- }
- publicTitleView.viewIsHiddenCallBack = { [weak self] in
-
- self?.changPlayerIsPause(isPause: false)
-
- }
-
- return publicTitleView
- }()
-
- //编辑发布封面
- lazy var publicEditCoverView: PQEditPublicCoverImageView = {
- let publicEditCoverView = PQEditPublicCoverImageView.init(frame: CGRect.init(x: 0, y: 0, width: cScreenWidth, height: cScreenHeigth))
- publicEditCoverView.isHidden = true
- return publicEditCoverView
- }()
-
- //分享到朋友圈
- lazy var shareWechatBtn: UIButton = {
- let shareWechatBtn = UIButton(type: .custom)
- shareWechatBtn.frame = CGRect(x: 0, y: 0, width: 70, height: 70)
- shareWechatBtn.setImage(UIImage().BF_Image(named: "reCreate_opration_wechat"), for: .normal)
- shareWechatBtn.backgroundColor = PQBFConfig.shared.styleBackGroundColor
- shareWechatBtn.addCorner(corner: 6)
- shareWechatBtn.tag = 2
- shareWechatBtn.addTarget(self, action: #selector(btnClick(sender:)), for: .touchUpInside)
- return shareWechatBtn
- }()
- //分享到好友
- lazy var shareFriendBtn: UIButton = {
- let shareFriendBtn = UIButton(type: .custom)
- shareFriendBtn.frame = CGRect(x: 0, y: 0, width: 70, height: 70)
- shareFriendBtn.setImage(UIImage().BF_Image(named: "reCreate_opration_friend"), for: .normal)
- shareFriendBtn.backgroundColor = PQBFConfig.shared.styleBackGroundColor
- shareFriendBtn.addCorner(corner: 6)
- shareFriendBtn.tag = 1
- shareFriendBtn.addTarget(self, action: #selector(btnClick(sender:)), for: .touchUpInside)
- return shareFriendBtn
- }()
-
- //关闭
- lazy var finishedBtn: UIButton = {
- let finishedBtn = UIButton(type: .custom)
- finishedBtn.setTitle("完成", for: .normal)
- finishedBtn.setTitleColor(UIColor.hexColor(hexadecimal: "#BDBDBD"), for: .normal)
- finishedBtn.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .medium)
- finishedBtn.backgroundColor = .clear
- finishedBtn.tag = 3
- finishedBtn.addCorner(corner: 3)
- finishedBtn.addTarget(self, action: #selector(btnClick(sender:)), for: .touchUpInside)
- return finishedBtn
-
- }()
- /// 背景View
- lazy var oprationBgView: UIView = {
- let oprationBgView = UIView(frame: CGRect(x: 0, y: cDevice_iPhoneNavBarAndStatusBarHei, width: cScreenWidth, height: view.frame.height - cDevice_iPhoneNavBarAndStatusBarHei))
- oprationBgView.backgroundColor = .clear
- return oprationBgView
- }()
-
- //除了播放器以外的 下半部分操作区
- lazy var bottomOprationBgView: UIView = {
- let bottomOprationBgView = UIView(frame: CGRect(x: 0, y: cDevice_iPhoneNavBarAndStatusBarHei + maxHeight, width: cScreenWidth, height: view.frame.height - cDevice_iPhoneNavBarAndStatusBarHei - maxHeight))
- bottomOprationBgView.backgroundColor = .clear
- bottomOprationBgView.isHidden = true
- return bottomOprationBgView
- }()
-
- override func backBtnClick() {
- if isExportSuccess {
- navigationController?.popViewController(animated: true)
- } else {
- view.endEditing(true)
- let remindData = PQBaseModel()
- remindData.title = "编辑的内容,将不会被保存"
- remindView = PQRemindView(frame: CGRect(x: 0, y: 0, width: cScreenWidth, height: cScreenHeigth))
- remindView?.isBanned = true
- remindView?.confirmBtn.setTitle("确认", for: .normal)
- remindView?.cancelBtn.setTitleColor(UIColor.hexColor(hexadecimal: "#333333"), for: .normal)
- remindView?.confirmBtn.setTitleColor(UIColor.hexColor(hexadecimal: "#EE0051"), for: .normal)
- UIApplication.shared.keyWindow?.addSubview(remindView!)
- remindView?.remindData = remindData
- remindView?.remindBlock = { [weak self] item, _ in
- if item.tag == 2 {
- // 取消导出
- if self?.exporter != nil {
- self?.exporter.cancel()
- }
- self?.navigationController?.popViewController(animated: true)
- }
- }
- }
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- // 注册上传成功的通知
- addNotification(self, selector: #selector(uploadSuccess(notify:)), name: cUploadSuccessKey, object: nil)
- PQNotification.addObserver(self, selector: #selector(didBecomeActiveNotification), name: UIApplication.didBecomeActiveNotification, object: nil)
- leftButton(image: "icon_detail_back", tintColor: PQBFConfig.shared.styleTitleColor)
- navHeadImageView?.backgroundColor = UIColor.clear
- lineView?.removeFromSuperview()
- view.addSubview(bgTopView)
- playerHeaderView.frame = CGRect(origin: CGPoint(x: (cScreenWidth - preViewSize.width) / 2, y: (maxHeight - preViewSize.height) / 2), size: preViewSize)
- let ges = UITapGestureRecognizer(target: self, action: #selector(playVideo))
- playerHeaderView.addGestureRecognizer(ges)
-
-
- if playerLayer.superlayer == nil {
- playerHeaderView.layer.insertSublayer(playerLayer, at: 0)
- }
- playerHeaderView.addSubview(playBtn)
- playerHeaderView.addSubview(progressView)
- view.addSubview(oprationBgView)
- oprationBgView.addSubview(progressTipsLab)
-
- // 添加导出view
- bgTopView.addSubview(playerHeaderView)
-
- playerHeaderCoverImageView.frame = playerHeaderView.frame
- playerHeaderCoverImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(playVideo)))
-
- (playerHeaderCoverImageView.viewWithTag(4))?.frame =
- CGRect(x: (preViewSize.width - cDefaultMargin * 5) / 2, y: (preViewSize.height - cDefaultMargin * 5) / 2, width: cDefaultMargin * 5, height: cDefaultMargin * 5)
- bgTopView.addSubview(playerHeaderCoverImageView)
-
-
- view.addSubview(bottomOprationBgView)
- bottomOprationBgView.addSubview(remindLab)
- bottomOprationBgView.addSubview(shareWechatBtn)
- bottomOprationBgView.addSubview(shareFriendBtn)
- bottomOprationBgView.addSubview(finishedBtn)
- bottomOprationBgView.addSubview(inputBackView)
- bottomOprationBgView.addSubview(pinView)
- inputBackView.addSubview(coverImageView)
- coverImageView.addSubview(coverImageTitle)
- inputBackView.addSubview(titleLabel)
-
- view.addSubview(publicTitleView)
- view.addSubview(publicEditCoverView)
-
- coverImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(settingCoverImage)))
-
- progressView.snp.makeConstraints { make in
- make.left.right.centerY.equalTo(playerHeaderView)
- make.height.equalTo(3)
- }
- progressTipsLab.snp.makeConstraints { make in
- make.centerX.equalToSuperview()
- make.top.equalToSuperview().offset(((preViewSize.height - 90) / 2) + ((maxHeight - preViewSize.height) / 2))
- make.width.equalToSuperview()
- make.height.equalTo(90)
- }
- finishedBtn.snp.makeConstraints { make in
- make.centerX.equalToSuperview()
- make.bottom.equalToSuperview().offset(-cSafeAreaHeight)
- make.width.equalTo(100)
- make.height.equalTo(22)
- }
- shareWechatBtn.snp.makeConstraints { make in
- make.right.equalTo(view.snp_centerX).offset(-cDefaultMargin)
- make.width.equalTo(164)
- make.height.equalTo(52)
- make.bottom.equalTo(finishedBtn.snp_top).offset(-34)
- }
- shareFriendBtn.snp.makeConstraints { make in
- make.left.equalTo(view.snp_centerX).offset(cDefaultMargin)
- make.width.bottom.height.equalTo(shareWechatBtn)
- }
-
- inputBackView.snp.makeConstraints { make in
- make.centerX.equalToSuperview()
- make.bottom.equalTo(shareWechatBtn.snp_top).offset(-16)
- make.width.equalTo(343)
- make.height.equalTo(109)
-
- }
-
- //根据横竖屏设置不同的 UI
- let isWidth:Bool = (Float(editProjectModel?.sData?.videoMetaData?.videoWidth ?? 0) / Float(editProjectModel?.sData?.videoMetaData?.videoHeight ?? 0) ) >= 1
- var coverImageViewHeight = 50.0 * Float(editProjectModel?.sData?.videoMetaData?.videoHeight ?? 0) / Float(editProjectModel?.sData?.videoMetaData?.videoWidth ?? 0)
- if(coverImageViewHeight > 89){
- coverImageViewHeight = 89
- }
-
- coverImageView.snp.makeConstraints { make in
- make.left.equalToSuperview().offset(12)
- make.width.equalTo(50)
- make.top.equalToSuperview().offset(10)
- make.height.equalTo(coverImageViewHeight)
- }
-
- coverImageTitle.snp.makeConstraints { make in
- make.left.equalToSuperview()
- make.width.equalTo(50)
- make.top.equalTo(coverImageView.snp_bottom).offset(isWidth ? 0 : -23)
- make.height.equalTo(23)
- }
-
- remindLab.snp.makeConstraints { make in
- make.centerX.equalToSuperview()
- make.bottom.equalTo(inputBackView.snp_top).offset(-16)
- }
-
- titleLabel.snp.makeConstraints { make in
- make.height.equalTo(48)
- make.left.equalTo(coverImageView.snp_right).offset(12)
- make.right.equalToSuperview().offset(-14)
- make.top.equalToSuperview().offset(10)
- }
-
- pinView.snp.makeConstraints { make in
- make.height.width.equalTo(72)
- make.right.equalToSuperview()
- make.bottom.equalTo(inputBackView.snp_bottom)
- }
-
- publicTitleView.snp.makeConstraints { make in
- make.height.equalTo(cScreenHeigth)
- make.width.equalTo(cScreenWidth)
- make.bottom.equalToSuperview()
- }
-
-
- // 取消所有的导出
- PQSingletoMemoryUtil.shared.allExportSession.forEach { _, exportSession in
- exportSession.cancelExport()
- }
- // 开始导出
- beginExport()
- /// 保存草稿
- saveDraftbox()
- // 曝光上报:窗口曝光
- PQEventTrackViewModel.baseReportUpload(businessType: .bt_windowView, objectType: .ot_view_publishSyncedUp, pageSource: .sp_stuck_publishSyncedUp, extParams: nil, remindmsg: "卡点视频数据上报-(曝光上报:窗口曝光)")
-
- //取推荐标题
- getTitles()
- }
- override func viewWillAppear(_ animated: Bool) {
- super.viewWillAppear(animated)
- PQNotification.addObserver(self, selector: #selector(enterBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
- PQNotification.addObserver(self, selector: #selector(willEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)
- UIApplication.shared.isIdleTimerDisabled = true
-
-
- //从相册选择一个照片后回调
- addNotification(self, selector: #selector(imageSelectedImage(notify:)), name: cSelectedImageSuccessKey, object: nil)
- #if swift(>=4.2)
- let memoryNotification = UIApplication.didReceiveMemoryWarningNotification
- _ = UIApplication.willTerminateNotification
- _ = UIApplication.didEnterBackgroundNotification
- #else
- let memoryNotification = NSNotification.Name.UIApplicationDidReceiveMemoryWarning
- let terminateNotification = NSNotification.Name.UIApplicationWillTerminate
- let enterbackgroundNotification = NSNotification.Name.UIApplicationDidEnterBackground
- #endif
- NotificationCenter.default.addObserver(
- self, selector: #selector(clearMemoryCache), name: memoryNotification, object: nil
- )
- }
- @objc public func clearMemoryCache() {
- BFLog(message: "收到内存警告")
- }
- override func viewWillDisappear(_ animated: Bool) {
- super.viewWillDisappear(animated)
- UIApplication.shared.isIdleTimerDisabled = false
- }
- deinit {
- view.endEditing(true)
- PQNotification.removeObserver(self)
- // 取消导出
- if exporter != nil {
- exporter.cancel()
- }
- avPlayer.pause()
- avPlayer.replaceCurrentItem(with: nil)
- // 点击上报:返回按钮
- PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_click_back, pageSource: .sp_stuck_publishSyncedUp, extParams: nil, remindmsg: "卡点视频数据上报-(点击上报:返回按钮)")
- }
- }
- // MARK: - 导出/上传/下载及其他方法
- /// 导出/上传/下载及其他方法
- extension PQStuckPointPublicController {
- /// fp1 - 导出视频
- /// 开始导出视频
- func beginExport() {
- if !(editProjectModel?.sData?.sections != nil && (editProjectModel?.sData?.sections.count ?? 0) > 0) {
- BFLog(message: "项目段落错误❌")
- return
- }
- // 输出视频地址
- var outPutMP4Path = exportVideosDirectory
- if !directoryIsExists(dicPath: outPutMP4Path) {
- BFLog(message: "文件夹不存在")
- createDirectory(path: outPutMP4Path)
- }
- outPutMP4Path.append("video_\(String.qe.timestamp()).mp4")
- let outPutMP4URL = URL(fileURLWithPath: outPutMP4Path)
- BFLog(message: "导出视频地址 \(outPutMP4URL)")
- let inputAsset = AVURLAsset(url: URL(fileURLWithPath: documensDirectory + (audioMixModel?.localPath ?? "")), options: nil)
- // 每次初始化的时候设置初始值 为 nIl
- exporter = PQCompositionExporter(asset: inputAsset, videoComposition: nil, audioMix: nil, filters: nil, stickers: mStickers, animationTool: nil, exportURL: outPutMP4URL)
- var orgeBitRate = (editProjectModel?.sData?.videoMetaData?.videoWidth ?? 0) * (editProjectModel?.sData?.videoMetaData?.videoHeight ?? 0) * 3
- if(mStickers != nil){
- for stick in mStickers! {
- if stick.type == StickerType.VIDEO.rawValue {
- let asset = AVURLAsset(url: URL(fileURLWithPath: documensDirectory + stick.locationPath), options: avAssertOptions)
- let cbr = asset.tracks(withMediaType: .video).first?.estimatedDataRate
- if Int(cbr ?? 0) > orgeBitRate {
- orgeBitRate = Int(cbr ?? 0)
- }
- }
- }
- }
- BFLog(message: "导出设置的码率为:\(orgeBitRate)")
- exporter.showGaussianBlur = true
- if exporter.prepare(videoSize: CGSize(width: editProjectModel?.sData?.videoMetaData?.videoWidth ?? 0, height: editProjectModel?.sData?.videoMetaData?.videoHeight ?? 0), videoAverageBitRate: orgeBitRate) {
- let playeTimeRange: CMTimeRange = CMTimeRange(start: CMTime(value: CMTimeValue(Int((audioMixModel?.startTime ?? 0) * 600)), timescale: 600), end: CMTime(value: CMTimeValue(Int((audioMixModel?.endTime ?? 0) * 600)), timescale: 600))
- BFLog(message: "开始导出 \(String(describing: audioMixModel?.startTime)) 结束 \(String(describing: audioMixModel?.endTime))")
- exporter.start(playeTimeRange: playeTimeRange)
- BFLog(message: "开始导出")
- }
- exporter.progressClosure = { [weak self] _, _, progress in
- BFLog(message: "合成进度 \(progress)")
- let useProgress = progress > 1 ? 1 : progress
- if progress > 0, Int(useProgress * 100) > (self?.exportProgrss ?? 0) {
- // 更新进度
- self?.updatePublicCurrentProgress(useProgress: useProgress * 0.88)
- }
- }
- exporter.completion = { [weak self] url in
- BFLog(message: "MovieOutput total frames appended:导了完成: \(url) 生成视频时长为:\(CMTimeGetSeconds(AVAsset.init(url: url).duration))")
-
- // 导出完成后取消导出
- if self?.exporter != nil {
- self?.exporter.cancel()
- }
- self?.remindView?.removeFromSuperview()
- if !(self?.isExportSuccess ?? false) {
- self?.isExportSuccess = true
- self?.exportEndDate = Date().timeIntervalSince1970
- BFLog(message: "视频导出完成-开始去发布视频")
- self?.exportLocalURL = url
- /// fp2-1-1 - 请求权限
- // self?.authorizationStatus()
- /// fp2-2 - 保存草稿
- // self?.saveDraftbox()
- /// fp2 - 处理视频数据
- self?.dealWithVideoData()
- }
- }
- }
- /// fp2-1-1 - 请求权限
- func authorizationStatus() {
- let authStatus = PHPhotoLibrary.authorizationStatus()
- if authStatus == .notDetermined {
- // 第一次触发授权 alert
- PHPhotoLibrary.requestAuthorization { [weak self] (status: PHAuthorizationStatus) -> Void in
- if status != .authorized {
- cShowHUB(superView: nil, msg: "您尚未打开相册权限,请到设置页打开相册权限")
- } else {
- /// fp2-1-2 - 保存视频到相册
- self?.saveStuckPointVideo()
- }
- }
- } else if authStatus == .authorized {
- /// fp2-1-2 - 保存视频到相册
- saveStuckPointVideo()
- } else {
- // cShowHUB(superView: nil, msg: "您尚未打开相册权限,请到设置页打开相册权限")
- }
- }
- /// fp2-1-2 - 保存视频到相册
- /// - Parameter localPath: localPath description
- /// - Returns: <#description#>
- func saveStuckPointVideo() {
- let authStatus = PHPhotoLibrary.authorizationStatus()
- if authStatus == .authorized {
- let photoLibrary = PHPhotoLibrary.shared()
- photoLibrary.performChanges({ [weak self] in
- PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: (self?.exportLocalURL)!)
- }) { [weak self] isFinished, _ in
- DispatchQueue.main.async { [weak self] in
- if self?.view != nil {
- if isFinished {
- // cShowHUB(superView: self!.view, msg: "视频已保存至相册")
- } else {
- // cShowHUB(superView: self!.view, msg: "视频保存失败")
- }
- }
- }
- }
- } else {
- // cShowHUB(superView: nil, msg: "您尚未打开相册权限,请到设置页打开相册权限")
- }
- }
- /// fp2-2 - 保存草稿
- /// - Returns: <#description#>
- @objc func saveDraftbox() {
- let sdata = editProjectModel?.sData?.toJSONString(prettyPrint: false)
- if sdata != nil, (sdata?.count ?? 0) > 0 {
- DispatchQueue.global().async { [weak self] in
- PQBaseViewModel.saveDraftbox(draftboxId: self?.editProjectModel?.draftboxId, title: self?.editProjectModel?.sData?.videoMetaData?.title, coverUrl: self?.editProjectModel?.sData?.videoMetaData?.coverUrl, sdata: sdata!, videoFromScene: .stuckPoint, copyType: (self?.audioMixModel != nil && self?.audioMixModel?.originProjectId != nil && (self?.audioMixModel?.originProjectId?.count ?? 0) > 0) ? 3 : nil, originProjectId: self?.audioMixModel?.originProjectId) { [weak self] draftboxInfo, _ in
- if draftboxInfo != nil {
- self?.editProjectModel?.draftboxId = draftboxInfo?["draftboxId"] as? String ?? ""
- self?.editProjectModel?.sData?.videoMetaData?.title = draftboxInfo?["title"] as? String ?? ""
- self?.editProjectModel?.sData?.videoMetaData?.coverUrl = draftboxInfo?["coverUrl"] as? String ?? ""
- self?.editProjectModel?.dataVersionCode = draftboxInfo?["dataVersionCode"] as? Int ?? 0
- BFLog(message: "保存远程的草稿成功")
- self?.isSaveDraftSuccess = true
- /// fp3 - 保存项目
- self?.saveProject()
- } else {
- // 保存草稿失败-播放视频
- // self?.publicEnd(isError: true)
- }
- }
- }
- } else {
- cShowHUB(superView: nil, msg: "您尚未打开相册权限,请到设置页打开相册权限")
- // 保存草稿失败-播放视频
- publicEnd(isError: true)
- }
- }
- /// fp3 - 保存项目
- /// - Returns: description
- func saveProject() {
- if isSaveDraftSuccess {
- let sdata = editProjectModel?.sData?.toJSONString(prettyPrint: false) ?? ""
- let draftboxId: String? = editProjectModel?.draftboxId
- PQBaseViewModel.saveProject(draftboxId: draftboxId, sdata: sdata, videoFromScene: .stuckPoint) { [weak self] projectId, msg in
- BFLog(message: "生成的项目id1111 :\(projectId ?? ""),msg = \(msg ?? "")")
- if projectId == nil || (projectId?.count ?? 0) <= 0 {
- PQBaseViewModel.saveProject(draftboxId: draftboxId, sdata: sdata, videoFromScene: .stuckPoint) { [weak self] projectId, msg in
- BFLog(message: "生成的项目id222 :\(projectId ?? ""),msg = \(msg ?? "")")
- if projectId == nil || (projectId?.count ?? 0) <= 0 {
- PQBaseViewModel.saveProject(draftboxId: draftboxId, sdata: sdata, videoFromScene: .stuckPoint) { [weak self] projectId, msg in
- BFLog(message: "生成的项目id 3333:\(projectId ?? ""),msg = \(msg ?? "")")
- if projectId != nil, (projectId?.count ?? 0) > 0 {
- self?.editProjectModel?.projectId = projectId ?? ""
- }
- /// fp4 - 处理视频数据
- // self?.dealWithVideoData()
- }
- } else {
- self?.editProjectModel?.projectId = projectId ?? ""
- /// fp4 - 处理视频数据
- // self?.dealWithVideoData()
- }
- }
- } else {
- self?.editProjectModel?.projectId = projectId ?? ""
- /// fp4 - 处理视频数据
- // self?.dealWithVideoData()
- }
- }
- }
- }
- /// fp4 - 处理视频数据
- /// - Returns: description
- @objc func dealWithVideoData() {
- BFLog(message: "开始去发布视频12")
- isSaveProjectSuccess = true
- if isExportSuccess && exportLocalURL != nil {
- BFLog(message: "素材上传完成同时视频导出完成开始发布视频")
- // 更新项目
- PQBaseViewModel.updateProject(projectId: editProjectModel?.projectId ?? "", produceStatus: "5") { repseon, _ in
- BFLog(message: "updateProject 结果 is \(String(describing: repseon))")
- }
- let asset = AVURLAsset(url: exportLocalURL!, options: nil)
- let tempUploadData = PQUploadModel()
- tempUploadData.duration = CMTimeGetSeconds(asset.duration)
- tempUploadData.localPath = exportLocalURL?.absoluteString
- tempUploadData.videoWidth = CGFloat(editProjectModel?.sData?.videoMetaData?.videoWidth ?? 0)
- tempUploadData.videoHeight = CGFloat(editProjectModel?.sData?.videoMetaData?.videoHeight ?? 0)
- tempUploadData.image = PQVideoSnapshotUtil.videoSnapshot(videoURL: exportLocalURL!, time: 0)
- if tempUploadData.image == nil {
- tempUploadData.image = coverImage
- }
- tempUploadData.videoFromScene = .stuckPoint
- eventTrackData = getExportEventTrackData()
- eventTrackData?.projectId = editProjectModel?.projectId ?? ""
- uploadData = tempUploadData
- if uploadData?.image == nil {
- uploadData?.image = PQVideoSnapshotUtil.videoSnapshot(videoURL: exportLocalURL!, time: 0)
- }
- if uploadData?.image != nil {
- playerHeaderView.image = uploadData?.image
- coverImageView.image = uploadData?.image
- }
- if isExportSuccess, exportLocalURL != nil {
- let size = try! exportLocalURL?.resourceValues(forKeys: [.fileSizeKey])
- BFLog(message: "size = \(String(describing: size))")
- if Float64(size?.fileSize ?? 0) <= maxUploadSize {
- /// fp5 - 上传视频
- reUploadVideo()
- }
- }
- }
- }
- /// fp5 - 上传视频
- /// - Returns: <#description#>
- @objc func reUploadVideo() {
- if uploadData?.stsToken != nil {
- multipartUpload(response: uploadData?.stsToken)
- } else {
- uploadVideo()
- }
- }
- /// fp5-1 - 开始上传视频
- /// - Returns: <#description#>
- func uploadVideo() {
- let uploadRequest: OSSMultipartUploadRequest? = PQAliOssUtil.shared.allTasks[uploadData?.videoBucketKey ?? ""]
- if uploadRequest != nil, "\(uploadRequest?.callbackParam["code"] ?? "0")" == "1" {
- return
- }
- // 更新进度
- updatePublicCurrentProgress(useProgress: 0.89)
- DispatchQueue.global().async {
- PQBaseViewModel.getStsToken { [weak self] response, _ in
- if response == nil {
- self?.showUploadRemindView(isNetCollected: false, msg: "获取数据失败了哦~")
- return
- }
- // 更新进度
- self?.updatePublicCurrentProgress(useProgress: 0.90)
- BFLog(message: "取我方服务器STS 返回数据 \(String(describing: response))")
- self?.multipartUpload(response: response)
- }
- }
- }
- /// fp5-2 - 继续上传视频
- /// - Parameter response: <#response description#>
- func multipartUpload(response: [String: Any]?) {
- let FileName: String = "\(response?["FileName"] ?? "")"
- let uploadID: String = "\(response?["Upload"] ?? "")"
- uploadData?.stsToken = response
- uploadData?.videoBucketKey = FileName
- uploadData?.uploadID = uploadID
- if uploadData?.asset != nil && isValidURL(url: uploadData?.localPath) {
- PQPHAssetVideoParaseUtil.exportPHAssetToMP4(phAsset: (uploadData?.asset)!, isCancelCurrentExport: true) { [weak self] _, _, filePath, _ in
- if filePath != nil, (filePath?.count ?? 0) > 0 {
- self?.uploadData?.localPath = filePath
- PQAliOssUtil.multipartUpload(localPath: self?.uploadData?.localPath ?? "", response: response)
- }
- }
- } else {
- PQAliOssUtil.multipartUpload(localPath: uploadData?.localPath ?? "", response: response)
- }
- PQAliOssUtil.shared.aliOssHander = { [weak self] isMatarialUpload, materialType, _, code, objectkey, _, _, _, _, _, _, _, _, _ in
- if !isMatarialUpload, materialType == .VIDEO, self?.uploadData?.videoBucketKey == objectkey {
- if code == 6 { // 无网
- let uploadRequest: OSSMultipartUploadRequest? = PQAliOssUtil.shared.allTasks[self?.uploadData?.videoBucketKey ?? ""]
- if !(uploadRequest != nil && "\(uploadRequest?.callbackParam["code"] ?? "0")" == "1") {
- self?.showUploadRemindView()
- }
- } else if code == 260 {
- self?.showUploadRemindView(isNetCollected: false)
- } else if code != 1 {
- // 上传失败-播放视频
- self?.publicEnd(isError: true)
- }
- }
- }
- PQAliOssUtil.shared.aliOssProgressHander = { [weak self] bytesSent, totalBytesSent, totalBytesExpectedToSend, _, _ in
- let progress: Float = 0.90 + Float(Float(totalBytesSent) / Float(totalBytesExpectedToSend)) * 0.09
- BFLog(message: "卡点视频上传:bytesSent = \(bytesSent),totalBytesSent = \(totalBytesSent),totalBytesExpectedToSend = \(totalBytesExpectedToSend),progress = \(progress)")
- if progress >= 0.90, progress <= 0.99 {
- // 更新进度
- self?.updatePublicCurrentProgress(useProgress: progress)
- }
- }
- }
- /// fp6 - 视频上传成功,处理要发布视频数据
- /// - Parameter notify: <#notify description#>
- @objc func uploadSuccess(notify: NSNotification) {
- let objectKey: String = "\(notify.userInfo?["objectKey"] ?? "")"
- BFLog(message: "收到上传成功请求==\(notify.userInfo ?? [:])")
- if uploadData?.videoBucketKey == objectKey {
- // 上传成功
- isUploadSuccess = true
- /// fp7 - 处理要发布视频数据
- dealWithPublicData()
- }
- }
- /// fp7 - 处理要发布视频数据
- /// - Returns: <#description#>
- func dealWithPublicData() {
- if uploadData?.localPath != nil {
- let size = try! URL(string: uploadData?.localPath ?? "")?.resourceValues(forKeys: [.fileSizeKey])
- BFLog(message: "size = \(String(describing: size))")
- if Float64(size?.fileSize ?? 0) > maxUploadSize {
- cShowHUB(superView: nil, msg: "无法发布大于10G的视频,请重新选择/合成发布")
- // 上传失败-播放视频
- publicEnd(isError: true)
- return
- }
- }
- let projectId: String? = editProjectModel?.projectId
- let uploadRequest: OSSMultipartUploadRequest? = PQAliOssUtil.shared.allTasks[uploadData?.videoBucketKey ?? ""]
- if uploadRequest == nil {
- reUploadVideo()
- return
- }
- let tempModel = PQVideoListModel()
- tempModel.title = selectTitle
- tempModel.summary = ""
- tempModel.duration = CGFloat(uploadData?.duration ?? 0)
- tempModel.uplpadImage = uploadData?.image
- tempModel.uplpadBucketKey = uploadRequest?.objectKey
- tempModel.localPath = uploadData?.localPath
- tempModel.reCreateVideoData = reCreateData
- tempModel.eventTrackData = eventTrackData
- tempModel.uplpadStatus = 1
- tempModel.videoFromScene = .stuckPoint
- tempModel.uid = Int(BFLoginUserInfo.shared.uid) ?? 0
- tempModel.uplpadRequest = PQAliOssUtil.shared.allTasks[uploadData?.videoBucketKey ?? ""]
- tempModel.stsToken = uploadData?.stsToken
- tempModel.projectId = projectId
- /// fp8 - 发布视频
- publicVideo(videoData: tempModel)
- }
- /// fp8 - 发布视频
- /// - Parameter videoData: <#videoData description#>
- func publicVideo(videoData: PQVideoListModel) {
- if videoData.uplpadBucketKey == nil {
- BFLog(message: "发布视频:视频uplpadBucketKey为空-\(String(describing: videoData.uplpadBucketKey))")
- // 上传失败-播放视频
- publicEnd(isError: true)
- return
- }
- BFLog(message: "开始发布")
- if (videoData.eventTrackData?.endUploadDate ?? 0) <= 0 {
- // 结束上传时间
- videoData.eventTrackData?.endUploadDate = Date().timeIntervalSince1970
- }
- DispatchQueue.global().async {
- // PQBaseViewModel.ossTempToken { [weak self] response, _ in
- // let image: UIImage = videoData.uplpadImage ?? UIImage()
- // let data = image.jpegData(compressionQuality: 1)
- // let accessKeyId: String = "\(response?["accessKeyId"] ?? "")"
- // let secretKeyId: String = "\(response?["accessKeySecret"] ?? "")"
- // let securityToken: String = "\(response?["securityToken"] ?? "")"
- // let endpoint: String = "\(response?["uploadDomain"] ?? "")"
- // let bucketName: String = "\(response?["bucketName"] ?? "")"
- // let objectKey: String = "\(response?["objectKey"] ?? "")"
- // BFLog(message: "开始上传视频图片==\(videoData.title ?? ""),uplpadBucketKey = \(videoData.uplpadBucketKey ?? ""),objectKey =\(objectKey)")
- // PQAliOssUtil.shared
- // .startClient(
- // accessKeyId: accessKeyId,
- // secretKeyId: secretKeyId,
- // securityToken: securityToken,
- // endpoint: endpoint
- // )
- // .uploadObjectAsync(bucketName: bucketName, objectKey: objectKey, data: data!, fileExtensions: "png", imageUploadBlock: { _, code, ossObjectKey, _ in
- // BFLog(message: "图片上传完成==\(videoData.title ?? ""),uplpadBucketKey = \(videoData.uplpadBucketKey ?? ""),objectKey =\(objectKey),ossObjectKey = \(ossObjectKey)")
- // if code == 1 && ossObjectKey == objectKey && objectKey.count > 0 {
- // BFLog(message: "开始发布==\(videoData.title ?? ""),uplpadBucketKey = \(videoData.uplpadBucketKey ?? ""),objectKey =\(objectKey),ossObjectKey = \(ossObjectKey)")
- PQUploadViewModel.publishVideo(projectId: videoData.projectId, fileExtensions: videoData.localPath?.pathExtension, title: videoData.title ?? "", videoPath: videoData.uplpadBucketKey ?? "", coverImgPath: nil, descr: videoData.summary ?? "", videoFromScene: .stuckPoint, reCreateData: videoData.reCreateVideoData, eventTrackData: videoData.eventTrackData) { [weak self] newVideoData, _, _ in
- self?.videoData = newVideoData
- self?.videoData?.title = self?.titleLabel.text
- if self?.videoData?.reCreateVideoData == nil {
- let reCreateVideo = PQReCreateModel()
- reCreateVideo.reProduceVideoFlag = 1
- self?.videoData?.reCreateVideoData = reCreateVideo
- }
- postNotification(name: cPublishStuckPointSuccessKey, userInfo: ["newVideoData": self?.videoData!])
- BFLog(message: "发布成功==\(videoData.title ?? ""),uplpadBucketKey = \(videoData.uplpadBucketKey ?? "")")
- // cShowHUB(superView: nil, msg: "视频发布成功")
- // 发布成功后续操作
- self?.publicEnd()
- PQEventTrackViewModel.publishReportUpload(projectId: videoData.projectId, businessType: .bt_publish_success, ossInfo: videoData.stsToken ?? [:], params: ["title": videoData.title ?? "", "videoPath": videoData.uplpadBucketKey ?? "", "descr": videoData.summary ?? ""])
- }
- // } else {
- // // 图片上传失败
- // BFLog(message: "图片上传失败重新发布视频==\(videoData.title ?? ""),\(videoData.uplpadBucketKey ?? "")")
- // self?.publicVideo(videoData: videoData)
- // }
- // })
- // }
- }
- }
- /// 发布结束操作
- /// - Parameter isError: <#isError description#>
- /// - Returns: <#description#>
- func publicEnd(isError: Bool = false) {
- UIApplication.shared.keyWindow?.viewWithTag(100_100)?.removeFromSuperview()
- isPublicSuccess = true
- progressView.removeFromSuperview()
- progressTipsLab.removeFromSuperview()
- oprationBgView.removeFromSuperview()
- playBtn.isHidden = true
- avPlayer.replaceCurrentItem(with: AVPlayerItem(url: URL(fileURLWithPath: (exportLocalURL?.absoluteString ?? "").replacingOccurrences(of: "file:///", with: ""))))
- avPlayer.play()
- if isError {
- cShowHUB(superView: nil, msg: "视频发布失败,请重新合成")
- } else {
- bottomOprationBgView.isHidden = false
- /// fp2-1-1 - 请求权限
- authorizationStatus()
- }
- }
- /// 生成创作工具埋点数据
- /// - Returns: <#description#>
- func getExportEventTrackData() -> PQVideoMakeEventTrackModel? {
- let eventTrackData = PQVideoMakeEventTrackModel(projectModel: editProjectModel, reCreateData: reCreateData)
- eventTrackData.entrance = .entranceStuckPointPublic
- eventTrackData.editTimeCost = 0
- eventTrackData.composeTimeCost = (exportEndDate - startExportDate) * 1000
- eventTrackData.musicName = audioMixModel?.musicName ?? ""
- eventTrackData.syncedUpMusicName = audioMixModel?.musicName ?? ""
- eventTrackData.musicId = audioMixModel?.musicId ?? ""
- eventTrackData.syncedUpMusicId = audioMixModel?.musicId ?? ""
- eventTrackData.musicUrl = audioMixModel?.selectVoiceType == 1 ? (audioMixModel?.musicPath ?? "") : (audioMixModel?.accompanimentPath ?? "")
- eventTrackData.musicType = audioMixModel != nil ? (audioMixModel?.selectVoiceType == 1 ? "original" : "accompaniment") : ""
- eventTrackData.isMusicClip = (audioMixModel?.startTime ?? 0) > 0
- if editProjectModel?.sData?.videoMetaData?.canvasType == videoCanvasType.origin.rawValue {
- eventTrackData.canvasRatio = "original"
- } else if editProjectModel?.sData?.videoMetaData?.canvasType == videoCanvasType.nineToSixteen.rawValue {
- eventTrackData.canvasRatio = "9:16"
- } else if editProjectModel?.sData?.videoMetaData?.canvasType == videoCanvasType.oneToOne.rawValue {
- eventTrackData.canvasRatio = "1:1"
- } else if editProjectModel?.sData?.videoMetaData?.canvasType == videoCanvasType.sixteenToNine.rawValue {
- eventTrackData.canvasRatio = "16:9"
- }
- eventTrackData.syncedUpVideoNumber = selectedDataCount - selectedImageDataCount
- eventTrackData.syncedUpImageNumber = selectedImageDataCount
- eventTrackData.syncedUpOriginalMaterialDuration = selectedTotalDuration * 1000
- eventTrackData.syncedUpRhythmNumber = audioMixModel?.speed ?? 2
- eventTrackData.syncedUpVideoDuration = ((audioMixModel?.endTime ?? 0) - (audioMixModel?.startTime ?? 0)) * 1000
- return eventTrackData
- }
- /// 播放视频
- /// - Returns: description
- @objc func playVideo() {
- playBtn.isHidden = !playBtn.isHidden
-
- changPlayerIsPause(isPause: !playBtn.isHidden)
-
- }
- /// 按钮点击事件
- /// - Parameter sender: <#sender description#>
- /// - Returns: <#description#>
- @objc func btnClick(sender: UIButton) {
- switch sender.tag {
- case 1:
- if !(isExportSuccess && isSaveProjectSuccess && isUploadSuccess && isPublicSuccess) {
- cShowHUB(superView: nil, msg: "视频发布失败,请重新合成")
- return
- }
- if !PQSingletoWXApiUtil.shared.isInstallWX() {
- cShowHUB(superView: nil, msg: "您还未安装微信客户端!")
- return
- }
- cShowHUB(superView: nil, msg: nil)
- let shareId = getUniqueId(desc: "\(videoData?.uniqueId ?? "")shareId")
- PQBaseViewModel.wxFriendShareInfo(videoId: (videoData?.uniqueId)!) { [weak self] imagePath, title, shareWeappRawId, msg in
- if msg != nil {
- cShowHUB(superView: nil, msg: "网络不佳哦")
- return
- }
- self?.isShared = true
- PQSingletoWXApiUtil.shared.share(type: 3, scene: Int32(WXSceneSession.rawValue), shareWeappRawId: shareWeappRawId, title: title, description: title, imageUrl: imagePath, path: self?.videoData?.videoPath, videoId: (self?.videoData?.uniqueId)!, pageSource: self?.videoData?.pageSource ?? .sp_category, shareId: shareId).wxApiUtilHander = { _, _ in
- }
- cHiddenHUB(superView: nil)
- }
- // 点击上报:分享微信
- PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_click_shareWechat, pageSource: .sp_stuck_publishSyncedUp, extParams: ["videoId": videoData?.uniqueId ?? ""], remindmsg: "卡点视频数据上报-(点击上报:分享微信)")
- case 2:
- if !(isExportSuccess && isSaveProjectSuccess && isUploadSuccess && isPublicSuccess) {
- cShowHUB(superView: nil, msg: "视频发布失败,请重新合成")
- return
- }
- if !PQSingletoWXApiUtil.shared.isInstallWX() {
- cShowHUB(superView: nil, msg: "您还未安装微信客户端!")
- return
- }
- let shareId = getUniqueId(desc: "\(videoData?.uniqueId ?? "")shareId")
- PQBaseViewModel.h5ShareLinkInfo(videoId: videoData?.uniqueId ?? "", pageSource: videoData?.pageSource ?? .sp_category) { [weak self] path, _ in
- cHiddenHUB(superView: nil)
- if path != nil {
- self?.isShared = true
- PQSingletoWXApiUtil.shared.share(type: 1, scene: Int32(WXSceneTimeline.rawValue), title: BFLoginUserInfo.shared.isLogin() ? "\(BFLoginUserInfo.shared.nickName)made a music video for you" : "Music Video for U", description: "", imageUrl: self?.videoData?.shareImgPath, path: path, videoId: (self?.videoData?.uniqueId)!, pageSource: self?.videoData?.pageSource ?? .sp_category, shareId: shareId).wxApiUtilHander = { _, _ in
- }
- } else {
- cShowHUB(superView: nil, msg: "网络不佳哦")
- }
- }
- // 点击上报:分享朋友圈
- PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_click_shareWechatMoment, pageSource: .sp_stuck_publishSyncedUp, extParams: ["videoId": videoData?.uniqueId ?? ""], remindmsg: "卡点视频数据上报-(点击上报:分享朋友圈)")
- case 3:
-
- // 点击上报:完成
- PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_click_finished, pageSource: .sp_stuck_publishSyncedUp, extParams: ["videoId": videoData?.uniqueId ?? ""], remindmsg: "卡点视频数据上报-(点击上报:完成)")
- navigationController?.viewControllers = [(navigationController?.viewControllers.first)!]
- // 发送通知
- postNotification(name: cFinishedPublishedNotiKey)
- postNotification(name: cFinishSharedNotiKey)
- default:
- break
- }
- }
- /// 添加提示视图
- /// - Parameters:
- /// - isNetCollected: <#isNetCollected description#>
- /// - msg: <#msg description#>
- func showUploadRemindView(isNetCollected _: Bool = true, msg _: String? = nil) {
- view.endEditing(true)
- // PQUploadRemindView.showUploadRemindView(title: isNetCollected ? "上传中断" : "上传失败", summary: (isNetCollected ? "似乎已断开与互联网的连接" : (msg != nil ? msg : "视频文件已丢失"))!, confirmTitle: isNetCollected ? "重新连接网络" : "重新上传") { [weak self] _, _ in
- // if isNetCollected {
- // openAppSetting()
- // } else {
- // self?.navigationController?.popToViewController((self?.navigationController?.viewControllers[1])!, animated: true)
- // }
- // }
- }
- @objc func enterBackground() {
- BFLog(message: "进入到后台")
- // 取消导出
- if exporter != nil {
- exporter.cancel()
- }
- playBtn.isHidden = false
- avPlayer.pause()
- }
- @objc func willEnterForeground() {
- BFLog(message: "进入到前台")
- if !isExportSuccess {
- beginExport()
- }
- playBtn.isHidden = true
- avPlayer.play()
- }
- @objc func didBecomeActiveNotification() {
- if isShared {
- DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) { [weak self] in
- self?.isShared = false
- cShowHUB(superView: nil, msg: "分享成功")
-
- }
- }
- }
- /// 更新进度
- /// - Returns: <#description#>
- func updatePublicCurrentProgress(useProgress: Float) {
- exportProgrss = Int(useProgress * 100)
- progressView.setProgress(useProgress, animated: true)
- let attributedText = NSMutableAttributedString(string: "\(exportProgrss)%\n视频正在处理中,请勿离开")
- attributedText.addAttributes([.font: UIFont.systemFont(ofSize: 34)], range: NSRange(location: 0, length: "\(exportProgrss)%".count))
- progressTipsLab.attributedText = attributedText
- }
-
- func changPlayerIsPause(isPause:Bool) {
- if(isPause){
- playBtn.isHidden = false
- avPlayer.pause()
- playerHeaderCoverImageView.isHidden = false
- }else{
- playBtn.isHidden = true
- avPlayer.play()
- playerHeaderCoverImageView.isHidden = true
- }
- }
-
- @objc func titleLabelClick() {
- BFLog(message: "点击输入框")
- changPlayerIsPause(isPause: true)
-
- pinView.isHidden = true
- publicTitleView.isHidden = false
-
- if(publicTitleView.inputTV.text.count > 0){
- publicTitleView.inputTV.text = titleLabel.text
- }
-
- PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_shanyinApp_clickButton_changeTitle, pageSource: .sp_stuck_publishSyncedUp, eventData: ["videoId":videoData?.uniqueId ?? "","rootPageSource":reCreateData != nil ? "shanyinApp-main-syncedUpMusicRecreate" :"shanyinApp-main-syncedUpMusic"], remindmsg: "")
- }
-
- @objc func settingCoverImage() {
-
- if(exportLocalURL == nil){
- BFLog(message: "导出的视频地址错误???。。。")
- return
- }
- changPlayerIsPause(isPause: true)
-
- let asset = AVURLAsset(url: exportLocalURL!, options: nil)
- publicEditCoverView.show(videoURL: exportLocalURL!, duration: CMTimeGetSeconds(asset.duration))
-
- //点击了确认 btn
- publicEditCoverView.selectImageCallBack = { [weak self] imageData in
-
- self?.changPlayerIsPause(isPause: false)
- if(imageData != nil){
-
- self?.coverImageView.image = imageData
- self?.playerHeaderCoverImageView.image = imageData
- self?.uploadData?.image = imageData
- self?.updateCoverImagegOrTitle()
- }
-
- }
- //点击了从相册选择
- publicEditCoverView.selectPhotoBtnCallBack = { [weak self] in
- let imageSelected = PQImageSelectedController()
- imageSelected.isAssetImage = true
- imageSelected.videoWidth = CGFloat(self?.editProjectModel?.sData?.videoMetaData?.videoWidth ?? 0)
- imageSelected.videoHeight = CGFloat(self?.editProjectModel?.sData?.videoMetaData?.videoHeight ?? 0)
- // imageSelected.uploadData = uploadData
- // imageSelected.updataVideoData = updataVideoData
- self?.navigationController?.pushViewController(imageSelected, animated: true)
- }
-
- PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_shanyinApp_clickButton_changeCover, pageSource: .sp_stuck_publishSyncedUp, eventData: ["videoId":videoData?.uniqueId ?? "","rootPageSource":reCreateData != nil ? "shanyinApp-main-syncedUpMusicRecreate" :"shanyinApp-main-syncedUpMusic"], remindmsg: "")
-
- }
-
- //更新标题或封面
- func updateCoverImagegOrTitle() {
-
- PQLoadingHUB.shared.showHUB()
-
- PQBaseViewModel.ossTempToken { [weak self] response, msg in
- let image: UIImage = (self?.uploadData?.image)!
- let data = image.jpegData(compressionQuality: 1)
- let accessKeyId: String = "\(response?["accessKeyId"] ?? "")"
- let secretKeyId: String = "\(response?["accessKeySecret"] ?? "")"
- let securityToken: String = "\(response?["securityToken"] ?? "")"
- let endpoint: String = "\(response?["endPoint"] ?? "")"
- let bucketName: String = "\(response?["bucketName"] ?? "")"
- let objectKey: String = "\(response?["objectKey"] ?? "")"
- PQAliOssUtil.shared
- .startClient(
- accessKeyId: accessKeyId,
- secretKeyId: secretKeyId,
- securityToken: securityToken,
- endpoint: endpoint
- )
- .uploadObjectAsync(bucketName: bucketName, objectKey: objectKey, data: data!, fileExtensions: "png", imageUploadBlock: { _, code, ossObjectKey, _ in
- if code == 1 && ossObjectKey == objectKey && ossObjectKey.count > 0 {
- PQUploadViewModel.updateVideo(title: self?.videoData?.title ?? "", videoId: self?.videoData?.uniqueId ?? "", coverImgPath: objectKey, descr: "") { newVideoData, msg in
- PQLoadingHUB.shared.dismissHUB()
- if newVideoData == nil {
- cShowHUB(superView: self?.view, msg: msg)
- return
- }
- // self?.navigationController?.popToRootViewController(animated: true)
- // postNotification(name: cUpdateVideoSuccessKey, userInfo: ["videoData": newVideoData!])
- }
- } else {
- PQLoadingHUB.shared.dismissHUB()
- }
- })
-
- }}
- func setTitleText(text:String ,textColor:UIColor = UIColor.hexColor(hexadecimal: "#ABABAB")) {
-
- selectTitle = text
- //更新 UI
- titleLabel.text = text
- titleLabel.textColor = textColor
- publicTitleView.inputTV.placeHolder = text
- //更新数据
- videoData?.title = text
-
- }
- //取推荐的10个标题
- func getTitles(){
- PQBaseViewModel.getBaseConfig(completeHander: {[weak self] titles in
-
- if((titles?.count ?? 0) > 0){
- self?.publicTitleView.titles = titles!
-
- let numberRandom: UInt32 = UInt32(arc4random_uniform(UInt32(titles!.count)))
- BFLog(message: "接收到的 titles\(String(describing: titles))")
- self?.setTitleText(text: titles?[Int(numberRandom)] ?? "")
-
- }
-
- })
- }
-
- @objc func imageSelectedImage(notify: Notification) {
- let imageData: UIImage? = notify.userInfo?["image"] as? UIImage
- if imageData != nil {
- changPlayerIsPause(isPause: false)
- BFLog(message: "从系统相册选择了一个照片")
- publicEditCoverView.isHidden = true
- coverImageView.image = imageData
- playerHeaderCoverImageView.image = imageData
- uploadData?.image = imageData
- updateCoverImagegOrTitle()
- }
-
- }
- }
|