如何隐藏 'pydub' 输出信息?
How can I hide 'pydub' output information?
我的代码
from pydub import generators
from pydub.playback import play
play(generators.Sine(440).to_audio_segment(duration=1500))
在控制台输出中:
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
我的电脑目前有问题,所以我无法检查,但这应该可以:
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
这是代码中的一种 'hack',每次发送输出时都会清除控制台。
像这样的东西可能有用,但我还是没能测试它:
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))
我还没有找到解决方案,但我找到了除了“pydub”之外的三个替代方案。最后一个没有输出文本。 (数字 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)
见github上的请求讨论:https://github.com/jiaaro/pydub/issues/247(我觉得还是要做)。但是在 annaproxy 之后,您可以修改 pydub.playback 中的 _play_with_ffplay 以将 stdout 和 stderr 重定向到 devnull。
我的代码
from pydub import generators
from pydub.playback import play
play(generators.Sine(440).to_audio_segment(duration=1500))
在控制台输出中:
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
我的电脑目前有问题,所以我无法检查,但这应该可以:
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
这是代码中的一种 'hack',每次发送输出时都会清除控制台。 像这样的东西可能有用,但我还是没能测试它:
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))
我还没有找到解决方案,但我找到了除了“pydub”之外的三个替代方案。最后一个没有输出文本。 (数字 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)
见github上的请求讨论:https://github.com/jiaaro/pydub/issues/247(我觉得还是要做)。但是在 annaproxy 之后,您可以修改 pydub.playback 中的 _play_with_ffplay 以将 stdout 和 stderr 重定向到 devnull。