{"id":3335,"date":"2021-06-30T14:37:24","date_gmt":"2021-06-30T06:37:24","guid":{"rendered":"http:\/\/123.57.164.21\/?p=3335"},"modified":"2021-06-30T14:37:24","modified_gmt":"2021-06-30T06:37:24","slug":"swift-%e8%a7%86%e9%a2%91%e5%bd%95%e5%88%b6%e6%95%99%e7%a8%8b3%ef%bc%88%e8%ae%be%e7%bd%ae%e6%8b%8d%e6%91%84%e7%aa%97%e5%8f%a3%e5%a4%a7%e5%b0%8f%ef%bc%8c%e5%bd%95%e5%88%b6%e6%ad%a3%e6%96%b9%e5%bd%a2","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=3335","title":{"rendered":"Swift &#8211; \u89c6\u9891\u5f55\u5236\u6559\u7a0b3\uff08\u8bbe\u7f6e\u62cd\u6444\u7a97\u53e3\u5927\u5c0f\uff0c\u5f55\u5236\u6b63\u65b9\u5f62\u89c6\u9891\uff09"},"content":{"rendered":"\n<p>\u5728\u4e4b\u524d\u7684\u4e24\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4ecb\u7ecd\u4e86\u5982\u4f55\u901a\u8fc7\u00a0<strong>AVFoundation.framework\u00a0<\/strong>\u6846\u67b6\u63d0\u4f9b\u7684\u00a0<strong>AVCaptureSession<\/strong>\u00a0\u7c7b\u6765\u5b9e\u73b0\u89c6\u9891\u7684\u5f55\u5236\u3002\u4ee5\u53ca\u901a\u8fc7\u00a0<strong>AVMutableComposition<\/strong>\u00a0\u6765\u5c06\u591a\u6bb5\u89c6\u9891\u7247\u6bb5\u7684\u89c6\u9891\u3001\u97f3\u9891\u8f68\u9053\u8fdb\u884c\u62fc\u63a5\u5408\u6210\u3002<\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/www.hangge.com\/blog\/cache\/detail_1184.html\" target=\"_blank\">Swift &#8211; \u89c6\u9891\u5f55\u5236\u6559\u7a0b1\uff08\u8c03\u7528\u6444\u50cf\u5934\u5f55\u50cf\uff0c\u5e76\u4fdd\u5b58\u5230\u7cfb\u7edf\u76f8\u518c\uff09<\/a><br><a rel=\"noreferrer noopener\" href=\"https:\/\/www.hangge.com\/blog\/cache\/detail_1185.html\" target=\"_blank\">Swift &#8211; \u89c6\u9891\u5f55\u5236\u6559\u7a0b2\uff08\u5c0f\u89c6\u9891\u62cd\u6444\uff0c\u5c06\u591a\u6bb5\u89c6\u9891\u8fdb\u884c\u5408\u5e76\uff09<\/a><\/p>\n\n\n\n<p>\u8fd9\u4e24\u4e2a\u6837\u4f8b\u6211\u4eec\u7528\u7684\u90fd\u662f\u5168\u5c4f\u5f55\u50cf\uff0c\u62cd\u6444\u4e0b\u7684\u89c6\u9891\u662f\u7ad6\u7684\uff08iPhone6\u62cd\u6444\u7684\u5206\u8fa8\u7387\u662f1080*1920\uff09\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"576\" height=\"1024\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/06\/image-96-576x1024.png\" alt=\"\" class=\"wp-image-3337\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-96-576x1024.png 576w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-96-169x300.png 169w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-96-230x409.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-96-350x623.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-96-480x854.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-96.png 598w\" sizes=\"(max-width: 576px) 100vw, 576px\" \/><\/figure><\/div>\n\n\n\n<p>\u4f46\u6211\u4eec\u770b\u5e02\u9762\u4e0a\u5e38\u89c1\u7684\u89c6\u9891APP\uff0c\u62cd\u6444\u7684\u5c0f\u89c6\u9891\u90fd\u662f\u6b63\u65b9\u5f62\uff0c\u6216\u8005\u662f\u6a2a\u5411\u7684\u77e9\u5f62\u3002\u672c\u6587\u6f14\u793a\u5982\u4f55\u4fee\u6539\u89c6\u9891\u62cd\u6444\u5c3a\u5bf8\uff0c\u8fd9\u91cc\u4ee5\u5b9e\u73b0\u6b63\u65b9\u5f62\u89c6\u9891\u7684\u62cd\u6444\u4e3a\u4f8b\u3002<\/p>\n\n\n\n<p><strong>1\uff0c\u8981\u5b9e\u73b0\u6b63\u65b9\u5f62\u89c6\u9891\u7684\u62cd\u6444\u8981\u4ece\u4e0b\u9762\u51e0\u65b9\u9762\u5165\u624b\uff1a<\/strong><\/p>\n\n\n\n<ul><li>\uff081\uff09\u9884\u89c8\u7a97\u53e3\uff08<strong>AVCaptureVideoPreviewLayer<\/strong>\uff09\u5c3a\u5bf8\u8bbe\u7f6e\u4e3a\u6b63\u65b9\u77e9\u5f62\uff08\u957f\u5bbd\u5747\u4e3a\u5c4f\u5e55\u5bbd\u5ea6\uff09\uff0c\u5e76\u5728\u6574\u4e2a\u5c4f\u5e55\u5782\u76f4\u5c45\u4e2d\u3002<\/li><li>\uff082\uff09\u89c6\u9891\u7247\u6bb5\u5f55\u5236\u7684\u65f6\u5019\u8fd8\u662f\u4f7f\u7528\u5b8c\u6574\u7684\u5206\u8fa8\u7387\u5c3a\u5bf8\u8fdb\u884c\u5f55\u5236\u3002<\/li><\/ul>\n\n\n\n<ul><li>\uff083\uff09\u5728\u5c06\u5404\u4e2a\u89c6\u9891\u7247\u6bb5\u5408\u5e76\u8f93\u51fa\u65f6\uff0c\u6211\u4eec\u901a\u8fc7\u00a0<strong>AVMutableVideoComposition<\/strong>\u00a0\u5b9e\u73b0\u89c6\u9891\u7684\u88c1\u526a\uff0c\u5373\u4ece\u89c6\u9891\u7684\u4e2d\u592e\u4f4d\u7f6e\u88c1\u51fa\u4e00\u4e2a\u6b63\u65b9\u5f62\u7684\u5185\u5bb9\u5e76\u4fdd\u5b58\u6587\u4ef6\u3002<\/li><\/ul>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/06\/image-97.png\" alt=\"\" class=\"wp-image-3338\" width=\"552\" height=\"231\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-97.png 876w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-97-300x126.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-97-768x323.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-97-830x349.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-97-230x97.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-97-350x147.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-97-480x202.png 480w\" sizes=\"(max-width: 552px) 100vw, 552px\" \/><\/figure><\/div>\n\n\n\n<p><br><strong>2\uff0c\u6548\u679c\u56fe\u5982\u4e0b\uff1a<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/06\/image-98-1024x868.png\" alt=\"\" class=\"wp-image-3339\" width=\"547\" height=\"463\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-98-1024x868.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-98-300x254.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-98-768x651.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-98-830x703.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-98-230x195.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-98-350x297.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-98-480x407.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-98.png 1270w\" sizes=\"(max-width: 547px) 100vw, 547px\" \/><\/figure><\/div>\n\n\n\n<p><strong>3\uff0cInfo.plist \u914d\u7f6e\u00a0<\/strong><\/p>\n\n\n\n<p>\u7531\u4e8e\u82f9\u679c\u5b89\u5168\u7b56\u7565\u66f4\u65b0\uff0c\u5728\u4f7f\u7528\u00a0<strong>Xcode8\u00a0<\/strong>\u5f00\u53d1\u65f6\uff0c\u9700\u8981\u5728\u00a0<strong>Info.plist<\/strong>\u00a0\u914d\u7f6e\u8bf7\u6c42\u6444\u50cf\u5934\u3001\u9ea6\u514b\u98ce\u3001\u76f8\u518c\u6743\u9650\u7684\u63cf\u8ff0\u5b57\u6bb5\uff1a\u00a0<\/p>\n\n\n\n<ul><li><strong>Privacy &#8211; Camera Usage Description<\/strong><\/li><li><strong>Privacy &#8211; Microphone Usage Description<\/strong><\/li><li><strong>Privacy &#8211; Photo Library Usage Description<\/strong><\/li><\/ul>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/06\/image-99-1024x758.png\" alt=\"\" class=\"wp-image-3340\" width=\"558\" height=\"412\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-99-1024x758.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-99-300x222.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-99-768x569.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-99-830x614.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-99-230x170.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-99-350x259.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-99-480x355.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-99.png 1140w\" sizes=\"(max-width: 558px) 100vw, 558px\" \/><\/figure><\/div>\n\n\n\n<p><strong>4\uff0c\u6837\u4f8b\u4ee3\u7801<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import UIKit\nimport AVFoundation\nimport Photos\nimport AVKit\n \nclass ViewController: UIViewController , AVCaptureFileOutputRecordingDelegate {\n     \n    \/\/\u89c6\u9891\u6355\u83b7\u4f1a\u8bdd\u3002\u5b83\u662finput\u548coutput\u7684\u6865\u6881\u3002\u5b83\u534f\u8c03\u7740intput\u5230output\u7684\u6570\u636e\u4f20\u8f93\n    let captureSession = AVCaptureSession()\n    \/\/\u89c6\u9891\u8f93\u5165\u8bbe\u5907\n    let videoDevice = AVCaptureDevice.default(for: AVMediaType.video)\n    \/\/\u97f3\u9891\u8f93\u5165\u8bbe\u5907\n    let audioDevice = AVCaptureDevice.default(for: AVMediaType.audio)\n    \/\/\u5c06\u6355\u83b7\u5230\u7684\u89c6\u9891\u8f93\u51fa\u5230\u6587\u4ef6\n    let fileOutput = AVCaptureMovieFileOutput()\n     \n    \/\/\u5f55\u5236\u3001\u4fdd\u5b58\u6309\u94ae\n    var recordButton, saveButton : UIButton!\n     \n    \/\/\u4fdd\u5b58\u6240\u6709\u7684\u5f55\u50cf\u7247\u6bb5\u6570\u7ec4\n    var videoAssets = [AVAsset]()\n    \/\/\u4fdd\u5b58\u6240\u6709\u7684\u5f55\u50cf\u7247\u6bb5url\u6570\u7ec4\n    var assetURLs = [String]()\n    \/\/\u5355\u72ec\u5f55\u50cf\u7247\u6bb5\u7684index\u7d22\u5f15\n    var appendix: Int32 = 1\n     \n    \/\/\u6700\u5927\u5141\u8bb8\u7684\u5f55\u5236\u65f6\u95f4\uff08\u79d2\uff09\n    let totalSeconds: Float64 = 15.00\n    \/\/\u6bcf\u79d2\u5e27\u6570\n    var framesPerSecond:Int32 = 30\n    \/\/\u5269\u4f59\u65f6\u95f4\n    var remainingTime : TimeInterval = 15.0\n     \n    \/\/\u8868\u793a\u662f\u5426\u505c\u6b62\u5f55\u50cf\n    var stopRecording: Bool = false\n    \/\/\u5269\u4f59\u65f6\u95f4\u8ba1\u65f6\u5668\n    var timer: Timer?\n    \/\/\u8fdb\u5ea6\u6761\u8ba1\u65f6\u5668\n    var progressBarTimer: Timer?\n    \/\/\u8fdb\u5ea6\u6761\u8ba1\u65f6\u5668\u65f6\u95f4\u95f4\u9694\n    var incInterval: TimeInterval = 0.05\n    \/\/\u8fdb\u5ea6\u6761\n    var progressBar: UIView = UIView()\n    \/\/\u5f53\u524d\u8fdb\u5ea6\u6761\u7ec8\u70b9\u4f4d\u7f6e\n    var oldX: CGFloat = 0\n     \n    override func viewDidLoad() {\n        super.viewDidLoad()\n         \n        \/\/\u6dfb\u52a0\u89c6\u9891\u3001\u97f3\u9891\u8f93\u5165\u8bbe\u5907\n        let videoInput = try! AVCaptureDeviceInput(device: self.videoDevice!)\n        self.captureSession.addInput(videoInput)\n        let audioInput = try! AVCaptureDeviceInput(device: self.audioDevice!)\n        self.captureSession.addInput(audioInput)\n         \n        \/\/\u6dfb\u52a0\u89c6\u9891\u6355\u83b7\u8f93\u51fa\n        let maxDuration = CMTimeMakeWithSeconds(totalSeconds, preferredTimescale: framesPerSecond)\n        self.fileOutput.maxRecordedDuration = maxDuration\n        self.captureSession.addOutput(self.fileOutput)\n         \n        \/\/\u4f7f\u7528AVCaptureVideoPreviewLayer\u53ef\u4ee5\u5c06\u6444\u50cf\u5934\u7684\u62cd\u6444\u7684\u5b9e\u65f6\u753b\u9762\u663e\u793a\u5728ViewController\u4e0a\n        let videoLayer = AVCaptureVideoPreviewLayer(session: self.captureSession)\n        \/\/\u9884\u89c8\u7a97\u53e3\u662f\u6b63\u65b9\u5f62\uff0c\u5728\u5c4f\u5e55\u5c45\u4e2d\uff08\u663e\u793a\u7684\u4e5f\u662f\u6444\u50cf\u5934\u62cd\u6444\u7684\u4e2d\u5fc3\u533a\u57df\uff09\n        videoLayer.frame = CGRect(x:0, y:self.view.bounds.height\/4,\n                                  width: self.view.bounds.width,\n                                  height: self.view.bounds.width)\n        videoLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill\n        self.view.layer.addSublayer(videoLayer)\n         \n        \/\/\u521b\u5efa\u6309\u94ae\n        self.setupButton()\n        \/\/\u542f\u52a8session\u4f1a\u8bdd\n        self.captureSession.startRunning()\n         \n        \/\/\u6dfb\u52a0\u8fdb\u5ea6\u6761\n        progressBar.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width,\n                                   height: self.view.bounds.height * 0.1)\n        progressBar.backgroundColor = UIColor(red: 4, green: 3, blue: 3, alpha: 0.5)\n        self.view.addSubview(progressBar)\n    }\n     \n    \/\/\u521b\u5efa\u6309\u94ae\n    func setupButton(){\n        \/\/\u521b\u5efa\u5f55\u5236\u6309\u94ae\n        self.recordButton = UIButton(frame: CGRect(x:0,y:0,width:120,height:50))\n        self.recordButton.backgroundColor = UIColor.red\n        self.recordButton.layer.masksToBounds = true\n        self.recordButton.setTitle(\"\u6309\u4f4f\u5f55\u50cf\", for: .normal)\n        self.recordButton.layer.cornerRadius = 20.0\n        self.recordButton.layer.position = CGPoint(x: self.view.bounds.width\/2,\n                                                   y:self.view.bounds.height-50)\n        self.recordButton.addTarget(self, action: #selector(onTouchDownRecordButton(_:)),\n                                    for: .touchDown)\n        self.recordButton.addTarget(self, action: #selector(onTouchUpRecordButton(_:)),\n                                    for: .touchUpInside)\n         \n        \/\/\u521b\u5efa\u4fdd\u5b58\u6309\u94ae\n        self.saveButton = UIButton(frame: CGRect(x:0,y:0,width:70,height:50))\n        self.saveButton.backgroundColor = UIColor.gray\n        self.saveButton.layer.masksToBounds = true\n        self.saveButton.setTitle(\"\u4fdd\u5b58\", for: .normal)\n        self.saveButton.layer.cornerRadius = 20.0\n         \n        self.saveButton.layer.position = CGPoint(x: self.view.bounds.width - 60,\n                                                 y:self.view.bounds.height-50)\n        self.saveButton.addTarget(self, action: #selector(onClickStopButton(_:)),\n                                  for: .touchUpInside)\n         \n        \/\/\u6dfb\u52a0\u6309\u94ae\u5230\u89c6\u56fe\u4e0a\n        self.view.addSubview(self.recordButton);\n        self.view.addSubview(self.saveButton);\n    }\n     \n    \/\/\u6309\u4e0b\u5f55\u5236\u6309\u94ae\uff0c\u5f00\u59cb\u5f55\u5236\u7247\u6bb5\n    @objc func  onTouchDownRecordButton(_ sender: UIButton){\n        if(!stopRecording) {\n            let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory,\n                                                            .userDomainMask, true)\n            let documentsDirectory = paths[0] as String\n            let outputFilePath = \"\\(documentsDirectory)\/output-\\(appendix).mov\"\n            appendix += 1\n            let outputURL = URL(fileURLWithPath: outputFilePath)\n            let fileManager = FileManager.default\n            if(fileManager.fileExists(atPath: outputFilePath)) {\n                 \n                do {\n                    try fileManager.removeItem(atPath: outputFilePath)\n                } catch _ {\n                }\n            }\n            print(\"\u5f00\u59cb\u5f55\u5236\uff1a\\(outputFilePath) \")\n            fileOutput.startRecording(to: outputURL, recordingDelegate: self)\n        }\n    }\n     \n    \/\/\u677e\u5f00\u5f55\u5236\u6309\u94ae\uff0c\u505c\u6b62\u5f55\u5236\u7247\u6bb5\n    @objc func  onTouchUpRecordButton(_ sender: UIButton){\n        if(!stopRecording) {\n            timer?.invalidate()\n            progressBarTimer?.invalidate()\n            fileOutput.stopRecording()\n        }\n    }\n     \n    \/\/\u5f55\u50cf\u5f00\u59cb\u7684\u4ee3\u7406\u65b9\u6cd5\n    func fileOutput(_ output: AVCaptureFileOutput,\n                    didStartRecordingTo fileURL: URL,\n                    from connections: [AVCaptureConnection]) {\n        startProgressBarTimer()\n        startTimer()\n    }\n     \n    \/\/\u5f55\u50cf\u7ed3\u675f\u7684\u4ee3\u7406\u65b9\u6cd5\n    func fileOutput(_ output: AVCaptureFileOutput,\n                    didFinishRecordingTo outputFileURL: URL,\n                    from connections: [AVCaptureConnection], error: Error?) {\n        let asset = AVURLAsset(url: outputFileURL, options: nil)\n        var duration : TimeInterval = 0.0\n        duration = CMTimeGetSeconds(asset.duration)\n        print(\"\u751f\u6210\u89c6\u9891\u7247\u6bb5\uff1a\\(asset)\")\n        videoAssets.append(asset)\n        assetURLs.append(outputFileURL.path)\n        remainingTime = remainingTime - duration\n         \n        \/\/\u5230\u8fbe\u5141\u8bb8\u6700\u5927\u5f55\u5236\u65f6\u95f4\uff0c\u81ea\u52a8\u5408\u5e76\u89c6\u9891\n        if remainingTime &lt;= 0 {\n            mergeVideos()\n        }\n    }\n     \n    \/\/\u5269\u4f59\u65f6\u95f4\u8ba1\u65f6\u5668\n    func startTimer() {\n        timer = Timer(timeInterval: remainingTime, target: self,\n                      selector: #selector(ViewController.timeout), userInfo: nil,\n                      repeats:true)\n        RunLoop.current.add(timer!, forMode: .default)\n    }\n     \n    \/\/\u5f55\u5236\u65f6\u95f4\u8fbe\u5230\u6700\u5927\u65f6\u95f4\n    @objc func timeout() {\n        stopRecording = true\n        print(\"\u65f6\u95f4\u5230\u3002\")\n        fileOutput.stopRecording()\n        timer?.invalidate()\n        progressBarTimer?.invalidate()\n    }\n     \n    \/\/\u8fdb\u5ea6\u6761\u8ba1\u65f6\u5668\n    func startProgressBarTimer() {\n        progressBarTimer = Timer(timeInterval: incInterval, target: self,\n                                 selector: #selector(ViewController.progress),\n                                 userInfo: nil, repeats: true)\n        RunLoop.current.add(progressBarTimer!, forMode: .default)\n    }\n     \n    \/\/\u4fee\u6539\u8fdb\u5ea6\u6761\u8fdb\u5ea6\n    @objc func progress() {\n        let progressProportion: CGFloat = CGFloat(incInterval \/ totalSeconds)\n        let progressInc: UIView = UIView()\n        progressInc.backgroundColor = UIColor(red: 55\/255, green: 186\/255, blue: 89\/255,\n                                              alpha: 1)\n        let newWidth = progressBar.frame.width * progressProportion\n        progressInc.frame = CGRect(x: oldX , y: 0, width: newWidth,\n                                   height: progressBar.frame.height)\n        oldX = oldX + newWidth\n        progressBar.addSubview(progressInc)\n    }\n     \n    \/\/\u4fdd\u5b58\u6309\u94ae\u70b9\u51fb\n    @objc func onClickStopButton(_ sender: UIButton){\n        mergeVideos()\n    }\n     \n    \/\/\u5408\u5e76\u89c6\u9891\u7247\u6bb5\n    func mergeVideos() {\n        let duration = totalSeconds\n         \n        let composition = AVMutableComposition()\n        \/\/\u5408\u5e76\u89c6\u9891\u3001\u97f3\u9891\u8f68\u9053\n        let firstTrack = composition.addMutableTrack(\n            withMediaType: AVMediaType.video, preferredTrackID: CMPersistentTrackID())\n        let audioTrack = composition.addMutableTrack(\n            withMediaType: AVMediaType.audio, preferredTrackID: CMPersistentTrackID())\n         \n        var insertTime: CMTime = CMTime.zero\n        for asset in videoAssets {\n            print(\"\u5408\u5e76\u89c6\u9891\u7247\u6bb5\uff1a\\(asset)\")\n            do {\n                try firstTrack?.insertTimeRange(\n                    CMTimeRangeMake(start: CMTime.zero, duration: asset.duration),\n                    of: asset.tracks(withMediaType: AVMediaType.video)[0] ,\n                    at: insertTime)\n            } catch _ {\n            }\n            do {\n                try audioTrack?.insertTimeRange(\n                    CMTimeRangeMake(start: CMTime.zero, duration: asset.duration),\n                    of: asset.tracks(withMediaType: AVMediaType.audio)[0] ,\n                    at: insertTime)\n            } catch _ {\n            }\n             \n            insertTime = CMTimeAdd(insertTime, asset.duration)\n        }\n        \/\/\u65cb\u8f6c\u89c6\u9891\u56fe\u50cf\uff0c\u9632\u6b6290\u5ea6\u98a0\u5012\n        firstTrack?.preferredTransform = CGAffineTransform(rotationAngle: CGFloat.pi\/2)\n         \n        \/\/\u5b9a\u4e49\u6700\u7ec8\u751f\u6210\u7684\u89c6\u9891\u5c3a\u5bf8\uff08\u77e9\u5f62\u7684\uff09\n        print(\"\u89c6\u9891\u539f\u59cb\u5c3a\u5bf8\uff1a\", firstTrack!.naturalSize)\n        let renderSize = CGSize(width: firstTrack!.naturalSize.height,\n                                height:firstTrack!.naturalSize.height)\n        print(\"\u6700\u7ec8\u6e32\u67d3\u5c3a\u5bf8\uff1a\", renderSize)\n         \n        \/\/\u901a\u8fc7AVMutableVideoComposition\u5b9e\u73b0\u89c6\u9891\u7684\u88c1\u526a(\u77e9\u5f62\uff0c\u622a\u53d6\u6b63\u4e2d\u5fc3\u533a\u57df\u89c6\u9891)\n        let videoComposition = AVMutableVideoComposition()\n        videoComposition.frameDuration = CMTimeMake(value: 1, timescale: framesPerSecond)\n        videoComposition.renderSize = renderSize\n         \n        let instruction = AVMutableVideoCompositionInstruction()\n        instruction.timeRange = CMTimeRangeMake(\n            start: CMTime.zero,\n            duration: CMTimeMakeWithSeconds(Float64(duration),\n                                            preferredTimescale: framesPerSecond))\n         \n        let transformer: AVMutableVideoCompositionLayerInstruction =\n            AVMutableVideoCompositionLayerInstruction(assetTrack: firstTrack!)\n        let t1 = CGAffineTransform(translationX: firstTrack!.naturalSize.height,\n                y: -(firstTrack!.naturalSize.width - firstTrack!.naturalSize.height)\/2)\n        let t2 = t1.rotated(by: CGFloat.pi\/2)\n        let finalTransform: CGAffineTransform = t2\n        transformer.setTransform(finalTransform, at: CMTime.zero)\n         \n        instruction.layerInstructions = [transformer]\n        videoComposition.instructions = [instruction]\n         \n        \/\/\u83b7\u53d6\u5408\u5e76\u540e\u7684\u89c6\u9891\u8def\u5f84\n        let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory,\n                                                                .userDomainMask,true)[0]\n        let destinationPath = documentsPath + \"\/mergeVideo-\\(arc4random()%1000).mov\"\n        print(\"\u5408\u5e76\u540e\u7684\u89c6\u9891\uff1a\\(destinationPath)\")\n        let videoPath = URL(fileURLWithPath: destinationPath as String)\n        let exporter = AVAssetExportSession(asset: composition,\n                                            presetName:AVAssetExportPresetHighestQuality)!\n        exporter.outputURL = videoPath\n        exporter.outputFileType = AVFileType.mov\n        exporter.videoComposition = videoComposition \/\/\u8bbe\u7f6evideoComposition\n        exporter.shouldOptimizeForNetworkUse = true\n        exporter.timeRange = CMTimeRangeMake(\n            start: CMTime.zero,\n            duration: CMTimeMakeWithSeconds(Float64(duration), preferredTimescale: framesPerSecond))\n        exporter.exportAsynchronously(completionHandler: {\n            \/\/\u5c06\u5408\u5e76\u540e\u7684\u89c6\u9891\u4fdd\u5b58\u5230\u76f8\u518c\n            self.exportDidFinish(session: exporter)\n        })\n    }\n     \n    \/\/\u5c06\u5408\u5e76\u540e\u7684\u89c6\u9891\u4fdd\u5b58\u5230\u76f8\u518c\n    func exportDidFinish(session: AVAssetExportSession) {\n        print(\"\u89c6\u9891\u5408\u5e76\u6210\u529f\uff01\")\n        let outputURL = session.outputURL!\n        \/\/\u5c06\u5f55\u5236\u597d\u7684\u5f55\u50cf\u4fdd\u5b58\u5230\u7167\u7247\u5e93\u4e2d\n        PHPhotoLibrary.shared().performChanges({\n            PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: outputURL)\n        }, completionHandler: { (isSuccess: Bool, error: Error?) in\n            DispatchQueue.main.async {\n                \/\/\u91cd\u7f6e\u53c2\u6570\n                self.reset()\n                 \n                \/\/\u5f39\u51fa\u63d0\u793a\u6846\n                let alertController = UIAlertController(title: \"\u89c6\u9891\u4fdd\u5b58\u6210\u529f\",\n                                                        message: \"\u662f\u5426\u9700\u8981\u56de\u770b\u5f55\u50cf\uff1f\",\n                                                        preferredStyle: .alert)\n                let okAction = UIAlertAction(title: \"\u786e\u5b9a\", style: .default, handler: {\n                    action in\n                    \/\/\u5f55\u50cf\u56de\u770b\n                    self.reviewRecord(outputURL: outputURL)\n                })\n                let cancelAction = UIAlertAction(title: \"\u53d6\u6d88\", style: .cancel,\n                                                 handler: nil)\n                alertController.addAction(okAction)\n                alertController.addAction(cancelAction)\n                self.present(alertController, animated: true,\n                             completion: nil)\n            }\n        })\n    }\n     \n    \/\/\u89c6\u9891\u4fdd\u5b58\u6210\u529f\uff0c\u91cd\u7f6e\u5404\u4e2a\u53c2\u6570\uff0c\u51c6\u5907\u65b0\u89c6\u9891\u5f55\u5236\n    func reset() {\n        \/\/\u5220\u9664\u89c6\u9891\u7247\u6bb5\n        for assetURL in assetURLs {\n            if(FileManager.default.fileExists(atPath: assetURL)) {\n                do {\n                    try FileManager.default.removeItem(atPath: assetURL)\n                } catch _ {\n                }\n                print(\"\u5220\u9664\u89c6\u9891\u7247\u6bb5: \\(assetURL)\")\n            }\n        }\n         \n        \/\/\u8fdb\u5ea6\u6761\u8fd8\u539f\n        let subviews = progressBar.subviews\n        for subview in subviews {\n            subview.removeFromSuperview()\n        }\n         \n        \/\/\u5404\u4e2a\u53c2\u6570\u8fd8\u539f\n        videoAssets.removeAll(keepingCapacity: false)\n        assetURLs.removeAll(keepingCapacity: false)\n        appendix = 1\n        oldX = 0\n        stopRecording = false\n        remainingTime = totalSeconds\n    }\n     \n    \/\/\u5f55\u50cf\u56de\u770b\n    func reviewRecord(outputURL: URL) {\n        \/\/\u5b9a\u4e49\u4e00\u4e2a\u89c6\u9891\u64ad\u653e\u5668\uff0c\u901a\u8fc7\u672c\u5730\u6587\u4ef6\u8def\u5f84\u521d\u59cb\u5316\n        let player = AVPlayer(url: outputURL)\n        let playerViewController = AVPlayerViewController()\n        playerViewController.player = player\n        self.present(playerViewController, animated: true) {\n            playerViewController.player!.play()\n        }\n    }\n}<\/pre>\n\n\n\n<p>\u9644\uff1a\u5408\u5e76\u6210 mp4 \u683c\u5f0f\u7684\u89c6\u9891<\/p>\n\n\n\n<p>\u4e0a\u9762\u6837\u4f8b\u4e2d\uff0c\u6211\u4eec\u662f\u5148\u5f55\u5236\u597d\u00a0<strong>mov<\/strong>\u00a0\u683c\u5f0f\u7684\u89c6\u9891\u7247\u6bb5\uff0c\u7136\u540e\u5c06\u5b83\u4eec\u5408\u5e76\u6210\u4e00\u4e2a\u5b8c\u6574\u7684\u00a0<strong>mov<\/strong>\u00a0\u89c6\u9891\u3002\u5982\u679c\u60f3\u6700\u7ec8\u751f\u6210\u4e00\u4e2a\u00a0<strong>mp4<\/strong>\u00a0\u6587\u4ef6\uff0c\u53ea\u9700\u5728\u5408\u5e76\u65f6\u4fee\u6539\u4e0b\u8f93\u51fa\u683c\u5f0f\u548c\u6587\u4ef6\u540d\u5373\u53ef\uff08\u89c1\u4e0b\u65b9\u9ad8\u4eae\u5904\uff09\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/\u83b7\u53d6\u5408\u5e76\u540e\u7684\u89c6\u9891\u8def\u5f84\nlet documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory,\n                                                        .userDomainMask,true)[0]\nlet destinationPath = documentsPath + \"\/mergeVideo-\\(arc4random()%1000).mp4\"\nprint(\"\u5408\u5e76\u540e\u7684\u89c6\u9891\uff1a\\(destinationPath)\")\nlet videoPath = URL(fileURLWithPath: destinationPath as String)\nlet exporter = AVAssetExportSession(asset: composition,\n                                    presetName:AVAssetExportPresetHighestQuality)!\nexporter.outputURL = videoPath\nexporter.outputFileType = AVFileType.mp4<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u4e4b\u524d\u7684\u4e24\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4ecb\u7ecd\u4e86\u5982\u4f55\u901a\u8fc7\u00a0AVFoundation.framework\u00a0\u6846\u67b6\u63d0\u4f9b\u7684\u00a0AVCaptu [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,8],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/3335"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3335"}],"version-history":[{"count":2,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/3335\/revisions"}],"predecessor-version":[{"id":3342,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/3335\/revisions\/3342"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3335"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}