30秒分のビデオを録画してから、AVQueuePlayer
にエンキューしてシームレスに再生することで、必要な機能を実現できます。ビデオチャンクを記録することはMacOSでAVCaptureFileOutput
で非常に簡単になりますが、悲しいことに、iOSの上であなたはコマ落ちすることなく、新しいチャンクを作成することはできませんので、あなたはwordier、低いレベルAVAssetWriter
APIを使用する必要があります。
import UIKit
import AVFoundation
// TODO: delete old videos
// TODO: audio
class ViewController: UIViewController {
// capture
let captureSession = AVCaptureSession()
// playback
let player = AVQueuePlayer()
var playerLayer: AVPlayerLayer! = nil
// output. sadly not AVCaptureMovieFileOutput
var assetWriter: AVAssetWriter! = nil
var assetWriterInput: AVAssetWriterInput! = nil
var chunkNumber = 0
var chunkStartTime: CMTime! = nil
var chunkOutputURL: URL! = nil
override func viewDidLoad() {
super.viewDidLoad()
playerLayer = AVPlayerLayer(player: player)
view.layer.addSublayer(playerLayer)
// inputs
let videoCaptureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
let videoInput = try! AVCaptureDeviceInput(device: videoCaptureDevice)
captureSession.addInput(videoInput)
// outputs
// iOS AVCaptureFileOutput/AVCaptureMovieFileOutput still don't support dynamically
// switching files (?) so we have to re-implement with AVAssetWriter
let videoOutput = AVCaptureVideoDataOutput()
// TODO: probably something else
videoOutput.setSampleBufferDelegate(self, queue: DispatchQueue.main)
captureSession.addOutput(videoOutput)
captureSession.startRunning()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
playerLayer.frame = view.layer.bounds
}
func createWriterInput(for presentationTimeStamp: CMTime) {
let fileManager = FileManager.default
chunkOutputURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("chunk\(chunkNumber).mov")
try? fileManager.removeItem(at: chunkOutputURL)
assetWriter = try! AVAssetWriter(outputURL: chunkOutputURL, fileType: AVFileTypeQuickTimeMovie)
// TODO: get dimensions from image CMSampleBufferGetImageBuffer(sampleBuffer)
let outputSettings: [String: Any] = [AVVideoCodecKey:AVVideoCodecH264, AVVideoWidthKey: 1920, AVVideoHeightKey: 1080]
assetWriterInput = AVAssetWriterInput(mediaType: AVMediaTypeVideo, outputSettings: outputSettings)
assetWriterInput.expectsMediaDataInRealTime = true
assetWriter.add(assetWriterInput)
chunkNumber += 1
chunkStartTime = presentationTimeStamp
assetWriter.startWriting()
assetWriter.startSession(atSourceTime: chunkStartTime)
}
}
extension ViewController: AVCaptureVideoDataOutputSampleBufferDelegate {
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!) {
let presentationTimeStamp = CMSampleBufferGetPresentationTimeStamp(sampleBuffer)
if assetWriter == nil {
createWriterInput(for: presentationTimeStamp)
} else {
let chunkDuration = CMTimeGetSeconds(CMTimeSubtract(presentationTimeStamp, chunkStartTime))
if chunkDuration > 30 {
assetWriter.endSession(atSourceTime: presentationTimeStamp)
// make a copy, as finishWriting is asynchronous
let newChunkURL = chunkOutputURL!
let chunkAssetWriter = assetWriter!
chunkAssetWriter.finishWriting {
print("finishWriting says: \(chunkAssetWriter.status.rawValue, chunkAssetWriter.error)")
print("queuing \(newChunkURL)")
self.player.insert(AVPlayerItem(url: newChunkURL), after: nil)
self.player.play()
}
createWriterInput(for: presentationTimeStamp)
}
}
if !assetWriterInput.append(sampleBuffer) {
print("append says NO: \(assetWriter.status.rawValue, assetWriter.error)")
}
}
}
P.S.をあなたが30秒前に何をしていたのかを知ることは非常に興味があります。あなたは何を正確に作っていますか?
[ビデオとビデオを同時に再生する](https://stackoverflow.com/questions/7707427/record-video-and-play-video-at-the-same-time)の可能な複製 –