# mjpeg_on_movement.py example: How long would a recording last?

I’m looking at the mjpeg_on_movement.py example.
I see that it triggers recording when the difference in the background image and the difference in the current image has happened for 10 frames (during that while loop).
What I don’t see immediately is a way to specify when the recording stops/ how long the recording lasts.
After running it awhile, it seems the recordings will last about 15sec … but is there a way to have recording stop if there’s no motion for a time?
(I’m not talking about turning off the camera… I saw the topic on that. I’d like to continue recording files/use a standard naming scheme in the order that the files are recorded).

I’m imagining something like

``````
while(diff):
img = sensor.snapshot()
img.difference("temp/bg.bmp")
stats = img.statistics()
if (stats[5] > 20):
diff -= 1

m = mjpeg.Mjpeg("example-%d.mjpeg" % pyb.rng())

clock = time.clock() # Tracks FPS.
print("You're on camera!")
for i in range(200):
clock.tick()
m.add_frame(sensor.snapshot())
print(clock.fps())

# the idea is that the current image, when there's no motion for a short period of time, would be approximately
# equal to the original background image recorded above (since the camera hasn't moved) ... for 10 frames

[i][b] if ( (current image - "temp/bg.bmp") < diff_threshold_value && recording_off_counter == 10)
recording_off_counter = 1 + recording_off_counter
break for loop [/b][/i]

m.close(clock.fps())
pyb.LED(BLUE_LED_PIN).off()
print("Restarting...")
``````

My question is about that sorta-psuedo code up there… is that the best way? Is there a better method I’m not thinking of?
Thank you!

Hi, this was a really early on script we wrote. Later scripts are better.

Use pyb.millis() and pyb.elapsed_millis() (see the libray on them) to change the inner for loop from just a random 200 frame value to actual time elasped.

As for your second request, you can do algorithms on frames before they are saved to the disk. Just run an algorithm on the image and break out of the loop before adding the frame if you don’t detect any motion happening. This will of course cost some FPS…

Make sure to close the MJPEG file before starting another.

Great. Thank you.

Is there a good way to parse the numbers appended by the regular expression in to a more readable stamp? (after the fact I mean since I already have some files recorded and need to verify recording order… sure I could use a different expression on future recordings.)

That’s not a regex. It’s a standard formatted string. Nor is that a timestamp but just a random number. If you want to order videos you should just print out a counter value. Note that you’ll want to find the file with the highest value first to initialize the counter.

So… List all the files in the direction, find the one with the highest value, set your counter to that, and then start saving by counter. Note that there’s a limit to you RAM for doing os.listdir… so, avoid making directories huge…

crud.

so, am I understanding that there’s not really a way to know the order after the files have been created?

? My previous answer listed how to do that. You just need a time source. The OpenMV Cam doesn’t have an RTC but you can use the millisecond time counter as a substitute for short times. Otherwise buy an I2C RTC.

Ok. It looks like I didn’t understand what you wrote.
Sorry for the confusion.