1

My Code

from pydub import generators
from pydub.playback import play

play(generators.Sine(440).to_audio_segment(duration=1500))

In the console output:

Input #0, wav, from '/var/folders/_7/0q83l2vn4zjd7zgqpy3v97840000gn/T/tmphlm6i9s_.wav':
Duration: 00:00:01.50, bitrate: 705 kb/s
Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 44100 Hz, 1 channels, s16, 705 kb/s
Birkan
  • 121
  • 2
  • 9

4 Answers4

1

I find none of the answers mentioned above work.

I finally managed to hide the pydub playback output by using a subprocess.

If you are interested, I implemented at:

https://github.com/eliranwong/UniqueBible/blob/main/util/RemoteCliMainWindow.py#L287

eliranwong
  • 21
  • 2
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/33961695) – Niklas Mohrin Mar 07 '23 at 21:32
0

I'm currently having a problem with my computer, so I'm not able to check this, but this should work:

from pydub import generators
from pydub.playback import play
import subprocess, sys

play(generators.Sine(440).to_audio_segment(duration=1500))
if sys.platform in ('linux', 'osx'):
    subprocess.call('clear', shell=True)
elif sys.platform in ('nt', 'dos', 'ce'):
    subprocess.call('cls', shell=True)
else:
    pass

This is kind-of a 'hack' in the code that just clears console every time an output is sent. Something like this may work, but again, I haven't been able to test it:

import contextlib
import io

from pydub import generators
from pydub.playback import play

with contextlib.redirect_stdout(io.StringIO()):
    play(generators.Sine(440).to_audio_segment(duration=1500))
StevenHarvey
  • 126
  • 5
  • I tried it before but doesn't work:/ Thanks anyway – Birkan Apr 11 '21 at 04:21
  • Sorry to hear it didn't work, if anything: you can switch away from pydub. Pydub should give it an option to hide the data. Hope everything works out! – StevenHarvey Apr 11 '21 at 04:23
  • Comment from GreyAlien502- If you install simpleaudio (e.g. run pip install simpleaudio), it should play audio without emitting any text. N.B. This output is coming from ffplay, which is a used because simpleaudio is not installed. We don't currently have any way to suppress output from ffplay. We would need to pass the arguments -loglevel -8 to the ffplay call. Arguably, this should be the default. – Birkan Sep 07 '21 at 03:38
0

I didn't find a solution yet, but I found besides "pydub" three alternatives. The last one is without output text. (Number-4)

import time
from gtts import gTTS
tts = gTTS('Test', lang='en')
tts.save('Test.mp3')

#=================1
print("1")
from pygame import mixer
mixer.init()
mixer.music.load("Test.mp3") 
mixer.music.play()
time.sleep(1)

#=================2
print("2")
from pydub import AudioSegment
from pydub.playback import play
path_to_file = r"Test.mp3"
song = AudioSegment.from_mp3(path_to_file)
play(song)
time.sleep(1)

#=================3
print("3")
import os
os.system("mpg321 -q Test.mp3")
time.sleep(1)

#=================4
print("4")
from mpyg321.mpyg321 import MPyg321Player
player = MPyg321Player()
player.play_song("Test.mp3")
time.sleep(1)
Birkan
  • 121
  • 2
  • 9
0

See the request discussion on github: https://github.com/jiaaro/pydub/issues/247 (I think it's still to do). But following annaproxy you can modify _play_with_ffplay in pydub.playback to redirect stdout and stderr to devnull.

Dharman
  • 30,962
  • 25
  • 85
  • 135
rammy
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 26 '22 at 19:26