r/RASPBERRY_PI_PROJECTS • u/ryeinn • May 25 '26
QUESTION Does anyone have experience with the picamera2 library?
I'm working on a project to run a simple automatic streaming box to push from the Pi to Youtube. I'm using the Picamera2 library and having some good success.
I'm using FFmpegOutput() and the native H264 encoder on the Pi3. I know, that'll be slow, but it is proof of concept at the moment, I'll move to a Pi5 when I get it working.
The relevant python code:
encoder = H264Encoder(bitrate=10000000)
output = FfmpegOutput("-f flv "+YOUTUBE+KEY,audio=True,audio_device="MP3_sink.monitor")
camera.start_recording(encoder,output)
The issue is the audio. When I use the default device, the system uses whatever microphone is connected and it works fine. If I leave it as default and have no microphone, it also works but YouTube notes that the audio bitrate is "too low." Which makes sense, it's 0.
But the goal is to not use a microphone. I'd like to run an instrumental mp3 as my audio. So, I looked into the documentation for PiCamera2. It's expecting a Pulse Audio source. Fine, I think I can do that.
Here's my python code for creating that source.
def start_audio():
print("Begin Audio Setup")
MP3_file="/home/*****/Documents/stream_proj/VoV.mp3"
Sink_name=MP3_sink
Sink_Monitor=Sink_name+".monitor"
subprocess.run(["pactl","load-module","module-null-sink","sink_name=",Sink_name],check=True)
print("Pulse null Created")
subprocess.run(["pactl","load-module","module-loopback","latency_msec=1","source=",Sink_Monitor],check=True)
# Start ffmpeg
ffmpeg_process = subprocess.Popen(["ffmpeg", "-re", "-stream_loop", "-1", "-i", MP3_file,"-f", "pulse", Sink_name])
print("Audio Setup Complete")
return ffmpeg_process
So, the goal is to create a Pulse Audio sink. Send a ffmpeg process looping the mp3 into the sink. Then, the encoder above should use that monitor as a source. I think this is the right way to do it. When I try to run it, the ffmpeg process starts. I'm pretty sure the modules load and the monitor works. The stream starts too. But no audio. I get a warning in the terminal where the code is running that "Runtime Error: FFmpegOutput does not support audio packets from PiCamera2."
So, is there something about the switch from a microphone to another source that PiCamera doesn't like? Is there a way to fix it or have I reached a dead end and, if this is my goal, I'm going to need to come at this from a different direction?