Python Forum
When piping a FFMPEG stream to PyAudio, I get a "click" on every loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
When piping a FFMPEG stream to PyAudio, I get a "click" on every loop
#1
I am ultimately looking to do analysis on some streaming audio, but for now I am just trying to get it to play audio well as a first step.

I am generating a MP3 RTP steam on another device (sender) on my network. Using FFMPEG, I pipe that into python for playing via pyaudio. (receiver)

It does play on the receiver, but ever 1/2 second or so, you get a "click" noise that appear to occur on every loop of the while (True) loop. I setup PyAdio to use the callback function, as I thought it would help, but it still makes that noise. If I use FFPLAY to play the stream, it works fine.

Any input on how to make the audio play normally via pyadio? I did also try simpleaudio, pygame, and sounddevice with similar results. I tried to experiment by making a "buffer" of data, but couldn't make that work.

FFMPEG_BIN = "ffmpeg" # on Linux
#FFMPEG_BIN = "ffmpeg.exe" # on Windows
import pyaudio
import wave
import sys
import subprocess as sp
import time
import numpy

command = [ FFMPEG_BIN,
        '-i', 'rtp://172.168.1.231:8000/4',
        '-acodec', 'pcm_s32le',
        '-f', 's16le',
        '-ar', '44100', # ouput will have 44100 Hz
        '-ac', '2', # stereo (set to '1' for mono)
        '-']
pipe = sp.Popen(command, stdout=sp.PIPE, bufsize=10**8)

# instantiate PyAudio (1)
p = pyaudio.PyAudio()

# define callback (2)
def callback(in_data, frame_count, time_info, status):
    data = pipe.stdout.read(10000)
    data = numpy.frombuffer(data, dtype="int32")
    data = data.reshape((len(data)//2,2))
    #data = pipe.readbuffer(frame_count)
    return (data, pyaudio.paContinue)

# open stream using callback (3)
stream = p.open(format=pyaudio.paInt32,
                channels=2,
                rate=44100,
                output=True,
                stream_callback=callback)

# start the stream (4)
stream.start_stream()

# wait for stream to finish (5)
while stream.is_active():
    time.sleep(0.1)

# stop stream (6)
stream.stop_stream()
stream.close()


# close PyAudio (7)
p.terminate()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Returning data on button click by buttons created by a loop bradells 3 380 Apr-23-2025, 03:01 PM
Last Post: Pedroski55
  FFMPEG Leondagreatest 0 486 Feb-12-2025, 09:03 PM
Last Post: Leondagreatest
  Raspberry PI - PyAudio - Streaming to Web Page ultimatecodewarrior 2 924 Dec-25-2024, 10:54 AM
Last Post: pintailscratchy
  Get an FFMpeg pass to subprocess.PIPE to treat list as text file? haihal 2 1,065 Nov-21-2024, 11:48 PM
Last Post: haihal
  pyaudio seems to randomly halt input. elpidiovaldez5 2 1,427 Jan-22-2024, 09:07 AM
Last Post: elpidiovaldez5
  EEG stream data with mne and brainfolw PaulC 0 1,000 Aug-22-2023, 03:17 AM
Last Post: PaulC
  How to 'soft-embedd' subtitles with python-ffmpeg Pavel_47 2 3,897 Jul-04-2022, 12:33 PM
Last Post: Pavel_47
  Python Flask Realtime system printout (console) ffmpeg jttolleson 3 4,497 Apr-18-2022, 06:39 PM
Last Post: jttolleson
  MovieWriter ffmpeg unavailable; using Pillow instead. Joni_Engr 1 40,400 Aug-13-2021, 03:39 PM
Last Post: deanhystad
  Not getting response from pyaudio OceansBlue 1 3,766 Jul-03-2021, 06:22 AM
Last Post: OceansBlue

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020