С первого раза все работает хорошо. Проблема в том, что после сохранения первого файла цикл перезаписывает первый файл, а не создает новый.
Код:
Код: Выделить всё
#!/usr/bin/env python3
import time
import os
import subprocess
import picamera2
import datetime
import vlc
from picamera2.encoders import H264Encoder
from gpiozero import MotionSensor
# Set paths
video_path = '/home/path-to-file/test.mp4'
recording_folder = '/home/path-to-folder/Recordings/'
motion_sensor_pin = 4 # When the motion sensor is connected to GPIO pin 4
# Setup motion sensor and VLC player
motion_sensor = MotionSensor(motion_sensor_pin)
player = vlc.MediaPlayer()
# Setup Picamera2
picam2 = picamera2.Picamera2()
video_config = picam2.create_video_configuration()#main={"size":(1280,720)})
picam2.configure(video_config)
picam2.rotation = 180
encoder= H264Encoder(10000000)
output_video = recording_folder + datetime.datetime.now().strftime('%Y-%m-%d_%H.%M.%S.h264')
# Function to rotate and display the first frame of the video
def show_video_first_frame():
player.set_media(vlc.Media(video_path)) # Rotate video 90 degrees clockwise
player.set_fullscreen(False) # Ensure it's not fullscreen
player.play() # Wait for the first frame
time.sleep(.1) # Wait a bit for the video to load and render the first frame
player.pause()
# Function to record a 15-second video using Picamera2
def record_video():
picam2.start_recording(encoder, output_video)
time.sleep(15) # Record for 15 seconds
picam2.stop_recording()
# Main loop
if __name__ == "__main__":
print("Starting system...")
while True:
try:
# Show first frame of the video and wait for motion
show_video_first_frame()
print("Waiting for motion...")
motion_sensor.wait_for_motion()
print("Motion detected!")
# When motion is detected, start the video and record from Picamera2
print("Playing video and recording...")
player.set_mrl(video_path)
player.play()
record_video()
print(f"Video done after 15 seconds, recording saved as {output_video}")
except KeyboardInterrupt:
break
except:
continue
Есть ли способ сохранить файл, созданный в цикле с датой и временем?
Подробнее здесь: https://stackoverflow.com/questions/791 ... nt-in-loop