Note

Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.

Why Join?

  • Expert Support: Solve post-sale issues and technical challenges with help from our community and team.

  • Learn & Share: Exchange tips and tutorials to enhance your skills.

  • Exclusive Previews: Get early access to new product announcements and sneak peeks.

  • Special Discounts: Enjoy exclusive discounts on our newest products.

  • Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.

πŸ‘‰ Ready to explore and create with us? Click [here] and join today!

6. Record Video

This example will guide you how to use the recording function.

Run the Code

cd ~/picrawler/examples
sudo python3 6_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

../_images/display.png

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 Ctrl+C.

Code

from time import sleep, strftime, localtime
from vilib import Vilib
import readchar
from os import getlogin
import os

USERNAME = getlogin()
VIDEO_PATH = f"/home/{USERNAME}/Videos/"

MANUAL = '''
Press keys on keyboard to control recording:
    Q: record/pause/continue
    E: stop
    Ctrl+C: Quit
'''

def print_overwrite(msg, end='', flush=True):
    """Overwrite the current terminal line."""
    print('\r\033[2K', end='', flush=True)
    print(msg, end=end, flush=True)

def safe_stop_recording():
    """Stop recording safely (avoid exceptions during exit)."""
    try:
        Vilib.rec_video_stop()
    except Exception:
        pass

def safe_close_camera():
    """Close camera safely (avoid exceptions during exit)."""
    try:
        Vilib.camera_close()
    except Exception:
        pass

def main():
    rec_flag = 'stop'  # Possible states: start, pause, stop
    vname = None

    # Ensure the video directory exists
    os.makedirs(VIDEO_PATH, exist_ok=True)

    # Set save path for recorded videos
    Vilib.rec_video_set["path"] = VIDEO_PATH

    # Start camera and preview
    Vilib.camera_start(vflip=False, hflip=False)
    Vilib.display(local=False, web=True)
    sleep(0.8)  # Wait for camera startup

    print(MANUAL)

    try:
        while True:
            # Read keyboard input (no Enter needed)
            key = readchar.readkey().lower()

            # Q: start / pause / continue
            if key == 'q':
                if rec_flag == 'stop':
                    rec_flag = 'start'

                    # Generate filename based on timestamp
                    vname = strftime("%Y-%m-%d-%H.%M.%S", localtime())
                    Vilib.rec_video_set["name"] = vname

                    # Start recording
                    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')

            # E: stop recording
            elif key == 'e' and rec_flag != 'stop':
                rec_flag = 'stop'
                safe_stop_recording()
                print_overwrite(
                    "The video saved as %s%s.avi" % (Vilib.rec_video_set["path"], vname),
                    end='\n'
                )

            # Ctrl+C (readchar special key): quit
            elif key == readchar.key.CTRL_C:
                print('\nquit')
                break

            sleep(0.1)

    except KeyboardInterrupt:
        # Handle Ctrl+C from terminal as well
        print('\nquit')

    finally:
        # If recording is still active, stop it before closing camera
        if rec_flag != 'stop':
            safe_stop_recording()
        safe_close_camera()
        sleep(0.1)

if __name__ == "__main__":
    main()

How it works?

  1. What This Program Does

    This program allows you to control video recording using your keyboard.

    • Q β†’ Start / Pause / Continue recording

    • E β†’ Stop recording

    • Ctrl+C β†’ Quit the program

    The recorded video will be saved in the Videos folder.

  2. Prepare the Video Folder

    USERNAME = getlogin()
    VIDEO_PATH = f"/home/{USERNAME}/Videos/"
    os.makedirs(VIDEO_PATH, exist_ok=True)
    

    The program finds your current username and creates a Videos folder if it does not already exist.

    All recorded videos will be saved here.

  3. Start the Camera

    Vilib.camera_start(vflip=False, hflip=False)
    Vilib.display(local=False, web=True)
    sleep(0.8)
    

    The camera is turned on. Web preview is enabled so you can watch the live stream in your browser.

    The short delay allows the camera to start properly.

  4. Recording State Setup

    rec_flag = 'stop'
    vname = None
    

    The program uses a variable called rec_flag to remember the current recording state:

    • stop β†’ not recording

    • start β†’ recording

    • pause β†’ paused

  5. Wait for Keyboard Input

    key = readchar.readkey().lower()
    

    The program waits for a key press.

  6. Press Q to Start Recording

    if rec_flag == 'stop':
        vname = strftime("%Y-%m-%d-%H.%M.%S", localtime())
        Vilib.rec_video_set["name"] = vname
        Vilib.rec_video_run()
        Vilib.rec_video_start()
    

    When you press Q for the first time:

    • A filename is generated using the current date and time

    • Recording starts immediately

    Example filename: 2026-03-03-15.30.21.avi

  7. Press Q Again to Pause

    elif rec_flag == 'start':
        Vilib.rec_video_pause()
    

    If recording is already running, pressing Q will pause the recording.

  8. Press Q Again to Continue

    elif rec_flag == 'pause':
        Vilib.rec_video_start()
    

    If recording is paused, pressing Q again will continue recording.

  9. Press E to Stop Recording

    elif key == 'e' and rec_flag != 'stop':
        Vilib.rec_video_stop()
    

    Pressing E will completely stop the recording.

    The video file will be saved in: /home/your_username/Videos/

  10. Exit the Program Safely

    finally:
        if rec_flag != 'stop':
            Vilib.rec_video_stop()
        Vilib.camera_close()
    

    When the program exits:

    • Recording is stopped (if still running)

    • The camera is closed safely

    This prevents broken video files or camera errors.