Oct-20-2024, 10:10 AM
Can try this.
Make sure ffmpeg is installed and accessible in your system's PATH.
Script assumes that the incoming audio data is in f32le format with a sample rate of 44100 Hz and mono channel,
matching your original ffmpeg command.
Make sure ffmpeg is installed and accessible in your system's PATH.
Script assumes that the incoming audio data is in f32le format with a sample rate of 44100 Hz and mono channel,
matching your original ffmpeg command.
import socket import subprocess UDP_IP = "0.0.0.0" UDP_PORT = 3000 ffmpeg_cmd = [ 'ffmpeg', '-y', # Overwrite output files without asking '-f', 'f32le', # Input format: 32-bit float little-endian PCM '-ar', '44100', # Input sample rate: 44100 Hz '-ac', '1', # Input channels: 1 (mono) '-i', 'pipe:', # Input comes from a pipe '/home/xy/microphone.wav' ] ffmpeg_proc = subprocess.Popen(ffmpeg_cmd, stdin=subprocess.PIPE) sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.bind((UDP_IP, UDP_PORT)) print(f"Listening on UDP port {UDP_PORT}") try: while True: data, addr = sock.recvfrom(4096) # Adjust buffer size if needed ffmpeg_proc.stdin.write(data) except KeyboardInterrupt: print("Interrupted by user. Closing...") finally: ffmpeg_proc.stdin.close() ffmpeg_proc.wait() sock.close()