guyver2
Posts: 33
Joined: Thu Feb 26, 2015 4:05 pm

record video and power off

Thu Apr 27, 2017 3:31 pm

For a project I'm working on I want to start recording video from the camera as soon as the pi boots up and stop when it's powered down.
The pi will be headless and hard shutdown (sudden power off) can happen anytime. I tried multiple solutions , some kind of work, but none of them is working reliably. All have the same issue, most output files ends up being 0kb...

This is what I tried :
raspivid

Code: Select all

 raspivid -w 640 -h 480 -f -t 0  -o foo.h264
result : empty file

raspivid and direct transcode

Code: Select all

 raspivid -w 640 -h 480 -f -t 0  -o - | avconv -r 30 -i - -vcodec copy foo.mp4
result : file exist but cannot be read by vlc|mplayer|anything


python script and picamera infinite loop
reciepe 4.10 http://picamera.readthedocs.io/en/relea ... -to-a-file

Code: Select all

import picamera

with picamera.PiCamera() as camera:
    camera.resolution = (640, 480)
    camera.start_recording('my_video.h264')
while True: # infinite loop
    pass
#camera.wait_recording(60)
#camera.stop_recording()
result : file is always 0kb in size

reciepe 4.12 http://picamera.readthedocs.io/en/relea ... iple-files

Code: Select all

import picamera

with picamera.PiCamera() as camera:
    camera.resolution = (640, 480)
    camera.start_recording('0.h264')
    camera.wait_recording(5)
    i=0
    while True : # infinite loop
        camera.split_recording('%d.h264' % i)
        camera.wait_recording(5)
        i+=1
    camera.stop_recording() # useless, never executed
result : multiple files, all of size 0kb

same with the second method

Code: Select all

import picamera
import sys

with picamera.PiCamera() as camera:
    camera.resolution = (640, 480)
    for filename in camera.record_sequence('%d.h264' % i for i in xrange(sys.maxint)):
        camera.wait_recording(5)
result : many files, the first ones actually contain video, but most of them are empty.



thoughts ?

ethanol100
Posts: 581
Joined: Wed Oct 02, 2013 12:28 pm

Re: record video and power off

Thu Apr 27, 2017 4:27 pm

I'm afraid, the only real solution is to not have power offs without shutting down the pi.
You can add a button to clean shut down and then cut the power.
File systems just don't work that way, you always risk to destroy the file system when not shut down correctly.

guyver2
Posts: 33
Joined: Thu Feb 26, 2015 4:05 pm

Re: record video and power off

Thu Apr 27, 2017 4:55 pm

Dammit, I kinda knew it but was hoping for an alternative solution.
thanks for the information

m4rt1n
Posts: 1
Joined: Tue Aug 29, 2017 6:55 pm

Re: record video and power off

Tue Aug 29, 2017 7:00 pm

The problem is that those files are not being flushed to disk. See:

https://stackoverflow.com/questions/712 ... h-is-doing

When recording to a sequence of files, an easy way around this is to flush each file after you're done recording to it. That way, on a power off, you'll only lose the last file.

Example:

Code: Select all

class FlushedFileOutput(object):
    def __init__(self, filename):
        self.out_file = io.open(filename, 'wb')

    def write(self, buf):
        self.out_file.write(buf)

    def flush(self):
        self.out_file.flush()
        os.fsync(self.out_file.fileno())

    def close(self):
        self.out_file.close()


def record(camera):
    print('Starting recording...')

    with PiCamera() as camera:
        for filename in generate_filenames():
            camera.start_recording(FlushedFileOutput(filename), format='h264')
            camera.wait_recording(RECORDING_LENGTH)
            camera.stop_recording()

thelullandtherush
Posts: 1
Joined: Wed Jan 17, 2018 12:53 am

Re: record video and power off

Wed Jan 17, 2018 12:58 am

This is a late reply, but I've just stumbled upon your post.

What if you record the video in a series of small files instead of one large one? For example, have your script record for five minutes, then pause, then record for another five minutes in a new file, etc. When your pi is powered off, only the latest file can be corrupted. It should be fairly easy to stitch the video segments back into a single file in editing software.

User avatar
Ace Rimmer
Posts: 104
Joined: Wed May 27, 2015 1:51 am
Location: Wit's End

Re: record video and power off

Mon Feb 05, 2018 4:37 am

Try using an interrupt to finish the recording (or stills capture) and then shutdown. If you're interested let me know and I'll try and find the code I used in a similar situation.
There must be an easier way!!!!!
I hope there is.....I haven't found it yet.

Return to “Camera board”