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
  pyaudio seems to randomly halt input. elpidiovaldez5 2 383 Jan-22-2024, 09:07 AM
Last Post: elpidiovaldez5
  Microphone stream manipulation Talking2442 1 2,747 Nov-19-2023, 02:08 PM
Last Post: palumanic
  EEG stream data with mne and brainfolw PaulC 0 494 Aug-22-2023, 03:17 AM
Last Post: PaulC
  How to 'soft-embedd' subtitles with python-ffmpeg Pavel_47 2 2,115 Jul-04-2022, 12:33 PM
Last Post: Pavel_47
  Python Flask Realtime system printout (console) ffmpeg jttolleson 3 2,950 Apr-18-2022, 06:39 PM
Last Post: jttolleson
  MovieWriter ffmpeg unavailable; using Pillow instead. Joni_Engr 1 29,396 Aug-13-2021, 03:39 PM
Last Post: deanhystad
  Not getting response from pyaudio OceansBlue 1 2,658 Jul-03-2021, 06:22 AM
Last Post: OceansBlue
  Decoding a serial stream AKGentile1963 7 8,607 Mar-20-2021, 08:07 PM
Last Post: deanhystad
  help with PyAudio Leo12143 1 1,960 Jan-18-2021, 09:56 AM
Last Post: DT2000
  pyAudio playing variable Talking2442 3 3,041 Dec-01-2020, 06:20 PM
Last Post: Talking2442

Forum Jump:

User Panel Messages

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