Browse Source

合成后音画不同步问题

huzhiqiang 3 years ago
parent
commit
368677f0d7

+ 1 - 1
BFFramework/Classes/PQGPUImage/akfilters/PQMovieInput.swift

@@ -550,7 +550,7 @@ public class PQMovieInput: ImageSource {
             let startPTime = CFAbsoluteTimeGetCurrent()
 
             imageFramebuffer.lock()
-            BFLog(1, message: "mIsExport:\(mIsExport) 实际设置的每一帧时间戳:\(CMTimeGetSeconds(currentTime))")
+            BFLog(message: "mIsExport:\(mIsExport) 实际设置的每一帧时间戳:\(CMTimeGetSeconds(currentTime))")
             // 设置当前帧的时间戳
             imageFramebuffer.timingStyle = .videoFrame(timestamp: Timestamp(currentTime))
  

+ 1 - 1
BFFramework/Classes/Stuckpoint/Controller/PQStuckPointEditerController.swift

@@ -1262,7 +1262,7 @@ extension PQStuckPointEditerController {
 
                                         lastOutTime = deepCopySticker?.out ?? 0
                                     }
-                                    BFLog(1, message: "测试人员创建 sticker  crilp is in 视频 \(String(format: "%.6f", deepCopySticker?.model_in ?? 0))  out  \(String(format: "%.6f", deepCopySticker?.out ?? 0)) ,分段素材时长:\(String(format: "%.6f", (deepCopySticker?.out ?? 0) - (deepCopySticker?.model_in ?? 0))) ,分段显示时长:\(String(format: "%.6f", (deepCopySticker?.timelineOut ?? 0) - (deepCopySticker?.timelineIn ?? 0))), 视频素材原时长\(CMTimeGetSeconds(asset.duration)) timelineIN: \(String(format: "%.6f", deepCopySticker?.timelineIn ?? 0)) timelineOUT:\(String(format: "%.6f", deepCopySticker?.timelineOut ?? 0)) speedRate:\(deepCopySticker?.speedRate ?? 0.0)")
+                                    BFLog(message: "测试人员创建 sticker  crilp is in 视频 \(String(format: "%.6f", deepCopySticker?.model_in ?? 0))  out  \(String(format: "%.6f", deepCopySticker?.out ?? 0)) ,分段素材时长:\(String(format: "%.6f", (deepCopySticker?.out ?? 0) - (deepCopySticker?.model_in ?? 0))) ,分段显示时长:\(String(format: "%.6f", (deepCopySticker?.timelineOut ?? 0) - (deepCopySticker?.timelineIn ?? 0))), 视频素材原时长\(CMTimeGetSeconds(asset.duration)) timelineIN: \(String(format: "%.6f", deepCopySticker?.timelineIn ?? 0)) timelineOUT:\(String(format: "%.6f", deepCopySticker?.timelineOut ?? 0)) speedRate:\(deepCopySticker?.speedRate ?? 0.0)")
 
                                     if deepCopySticker != nil {
                                         stickers.append(deepCopySticker!)

+ 25 - 20
BFFramework/Classes/Stuckpoint/Controller/PQStuckPointPublicController.swift

@@ -644,7 +644,7 @@ extension PQStuckPointPublicController {
         let originaDuration = CMTimeGetSeconds(clipAudioRange.duration)
         BFLog(message: "处理主音频 原始时长startTime = \(originaDuration) 要显示时长totalDuration = \(mTotalDuration)")
         
-        if(originaDuration <= CMTimeGetSeconds(clipAudioRange.duration)){
+        if(Float64(mTotalDuration) <= originaDuration){
             
             BFLog(message: "不用拼接音频文件 \(originAsset.url) 时长is \(CMTimeGetSeconds(originAsset.duration))")
             completeHander(originAsset.url)
@@ -663,7 +663,7 @@ extension PQStuckPointPublicController {
         //第一段的区间
         var timeRange:CMTimeRange = CMTimeRange.zero
         if count > 0 {
-            for index in 0 ... count {
+            for index in 0 ..< count {
                 
                 duration = CMTime(value: CMTimeValue((CMTimeGetSeconds(clipAudioRange.end) - CMTimeGetSeconds(mStartTime)) * Double(playerTimescaleInt)), timescale: playerTimescaleInt)
                 BFLog(message: "每一个文件的 duration \(CMTimeGetSeconds(duration))")
@@ -671,8 +671,6 @@ extension PQStuckPointPublicController {
                 
                 if(index != 0){
                     //(CMTimeGetSeconds(clipAudioRange.end) - CMTimeGetSeconds(mStartTime))为用户选择的第一段时长
-                    duration = CMTime(value: CMTimeValue((CMTimeGetSeconds( clipAudioRange.duration) * Double(index) + (CMTimeGetSeconds(clipAudioRange.end) - CMTimeGetSeconds(mStartTime))) * Float64(playerTimescaleInt)), timescale: playerTimescaleInt)
-                    BFLog(message: "每一个文件的 duration \(CMTimeGetSeconds(duration))")
                     timeRange = clipAudioRange
                     
                 }
@@ -687,23 +685,28 @@ extension PQStuckPointPublicController {
                 let assetTrack: AVAssetTrack = tracks[0]
              
                 let compositionAudioTrack: AVMutableCompositionTrack = composition.addMutableTrack(withMediaType: AVMediaType.audio, preferredTrackID: CMPersistentTrackID())!
-
+       
                 do {
                     //
                     try compositionAudioTrack.insertTimeRange(timeRange, of: assetTrack, at: totalDuration)
 
                 } catch {
+                  
                     BFLog(message: "error is \(error)")
+                    completeHander(URL(string: ""))
+                    return
                 }
-
                 totalDuration = CMTimeAdd(totalDuration, timeRange.duration)
                 
+
+            
+                
             }
         }
         
         if(row > 0){
             duration = CMTime(value: CMTimeValue(Float(CMTimeGetSeconds(totalDuration)) * Float(playerTimescaleInt)), timescale: playerTimescaleInt)
-            timeRange = CMTimeRange(start: duration, end: CMTime(value: CMTimeValue((CMTimeGetSeconds(duration) + Double(row)) * Double(playerTimescaleInt)), timescale: playerTimescaleInt))
+            timeRange = CMTimeRange(start: clipAudioRange.start, duration: CMTime(value: Int64(Double(row) * Double(playerTimescaleInt)), timescale: playerTimescaleInt))
             
                 BFLog(message: "合并的文件地址: \(originAsset.url)")
                 let audioAsset = originAsset
@@ -712,19 +715,21 @@ extension PQStuckPointPublicController {
                     BFLog(message: "音频数据无效不进行合并,所有任务结束要确保输入的数据都正常! \(originAsset.url)")
 
                 }
-                let assetTrack: AVAssetTrack = tracks[0]
-             
-                let compositionAudioTrack: AVMutableCompositionTrack = composition.addMutableTrack(withMediaType: AVMediaType.audio, preferredTrackID: CMPersistentTrackID())!
-
-                do {
-                    //
-                    try compositionAudioTrack.insertTimeRange(timeRange, of: assetTrack, at: totalDuration)
-
-                } catch {
-                    BFLog(message: "error is \(error)")
-                }
+            let assetTrack: AVAssetTrack = tracks[0]
+         
+            let compositionAudioTrack: AVMutableCompositionTrack = composition.addMutableTrack(withMediaType: AVMediaType.audio, preferredTrackID: CMPersistentTrackID())!
+   
+            do {
+                //
+                try compositionAudioTrack.insertTimeRange(timeRange, of: assetTrack, at: totalDuration)
 
-                totalDuration = CMTimeAdd(totalDuration, audioAsset.duration)
+            } catch {
+              
+                BFLog(message: "error is \(error)")
+                completeHander(URL(string: ""))
+                return
+            }
+            totalDuration = CMTimeAdd(totalDuration, timeRange.duration)
                 
         }
  
@@ -750,7 +755,7 @@ extension PQStuckPointPublicController {
                 // 85.819125
                 let audioAsset = AVURLAsset(url: fileUrl, options: avAssertOptions)
 
-                BFLog(message: "拼接声音文件 完成 \(fileUrl) 时长is \(CMTimeGetSeconds(audioAsset.duration))")
+                BFLog(1,message: "拼接声音文件 完成 \(fileUrl) 时长is \(CMTimeGetSeconds(audioAsset.duration))")
                 completeHander(fileUrl)
 
             } else {

+ 1 - 1
BFFramework/Classes/Stuckpoint/View/PQSelecteMusicView.swift

@@ -74,7 +74,7 @@ class PQSelecteMusicView: UIView {
         categoryCollection.delegate = self
         categoryCollection.dataSource = self
         categoryCollection.backgroundColor = .clear
-        categoryCollection.register(PQSelectMusicTagsCell.self, forCellWithReuseIdentifier: String(describing: PQSelectMusicTagsCell.self))
+        categoryCollection.register(PQSelectMusicTagsCell.self, forCellWithReuseIdentifier: "PQSelectMusicTagsCell")
         categoryCollection.delaysContentTouches = false
 
         return categoryCollection

+ 8 - 9
BFFramework/Classes/Utils/PQCommonMethodUtil.swift

@@ -133,9 +133,9 @@ public func kf_imageCacheImage(originUrl: String, completeHandle: @escaping (_ i
 }
 
 /** 打印 */
-public func BFLog<T>( _ type : Int = 0, message: T) {
- 
-    if type == 2 {
+public func BFLog<T>( _ type : Int = 9, _ file:String = #file, _ line:Int = #line, message: T) {
+#if DEBUG
+    if type == 0 {
         let logger = NXLogger.shared
         
         logger.level = .info
@@ -145,13 +145,12 @@ public func BFLog<T>( _ type : Int = 0, message: T) {
         
         BuglyLog.level(.warn, logs: message as? String)
     }else if type == 1 {
-#if DEBUG
-        let file = (#file as NSString).lastPathComponent;
+        let file = (file as NSString).lastPathComponent;
         let dateFmt = DateFormatter()
-        dateFmt.dateFormat = "HH:mm:ss:SSS"
-        print("hhz-\(dateFmt.string(from: Date())) \(file):(\(#line))--\(message)");
-#endif
+        dateFmt.dateFormat = "HH:mm:ss:SSSS"
+        print("hhz-\(dateFmt.string(from: Date())) \(file):(\(line))--\(message)");
     }
+#endif
 //    if PQENVUtil.shared.envMode == .ENVModeTest {
 //     
 //    }else{
@@ -270,7 +269,7 @@ public func getUniqueId(desc: String) -> String {
     let uuid: String = getMachineCode()
     let code: String = "\(arc4random_uniform(1_000_000_000))"
     let uniqueId = (timeStr + desc + uuid + code).md5.md5
-    BFLog(message: "生成唯一码:desc = \(desc),timeStr = \(timeStr),uuid = \(uuid),code = \(code),uniqueId = \(uniqueId)")
+    BFLog(4, message: "生成唯一码:desc = \(desc),timeStr = \(timeStr),uuid = \(uuid),code = \(code),uniqueId = \(uniqueId)")
     return uniqueId
 }