使用 pygame 混音器,我打开一个音频文件并对其进行操作。我找不到将“声音对象”保存到磁盘上的本地文件的方法。
sound_file = "output.mp3"
sound = pygame.mixer.Sound(sound_file)
有没有办法做到这一点?我一直在研究 pygame 混音器文档,但找不到与此相关的任何内容。
使用 pygame 混音器,我打开一个音频文件并对其进行操作。我找不到将“声音对象”保存到磁盘上的本地文件的方法。
sound_file = "output.mp3"
sound = pygame.mixer.Sound(sound_file)
有没有办法做到这一点?我一直在研究 pygame 混音器文档,但找不到与此相关的任何内容。
您的问题已经有将近两年的历史了,但如果人们仍在寻找答案:您可以使用wave
模块(本机 Python)保存 PyGame Sound 实例。
# create a sound from NumPy array of file
snd = pygame.mixer.Sound(my_sound_source)
# open new wave file
sfile = wave.open('pure_tone.wav', 'w')
# set the parameters
sfile.setframerate(SAMPLINGFREQ)
sfile.setnchannels(NCHANNELS)
sfile.setsampwidth(2)
# write raw PyGame sound buffer to wave file
sfile.writeframesraw(snd.get_buffer().raw)
# close file
sfile.close()
GitHub 上的更多信息和示例:https ://github.com/esdalmaijer/Save_PyGame_Sound 。
我从来没有尝试过这个,所以我只是猜测它可能会起作用。该pygame.mixer.Sound
对象有一个被调用的函数get_raw()
,它在 Python 3.x 中返回一个字节数组,在 Python 2.x 中返回一个字符串。我认为您也许可以使用该字节数组来保存声音。
http://www.pygame.org/docs/ref/mixer.html#pygame.mixer.Sound.get_raw
我希望它看起来像这样:
sound = pygame.mixer.Sound(sound_file)
... # your code that manipulates the sound
sound_raw = sound.get_raw()
file = open("editedsound.mp3", "w")
file.write(sound_raw)
file.close()
这不是答案。这是一条评论,因为我上面写的评论不清楚,因为我不知道如何使回车起作用。
我的评论是:上述解决方案不起作用。
这是使用 ipython 进行的测试的摘录。
In [23]: sound = pygame.mixer.Sound('FishPolka.mid')
In [24]: sr = sound.get_raw()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
E:\Documents and Settings\Me\Desktop\<ipython console> in <module>()
AttributeError: 'Sound' object has no attribute 'get_raw'
In [25]: sound.g
sound.get_buffer sound.get_length sound.get_num_channels sound.get_volume