Record VideoΒΆ
This example will guide you how to use the recording function.
Run the Code
cd /home/pi/picrawler/examples
sudo python3 record_video.py
After the code runs, you can enter http://<your IP>:9000/mjpg
in the browser to view the video screen. such as: http://192.168.18.113:9000/mjpg

Recording can be stopped or started by pressing the keys on the keyboard.
Press
q
to begin recording or pause/continue,e
to stop recording or save.If you want to exit the program, press
esc
.
Code
from time import sleep,strftime,localtime
from vilib import Vilib
import readchar
manual = '''
Press keys on keyboard to control recording:
Q: record/pause/continue
E: stop
ESC: Quit
'''
def print_overwrite(msg, end='', flush=True):
print('\r\033[2K', end='',flush=True)
print(msg, end=end, flush=True)
def main():
rec_flag = 'stop' # start,pause,stop
vname = None
Vilib.rec_video_set["path"] = "/home/pi/Videos/" # set path
Vilib.camera_start(vflip=False,hflip=False)
Vilib.display(local=True,web=True)
sleep(0.8) # wait for startup
print(manual)
while True:
# read keyboard
key = readchar.readkey()
key = key.lower()
# start,pause
if key == 'q':
key = None
if rec_flag == 'stop':
rec_flag = 'start'
# set name
vname = strftime("%Y-%m-%d-%H.%M.%S", localtime())
Vilib.rec_video_set["name"] = vname
# start record
Vilib.rec_video_run()
Vilib.rec_video_start()
print_overwrite('rec start ...')
elif rec_flag == 'start':
rec_flag = 'pause'
Vilib.rec_video_pause()
print_overwrite('pause')
elif rec_flag == 'pause':
rec_flag = 'start'
Vilib.rec_video_start()
print_overwrite('continue')
# stop
elif key == 'e' and rec_flag != 'stop':
key = None
rec_flag = 'stop'
Vilib.rec_video_stop()
print_overwrite("The video saved as %s%s.avi"%(Vilib.rec_video_set["path"],vname),end='\n')
# quit
elif key == readchar.key.CTRL_C or key in readchar.key.ESCAPE_SEQUENCES:
Vilib.camera_close()
print('\nquit')
break
sleep(0.1)
if __name__ == "__main__":
main()
How it works?
Functions related to recording include the following:
Vilib.rec_video_run(video_name)
: Started the thread to record the video.video_name
is the name of the video file, it should be a string.Vilib.rec_video_start()
: Start or continue video recording.Vilib.rec_video_pause()
: Pause recording.Vilib.rec_video_stop()
: Stop recording.
Vilib.rec_video_set["path"] = "/home/pi/video/test/"
sets the storage location of video files.