Quellcode durchsuchen

merge branch 'master' of https://git.yishihui.com/iOS/BFRecordScreenKit

wenweiwei vor 3 Jahren
Ursprung
Commit
1dadd2d286

+ 58 - 28
BFRecordScreenKit/Classes/BFRecordExport.swift

@@ -57,11 +57,12 @@ public class BFRecordExport {
         // 1,背景视频素材
         if let itemModels = data {
             var totalDur = 0.0
+            var titleStickers = [PQEditSubTitleModel]()
+            var voiceList = [PQVoiceModel]()
+            var videoStickers = [PQEditVisionTrackMaterialsModel]()
 
             // 切割视频素材
-            for (_, itemModel) in itemModels.enumerated() {
-                itemModel.videoStickers.removeAll()
-                
+            for (_, itemModel) in itemModels.enumerated() {                
                 // 保留录音部分时,是否按录音顺序合成最终视频;
                 // 如果需要排序,则排视频的顺序;否则排音频的顺序
                 let needSort = false
@@ -80,19 +81,39 @@ public class BFRecordExport {
                 
                 if itemModel.mediaType == .IMAGE {
                     // 图片素材
-                    let duration = itemModel.materialDuraion
+                    if !synthesisAll && itemModel.voiceStickers.count == 0 {
+                        // 图片无录音在保留模式里不合成
+                        continue
+                    }
+                    
+                    var duration = itemModel.materialDuraion
+                    if itemModel.voiceStickers.count == 0 {
+                        // 图片无录音保持2s
+                        duration = 2
+                        let voice = PQVoiceModel()
+                        voice.startTime = 0
+                        voice.endTime = 2
+                        voice.duration = "2"
+                        voice.voiceType = VOICETYPT.None.rawValue
+                        voiceList.append(voice)
+                    }
+                    
                     let sticker = splitBaseMaterial(timelineIn: totalDur, model_in: 0, duration: duration)
                     sticker.originalData = itemModel.coverImg?.pngData()
                     sticker.volumeGain = 0
                     sticker.type = StickerType.IMAGE.rawValue
-                    itemModel.videoStickers.append(sticker)
+                    videoStickers.append(sticker)
                     BFLog(1, message: "image sticker - timIn:\(sticker.timelineIn), modIn:\(sticker.model_in), dur:\(duration)")
                     
                     var subDur = 0.0
                     for titleS in itemModel.titleStickers {
                         let leng = titleS.timelineOut - titleS.timelineIn
-                        titleS.timelineIn = totalDur + subDur
-                        titleS.timelineOut = totalDur + subDur + leng
+                        let newTitleSticker = PQEditSubTitleModel()
+                        titleStickers.append(newTitleSticker)
+                        newTitleSticker.text = titleS.text
+                        newTitleSticker.setting = titleS.setting
+                        newTitleSticker.timelineIn = totalDur + subDur
+                        newTitleSticker.timelineOut = totalDur + subDur + leng
                         subDur += leng
                     }
                     
@@ -108,6 +129,7 @@ public class BFRecordExport {
                         return
                     }
                    
+                    voiceList.append(contentsOf: itemModel.voiceStickers)
                     
                     if synthesisAll {
                         var subDur = 0.0
@@ -117,17 +139,22 @@ public class BFRecordExport {
                             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)
+                            videoStickers.append(sticker)
                             subDur += range.duration.seconds
                         }
                         
                         for titleS in itemModel.titleStickers {
-                            titleS.timelineIn = totalDur + titleS.timelineIn
-                            titleS.timelineOut = totalDur + titleS.timelineOut
+                            let newTitleSticker = PQEditSubTitleModel()
+                            titleStickers.append(newTitleSticker)
+                            newTitleSticker.text = titleS.text
+                            newTitleSticker.setting = titleS.setting
+                            newTitleSticker.timelineIn = totalDur + titleS.timelineIn
+                            newTitleSticker.timelineOut = totalDur + titleS.timelineOut
                         }
                         
                         totalDur += subDur
                     } else {
+                        // 只保留录音部分
                         var subDur = 0.0
                         var drangs = itemModel.dealedDurationRanges.filter { srange in
                             srange.isRecord == true
@@ -139,25 +166,34 @@ public class BFRecordExport {
                             }
                         }
                         
-                        for srange in drangs {
+                        for (index, srange) in drangs.enumerated() {
                             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)
+                            videoStickers.append(sticker)
+                            
+                            let voiceSticker = itemModel.voiceStickers[index]
+                            let titleModels = itemModel.titleStickers.filter({ mod in
+                                mod.audioFilePath == voiceSticker.wavFilePath
+                            })
+                            for titleS in titleModels {
+                                let newTitleSticker = PQEditSubTitleModel()
+                                titleStickers.append(newTitleSticker)
+                                newTitleSticker.text = titleS.text
+                                newTitleSticker.setting = titleS.setting
+                                newTitleSticker.timelineIn = totalDur + subDur + titleS.timelineIn - voiceSticker.startTime
+                                newTitleSticker.timelineOut = totalDur + subDur + titleS.timelineOut - voiceSticker.startTime
+                                BFLog(1, message: "timein - \(newTitleSticker.timelineIn)")
+                            }
                             subDur += range.duration.seconds
-                        }
-                        
-                        for titleS in itemModel.titleStickers {
-//                            let leng = titleS.timelineOut - titleS.timelineIn
-                            titleS.timelineIn = totalDur + titleS.timelineIn
-                            titleS.timelineOut = totalDur + titleS.timelineOut
+
                         }
                         totalDur += subDur
                     }
                 }
             }
-            beginExport(synthesisAll: synthesisAll)
+            beginExport(synthesisAll: synthesisAll, videoStickers: videoStickers, voiceList:voiceList, titleStickers:titleStickers)
         }
     }
 
@@ -205,7 +241,9 @@ public class BFRecordExport {
         return bgMovieInfo
     }
 
-    func beginExport(synthesisAll: Bool) {
+    // 因为titleStickers 是传递过来的,会修改timelinein,需要重新生成,以免影响原来的数据
+    // voiceList是考虑到图片有时候没有录音,在保留全部时,需要添加一个2秒的空sticker
+    func beginExport(synthesisAll: Bool, videoStickers: [PQEditVisionTrackMaterialsModel], voiceList: [PQVoiceModel], titleStickers: [PQEditSubTitleModel]) {
         // 输出视频地址
 //        exprotVideo()
 //        return;
@@ -224,14 +262,6 @@ public class BFRecordExport {
         }
 
         // 处理导出
-        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

+ 0 - 11
BFRecordScreenKit/Classes/BFRecordItemModel.swift

@@ -110,17 +110,6 @@ public class BFRecordItemModel: NSObject {
         dealedDurationRanges.removeAll()
 
         var start: Double = 0
-        
-        if voiceStickers.count == 0 {
-            let model = PQVoiceModel()
-            model.startTime = 0
-            model.endTime = 2
-            model.duration = "2"
-            model.voiceType = VOICETYPT.None.rawValue
-            voiceStickers.append(model)
-            
-            materialDuraion = 2
-        }
 
         var list: [PQVoiceModel]
         list = voiceStickers.sorted { model1, model2 in

+ 8 - 8
BFRecordScreenKit/Classes/BFVoiceRecordManager.swift

@@ -20,10 +20,10 @@ class BFVoiceRecordManager: NSObject {
     var recorderFilePath: String = ""
     var beginRecordTime: Date = Date()
     var voiceModel: PQVoiceModel?
-
-    // 字幕的回调
-    var subtitleRecordHandle: ((String?) -> Void)?
-    // 进度回调
+    ///字幕的回调 参数1: 字幕数据 ,参数2 :对应的录音文件
+    var subtitleRecordHandle: ((String?,String?) -> Void)?
+    //进度回调
+ 
     var recorderProgrossHandle: ((Float64?) -> Void)?
 
     /// 初始化方法
@@ -86,7 +86,7 @@ extension BFVoiceRecordManager: BFRecorderManagerDelegate {
         let duration = Date().timeIntervalSince(beginRecordTime)
         if duration > 1 {
             // 处理降噪
-            let noiseFilePath = outfile.replacingOccurrences(of: ".wav", with: "_noise_\(1)_.wav")
+            let noiseFilePath = outfile.replacingOccurrences(of: ".wav", with: "_noise.wav")
             BFLog(1, message: "降噪后地址:\(noiseFilePath) 原地址:\(outfile)")
             NXNoiseReduction().denoise(outfile, outFile: noiseFilePath)
             if let model = voiceModel {
@@ -107,8 +107,8 @@ extension BFVoiceRecordManager: BFRecorderManagerDelegate {
             cancelRecordHandle?(voiceModel)
         }
     }
-
-    public func eventCallback(_: BFRecorderManager, asrResult: String) {
-        subtitleRecordHandle?(asrResult)
+ 
+    public func eventCallback(_: BFRecorderManager, asrResult: String,audioFilePath:String) {
+        subtitleRecordHandle?(asrResult, audioFilePath)
     }
 }

+ 12 - 7
BFRecordScreenKit/Classes/RecordScreen/Controller/BFRecordScreenController.swift

@@ -398,23 +398,27 @@ public class BFRecordScreenController: BFBaseViewController {
                 self?.drawProgressIndication(progress: progress ?? 0)
             }
 
-            // 录音字幕回调
-            self?.recorderManager?.subtitleRecordHandle = { [weak self] asrResult in
-                if asrResult == nil {
+            //录音字幕回调
+            self?.recorderManager?.subtitleRecordHandle = {[weak self] asrResult ,audioFilePath in
+                if(asrResult == nil){
                     BFLog(message: "识别结果为空????不能生成字幕数据")
                     return
                 }
                 let dicResult: [String: Any]? = jsonStringToDictionary(asrResult!)
 
+                let header = dicResult?["header"] as? [String: Any]
                 let payload = dicResult?["payload"] as? [String: Any]
-
+ 
                 BFLog(message: "识别结果:) \((payload?["result"])!) startTime:\(self?.recorderManager?.voiceModel?.startTime ?? 0.0)")
                 DispatchQueue.main.async {
                     // 1,保存字幕数据 begin_time是开始出现文字的时间,time 是结束文字出现的时间 单位都为毫秒,都是相对于录制音频数据整段时间。self.recorderManager.voiceModel?.startTime 为开始的录制的时间,开始和结束都要加上这个时差
 
                     let newSubtitle = PQEditSubTitleModel()
-
-                    // 这里加300ms 是因为返回结果为了切到字,时长提前一些时间,具体时间官方没说和原音频有关系。这里我们先延后300ms 单位:毫秒。
+                    //任务全局唯一ID,请记录该值,便于排查问题。 每次 startRecorder 和 stopRecoder 之间  task_Id都不会变化
+                    newSubtitle.taskID = (header?["task_id"] as? String) ?? ""
+                    
+                    //这里加300ms 是因为返回结果为了切到字,时长提前一些时间,具体时间官方没说和原音频有关系。这里我们先延后300ms 单位:毫秒。
+ 
                     newSubtitle.timelineIn = (self?.recorderManager?.voiceModel?.startTime ?? 0.0) + Float64((((payload?["begin_time"]) as? Int) ?? 0) + 300) / 1000.0
 
                     newSubtitle.timelineOut = (self?.recorderManager?.voiceModel?.startTime ?? 0.0) + Float64(((payload?["time"]) as? Int) ?? 0) / 1000.0
@@ -424,8 +428,9 @@ public class BFRecordScreenController: BFBaseViewController {
                         showText += "..."
                     }
                     newSubtitle.text = showText
+                    newSubtitle.audioFilePath = audioFilePath ?? ""
 
-                    BFLog(message: "添加字幕数据 timelineIn \(newSubtitle.timelineIn) timelineOut \(newSubtitle.timelineOut) text \(newSubtitle.text)")
+                    BFLog(message: "添加字幕数据 timelineIn \(newSubtitle.timelineIn) timelineOut \(newSubtitle.timelineOut) text r\(newSubtitle.text) 音频路径为:\(audioFilePath ?? "")")
                     newSubtitle.setting = self?.subtitleSettingView.subtitle.setting ?? BFSubTitileSettingModel()
 
                     self?.itemModels[self?.currItemModelIndex ?? 0].titleStickers.append(newSubtitle)

+ 1 - 0
BFRecordScreenKit/Classes/RecordScreen/View/BFVideoThumbProgressView.swift

@@ -180,6 +180,7 @@ class BFVideoThumbProgressView: UIView {
                         lastImg = iv
                     }
                     if sself.recordItem?.mediaType == .IMAGE {
+                        // 图片需要动态修改宽度
                         lastImg.snp.makeConstraints { make in
                             make.right.equalTo(sself.width * -0.5)
                         }