you can do something like I did by analyzing one frame to the next and thresholding the difference (I didn't scale the image down yet, and probably should)
// called everytime a frame is captured
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
guard let imageBufferRef = CMSampleBufferGetImageBuffer(sampleBuffer) else {
return
}
CVPixelBufferLockBaseAddress(imageBufferRef, []);
// set up for comparisons between prev and next images
if prevPB == nil {
prevPB = imageBufferRef
}
if (!isPlaying && motionIsDetected(prevPB, imageBufferRef) == 1) {
isPlaying = true
print("play video")
}
CVPixelBufferUnlockBaseAddress(imageBufferRef,[]);
// if true, play the video
prevPB = imageBufferRef
}
func pixelFrom(x: Int, y: Int, current: CVPixelBuffer) -> (UInt8, UInt8, UInt8) {
let baseAddress = CVPixelBufferGetBaseAddress(current)
let bytesPerRow = CVPixelBufferGetBytesPerRow(current)
let buffer = baseAddress!.assumingMemoryBound(to: UInt8.self)
let index = x*bytesPerRow+y
let b = buffer[index]
let g = buffer[index+1]
let r = buffer[index+2]
return (r, g, b)
}
func motionIsDetected(_ prev:CVPixelBuffer, _ current:CVPixelBuffer) -> Int {
var differences = 0
let baseAddress = CVPixelBufferGetBaseAddress(current)
let width = CVPixelBufferGetWidth(current)
let height = CVPixelBufferGetHeight(current)
// THRESHOLDING: clamp by abs(aa-bb) for tuple of r,b,g if 150 difference and at least 10 different pixels
var MAGIC_THRESHOLD = 120
var ENOUGH_DIFFERENCES = 10
if (current != nil && prev != nil) {
for x in 0..<height { //rows
for y in 0..<width { //cols
var setA = pixelFrom(x: x, y: y, current: prev)
var setB = pixelFrom(x: x, y: y, current: current)
if abs(Int(setA.0) - Int(setB.0)) > MAGIC_THRESHOLD && abs(Int(setA.1) - Int(setB.1)) > MAGIC_THRESHOLD && abs(Int(setA.2) - Int(setB.2)) > MAGIC_THRESHOLD {
differences = differences + 1
}
}
}
}
print(" difference" )
print(differences)
if differences > ENOUGH_DIFFERENCES {
return 1
}
return 0
}