123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- //
- // BFRecordExport.swift
- // BFRecordScreenKit
- //
- // Created by 胡志强 on 2021/11/25.
- // 录屏视频导出
- import Foundation
- import AVFoundation
- import BFFramework
- import BFVideoEditKit
- import Photos
- import GPUImage
- public class BFRecordExport {
- public var progress : ((Float)->Void)?
- public var exportCompletion : ((Error?, URL?)->Void)?
-
- public var data:[BFRecordItemModel]? {
- didSet{
- if data?.count ?? 0 > 0 {
- for item in data! {
- item.generationTimeRanges()
- }
- }
- }
- }
- var count = 0
-
- var stickerRanges = [CMTimeRange]()
-
- var exporter : PQCompositionExporter?
- // var mStickers = [PQEditVisionTrackMaterialsModel]()
-
- deinit {
- }
- public init(){}
-
- //MARK: -
- /// synthesisAll: 合成所有还是只合成录音部分
- public func startExprot(synthesisAll:Bool){
- // 1,背景视频素材
- if let itemModels = data {
-
- var totalDur = 0.0
- for (index, itemModel) in itemModels.enumerated() {
- itemModel.videoStickers.removeAll()
- let asset = itemModel.baseMaterial
- if let dur = asset?.duration.seconds {
- if synthesisAll {
- let bgMovieInfo = splitBaseMaterial(timelineIn: totalDur, model_in: 0, duration: dur)
- bgMovieInfo.volumeGain = 0
- itemModel.videoStickers.append(bgMovieInfo)
- totalDur += dur
- } else {
- var subDur = 0.0
- let drangs = itemModel.dealedDurationRanges.filter { ranges in
- ranges.isRecord == true
- }
- for srange in drangs {
- let range = srange.range
- let sticker = splitBaseMaterial(timelineIn: (totalDur + subDur), model_in: range.start.seconds, duration: range.duration.seconds)
- sticker.volumeGain = 0
- itemModel.videoStickers.append(sticker)
- subDur += range.duration.seconds
- }
- totalDur += subDur
- }
- }
- }
- beginExport(synthesisAll:synthesisAll)
- }
- }
-
- public func cancelExport(){
- self.exporter?.cancel()
- }
-
-
- enum DispatchError: Error {
- case timeout
- }
-
- func getOutputFilePath() -> URL{
- var outPutMP4Path = exportVideosDirectory
- if !directoryIsExists(dicPath: outPutMP4Path) {
- createDirectory(path: outPutMP4Path)
- }
- outPutMP4Path.append("video_\(String.qe.timestamp()).mp4")
- return URL(fileURLWithPath: outPutMP4Path)
-
- }
-
- func splitBaseMaterial(timelineIn:Double, model_in:Double, duration:Double) -> PQEditVisionTrackMaterialsModel{
- let bgMovieInfo: PQEditVisionTrackMaterialsModel = PQEditVisionTrackMaterialsModel()
- if let asset = data?.first?.baseMaterial {
- bgMovieInfo.type = StickerType.VIDEO.rawValue
- bgMovieInfo.locationPath = ((asset.url.absoluteString).removingPercentEncoding ?? "").replacingOccurrences(of: "file://", with: "")
- bgMovieInfo.timelineIn = timelineIn
- bgMovieInfo.timelineOut = timelineIn + duration
- bgMovieInfo.model_in = model_in
- bgMovieInfo.out = model_in + duration
- bgMovieInfo.canvasFillType = stickerContentMode.aspectFitStr.rawValue
- bgMovieInfo.volumeGain = 1
- bgMovieInfo.aptDuration = bgMovieInfo.timelineOut
- bgMovieInfo.duration = bgMovieInfo.timelineOut
- BFLog(1, message: "hhh- timIn:\(timelineIn), modIn:\(model_in), dur:\(duration)")
- }
-
- return bgMovieInfo
- }
-
- func beginExport(synthesisAll:Bool) {
- // 输出视频地址
- // exprotVideo()
- // return;
- var outPutMP4Path = exportVideosDirectory
- if !directoryIsExists(dicPath: outPutMP4Path) {
- createDirectory(path: outPutMP4Path)
- }
- outPutMP4Path.append("video_\(String.qe.timestamp()).mp4")
- let outPutMP4URL = URL(fileURLWithPath: outPutMP4Path)
- BFLog(1, message: "导出视频地址 \(outPutMP4URL)")
-
- guard let itemModel = data?.first else {
- return
- }
- // 处理导出
- let voiceList = itemModel.voiceStickers
- let videoStickers = itemModel.videoStickers
- if voiceList.count > 0 || videoStickers.count > 1 {
- let (audioMix, composition) = mergeAudio(videoStickers: videoStickers, audios: voiceList,synthesisAll:synthesisAll)
-
- let filter = videoStickers.map { sticker in
- PQMovieFilter(movieSticker: sticker)
- }
- // 有
- // if let completURL = audioUrl {
- // let inputAsset = AVURLAsset(url: completURL, options: avAssertOptions)
- //// (audioMix, composition) = PQVideoEditViewModel.setupAudioMix(originAsset: inputAsset, bgmData: nil, videoStickers: videoStickers)
- // //使用原视频无音版
- // (audioMix, composition) = PQVideoEditViewModel.setupAudioMix(originAsset: inputAsset, bgmData: nil, videoStickers: nil)
- //
- // if composition != nil {
- // exporter = PQCompositionExporter(asset: composition!, videoComposition: nil, audioMix: audioMix, filters: filter, animationTool: nil, exportURL: outPutMP4URL)
- // }else {
- // exporter = PQCompositionExporter(asset: inputAsset, videoComposition: nil, audioMix: nil, filters: filter, animationTool: nil, exportURL: outPutMP4URL)
- // }
- // }
- exporter = PQCompositionExporter(asset: composition, videoComposition: nil, audioMix: audioMix, filters: filter, animationTool: nil, exportURL: outPutMP4URL)
-
- let asset = data?.first?.baseMaterial
- let size = getVideoSize(asset: asset!)
- var orgeBitRate = Int(size.width * size.height * 3)
-
- for stick in videoStickers {
- if stick.type == StickerType.VIDEO.rawValue {
- let asset = AVURLAsset(url: URL(fileURLWithPath: stick.locationPath), options: avAssertOptions)
-
- let cbr = asset.tracks(withMediaType: .video).first?.estimatedDataRate
- if Int(cbr ?? 0) > orgeBitRate {
- orgeBitRate = Int(cbr ?? 0)
- }
- }
- }
-
- BFLog(message: "导出设置的码率为:\(orgeBitRate)")
- if exporter!.prepare(videoSize: size, videoAverageBitRate: orgeBitRate) {
- exporter!.start(playeTimeRange: CMTimeRange(start: CMTime.zero, end: synthesisAll ? asset?.duration as! CMTime : composition.duration))
- }
- exporter?.progressClosure = { [weak self] _, _, progress in
- // BFLog(message: "正片合成进度 \(progress * 100)%")
- let useProgress = progress > 1 ? 1 : progress
- if progress > 0 { // 更新进度
- self?.progress?(useProgress)
- }
- }
- exporter?.completion = { [weak self] url in
- // 输出视频时长
- if let url = url {
- let outSeconds = CMTimeGetSeconds(AVAsset(url: url).duration)
-
- BFLog(1, message: "无水印的视频导出完成: \(String(describing: url)) 生成视频时长为:\(outSeconds)")
- cShowHUB(superView: nil, msg: ( outSeconds == 0) ? "合成失败请重试。" : "合成成功")
- self?.exportCompletion?(nil, url)
- }else{
- let error = NSError(domain: "err", code: -1, userInfo: nil)
- self?.exportCompletion?(error as Error, nil)
- cShowHUB(superView: nil, msg: "导出失败")
- }
-
- // 导出完成后取消导出
- self?.exporter?.cancel()
- }
- } else {
- // 没有处理,直接copy原文件
- if let ass = data?.first?.baseMaterial{
- self.exportCompletion?(nil, ass.url)
- }
- }
- }
-
- func dealAsset(){
- // asset?.tracks.first(where: { track in
- // if track.mediaType == .audio{
- //
- // }
- // })
- }
-
- func getVideoSize(asset:AVURLAsset) -> CGSize{
- var size = CGSize.zero
- asset.tracks.forEach({ track in
- if track.mediaType == .video{
- let realSize = __CGSizeApplyAffineTransform(track.naturalSize, track.preferredTransform)
- size = CGSize(width: ceil(abs(realSize.width)), height: ceil(abs(realSize.height)))
- }
- })
-
- return size
- }
- }
- extension BFRecordExport {
- func mergeAudio(videoStickers:[PQEditVisionTrackMaterialsModel], audios:[PQVoiceModel]?, synthesisAll:Bool) -> (AVMutableAudioMix, AVMutableComposition){
- let composition = AVMutableComposition()
- let audioMix = AVMutableAudioMix()
- var tempParameters = [AVMutableAudioMixInputParameters]()
-
- var totalDuration : Float64 = 0
- for sticker in videoStickers {
- if sticker.volumeGain == 0 {
- // 如果添加了会有刺啦音
- BFLog(message: "音频音量 为0 不添加")
- continue
- }
- sticker.volumeGain = 50
- totalDuration = max(totalDuration, sticker.duration)
- tempParameters += PQVideoEditViewModel.dealWithMaterialTrack(stickerModel: sticker, composition: composition)
- }
- if let voices = audios {
- if synthesisAll {
- tempParameters += mergeRecordVoiceAll(voices:voices, composition)
- }else {
- tempParameters += mergeRecordVoiceOnly(voices:voices, composition)
- }
- }
- audioMix.inputParameters = tempParameters
- return (audioMix, composition)
- }
-
- func mergeRecordVoiceOnly(voices:[PQVoiceModel], _ composition:AVMutableComposition) -> [AVMutableAudioMixInputParameters] {
- var tempParameters = [AVMutableAudioMixInputParameters]()
- var totalDur:Double = 0.0
- for model in voices {
- if model.volume == 0 {
- // 如果添加了会有刺啦音
- BFLog(message: "音频音量 为0 不添加")
- continue
- }
- let duration = model.endTime - model.startTime
- let sticker = PQEditVisionTrackMaterialsModel()
- sticker.model_in = 0
- sticker.out = duration
- sticker.timelineIn = totalDur
- sticker.aptDuration = duration
- sticker.duration = duration
- sticker.locationPath = model.wavFilePath
- sticker.volumeGain = 100 //Float64(model.volume)
- tempParameters += PQVideoEditViewModel.dealWithMaterialTrack(stickerModel: sticker, composition: composition)
- totalDur += duration
- }
- return tempParameters
- }
-
- func mergeRecordVoiceAll(voices:[PQVoiceModel], _ composition:AVMutableComposition) -> [AVMutableAudioMixInputParameters] {
- var tempParameters = [AVMutableAudioMixInputParameters]()
- for model in voices {
- if model.volume == 0 {
- // 如果添加了会有刺啦音
- BFLog(message: "音频音量 为0 不添加")
- continue
- }
- let sticker = PQEditVisionTrackMaterialsModel()
- sticker.model_in = 0
- sticker.timelineIn = model.startTime
- sticker.out = model.endTime
- sticker.aptDuration = model.endTime - model.startTime
- sticker.duration = sticker.aptDuration
- sticker.locationPath = model.wavFilePath
- sticker.volumeGain = 100 //Float64(model.volume)
- tempParameters += PQVideoEditViewModel.dealWithMaterialTrack(stickerModel: sticker, composition: composition)
- }
-
- return tempParameters
- }
-
- }
|