123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380 |
- //
- // 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 = ""
-
- //add by ak 是否是再创作模式
- var isReCreate:Bool = false
- // 预览大小
- 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)
- 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.isUserInteractionEnabled = true
- 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))")
- if(title?.count != 0 && title != self?.titleLabel.text){
- self?.changPlayerIsPause(isPause: false)
- //判断文字是否有效
- var inputText = ""
- inputText = title?.replacingOccurrences(of: "\n", with: "") ?? ""
- inputText = inputText.replacingOccurrences(of: " ", with: "")
-
- if(inputText.count > 0){
- 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: PQBFConfig.shared.styleColor.rawValue), 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)
-
- coverImageTitle.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(settingCoverImage)))
- 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 {
-
- PQBaseViewModel.wxFriendShareInfo(videoId: (self?.videoData?.uniqueId)!) { [weak self] imagePath, title, shareWeappRawId, msg in
- if msg != nil {
- cShowHUB(superView: nil, msg: "网络不佳哦")
- return
- }
- self?.isShared = true
-
-
- PQSingletoWXApiUtil.shared.share(type: 1, scene: Int32(WXSceneTimeline.rawValue), title: self?.videoData?.title ?? "\(BFLoginUserInfo.shared.nickName)made a music video for you", description: "", imageUrl: imagePath, 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)
- 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: "分享成功")
- self?.playBtn.isHidden = true
- self?.avPlayer.play()
- }
- }
- }
- /// 更新进度
- /// - 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.show()
-
- 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":isReCreate ? "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":isReCreate ? "shanyinApp-main-syncedUpMusicRecreate" :"shanyinApp-main-syncedUpMusic"], remindmsg: "")
-
- }
-
- //更新标题或封面
- func updateCoverImagegOrTitle() {
-
- PQLoadingHUB.shared.showHUB(isMode: true)
-
- 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 {
- //add by ak 这里会在服务器生成分享使用的图片到1-2S 时间
- PQUploadViewModel.updateVideo(title: self?.videoData?.title ?? "", videoId: self?.videoData?.uniqueId ?? "", coverImgPath: objectKey, descr: "") { newVideoData, msg in
-
- if newVideoData == nil {
- cShowHUB(superView: self?.view, msg: msg)
- //可能有敏感词 要刷一组新标题并自动更新
- let numberRandom: UInt32 = UInt32(arc4random_uniform(UInt32( self?.publicTitleView.titles.count ?? 0)))
-
- self?.setTitleText(text: self?.publicTitleView.titles[Int(numberRandom)] ?? "")
- self?.updateCoverImagegOrTitle()
- sleep(UInt32(1.5))
- PQLoadingHUB.shared.dismissHUB()
-
- return
- }else{
-
- PQLoadingHUB.shared.dismissHUB()
- }
- }
- } 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()
- }
-
- }
- }
|