123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378 |
- //
- // BFRecordExport.swift
- // BFRecordScreenKit
- //
- // Created by 胡志强 on 2021/11/25.
- // 录屏视频导出
- import AVFoundation
- import BFCommonKit
- import BFMediaKit
- import Foundation
- import GPUImage
- import Photos
- public enum ExportError : Int {
- case FileNotExist = -31001
- case DataLost = -31002
- case VoiceLost = -31003
- case TotalDurError = -31004
- case ExportExcept = -31005
- case DiskNoSpace = -31006
- }
- public class BFRecordExport {
- public var progress: ((Float) -> Void)?
- public var exportCompletion: ((Error?, URL?) -> Void)?
-
- public var originSoundVolumn : Float = 1.0 // 无录音时原声大小
- public var originSoundInRecordVolumn : Float = 0.0 // 录音时原声大小
-
- 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 (_, itemModel) in itemModels.enumerated() {
-
- if let localPath = itemModel.localPath {
- if !FileManager.default.fileExists(atPath: localPath) {
- let error = NSError(domain: "err", code: ExportError.FileNotExist.rawValue, userInfo: ["msg": "file not exist"])
- exportCompletion?(error as Error, nil)
- return
- }
- itemModel.videoStickers.removeAll()
- if synthesisAll {
- var subDur = 0.0
- let drangs = itemModel.dealedDurationRanges
- for srange in drangs {
- let range = srange.range
- let sticker = splitBaseMaterial(timelineIn: totalDur + subDur, model_in: range.start.seconds, duration: range.duration.seconds)
- sticker.locationPath = localPath
- sticker.volumeGain = Float64(srange.isRecord ? originSoundInRecordVolumn*100 : originSoundVolumn*100)
- itemModel.videoStickers.append(sticker)
- subDur += range.duration.seconds
-
- }
- totalDur += subDur
- } else {
- var subDur = 0.0
- var drangs = itemModel.dealedDurationRanges.filter { srange in
- srange.isRecord == true
- }
-
- // 是否按录音顺序排列
- let needSort = false
- if needSort {
- drangs.sort { range1, range2 in
- range1.index < range2.index
- }
- }
- for srange in drangs {
- let range = srange.range
- let sticker = splitBaseMaterial(timelineIn: totalDur + subDur, model_in: range.start.seconds, duration: range.duration.seconds)
- sticker.locationPath = localPath
- sticker.volumeGain = Float64(srange.isRecord ? originSoundInRecordVolumn*100 : originSoundVolumn*100)
- itemModel.videoStickers.append(sticker)
- subDur += range.duration.seconds
- }
- totalDur += subDur
- }
- }
- }
- beginExport(synthesisAll: synthesisAll)
- }
- }
- public func cancelExport() {
- exporter?.cancel()
- }
- public func clearFileCache() {
- data?.forEach { itemModel in
- itemModel.voiceStickers.forEach { model in
- if let localPath = model.wavFilePath {
- try? FileManager.default.removeItem(atPath: localPath)
- }
- }
- }
- }
- 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()
- bgMovieInfo.type = StickerType.VIDEO.rawValue
- 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 itemData = data else {
- let error = NSError(domain: "err", code: ExportError.DataLost.rawValue, userInfo: ["msg": "所有数据丢失"])
- exportCompletion?(error as Error, nil)
- return
- }
- // 处理导出
- var voiceList = [PQVoiceModel]()
- var videoStickers = [PQEditVisionTrackMaterialsModel]()
- var titleStickers = [PQEditSubTitleModel]()
- for itemModel in itemData {
- voiceList.append(contentsOf: itemModel.voiceStickers)
- videoStickers.append(contentsOf: itemModel.videoStickers)
- titleStickers.append(contentsOf: itemModel.titleStickers)
- }
- guard let voiceCount = data?.reduce(0, { partialResult, itemModell in
- itemModell.voiceStickers.count + partialResult
- }) else {
- BFLog(1, message: "voiceStickers count += nil")
- let error = NSError(domain: "err", code: ExportError.VoiceLost.rawValue, userInfo: ["msg": "voiceStickers count += nil"])
- exportCompletion?(error as Error, nil)
- return
- }
- guard let totalDuration = data?.reduce(0, { partialResult, itemModell in
- itemModell.materialDuraion + partialResult
- }) else {
- let error = NSError(domain: "err", code: ExportError.TotalDurError.rawValue, userInfo: ["msg": "时长计算出错"])
- exportCompletion?(error as Error, nil)
- return
- }
- // 有录音操作或者多个视频,就会进入合成步骤,否则就是一个没有处理的素材,直接导出就行了
- if voiceCount > 0 || videoStickers.count > 1 {
- let (audioMix, composition) = mergeAudio(videoStickers: videoStickers, audios: voiceList, synthesisAll: synthesisAll)
- var filters:[PQBaseFilter] = Array.init()
- for sticker in videoStickers {
- filters.append(PQMovieFilter(movieSticker: sticker))
- }
- let outputSize:CGSize = CGSize(width: 1080, height: Int(1080 * CGFloat(UIScreen.main.bounds.size.height / UIScreen.main.bounds.size.width)))
- BFLog(message: "输出视频大小:\(outputSize)")
-
- //add by ak 有字幕数据 & 显示字幕开关打开 添加字幕filter
- if(titleStickers.count > 0 && ( titleStickers.first?.setting.subtitleIsShow ?? true)){
- filters.append(PQSubTitleFilter.init(st: titleStickers, inputSize: outputSize))
- }
-
-
- exporter = PQCompositionExporter(asset: composition, videoComposition: nil, audioMix: audioMix, filters: filters, animationTool: nil, exportURL: outPutMP4URL)
-
- var orgeBitRate = Int(outputSize.width * outputSize.height * 3)
- for stick in videoStickers {
- if stick.type == StickerType.VIDEO.rawValue, stick.locationPath.count > 0 {
- 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(1, message: "导出设置的码率为:\(Double(orgeBitRate)/1024.0/1024.0)M")
- let preSize = Double(orgeBitRate) * totalDuration / (1024*1024)
- let freeSize = PQBridgeObject.getPhoneDiskFreeSize()
- if preSize + 100.0 > freeSize { // 存储完后磁盘剩余至少100M
- let error = NSError(domain: "err", code: ExportError.DiskNoSpace.rawValue, userInfo: ["msg":"磁盘空间不足"])
- self.exportCompletion?(error as Error, nil)
- return
- }
- let tempBeginExport = Date().timeIntervalSince1970
- if exporter!.prepare(videoSize: outputSize, videoAverageBitRate: orgeBitRate) {
- exporter!.start(playeTimeRange: CMTimeRange(start: CMTime.zero, end: synthesisAll ? CMTime(seconds: totalDuration, preferredTimescale: 100) : 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)
- let exportEndTime = Date().timeIntervalSince1970
- // BFLog(1, message: "视频导出完成: \(String(describing: url)) 生成视频时长为:\(outSeconds) 总用时:\(exportEndTime - tempBeginExport)")
- print("生成视频时长为:\(outSeconds) 总用时:\(exportEndTime - tempBeginExport)")
-
- cShowHUB(superView: nil, msg: (outSeconds == 0) ? "合成失败请重试。" : "合成成功")
- self?.exportCompletion?(nil, url)
- } else {
- let error = NSError(domain: "err", code: ExportError.ExportExcept.rawValue, userInfo: ["msg":"导出异常失败"])
- self?.exportCompletion?(error as Error, nil)
- cShowHUB(superView: nil, msg: "导出失败")
- }
- // 导出完成后取消导出
- self?.exporter?.cancel()
- }
- } else {
- // 没有处理,直接copy原文件
- if let localPath = data?.first?.localPath {
- exportCompletion?(nil, URL(fileURLWithPath: localPath))
- }
- }
- }
- 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 += PQPlayerViewModel.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 += PQPlayerViewModel.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 += PQPlayerViewModel.dealWithMaterialTrack(stickerModel: sticker, composition: composition)
- }
- return tempParameters
- }
- }
|