简体   繁体   English

在Python中不使用默认输出(播放)设备播放mp3文件

[英]Play mp3 file not using default output (playback) device in Python

I know that on PyAudio it is possible to choose on what playback device to stream on but PyAudio doesnt support mp3 files. 我知道在PyAudio上可以选择要在哪个播放设备上流式传输,但是PyAudio不支持mp3文件。 My script would work if one of the following problem was solved: 如果解决以下问题之一,我的脚本将起作用:

  1. Play mp3 file on PyAudio 在PyAudio上播放mp3文件

  2. Convert from mp3 to wav without without any data loss and without PyDub (with this method of converting, I found out that in a longer audio, the ends are missing) 从mp3转换为wav,没有任何数据丢失,也没有PyDub(通过这种转换方法 ,我发现在较长的音频中,末端丢失了)

  3. Somehow play sound from a mp3 file to different playback device (the device is not a default one) 以某种方式将声音从mp3文件播放到其他播放设备(该设备不是默认播放设备)

Personally I want to solve the first problem. 我个人想解决第一个问题。

I would be grateful if I can play mp3 file on PyAudio without any data loss or quality loss. 如果能在PyAudio上播放mp3文件而不造成任何数据丢失或质量损失,我将不胜感激。

EDIT: I found out that when I play my mp3 file (which is btw a file generated from Google Text To Speech) with PyMedia, the endings also gone missing for some strange reason so perhaps the converting method (point 2.) is working correctly but the problem is probably somewhere else. 编辑:我发现当我用PyMedia播放mp3文件(这是从Google Text To Speech生成的文件)时,由于某些奇怪的原因,尾部也丢失了,所以转换方法(第2点)可能正常工作但问题可能出在其他地方。

First of all, The link you mentioned in your answer is about pymedia and not pydub . 首先,您在答案中提到的链接是关于pymedia而非pydub

Next, I've converted many files from mp3 to wav using pydub without issues. 接下来,我已经使用pydub将许多文件从mp3转换为wav没有问题。

If you want to give it a try, here is an one liner code to covnert from mp3 to wav . 如果您想尝试一下,这里有一个将mp3wav线性代码。 You will be able to use rest of your code this way using option-2. 您可以使用option-2以这种方式使用其余代码。

from pydub import AudioSegment
AudioSegment.from_file("audio.mp3", format="mp3").export("audio.wav", format="wav")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM