如何在Python中只将字节对象(raw.ogg声音文件)的“x”量写入文件

2024-09-28 05:24:30 发布

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

我有一个脚本,可以根据HTTP请求返回的byte对象将音符写入目录。我遇到的问题是.ogg声音文件是5秒长,理想情况下我想缩短到0.5秒。这是否可以通过简单地删除byte对象的块来实现? 我知道通过pysoundfile可以使用帧和采样率来计算持续时间,从而写入x帧。这只适用于静态速率,但是由于提取的是原始形式的音符,因此这些文件的采样速率未知

下面是我写的一些代码

        for notenumbers in range(48, 64+1):
            note = requests.get(url.format(instrument, notenumbers))
            notebinary = note.content
            time.sleep(3)
            with open("E:\\useraccount\\x\\x\\"+str(dirname)+"\\"+str(instrname)+"\\"+str(instrname) +"-" +str(notenumbers) +".ogg", "wb") as o:
                print("Creating file named: " +str(instrname) +":" +str(notenumbers) +".ogg")
                o.write(notebinary)

如果你能帮忙的话,谢谢你


Tags: 对象目录脚本http速率bytenote音符

热门问题