Переглянути джерело

保留录音合成时字幕矫正

harry 3 роки тому
батько
коміт
840aeb354d

+ 58 - 28
BFRecordScreenKit/Classes/BFRecordExport.swift

@@ -57,11 +57,12 @@ public class BFRecordExport {
         // 1,背景视频素材
         // 1,背景视频素材
         if let itemModels = data {
         if let itemModels = data {
             var totalDur = 0.0
             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
                 let needSort = false
@@ -80,19 +81,39 @@ public class BFRecordExport {
                 
                 
                 if itemModel.mediaType == .IMAGE {
                 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)
                     let sticker = splitBaseMaterial(timelineIn: totalDur, model_in: 0, duration: duration)
                     sticker.originalData = itemModel.coverImg?.pngData()
                     sticker.originalData = itemModel.coverImg?.pngData()
                     sticker.volumeGain = 0
                     sticker.volumeGain = 0
                     sticker.type = StickerType.IMAGE.rawValue
                     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)")
                     BFLog(1, message: "image sticker - timIn:\(sticker.timelineIn), modIn:\(sticker.model_in), dur:\(duration)")
                     
                     
                     var subDur = 0.0
                     var subDur = 0.0
                     for titleS in itemModel.titleStickers {
                     for titleS in itemModel.titleStickers {
                         let leng = titleS.timelineOut - titleS.timelineIn
                         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
                         subDur += leng
                     }
                     }
                     
                     
@@ -108,6 +129,7 @@ public class BFRecordExport {
                         return
                         return
                     }
                     }
                    
                    
+                    voiceList.append(contentsOf: itemModel.voiceStickers)
                     
                     
                     if synthesisAll {
                     if synthesisAll {
                         var subDur = 0.0
                         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)
                             let sticker = splitBaseMaterial(timelineIn: totalDur + subDur, model_in: range.start.seconds, duration: range.duration.seconds)
                             sticker.locationPath = localPath
                             sticker.locationPath = localPath
                             sticker.volumeGain = Float64(srange.isRecord ? originSoundInRecordVolumn*100 : originSoundVolumn*100)
                             sticker.volumeGain = Float64(srange.isRecord ? originSoundInRecordVolumn*100 : originSoundVolumn*100)
-                            itemModel.videoStickers.append(sticker)
+                            videoStickers.append(sticker)
                             subDur += range.duration.seconds
                             subDur += range.duration.seconds
                         }
                         }
                         
                         
                         for titleS in itemModel.titleStickers {
                         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
                         totalDur += subDur
                     } else {
                     } else {
+                        // 只保留录音部分
                         var subDur = 0.0
                         var subDur = 0.0
                         var drangs = itemModel.dealedDurationRanges.filter { srange in
                         var drangs = itemModel.dealedDurationRanges.filter { srange in
                             srange.isRecord == true
                             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 range = srange.range
                             let sticker = splitBaseMaterial(timelineIn: totalDur + subDur, model_in: range.start.seconds, duration: range.duration.seconds)
                             let sticker = splitBaseMaterial(timelineIn: totalDur + subDur, model_in: range.start.seconds, duration: range.duration.seconds)
                             sticker.locationPath = localPath
                             sticker.locationPath = localPath
                             sticker.volumeGain = Float64(srange.isRecord ? originSoundInRecordVolumn*100 : originSoundVolumn*100)
                             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
                             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
                         totalDur += subDur
                     }
                     }
                 }
                 }
             }
             }
-            beginExport(synthesisAll: synthesisAll)
+            beginExport(synthesisAll: synthesisAll, videoStickers: videoStickers, voiceList:voiceList, titleStickers:titleStickers)
         }
         }
     }
     }
 
 
@@ -205,7 +241,9 @@ public class BFRecordExport {
         return bgMovieInfo
         return bgMovieInfo
     }
     }
 
 
-    func beginExport(synthesisAll: Bool) {
+    // 因为titleStickers 是传递过来的,会修改timelinein,需要重新生成,以免影响原来的数据
+    // voiceList是考虑到图片有时候没有录音,在保留全部时,需要添加一个2秒的空sticker
+    func beginExport(synthesisAll: Bool, videoStickers: [PQEditVisionTrackMaterialsModel], voiceList: [PQVoiceModel], titleStickers: [PQEditSubTitleModel]) {
         // 输出视频地址
         // 输出视频地址
 //        exprotVideo()
 //        exprotVideo()
 //        return;
 //        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
         guard let voiceCount = data?.reduce(0, { partialResult, itemModell in
             itemModell.voiceStickers.count + partialResult
             itemModell.voiceStickers.count + partialResult

+ 0 - 11
BFRecordScreenKit/Classes/BFRecordItemModel.swift

@@ -110,17 +110,6 @@ public class BFRecordItemModel: NSObject {
         dealedDurationRanges.removeAll()
         dealedDurationRanges.removeAll()
 
 
         var start: Double = 0
         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]
         var list: [PQVoiceModel]
         list = voiceStickers.sorted { model1, model2 in
         list = voiceStickers.sorted { model1, model2 in

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

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