{"id":3326,"date":"2021-06-30T14:31:48","date_gmt":"2021-06-30T06:31:48","guid":{"rendered":"http:\/\/123.57.164.21\/?p=3326"},"modified":"2021-07-04T20:24:05","modified_gmt":"2021-07-04T12:24:05","slug":"swift-%e8%a7%86%e9%a2%91%e5%bd%95%e5%88%b6%e6%95%99%e7%a8%8b2%ef%bc%88%e5%b0%8f%e8%a7%86%e9%a2%91%e6%8b%8d%e6%91%84%ef%bc%8c%e5%b0%86%e5%a4%9a%e6%ae%b5%e8%a7%86%e9%a2%91%e8%bf%9b%e8%a1%8c%e5%90%88","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=3326","title":{"rendered":"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"},"content":{"rendered":"\n<p>\u5728\u524d\u9762\u4e00\u7bc7\u6587\u7ae0\uff1a<a rel=\"noreferrer noopener\" href=\"https:\/\/www.hangge.com\/blog\/cache\/detail_1184.html\" target=\"_blank\">Swift &#8211; \u89c6\u9891\u5f55\u5236\u7cfb\u5217\u6559\u7a0b1\uff08\u8c03\u7528\u6444\u50cf\u5934\u62cd\u6444\u5f55\u50cf\uff0c\u5e76\u4fdd\u5b58\u5230\u7cfb\u7edf\u76f8\u518c\uff09<\/a>\u3002\u6211\u4ecb\u7ecd\u4e86\u5982\u4f55\u901a\u8fc7&nbsp;<strong>AVFoundation.framework&nbsp;<\/strong>\u6846\u67b6\u63d0\u4f9b\u7684&nbsp;<strong>AVCaptureSession<\/strong>&nbsp;\u7c7b\u6765\u5b9e\u73b0\u89c6\u9891\u7684\u5f55\u5236\u3002\u5f53\u65f6\u7684\u7a0b\u5e8f\u662f\u70b9\u51fb\u201c\u5f00\u59cb\u201d\u6309\u94ae\u5c31\u5f00\u59cb\u89c6\u9891\u5f55\u5236\uff0c\u70b9\u51fb\u201c\u505c\u6b62\u201d\u5219\u5c06\u89c6\u9891\u4fdd\u5b58\u8d77\u6765\u3002\u6574\u4e2a\u89c6\u9891\u662f\u8fde\u7eed\u5730\u5f55\u5236\uff0c\u6ca1\u6709\u65f6\u95f4\u9650\u5236\u3002\u4eca\u5929\u7ee7\u7eed\u5728\u5176\u57fa\u7840\u4e4b\u4e0a\u505a\u4e2a\u6539\u8fdb\uff0c\u5b9e\u73b0\u5c0f\u89c6\u9891\u62cd\u6444\u529f\u80fd\u3002<\/p>\n\n\n\n<p><strong>1\uff0c\u5c0f\u89c6\u9891\u62cd\u6444\u8981\u5b9e\u73b0\u7684\u529f\u80fd<\/strong><\/p>\n\n\n\n<ul><li>\uff081\uff09\u89c6\u9891\u53ef\u4ee5\u5206\u6bb5\u5f55\u5236\u3002\u6309\u4f4f\u201c\u5f55\u50cf\u201d\u6309\u94ae\uff0c\u5219\u5f00\u59cb\u6355\u83b7\u6444\u50cf\u5934\u8fdb\u884c\u89c6\u9891\u5f55\u5236\uff0c\u653e\u5f00\u6309\u94ae\u5219\u6682\u505c\u5f55\u5236\u3002<\/li><li>\uff082\uff09\u6240\u6709\u89c6\u9891\u7247\u6bb5\u52a0\u8d77\u6765\u7684\u65f6\u95f4\u957f\u5ea6\u4f1a\u6709\u9650\u5236\uff08\u672c\u6837\u4f8b\u9650\u5236\u4e3a15\u79d2\uff09\u3002\u5f55\u5236\u7684\u65f6\u5019\u9876\u90e8\u4f1a\u6709\u5b9e\u65f6\u7684\u8fdb\u5ea6\u6761\u3002<\/li><li>\uff083\uff09\u70b9\u51fb\u201c\u4fdd\u5b58\u201d\u6309\u94ae\uff0c\u6216\u8005\u603b\u65f6\u957f\u5230\u8fbe15\u79d2\u65f6\uff0c\u5219\u505c\u6b62\u7ee7\u7eed\u5f55\u50cf\u3002\u7a0b\u5e8f\u4f1a\u5c06\u5404\u4e2a\u89c6\u9891\u7247\u6bb5\u8fdb\u884c\u5408\u5e76\uff0c\u5e76\u4fdd\u5b58\u5230\u7cfb\u7edf\u76f8\u518c\u4e2d\u3002<\/li><li>\uff084\uff09\u4fdd\u5b58\u6210\u529f\u540e\uff0c\u53ef\u4ee5\u56de\u770b\u751f\u6210\u7684\u5f55\u50cf\u3002<\/li><\/ul>\n\n\n\n<p><strong>2\uff0c\u6548\u679c\u56fe\u5982\u4e0b<\/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-89-1024x858.png\" alt=\"\" class=\"wp-image-3328\" width=\"534\" height=\"447\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-89-1024x858.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-89-300x251.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-89-768x643.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-89-830x695.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-89-230x193.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-89-350x293.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-89-480x402.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-89.png 1304w\" sizes=\"(max-width: 534px) 100vw, 534px\" \/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"857\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/06\/image-91-1024x857.png\" alt=\"\" class=\"wp-image-3330\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-91-1024x857.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-91-300x251.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-91-768x643.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-91-830x694.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-91-230x192.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-91-350x293.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-91-480x402.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-91.png 1310w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n\n<p><strong>3\uff0c\u5b9e\u73b0\u539f\u7406<\/strong>&#8216;<\/p>\n\n\n\n<ul><li>\uff081\uff09\u6bcf\u6b21\u6309\u4e0b\u6309\u94ae\u5f55\u5236\u7684\u89c6\u9891\u7247\u6bb5\uff0c\u540c\u6837\u662f\u4f7f\u7528 AVCaptureMovieFileOutput \u8f93\u51fa\u5230 Documents \u6587\u4ef6\u5939\u4e0b\uff0c\u547d\u540d\u4e3a output-1.mov\u3001output-2.mov\u2026.<\/li><li>\uff082\uff09\u901a\u8fc7&nbsp;<strong>AVMutableComposition<\/strong>&nbsp;\u6765\u62fc\u63a5\u5408\u6210\u5404\u4e2a\u89c6\u9891\u7247\u6bb5\u7684\u89c6\u9891\u3001\u97f3\u9891\u8f68\u9053\u3002<\/li><li>\uff083\uff09\u4f7f\u7528&nbsp;<strong>AVAssetExportSession<\/strong>&nbsp;\u5c06\u5408\u5e76\u540e\u7684\u89c6\u9891\u538b\u7f29\u8f93\u51fa\u6210\u4e00\u4e2a\u6700\u7ec8\u7684\u89c6\u9891\u6587\u4ef6\uff1a<strong>mergeVideo-****.mov<\/strong>\uff08\u672c\u6587\u4f7f\u7528\u9ad8\u8d28\u91cf\u538b\u7f29\uff09\uff0c\u5e76\u4fdd\u5b58\u5230\u7cfb\u7edf\u76f8\u518c\u4e2d\u53bb\u3002<\/li><li>\uff084\uff09\u901a\u8fc7&nbsp;<strong>AVPlayerViewController<\/strong>&nbsp;\u8fdb\u884c\u5f55\u50cf\u7684\u56de\u770b\u3002<\/li><\/ul>\n\n\n\n<p><strong>4\uff0c\u5f55\u5236\u4e00\u6bb5\u7531\u4e09\u4e2a\u7247\u6bb5\u5408\u6210\u7684\u89c6\u9891\uff0c\u63a7\u5236\u53f0\u4fe1\u606f\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-93-1024x891.png\" alt=\"\" class=\"wp-image-3332\" width=\"561\" height=\"487\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-93-1024x891.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-93-300x261.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-93-768x668.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-93-830x722.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-93-230x200.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-93-350x304.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-93-480x417.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-93.png 1074w\" sizes=\"(max-width: 561px) 100vw, 561px\" \/><\/figure><\/div>\n\n\n\n<p><strong>5\uff0cInfo.plist \u914d\u7f6e&nbsp;<\/strong><\/p>\n\n\n\n<p>\u7531\u4e8e\u82f9\u679c\u5b89\u5168\u7b56\u7565\u66f4\u65b0\uff0c\u5728\u4f7f\u7528&nbsp;<strong>Xcode8&nbsp;<\/strong>\u5f00\u53d1\u65f6\uff0c\u9700\u8981\u5728&nbsp;<strong>Info.plist<\/strong>&nbsp;\u914d\u7f6e\u8bf7\u6c42\u6444\u50cf\u5934\u3001\u9ea6\u514b\u98ce\u3001\u76f8\u518c\u6743\u9650\u7684\u63cf\u8ff0\u5b57\u6bb5\uff1a&nbsp;<\/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-94-1024x753.png\" alt=\"\" class=\"wp-image-3333\" width=\"548\" height=\"402\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-94-1024x753.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-94-300x221.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-94-768x565.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-94-830x610.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-94-230x169.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-94-350x257.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-94-480x353.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-94.png 1156w\" sizes=\"(max-width: 548px) 100vw, 548px\" \/><\/figure><\/div>\n\n\n\n<p><strong>6\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,\n                                            preferredTimescale: framesPerSecond)\n        self.fileOutput.maxRecordedDuration = maxDuration\n        \/\/ \u91cd\u8981 \u8bbe\u5b9a\u8f93\u51fa\u7684\u89c6\u9891MP4\u6587\u4ef6\u6a2a\u5c4f\n        \/\/ self.fileOutput.connection(with:AVMediaType.video)!.videoOrientation = .landscapeLeft\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        videoLayer.frame = self.view.bounds\n        videoLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill\n        \/\/ \u91cd\u8981 \u8bbe\u5b9a\u624b\u673a\u6a2a\u5c4f\u663e\u793a\n        \/\/ videoLayer.connection!.videoOrientation = AVCaptureVideoOrientation.landscapeLeft\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        \/\/\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       \/\/ let exporter = AVAssetExportSession(asset: composition,\n                                            presetName: AVAssetExportPreset3840x2160)!\n        exporter.outputURL = videoPath\n        exporter.outputFileType = AVFileType.mov\n        exporter.shouldOptimizeForNetworkUse = true\n        exporter.timeRange = CMTimeRangeMake(\n            start: CMTime.zero,\n            duration: CMTimeMakeWithSeconds(Float64(duration),\n                                            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<h5 class=\"wp-block-heading\">\u9644\uff1a\u5408\u5e76\u6210 mp4 \u683c\u5f0f\u7684\u89c6\u9891<\/h5>\n\n\n\n<p>\u4e0a\u9762\u6837\u4f8b\u4e2d\uff0c\u6211\u4eec\u662f\u5148\u5f55\u5236\u597d&nbsp;<strong>mov<\/strong>&nbsp;\u683c\u5f0f\u7684\u89c6\u9891\u7247\u6bb5\uff0c\u7136\u540e\u5c06\u5b83\u4eec\u5408\u5e76\u6210\u4e00\u4e2a\u5b8c\u6574\u7684&nbsp;<strong>mov<\/strong>&nbsp;\u89c6\u9891\u3002\u5982\u679c\u60f3\u6700\u7ec8\u751f\u6210\u4e00\u4e2a&nbsp;<strong>mp4<\/strong>&nbsp;\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=\"\">\/\/\u5408\u5e76\u89c6\u9891\u7247\u6bb5\nfunc 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    \/\/\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).mp4\"\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.mp4\n    exporter.shouldOptimizeForNetworkUse = true\n    exporter.timeRange = CMTimeRangeMake(\n        start: CMTime.zero,\n        duration: CMTimeMakeWithSeconds(Float64(duration),\n                                        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}<\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u91cd\u8981\uff1a\u53d1\u73b0\u7528ios\u5f55\u5236\u5b8c\u5bfc\u51fa\u7684\u89c6\u9891mov\u548cmp4\uff0c\u5728Chrome\u548c\u706b\u72d0\u6d4f\u89c8\u5668\u5e76\u4e0d\u80fd\u64ad\u653e\u51fa\u6765\uff0csafari\u6d4f\u89c8\u5668\u662f\u53ef\u4ee5\u7684\u3002<\/h5>\n\n\n\n<p>\u539f\u56e0\u662f\uff0cios\u5bfc\u51fa\u7684\u89c6\u9891\u9ed8\u8ba4\u662fmp4\u7684m4v\u683c\u5f0f\u7684\uff0c\u8fd9\u79cd\u683c\u5f0f\u4e00\u822c\u7684\u6d4f\u89c8\u5668\u90fd\u4e0d\u652f\u6301\uff0c\u9664\u4e86safari\u3002\u90a3\u4e48\u5982\u4f55\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u5462\uff0c\u53ef\u4ee5\u5728\u5bfc\u51fa\u89c6\u9891\u65f6\uff0c\u6307\u5b9a\u4e00\u4e0b\u53c2\u6570 <\/p>\n\n\n\n<p>Using&nbsp;<code>AVAssetExportPreset3840x2160<\/code>&nbsp;instead of&nbsp;<code>AVAssetExportPresetPassthrough<\/code><\/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=\"\">let exportSession: AVAssetExportSession = AVAssetExportSession.init(asset: asset2, presetName: AVAssetExportPreset3840x2160)!<\/pre>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/54178940\/video-converted-from-mov-to-mp4-on-ios-can-not-be-played-on-browser-etc\">https:\/\/stackoverflow.com\/questions\/54178940\/video-converted-from-mov-to-mp4-on-ios-can-not-be-played-on-browser-etc<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/developer.apple.com\/documentation\/avfoundation\/avassetexportsession\">https:\/\/developer.apple.com\/documentation\/avfoundation\/avassetexportsession<\/a><\/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\/07\/image-1-1024x440.png\" alt=\"\" class=\"wp-image-3359\" width=\"569\" height=\"244\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-1-1024x440.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-1-300x129.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-1-768x330.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-1-830x357.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-1-230x99.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-1-350x150.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-1-480x206.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-1.png 1414w\" sizes=\"(max-width: 569px) 100vw, 569px\" \/><\/figure><\/div>\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\/07\/image-1024x610.png\" alt=\"\" class=\"wp-image-3357\" width=\"594\" height=\"354\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-1024x610.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-300x179.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-768x457.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-830x494.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-230x137.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-350x208.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-480x286.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image.png 1528w\" sizes=\"(max-width: 594px) 100vw, 594px\" \/><\/figure><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u524d\u9762\u4e00\u7bc7\u6587\u7ae0\uff1aSwift &#8211; \u89c6\u9891\u5f55\u5236\u7cfb\u5217\u6559\u7a0b1\uff08\u8c03\u7528\u6444\u50cf\u5934\u62cd\u6444\u5f55\u50cf\uff0c\u5e76\u4fdd\u5b58\u5230\u7cfb\u7edf\u76f8\u518c\uff09\u3002\u6211\u4ecb\u7ecd [&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\/3326"}],"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=3326"}],"version-history":[{"count":6,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/3326\/revisions"}],"predecessor-version":[{"id":3363,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/3326\/revisions\/3363"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3326"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3326"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3326"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}