pygame混音器将音频保存到磁盘?

2024-06-16 13:39:33 发布

您现在位置:Python中文网/ 问答频道 /正文

使用pygame混音器,我打开一个音频文件并对其进行操作。我找不到将“声音对象”保存到磁盘上的本地文件的方法。在

sound_file = "output.mp3"
  sound = pygame.mixer.Sound(sound_file)

有办法吗?我一直在研究pygame混音器文档,但我找不到任何与此相关的东西。在


Tags: 文件对象方法声音outputpygamemp3音频文件
3条回答

我从来没试过这个,所以我只是猜测它可能有用。pygame.mixer.Sound对象有一个名为get_raw()的函数,它在python3.x中返回一个字节数组,在python2.x中返回一个字符串。我想您可以使用这个字节数组来保存声音。在

{a1}

我想应该是这样的:

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

您的问题已经有将近两年的历史了,但是万一人们还在寻找答案:您可以使用wave模块(原生Python)来保存PyGame声音实例。在

# 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。在

相关问题 更多 >