如何解决如何解决pygame中编解码器错误不支持的'set_pos'?
我做了一个项目,在我的python脚本中需要'set_pos()'函数,但是它给出了编解码器不支持的错误。
这是我的代码:
#code
fast_forward_icon= tk.PhotoImage(file=__file__+ '/../images/JPEG,PNG/icons/fast_forward.png')
def fast_forward(event=None):
def func():
global progressbar_music
current_song_Length = mixer.music.get_pos()//1000
print(current_song_Length)
# mixer.music.pause()
f= current_song_Length+10
mixer.music.set_pos(f)
progressbar_music['value']=f
# mixer.music.progressbar_music(seconds=current_song_Length+10)
progressbar_music_starttime.configure(text='{}'.format(str(datetime.timedelta(seconds=f))))
progressbar_music.after(2,func)
func()
fastForwardBtn = Button(root,bg='#310A3D',image=fast_forward_icon,command=fast_forward)
我的错误:
Tkinter回调Traceback中的异常(最近一次调用最后一次):
文件 “ C:\ Users \ soham \ AppData \ Local \ Programs \ Python \ python36 \ lib \ tkinter_ init _。py”, 第1702行,在致电中 返回self.func(* args)文件“ C:\ Users \ soham \ AppData \ Local \ Programs \ Python \ python36 \ lib \ tkinter_ init _。py”, 呼叫中的746行 func(* args)在func中的文件“ e:/ python projects / MUSIC PLAYER / music_player.py”,第346行 mixer.music.set_pos(f)pygame.error:此编解码器不支持set_pos
解决方法
一种简单的方法是使用pydub
库,并通过python -m pip install pydub
进行一次安装。我编写的下一个函数接受MP3字节(或任何其他音频格式)并返回WAV字节(或由format = '...'
参数表示的任何其他格式),该函数不使用任何文件系统(所有操作均在内存中完成) )。
# Needs: python -m pip install pydub
# Convert
def conv_aud(data,format = 'wav'):
import pydub,io
inp,out = io.BytesIO(data),io.BytesIO()
pydub.AudioSegment.from_file(inp).export(out,format = format)
return out.getvalue()
# Test
with open('test.mp3','rb') as f:
print(conv_aud(f.read())[:32].hex().upper())
我的test.mp3
的输出:
5249464624D0110357415645666D7420100000000100020044AC000010B10200
另一种方法是像在下一个解决方案中一样使用FFMpeg。
我编写了下一个函数conv_aud()
,该函数能够将任何音频格式转换为任何其他格式,实际上它也可以转换视频。
默认情况下,如果未提供格式,则会将MP3转换为WAV。
该功能使用FFMpeg,请安装一次。另外,如果它位于PATH系统变量之外的dir内,则应向函数ffmpeg = 'c:/path/to/ffmpeg.exe'
提供参数conv_aud()
。
该函数可以接受输入音频的字节数据或输入文件的str路径,也可以接受打开以读取的任何类似文件的对象。此外,它接受ifmt
参数作为输入格式字符串,例如'mp3'
和/或接受ofmt
参数作为输出格式字符串,例如'wav'
。
函数返回WAV的字节数据。
函数使用中间临时目录和在退出时删除的文件。如果出现错误,此功能还可以打印从FFMpeg输出的错误。如果出现错误,则该功能处于静默状态,不打印任何内容。
代码末尾有test()
函数,用于测试使用test.mp3
文件作为输入的调用函数的不同方式。
# Converts any audio/video format (MP3 by default) to any other format (WAV by default).
# Input to function either "bytes" data or "str" path or read-opened "file" object.
# "ifmt" is input format (taken from input path by default or MP3 if not provided)
# "ofmt" is output format,WAV by default
# "ffmpeg" should contain path to ffmpeg executable
# Function returns converted audio bytes.
def conv_aud(data,ifmt = None,ofmt = 'wav',*,ffmpeg = 'ffmpeg'):
import tempfile,subprocess,secrets,traceback
inp_path = None
if type(data) is str:
inp_path,data = data,None
elif type(data) is not bytes:
data = data.read()
assert inp_path is not None or type(data) is bytes,(inp_path,type(data))
if inp_path is not None:
ifmt_ = inp_path[inp_path.rfind('.') + 1:].lower()
assert ifmt is None or ifmt.lower() == ifmt_,(ifmt,ifmt_)
ifmt = ifmt_
elif ifmt is None:
ifmt = 'mp3'
assert ifmt is not None and ofmt is not None,ofmt)
with tempfile.TemporaryDirectory() as td:
if data is not None:
inp_path = str(td) + '/' + secrets.token_hex(8).upper() + '.' + ifmt.lower()
with open(inp_path,'wb') as f:
f.write(data)
out_path = str(td) + '/' + secrets.token_hex(8).upper() + '.' + ofmt.lower()
try:
with open(str(td) + '/out','wb') as fout,open(str(td) + '/err','wb') as ferr:
subprocess.run([ffmpeg,'-i',inp_path,out_path],check = True,stdout = fout,stderr = ferr)
good = True
except:
with open(str(td) + '/out','rb') as fout,'rb') as ferr:
for f,n in [(fout,' stdout '),(ferr,' stderr ')]:
print('-' * 20 + n + '-' * 20 + '\n',f.read().decode('utf-8','replace'),sep = '',end = '')
print('-' * 50)
good = False
if not good:
assert False,'FFMpeg returned error!'
with open(out_path,'rb') as f:
return f.read()
# Testing conv_aud
def test():
for inp in [open('test.mp3','rb').read(),'test.mp3',open('test.mp3','rb')]:
print(conv_aud(inp)[:32].hex().upper())
if __name__ == '__main__':
test()
我的test.mp3
文件的输出:
5249464654D0110357415645666D7420100000000100020044AC000010B10200
5249464654D0110357415645666D7420100000000100020044AC000010B10200
5249464654D0110357415645666D7420100000000100020044AC000010B10200
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。