SwiftDubbingLibPlugin.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import Flutter
  2. import UIKit
  3. import AVFoundation
  4. private let timerInterval: Double = 0.03
  5. private let preTime: Double = 0.3
  6. private let preLag: Double = 0.15
  7. enum DubbingStatus {
  8. case stop, record, playRecord
  9. }
  10. public class SwiftDubbingLibPlugin: NSObject, FlutterPlugin {
  11. var registrar: FlutterPluginRegistrar!
  12. var channel: FlutterMethodChannel!
  13. public static func register(with registrar: FlutterPluginRegistrar) {
  14. let channel = FlutterMethodChannel(name: "dubbing_lib", binaryMessenger: registrar.messenger())
  15. let instance = SwiftDubbingLibPlugin()
  16. registrar.addMethodCallDelegate(instance, channel: channel)
  17. instance.channel = channel
  18. instance.registrar = registrar
  19. }
  20. public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
  21. let args = call.arguments as? [String: Any];
  22. switch call.method {
  23. case "getPlatformVersion":
  24. result("iOS " + UIDevice.current.systemVersion)
  25. break
  26. case "mixinVideoAndAudio":
  27. let videoPath = args!["videoPath"] as! String;
  28. let bgmPath = args!["bgmPath"] as! String;
  29. let resultPath = args!["resultPath"] as! String;
  30. mixinVideo(bgmPath: bgmPath, videoPath: videoPath, outPath: resultPath, result: result)
  31. break
  32. case "mixinAudio":
  33. let bgmPath = args!["bgmPath"] as! String;
  34. let audioPathList = args!["audioPaths"] as! [String];
  35. let startTimeList = args!["startTimeList"] as! [Double];
  36. let pathVideoMixinDir = args!["resultPath"] as! String;
  37. mixinAudio(bgmPath: bgmPath, audioPathList: audioPathList, startTimeList: startTimeList, outPath: pathVideoMixinDir, result: result)
  38. break;
  39. case "setExtraFullScreen":
  40. result("")
  41. break
  42. default:
  43. result(FlutterMethodNotImplemented)
  44. }
  45. }
  46. /// 合成音频
  47. func mixinAudio(bgmPath: String, audioPathList: [String], startTimeList: [Double], outPath: String, result: @escaping FlutterResult) {
  48. let musicUrl = URL(fileURLWithPath: bgmPath)
  49. let composer = DubbingComposer(timeline: startTimeList, musicUrl: musicUrl, recordsUrl: audioPathList)
  50. composer.preTime = preLag
  51. let outputUrl = URL(fileURLWithPath: outPath)
  52. DispatchQueue.global().async {
  53. composer.compose(outputUrl, onSuccess: {
  54. result(outPath)
  55. }) { (message) in
  56. print("合成失败", message)
  57. result(FlutterError(code: "1005", message: "mix audio failed", details: nil))
  58. }
  59. }
  60. }
  61. /// 合成视频
  62. func mixinVideo(bgmPath: String, videoPath: String, outPath: String, result: @escaping FlutterResult) {
  63. let musicUrl = URL(fileURLWithPath: bgmPath)
  64. let videoUrl = URL(fileURLWithPath: videoPath)
  65. let composer = VideoComposer(videoUrl: videoUrl, musicUrl: musicUrl)
  66. let outputUrl = URL(fileURLWithPath: outPath)
  67. DispatchQueue.global().async {
  68. composer.compose(outputUrl, onSuccess: {
  69. result(outPath)
  70. }) { (message) in
  71. print("合成失败", message)
  72. result(FlutterError(code: "1006", message: "mix video failed", details: nil))
  73. }
  74. }
  75. }
  76. }
  77. extension String{
  78. /// check string cellection is whiteSpace
  79. var isBlank : Bool{
  80. return allSatisfy({$0.isWhitespace})
  81. }
  82. }
  83. extension Optional where Wrapped == String{
  84. var isBlank : Bool{
  85. return self?.isBlank ?? true
  86. }
  87. }