我正试图使用PySide的Phonon模块播放一些.flac文件(如果有区别的话,可以在Mac上播放),但它不是可供播放的mimetype。有没有办法启用这个或者我需要安装一个插件?
您可以使用Pydub和Pyaudio 播放所有流行的音频格式,包括flac
示例代码:
#-*- coding: utf-8 -*-
from pydub import AudioSegment
from pydub.utils import make_chunks
from pyaudio import PyAudio
from threading import Thread
class Song(Thread):
def __init__(self, f, *args, **kwargs):
self.seg = AudioSegment.from_file(f)
self.__is_paused = True
self.p = PyAudio()
print self.seg.frame_rate
Thread.__init__(self, *args, **kwargs)
self.start()
def pause(self):
self.__is_paused = True
def play(self):
self.__is_paused = False
def __get_stream(self):
return self.p.open(format=self.p.get_format_from_width(self.seg.sample_width),
channels=self.seg.channels,
rate=self.seg.frame_rate,
output=True)
def run(self):
stream = self.__get_stream()
chunk_count = 0
chunks = make_chunks(self.seg, 100)
while chunk_count <= len(chunks):
if not self.__is_paused:
data = (chunks[chunk_count])._data
chunk_count += 1
else:
free = stream.get_write_available()
data = chr(0)*free
stream.write(data)
stream.stop_stream()
self.p.terminate()
song = Song("song.flac")
song.play()
Phonon不直接支持音频格式,而是使用底层操作系统功能。因此,答案取决于是否存在为mime类型audio/flac
注册的服务。对我来说,这里有一个简短的脚本示例:
from PySide import QtCore
from PySide.phonon import Phonon
if __name__ == '__main__':
app = QtCore.QCoreApplication([])
app.setApplicationName('test')
mime_types = Phonon.BackendCapabilities.availableMimeTypes()
print(mime_types)
app.quit()
这里有一系列修改,允许从任何一个文件夹中选择多首歌曲播放,限制格式以避免用户看到隐藏文件或选择非音频文件。请让我知道这是否是一个合适的帖子,并批评我的代码-如果你这样做,严厉但彻底。我是一个初学者。。。我在这里的第一个帖子@S.O.在python3.x:
#-*- coding: utf-8 -*-
from threading import Thread
from pyaudio import PyAudio
from pydub import *
from pydub.utils import make_chunks
from tkinter.filedialog import askopenfilenames
from tkinter import Tk
import time
class Song(Thread):
def __init__(self, f, *args, **kwargs):
self.seg = AudioSegment.from_file(f)
self.__is_paused = True
self.p = PyAudio()
print(self.seg.frame_rate)
Thread.__init__(self, *args, **kwargs)
self.start()
def pause(self):
self.__is_paused = True
def play(self):
self.__is_paused = False
def __get_stream(self):
return self.p.open(format=self.p.get_format_from_width(self.seg.sample_width),
channels=self.seg.channels,
rate=self.seg.frame_rate,
output=True)
def run(self):
stream = self.__get_stream()
chunk_count = 0
chunks = make_chunks(self.seg, 100)
while chunk_count < len(chunks) and not self.__is_paused:
data = (chunks[chunk_count])._data
chunk_count += 1
stream.write(data)
stream.stop_stream()
self.p.terminate()
Tk().withdraw()
filename = askopenfilenames(initialdir='/default/path', title="choose your file(s)",
filetypes=(("audio files", "*.flac *.wav *.mp3 *.ogg"), ("none", "")))
# with this logic there is a short gap b/w files - os time to process,
# trying to shorten gap by removing
# 1 second from sleep time... works ok but will be system status
# dependent and may not work across runs??
# Better would be to kill each tread when self._is_paused = True.
# I have a bunch of active threads piling up
for a in filename:
song = Song(a)
song.play()
songLength = song.seg.duration_seconds
time.sleep(songLength - 1)