Jelajahi Sumber

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

wenweiwei 3 tahun lalu
induk
melakukan
eeb9377c58

+ 11 - 7
BFRecordScreenKit/Classes/BFRecordExport.swift

@@ -127,8 +127,8 @@ public class BFRecordExport {
                         titleStickers.append(newTitleSticker)
                         newTitleSticker.text = titleS.text
                         newTitleSticker.setting = titleS.setting
-                        newTitleSticker.timelineIn = totalDur + titleS.timelineIn
-                        newTitleSticker.timelineOut = totalDur + titleS.timelineOut
+                        newTitleSticker.timelineIn = titleS.timelineIn + CMTime(seconds: totalDur, preferredTimescale: 1000)
+                        newTitleSticker.timelineOut = titleS.timelineOut + CMTime(seconds: totalDur, preferredTimescale: 1000)
                     }
 
                     totalDur += duration
@@ -180,8 +180,8 @@ public class BFRecordExport {
                             titleStickers.append(newTitleSticker)
                             newTitleSticker.text = titleS.text
                             newTitleSticker.setting = titleS.setting
-                            newTitleSticker.timelineIn = totalDur + titleS.timelineIn
-                            newTitleSticker.timelineOut = totalDur + titleS.timelineOut
+                            newTitleSticker.timelineIn = titleS.timelineIn + CMTime(seconds: totalDur, preferredTimescale: 1000)
+                            newTitleSticker.timelineOut = titleS.timelineOut + CMTime(seconds: totalDur, preferredTimescale: 1000)
                         }
 
                         totalDur += subDur
@@ -220,17 +220,20 @@ public class BFRecordExport {
                             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)")
+                                newTitleSticker.timelineIn = titleS.timelineIn + CMTime(seconds: totalDur + subDur , preferredTimescale: 1000) - voiceSticker.startCMTime
+                                newTitleSticker.timelineOut = titleS.timelineOut + CMTime(seconds: totalDur + subDur, preferredTimescale: 1000) - voiceSticker.startCMTime
+                                BFLog(1, message: "timein - \(newTitleSticker.timelineIn), out - \(newTitleSticker.timelineOut)")
                             }
                             subDur += range.duration.seconds
                         }
+                        
                         totalDur += subDur
                     }
                 }
@@ -241,6 +244,7 @@ public class BFRecordExport {
 
     public func cancelExport() {
         exporter?.cancel()
+        
     }
 
     public func clearFileCache() {

+ 16 - 14
BFRecordScreenKit/Classes/RecordScreen/Controller/BFRecordScreenController.swift

@@ -479,23 +479,24 @@ public class BFRecordScreenController: BFBaseViewController {
 
                 // 这里加300ms 是因为返回结果为了切到字,时长提前一些时间,具体时间官方没说和原音频有关系。这里我们先延后300ms 单位:毫秒。
                 if URL(fileURLWithPath: audioFilePath ?? "b").deletingPathExtension().lastPathComponent.contains(URL(fileURLWithPath: self?.recorderManager?.voiceModel?.wavFilePath ?? "aa").deletingPathExtension().lastPathComponent) {
-                    newSubtitle.timelineIn = (self?.recorderManager?.voiceModel?.startCMTime.seconds ?? 0.0) + Float64((((payload?["begin_time"]) as? Int) ?? 0) + 300) / 1000.0
-                    newSubtitle.timelineOut = (self?.recorderManager?.voiceModel?.startCMTime.seconds ?? 0.0) + Float64(((payload?["time"]) as? Int) ?? 0) / 1000.0
+                    newSubtitle.timelineIn = (self?.recorderManager?.voiceModel?.startCMTime ?? .zero) + CMTime(seconds: Float64((((payload?["begin_time"]) as? Int) ?? 0) + 300) / 1000.0, preferredTimescale: 1000)
+                    newSubtitle.timelineOut = (self?.recorderManager?.voiceModel?.startCMTime ?? .zero) + CMTime(seconds: Float64(((payload?["time"]) as? Int) ?? 0) / 1000.0, preferredTimescale: 1000)
 
-                    if newSubtitle.timelineIn - 0.1 > (self?.recorderManager?.voiceModel?.endCMTime.seconds ?? 0) {
+                    if (newSubtitle.timelineIn - (self?.recorderManager?.voiceModel?.endCMTime ?? .zero)).seconds > 0.1 {
                         BFLog(1, message: "卡在录音尾巴上了1")
-                        newSubtitle.timelineIn -= 0.1
+                        newSubtitle.timelineIn = newSubtitle.timelineIn - CMTime(seconds: 0.1, preferredTimescale: 1000)
                     }
                 } else {
                     if let voice = self?.itemModels[self?.currItemModelIndex ?? 0].voiceStickers.first(where: { m in
                         m.wavFilePath == audioFilePath
                     }) {
-                        newSubtitle.timelineIn = voice.startCMTime.seconds + Float64((((payload?["begin_time"]) as? Int) ?? 0) + 300) / 1000.0
-                        newSubtitle.timelineOut = voice.startCMTime.seconds + Float64(((payload?["time"]) as? Int) ?? 0) / 1000.0
+                        newSubtitle.timelineIn = voice.startCMTime + CMTime(seconds:  Float64((((payload?["begin_time"]) as? Int) ?? 0) + 300) / 1000.0, preferredTimescale: 1000)
+                        newSubtitle.timelineOut = voice.startCMTime +  CMTime(seconds: Float64(((payload?["time"]) as? Int) ?? 0) / 1000.0, preferredTimescale: 1000)
 
-                        if newSubtitle.timelineIn - 0.1 > voice.endCMTime.seconds {
+                        
+                        if (newSubtitle.timelineIn - voice.endCMTime).seconds > 0.1 {
                             BFLog(1, message: "卡在录音尾巴上了1")
-                            newSubtitle.timelineIn -= 0.1
+                            newSubtitle.timelineIn = newSubtitle.timelineIn - CMTime(seconds: 0.1, preferredTimescale: 1000)
                         }
                     }
                 }
@@ -507,7 +508,8 @@ public class BFRecordScreenController: BFBaseViewController {
                 newSubtitle.text = showText
                 newSubtitle.audioFilePath = audioFilePath ?? ""
 
-                BFLog(message: "添加字幕数据 timelineIn \(newSubtitle.timelineIn) timelineOut \(newSubtitle.timelineOut) text: \(newSubtitle.text) 音频路径为:\(audioFilePath ?? "")")
+                BFLog(message: "添加字幕数据 timelineIn \(newSubtitle.timelineIn) timelineOut \(newSubtitle.timelineOut) text: \(newSubtitle.text) 音频路径为:\(audioFilePath ?? "") 传入的地址:\(self?.recorderManager?.voiceModel?.wavFilePath ?? "aa")")
+                
                 newSubtitle.setting = self?.subtitleSettingView.subtitle.setting ?? BFSubTitileSettingModel()
 
                 self?.itemModels[self?.currItemModelIndex ?? 0].titleStickers.append(newSubtitle)
@@ -579,7 +581,7 @@ public class BFRecordScreenController: BFBaseViewController {
                 if sself.itemModels[sself.currItemModelIndex].mediaType == .IMAGE {
                     var duration: Double = 0
                     sself.itemModels[sself.currItemModelIndex].voiceStickers.forEach { temp in
-                        BFLog(message: "录制结束-最终:\(temp.wavFilePath ?? "")-\(temp.startCMTime.seconds)-\(temp.endCMTime.seconds)-\(temp.endCMTime.seconds - temp.startCMTime.seconds)")
+                        BFLog(1, message: "录制结束-最终:\(temp.wavFilePath ?? "")-\(temp.startCMTime.seconds)-\(temp.endCMTime.seconds)-\(temp.endCMTime.seconds - temp.startCMTime.seconds)")
                         temp.duration = "\(temp.endCMTime.seconds - temp.startCMTime.seconds)"
                         duration = duration + (temp.endCMTime.seconds - temp.startCMTime.seconds)
                     }
@@ -709,9 +711,9 @@ public class BFRecordScreenController: BFBaseViewController {
         }
         var findShowSubtitle: PQEditSubTitleModel?
         for (index, subtitle) in itemModels[currItemModelIndex].titleStickers.enumerated() {
-            if subtitle.timelineIn <= CMTimeGetSeconds(time), subtitle.timelineOut >= CMTimeGetSeconds(time) {
+            if CMTimeCompare(subtitle.timelineIn, time) <= 0 , CMTimeCompare(subtitle.timelineOut, time) >= 0  {
                 findShowSubtitle = subtitle
-                BFLog(1, message: "找到要显示的字幕 in \(findShowSubtitle?.timelineIn ?? 0.0) out \(findShowSubtitle?.timelineOut ?? 0.0) text:\(findShowSubtitle?.text ?? "") currTime is \(CMTimeGetSeconds(time))")
+                BFLog(1, message: "找到要显示的字幕 in \((findShowSubtitle?.timelineIn ?? .zero).seconds) out \((findShowSubtitle?.timelineOut ?? .zero).seconds) text:\(findShowSubtitle?.text ?? "") currTime is \(CMTimeGetSeconds(time))")
                 showSubtitleIndex = index
                 break
             }
@@ -1697,7 +1699,7 @@ public class BFRecordScreenController: BFBaseViewController {
         if itemModels[currItemModelIndex].mediaType == .VIDEO {
             if CMTimeGetSeconds(item?.duration ?? CMTime.zero) > 0 {
                 currentAssetProgress = CMTime(seconds: time.seconds, preferredTimescale: 1000)
-                BFLog(1, message: "video curr:\(CMTimeGetSeconds(currentAssetProgress))")
+//                BFLog(1, message: "video curr:\(CMTimeGetSeconds(currentAssetProgress))")
                 DispatchQueue.main.async { [weak self] in
                     BFLog(message: "更新录音进度\(#function)-\(self?.currentAssetProgress.seconds ?? 0)")
                     self?.progreddL.text = String(format: "%@", CMTimeGetSeconds(time).formatDurationToHMS())
@@ -2052,7 +2054,7 @@ public extension BFRecordScreenController {
             if !(self?.isRecording ?? false), (self?.currentAssetProgress.seconds ?? 0) > (self?.itemModels[self?.currItemModelIndex ?? 0].materialDuraion ?? 0) {
                 return
             }
-            BFLog(1, message: "更新录音进度\(#function)-\(self?.currentAssetProgress.seconds ?? 0)")
+//            BFLog(1, message: "更新录音进度\(#function)-\(self?.currentAssetProgress.seconds ?? 0)")
             self?.progreddL.text = String(format: "%@", (self?.currentAssetProgress.seconds ?? 0).formatDurationToHMS())
             self?.progressThumV.progress = (self?.currentAssetProgress.seconds ?? 0)
             self?.updateSubtitle(time: self?.currentAssetProgress ?? CMTime.zero)