Browse Source

实现抛留功能

harry 3 years ago
parent
commit
8f1291d031

BIN
BFRecordScreenKit/Assets/BFRecordScreenKit.xcassets/gou.imageset/gou@3x.png


+ 0 - 21
BFRecordScreenKit/Assets/BFRecordScreenKit.xcassets/mic1.imageset/Contents.json

@@ -1,21 +0,0 @@
-{
-  "images" : [
-    {
-      "idiom" : "universal",
-      "scale" : "1x"
-    },
-    {
-      "idiom" : "universal",
-      "scale" : "2x"
-    },
-    {
-      "filename" : "mic1@3x.png",
-      "idiom" : "universal",
-      "scale" : "3x"
-    }
-  ],
-  "info" : {
-    "author" : "xcode",
-    "version" : 1
-  }
-}

BIN
BFRecordScreenKit/Assets/BFRecordScreenKit.xcassets/mic1.imageset/mic1@3x.png


BIN
BFRecordScreenKit/Assets/BFRecordScreenKit.xcassets/mic2.imageset/mic2@3x.png


+ 1 - 1
BFRecordScreenKit/Assets/BFRecordScreenKit.xcassets/gou.imageset/Contents.json → BFRecordScreenKit/Assets/BFRecordScreenKit.xcassets/preview_play.imageset/Contents.json

@@ -9,7 +9,7 @@
       "scale" : "2x"
     },
     {
-      "filename" : "gou@3x.png",
+      "filename" : "preview_play@3x.png",
       "idiom" : "universal",
       "scale" : "3x"
     }

BIN
BFRecordScreenKit/Assets/BFRecordScreenKit.xcassets/preview_play.imageset/preview_play@3x.png


+ 1 - 1
BFRecordScreenKit/Assets/BFRecordScreenKit.xcassets/mic2.imageset/Contents.json → BFRecordScreenKit/Assets/BFRecordScreenKit.xcassets/preview_sound_set.imageset/Contents.json

@@ -9,7 +9,7 @@
       "scale" : "2x"
     },
     {
-      "filename" : "mic2@3x.png",
+      "filename" : "preview_sound_set@3x.png",
       "idiom" : "universal",
       "scale" : "3x"
     }

BIN
BFRecordScreenKit/Assets/BFRecordScreenKit.xcassets/preview_sound_set.imageset/preview_sound_set@3x.png


+ 0 - 21
BFRecordScreenKit/Assets/BFRecordScreenKit.xcassets/xx.imageset/Contents.json

@@ -1,21 +0,0 @@
-{
-  "images" : [
-    {
-      "idiom" : "universal",
-      "scale" : "1x"
-    },
-    {
-      "idiom" : "universal",
-      "scale" : "2x"
-    },
-    {
-      "filename" : "xx@3x.png",
-      "idiom" : "universal",
-      "scale" : "3x"
-    }
-  ],
-  "info" : {
-    "author" : "xcode",
-    "version" : 1
-  }
-}

BIN
BFRecordScreenKit/Assets/BFRecordScreenKit.xcassets/xx.imageset/xx@3x.png


+ 71 - 41
BFRecordScreenKit/Classes/BFRecordExport.swift

@@ -54,33 +54,27 @@ public class BFRecordExport {
                 let asset = itemModel.baseMaterial
                 if let dur = asset?.duration.seconds {
                     if synthesisAll {
-                        let bgMovieInfo = splitBaseMaterial(range: CMTimeRange(start: CMTime(seconds: totalDur*100, preferredTimescale: 100), duration: CMTime(seconds: dur*100, preferredTimescale: 100)))
+                        let bgMovieInfo = splitBaseMaterial(timelineIn: totalDur, model_in: 0, duration: dur)
+                        bgMovieInfo.volumeGain = 0
                         itemModel.videoStickers.append(bgMovieInfo)
                         totalDur += dur
                     } else {
                         var subDur = 0.0
-                        for range in itemModel.dealedDurationRanges {
-                            let sticker = splitBaseMaterial(range: CMTimeRange(start: CMTime(seconds: (totalDur + subDur) * 100, preferredTimescale: 100), duration: CMTime(seconds: range.duration.seconds * 100, preferredTimescale: 100)))
+                        let drangs = itemModel.dealedDurationRanges.filter { ranges in
+                            ranges.isRecord == true
+                        }
+                        for srange in drangs {
+                            let range = srange.range
+                            let sticker = splitBaseMaterial(timelineIn: (totalDur + subDur), model_in: range.start.seconds, duration: range.duration.seconds)
                             sticker.volumeGain = 0
                             itemModel.videoStickers.append(sticker)
-                            subDur += range.start.seconds
+                            subDur += range.duration.seconds
                         }
                         totalDur += subDur
                     }
                 }
-//                let bgMovieInfo: PQEditVisionTrackMaterialsModel = PQEditVisionTrackMaterialsModel()
-//                bgMovieInfo.type = StickerType.VIDEO.rawValue
-//                bgMovieInfo.locationPath = ((asset?.url.absoluteString)?.removingPercentEncoding ?? "").replacingOccurrences(of: "file://", with: "")
-//                bgMovieInfo.timelineIn = 0
-//                bgMovieInfo.timelineOut = CMTimeGetSeconds(asset?.duration ?? CMTime.zero)
-//                bgMovieInfo.model_in = bgMovieInfo.timelineIn
-//                bgMovieInfo.out = bgMovieInfo.timelineOut
-//                bgMovieInfo.canvasFillType = stickerContentMode.aspectFitStr.rawValue
-//                bgMovieInfo.volumeGain = 30
-//                bgMovieInfo.aptDuration = bgMovieInfo.timelineOut
-//                bgMovieInfo.duration = bgMovieInfo.timelineOut
-                beginExport()
             }
+            beginExport(synthesisAll:synthesisAll)
         }
     }
     
@@ -119,15 +113,15 @@ public class BFRecordExport {
         return ranges
     }
     
-    func splitBaseMaterial(range:CMTimeRange) -> PQEditVisionTrackMaterialsModel{
+    func splitBaseMaterial(timelineIn:Double, model_in:Double, duration:Double) -> PQEditVisionTrackMaterialsModel{
         let bgMovieInfo: PQEditVisionTrackMaterialsModel = PQEditVisionTrackMaterialsModel()
         if let asset = data?.first?.baseMaterial {
             bgMovieInfo.type = StickerType.VIDEO.rawValue
             bgMovieInfo.locationPath = ((asset.url.absoluteString).removingPercentEncoding ?? "").replacingOccurrences(of: "file://", with: "")
-            bgMovieInfo.timelineIn = range.start.seconds
-            bgMovieInfo.timelineOut = range.end.seconds
-            bgMovieInfo.model_in = bgMovieInfo.timelineIn
-            bgMovieInfo.out = bgMovieInfo.timelineOut
+            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
@@ -137,7 +131,7 @@ public class BFRecordExport {
         return bgMovieInfo
     }
     
-    func beginExport() {
+    func beginExport(synthesisAll:Bool) {
         // 输出视频地址
 //        exprotVideo()
 //        return;
@@ -155,10 +149,9 @@ public class BFRecordExport {
         // 处理导出
         let voiceList = itemModel.voiceStickers
         let videoStickers = itemModel.videoStickers
-        
         if voiceList.count > 0 || videoStickers.count > 1 {
 
-            let (audioMix, composition) = mergeAudio(videoStickers: videoStickers, audios: voiceList)
+            let (audioMix, composition) = mergeAudio(videoStickers: videoStickers, audios: voiceList,synthesisAll:synthesisAll)
             
             let filter = videoStickers.map { sticker in
                 PQMovieFilter(movieSticker: sticker)
@@ -196,7 +189,7 @@ public class BFRecordExport {
             
             BFLog(message: "导出设置的码率为:\(orgeBitRate)")
             if exporter!.prepare(videoSize: size, videoAverageBitRate: orgeBitRate) {
-                exporter!.start(playeTimeRange: CMTimeRange(start: CMTime.zero, end: asset?.duration ?? CMTime.zero))
+                exporter!.start(playeTimeRange: CMTimeRange(start: CMTime.zero, end: composition.duration))
             }
             exporter?.progressClosure = { [weak self] _, _, progress in
                 //            BFLog(message: "正片合成进度 \(progress * 100)%")
@@ -267,7 +260,7 @@ public class BFRecordExport {
 }
 
 extension BFRecordExport {
-    func mergeAudio(videoStickers:[PQEditVisionTrackMaterialsModel], audios:[PQVoiceModel]?) -> (AVMutableAudioMix, AVMutableComposition){
+    func mergeAudio(videoStickers:[PQEditVisionTrackMaterialsModel], audios:[PQVoiceModel]?, synthesisAll:Bool) -> (AVMutableAudioMix, AVMutableComposition){
         let composition = AVMutableComposition()
         let audioMix = AVMutableAudioMix()
         var tempParameters = [AVMutableAudioMixInputParameters]()
@@ -284,25 +277,62 @@ extension BFRecordExport {
             tempParameters += PQVideoEditViewModel.dealWithMaterialTrack(stickerModel: sticker, composition: composition)
         }
         if let voices = audios {
-            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 += PQVideoEditViewModel.dealWithMaterialTrack(stickerModel: sticker, composition: composition)
+            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 += PQVideoEditViewModel.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 += PQVideoEditViewModel.dealWithMaterialTrack(stickerModel: sticker, composition: composition)
+        }
+        
+        return tempParameters
+    }
+    
 }

+ 22 - 9
BFRecordScreenKit/Classes/BFRecordItemModel.swift

@@ -9,9 +9,14 @@ import Foundation
 
 import BFFramework
 
+struct SplitRecordRange {
+    var isRecord:Bool = false
+    var range:CMTimeRange
+}
+
 public class BFRecordItemModel: NSObject {
     var baseMaterial : AVURLAsset?
-    var dealedDurationRanges = [CMTimeRange]()
+    var dealedDurationRanges = [SplitRecordRange]()
     public var voiceStickers = [PQVoiceModel]()
     public var videoStickers = [PQEditVisionTrackMaterialsModel]()
     public var imageStickers = [PQEditVisionTrackMaterialsModel]()
@@ -22,15 +27,23 @@ public class BFRecordItemModel: NSObject {
     
     func generationTimeRanges() {
         
-//        var start : Double = 0
+        dealedDurationRanges.removeAll()
+        
+        var start : Double = 0
         for model in voiceStickers {
-//            if model.startTime > start{
-//                let range = CMTimeRange(start: CMTime(seconds: start, preferredTimescale: 100), duration: CMTime(seconds: model.startTime - start, preferredTimescale: 100))
-//                dealedDurationRanges.append(range)
-//
-//            }
-            dealedDurationRanges.append(CMTimeRange(start: CMTime(seconds: model.startTime, preferredTimescale: 100), end: CMTime(seconds: model.endTime, preferredTimescale: 100)))
-//            start = model.endTime
+            if model.startTime > start{
+                //
+                let range = CMTimeRange(start: CMTime(seconds: start, preferredTimescale: 100), duration: CMTime(seconds: model.startTime - start, preferredTimescale: 100))
+                dealedDurationRanges.append(SplitRecordRange(isRecord: false, range: range))
+            }
+
+            let range = CMTimeRange(start: CMTime(seconds: model.startTime, preferredTimescale: 100), end: CMTime(seconds: model.endTime, preferredTimescale: 100))
+            dealedDurationRanges.append(SplitRecordRange(isRecord: true, range: range))
+            start = model.endTime
+        }
+        if start < baseMaterial?.duration.seconds ?? 0 {
+            let range = CMTimeRange(start: CMTime(seconds: start, preferredTimescale: 100), end: CMTime(seconds: baseMaterial?.duration.seconds ?? 0, preferredTimescale: 100))
+            dealedDurationRanges.append(SplitRecordRange(isRecord: false, range: range))
         }
         
     }

+ 14 - 24
BFRecordScreenKit/Classes/BFRecordScreenController.swift

@@ -66,9 +66,9 @@ public class BFRecordScreenController: BFBaseViewController {
     var isDragingProgressSlder : Bool = false   // 是否在拖动进度条
     var isStopAtRecordRange = -1
     //定义音频的编码参数
-    let recordSettings:[String : Any] = [AVSampleRateKey : 44100.0, //声音采样率
-                            AVFormatIDKey : kAudioFormatLinearPCM,  //编码格式
-                    AVNumberOfChannelsKey : 1,                      //采集音轨
+    let recordSettings:[String : Any] = [AVSampleRateKey : 44100.0, // 声音采样率
+                            AVFormatIDKey : kAudioFormatLinearPCM,  // 编码格式
+                    AVNumberOfChannelsKey : 1,                      // 采集音轨
                   AVEncoderBitDepthHintKey: 16,                     // 位深
                  AVEncoderAudioQualityKey : AVAudioQuality.medium.rawValue] //音频质量
 
@@ -127,6 +127,9 @@ public class BFRecordScreenController: BFBaseViewController {
     
     lazy var playBtn:UIButton = {
         let btn = UIButton(frame: view.bounds)
+        btn.setImage(imageInRecordScreenKit(by: "preview_play"), for: .normal)
+        let vv = UIView(frame: CGRect(x: 0, y: 0, width: 1, height: 1))
+        btn.setImage(vv.graphicsGetImage(), for: .selected)
         btn.addTarget(self, action: #selector(playVideo(btn:)), for: .touchUpInside)
         return btn
     }()
@@ -156,13 +159,6 @@ public class BFRecordScreenController: BFBaseViewController {
         return btn
     }()
     
-//    lazy var progessSildeBackV : UIView = {
-//        let vv = UIView()
-//        vv.backgroundColor = .orange // .clear
-//
-//        return vv
-//    }()
-    
     lazy var withDrawBtn:UIButton = {
         let btn = UIButton(type: .custom)
         btn.setImage(imageInRecordScreenKit(by: "withdraw_n"), for: .normal)
@@ -268,9 +264,8 @@ public class BFRecordScreenController: BFBaseViewController {
 //        view.addSubview(toolV)
         bottomeView.addSubview(recordBtn)
         bottomeView.addSubview(withDrawBtn)
-        bottomeView.addSubview(changeVoiceBtn)
+//        bottomeView.addSubview(changeVoiceBtn)
         bottomeView.addSubview(progressThumV)
-//        progressThumV.addSubview(progessSildeBackV)
 
         if checkStatus() {
             try? AVAudioSession.sharedInstance().setCategory(.playAndRecord, options: .defaultToSpeaker)
@@ -297,24 +292,18 @@ public class BFRecordScreenController: BFBaseViewController {
             make.width.height.equalTo(65)
             make.top.equalTo(128)
         }
-        changeVoiceBtn.snp.makeConstraints { make in
-            make.right.equalToSuperview()
-            make.top.width.height.equalTo(withDrawBtn)
-        }
+//        changeVoiceBtn.snp.makeConstraints { make in
+//            make.right.equalToSuperview()
+//            make.top.width.height.equalTo(withDrawBtn)
+//        }
         
         recordBtn.snp.makeConstraints { make in
             make.left.equalTo(withDrawBtn.snp.right)
-            make.right.equalTo(changeVoiceBtn.snp.left)
+            make.right.equalTo(-65)
             make.height.equalTo(42)
             make.top.equalTo(withDrawBtn).offset(6)
         }
-        
-//        progessSildeBackV.snp.makeConstraints { make in
-//            make.left.equalToSuperview()
-//            make.right.equalToSuperview()
-//            make.bottom.equalToSuperview()
-//            make.height.equalTo(8)
-//        }
+
         
         withDrawBtn.imageEdgeInsets = UIEdgeInsets(top: -withDrawBtn.imageView!.height, left: 0, bottom: 0, right: -withDrawBtn.titleLabel!.width);
         withDrawBtn.titleEdgeInsets = UIEdgeInsets(top: withDrawBtn.titleLabel!.height + 2, left: -withDrawBtn.imageView!.width, bottom: 0, right: 0);
@@ -392,6 +381,7 @@ public class BFRecordScreenController: BFBaseViewController {
 //        movie?.startProcessing()
         assetPlayer?.volume = 0
         assetPlayer?.play()
+        playBtn.isSelected = true
     }
     
     @objc func endRecord(){