How can I get the same bitrate of input and output file in pydub?

烂漫一生 提交于 2021-02-19 06:47:46

问题


I've use pydub to output a file(chop the file into shorter one), everything is great, but the bitrate has changed from 256k to 124k(why I will get this number instead 128k?). I know that AudioSegment has an argument to set bitrate, but I just want the same bitrate instead manually set every time. Any way to fix this issue?


回答1:


This has mainly to do with ffmpeg/avlib, but you can pass a flag to the AudioSegment().export() method to specify the bitrate you'd like:

from pydub import AudioSegment
from pydub.utils import mediainfo

source_file = "/path/to/sound.mp3"

original_bitrate = mediainfo(source_file)['bit_rate']
sound = AudioSegment.from_mp3(source_file)

sound.export("/path/to/output.mp3", format="mp3", bitrate=original_bitrate)



回答2:


I was unable to use the example above using the mediainfo object. I just found the way to calculate the bitrate for WAV files here and used that.

Translating it into python and pydub, and assuming the pydub object is called wav you would get that: bitrate = str((wav.frame_rate * wav.frame_width * 8 * wav.channels) / 1000)

Then you could pass it forward into the export function and not set it manually. Hope it helps :)



来源:https://stackoverflow.com/questions/33747728/how-can-i-get-the-same-bitrate-of-input-and-output-file-in-pydub

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!