gpt4 book ai didi

ios - Swift - 创建自定义捕获 session 预设

转载 作者:行者123 更新时间:2023-11-28 09:42:00 26 4
gpt4 key购买 nike

我想创建一个录制方形视频的自定义相机。然而,所有的捕获 session 预设都不是方形格式

例如:

  • 1920x1080

  • 1280x720

  • 640x480

无论如何我可以创建一个自定义预设。我仍然想要高质量的视频,但格式是方形的。最接近方形格式的是 640x480,但我仍然想要高清质量。

这可能吗,如果可能的话怎么办?谢谢

最佳答案

导入必要的框架:

import UIKit
import AVFoundation
import Photos

然后用需要的委托(delegate)定义你的类

class CameraViewController: UIViewController,
AVCaptureAudioDataOutputSampleBufferDelegate,
AVCaptureVideoDataOutputSampleBufferDelegate { }

您将需要下一个属性来捕获和保存音频和视频流

private var session: AVCaptureSession = AVCaptureSession()
private var deviceInput: AVCaptureDeviceInput?
private var previewLayer: AVCaptureVideoPreviewLayer?
private var videoOutput: AVCaptureVideoDataOutput = AVCaptureVideoDataOutput()
private var audioOutput: AVCaptureAudioDataOutput = AVCaptureAudioDataOutput()

private var videoDevice: AVCaptureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
private var audioConnection: AVCaptureConnection?
private var videoConnection: AVCaptureConnection?

private var assetWriter: AVAssetWriter?
private var audioInput: AVAssetWriterInput?
private var videoInput: AVAssetWriterInput?

private var fileManager: FileManager = FileManager()
private var recordingURL: URL?

private var isCameraRecording: Bool = false
private var isRecordingSessionStarted: Bool = false

private var recordingQueue = DispatchQueue(label: "recording.queue")

现在您可以从配置您的 AVCaptureSessionAVAssetWriter

开始
self.session.sessionPreset = AVCaptureSessionPresetHigh     
self.recordingURL = URL(fileURLWithPath: "\(NSTemporaryDirectory() as String)/file.mov")
if self.fileManager.isDeletableFile(atPath: self.recordingURL!.path) {
_ = try? self.fileManager.removeItem(atPath: self.recordingURL!.path)
}
self.assetWriter = try? AVAssetWriter(outputURL: self.recordingURL!,
fileType: AVFileTypeQuickTimeMovie)

描述音频和视频设置。例如,我们将制作全屏宽度的正方形

let audioSettings = [
AVFormatIDKey : kAudioFormatAppleIMA4,
AVNumberOfChannelsKey : 1,
AVSampleRateKey : 16000.0
] as [String : Any]

let videoSettings = [
AVVideoCodecKey : AVVideoCodecH264,
AVVideoWidthKey : UIScreen.main.bounds.width,
AVVideoHeightKey : UIScreen.main.bounds.width
] as [String : Any]

将此设置添加到 AVAssetWriterInput

self.videoInput = AVAssetWriterInput(mediaType: AVMediaTypeVideo,
outputSettings: videoSettings)
self.audioInput = AVAssetWriterInput(mediaType: AVMediaTypeAudio,
outputSettings: audioSettings)

self.videoInput?.expectsMediaDataInRealTime = true
self.audioInput?.expectsMediaDataInRealTime = true

if self.assetWriter!.canAdd(self.videoInput!) {
self.assetWriter?.add(self.videoInput!)
}

if self.assetWriter!.canAdd(self.audioInput!) {
self.assetWriter?.add(self.audioInput!)
}

AVCaptureDeviceInput 添加到您的 session 中

self.deviceInput = try? AVCaptureDeviceInput(device: self.videoDevice)
if self.session.canAddInput(self.deviceInput) {
self.session.addInput(self.deviceInput)
}

现在你可以将AVCaptureVideoPreviewLayer配置为正方形

self.previewLayer = AVCaptureVideoPreviewLayer(session: self.session)

//importent line of code what will did a trick
self.previewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill

let rootLayer = self.view.layer
rootLayer.masksToBounds = true
self.previewLayer?.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.width)

rootLayer.insertSublayer(self.previewLayer!, at: 0)
otLayer.insertSublayer(self.previewLayer!, at: 0)

开始 session

self.session.startRunning()

向 session 添加视频和音频输出

DispatchQueue.main.async {
self.session.beginConfiguration()

if self.session.canAddOutput(self.videoOutput) {
self.session.addOutput(self.videoOutput)
}

self.videoConnection = self.videoOutput.connection(withMediaType: AVMediaTypeVideo)
if self.videoConnection?.isVideoStabilizationSupported == true {
self.videoConnection?.preferredVideoStabilizationMode = .auto
}
self.session.commitConfiguration()

let audioDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeAudio)
let audioIn = try? AVCaptureDeviceInput(device: audioDevice)

if self.session.canAddInput(audioIn) {
self.session.addInput(audioIn)
}

if self.session.canAddOutput(self.audioOutput) {
self.session.addOutput(self.audioOutput)
}

self.audioConnection = self.audioOutput.connection(withMediaType: AVMediaTypeAudio)
}

至此,您的相机已正确配置。您需要实现下一个方法来录制视频和音频样本

func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer
sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!) {

if !self.isRecordingSessionStarted {
let presentationTime = CMSampleBufferGetPresentationTimeStamp(sampleBuffer)
self.assetWriter?.startSession(atSourceTime: presentationTime)
self.isRecordingSessionStarted = true
}

let description = CMSampleBufferGetFormatDescription(sampleBuffer)!

if CMFormatDescriptionGetMediaType(description) == kCMMediaType_Audio {
if self.audioInput!.isReadyForMoreMediaData {
print("appendSampleBuffer audio");
self.audioInput?.append(sampleBuffer)
}
} else {
if self.videoInput!.isReadyForMoreMediaData {
print("appendSampleBuffer video");
if !self.videoInput!.append(sampleBuffer) {
print("Error writing video buffer");
}
}
}

}

添加开始/停止录制的添加方法

private func startRecording() {
if self.assetWriter?.startWriting() != true {
print("error: \(self.assetWriter?.error.debugDescription ?? "")")
}

self.videoOutput.setSampleBufferDelegate(self, queue: self.recordingQueue)
self.audioOutput.setSampleBufferDelegate(self, queue: self.recordingQueue)
}

private func stopRecording() {
self.videoOutput.setSampleBufferDelegate(nil, queue: nil)
self.audioOutput.setSampleBufferDelegate(nil, queue: nil)

self.assetWriter?.finishWriting {
PHPhotoLibrary.shared().performChanges({
PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: self.recordingURL!)
}) { saved, error in
if saved {
let alertController = UIAlertController(title: "Your video was successfully saved", message: nil, preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
} else {
print(error.debugDescription)
}
}
print("saved")
}
}

现在您可以从IBAction函数开始/停止摄像头录制

@IBAction func recordingButton(_ sender: Any) {
if self.isCameraRecording {
self.stopRecording()
} else {
self.startRecording()
}
self.isCameraRecording = !self.isCameraRecording
}

这是 GitHub 示例:https://github.com/ChernyshenkoTaras/CustomCamera

关于ios - Swift - 创建自定义捕获 session 预设,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44917303/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com