as the title stated I’d love some criticcritiqie, pointers or any improvement to my code.
My project was mainly to record a video, through a Flask Application (online), then save it and display it on the Flask Application. Will probably do it through Vimeo API or something.
I am using SSH to control my raspberry piRaspberry Pi through VSC.
The camera is a Logitech that is plugged in with a USB to Raspberry piPi. I tried recording both video and audio at the same time, but I ran into trouble doing that, that’s why I decided to split it into recording separately and combining it later.
One potential issue I have noticed is that depending on which USB port I use it can switch the audio output, but that can occur randomly as well, often when a reboot has occurred. My solution might be to disable the other audio outputs, but I don’t know yet. But this again hasn’t been an issue lately
The.
The current code is this:
import subprocess
import os
import time
# Directory of files saved
output_directory = "/home/pi/Videos"
# Start recording
def start_recording():
# Filenames for video and audio
video_filename = os.path.join(output_directory, "video.mp4")
audio_filename = os.path.join(output_directory, "audio.wav")
# Start record video
start_video = [
"ffmpeg",
"-thread_queue_size", "512",
"-f", "v4l2",
"-input_format", "mjpeg",
"-video_size", "1280x720",
"-framerate", "10",
"-i", "/dev/video0",
"-c:v", "libx264",
"-maxrate", "1000k",
"-bufsize", "128k",
"-y",
video_filename
]
# Start record audio
start_audio = [
"ffmpeg",
"-f", "alsa",
"-acodec", "pcm_s16le",
"-ac", "2",
"-ar", "44100",
"-i", "hw:3,0", # Can sometimes change to hw:1,0
"-y",
audio_filename
]
# Execute video and audio recording commands in the background
video_process = subprocess.Popen(start_video)
audio_process = subprocess.Popen(start_audio, stderr=subprocess.PIPE)
return video_process, audio_process, video_filename, audio_filename
# Stop recording
def stop_recording(video_process, audio_process, video_filename, audio_filename):
# Terminate video recording process
video_process.terminate()
# Terminate audio recording process
audio_process.terminate()
# Wait for the processes to finish
video_process.wait()
audio_process.wait()
# Combine video and audio
output_filename = os.path.join(output_directory, "output.mp4")
combine_command = [
"ffmpeg",
"-i", video_filename,
"-i", audio_filename,
"-c:v", "copy",
"-c:a", "aac",
"-strict", "-2",
"-y",
output_filename
]
subprocess.run(combine_command)
print("Recording stopped and files merged.")
# Start recording when needed (when a button is pressed)
video_process, audio_process, video_filename, audio_filename = start_recording()
# Sleep for a while (simulating the recording duration)
# (For now demo reasons. will be removed)
time.sleep(10) # Record for 10 seconds; adjust as needed
# Stop recording when needed (when a button is pressed)
stop_recording(video_process, audio_process, video_filename, audio_filename)
import subprocess
import os
import time
# Directory of files saved
output_directory = "/home/pi/Videos"
# Start recording
def start_recording():
# Filenames for video and audio
video_filename = os.path.join(output_directory, "video.mp4")
audio_filename = os.path.join(output_directory, "audio.wav")
# Start record video
start_video = [
"ffmpeg",
"-thread_queue_size", "512",
"-f", "v4l2",
"-input_format", "mjpeg",
"-video_size", "1280x720",
"-framerate", "10",
"-i", "/dev/video0",
"-c:v", "libx264",
"-maxrate", "1000k",
"-bufsize", "128k",
"-y",
video_filename
]
# Start record audio
start_audio = [
"ffmpeg",
"-f", "alsa",
"-acodec", "pcm_s16le",
"-ac", "2",
"-ar", "44100",
"-i", "hw:3,0", # Can sometimes change to hw:1,0
"-y",
audio_filename
]
# Execute video and audio recording commands in the background
video_process = subprocess.Popen(start_video)
audio_process = subprocess.Popen(start_audio, stderr=subprocess.PIPE)
return video_process, audio_process, video_filename, audio_filename
# Stop recording
def stop_recording(video_process, audio_process, video_filename, audio_filename):
# Terminate video recording process
video_process.terminate()
# Terminate audio recording process
audio_process.terminate()
# Wait for the processes to finish
video_process.wait()
audio_process.wait()
# Combine video and audio
output_filename = os.path.join(output_directory, "output.mp4")
combine_command = [
"ffmpeg",
"-i", video_filename,
"-i", audio_filename,
"-c:v", "copy",
"-c:a", "aac",
"-strict", "-2",
"-y",
output_filename
]
subprocess.run(combine_command)
print("Recording stopped and files merged.")
# Start recording when needed (when a button is pressed)
video_process, audio_process, video_filename, audio_filename = start_recording()
# Sleep for a while (simulating the recording duration)
# (For now demo reasons. will be removed)
time.sleep(10) # Record for 10 seconds; adjust as needed
# Stop recording when needed (when a button is pressed)
stop_recording(video_process, audio_process, video_filename, audio_filename)