Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could export mp3 file with waveform data and sample rate? #352

Open
ucasiggcas opened this issue Dec 31, 2018 · 6 comments
Open

Could export mp3 file with waveform data and sample rate? #352

ucasiggcas opened this issue Dec 31, 2018 · 6 comments

Comments

@ucasiggcas
Copy link

Dear,
Could the project export mp3 file,using waveform data and sample rate?
Any advice and suggestion will be good.
Thx

@jooshua-inglis
Copy link

In order to do it properly you'lll need the number of channels and the sample width (in bytes). Generally the sample width will be an 16 bit, which is two bytes long, but can vary.
In the following example I have extracted the waveform data from file.raw, and set the sample sample width to 2 (16 bit), the sample rate to 44100 and number of channels to 1.

audio = pydub.AudioSegment.from_raw('file.raw', sample_width=2, frame_rate=44100, channels=1)
audio.export('output_file.mp3, format='mp3')

@ucasiggcas
Copy link
Author

ucasiggcas commented Jan 9, 2019

In order to do it properly you'lll need the number of channels and the sample width (in bytes). Generally the sample width will be an 16 bit, which is two bytes long, but can vary.
In the following example I have extracted the waveform data from file.raw, and set the sample sample width to 2 (16 bit), the sample rate to 44100 and number of channels to 1.

audio = pydub.AudioSegment.from_raw('file.raw', sample_width=2, frame_rate=44100, channels=1)
audio.export('output_file.mp3, format='mp3')

Dear,
Thanks,
but if I only have waveform data, sample_width, frame_rate and channels,
Could I export mp3 file directly?
Thx a lot

@shidephen
Copy link

shidephen commented Jan 24, 2019

# vo is a int array of samples
a = AudioSegment(vo, frame_rate=32000, sample_width=2,  channels=1)
a.export('a.mp3', format='mp3')

This may help?

@ucasiggcas
Copy link
Author

# vo is a int array of samples
a = AudioSegment(vo, frame_rate=32000, sample_width=2,  channels=1)
a.export('a.mp3', format='mp3')

This may help?

thanks,
but how to convert to int array decode by librosa.load
here is the wav file
12.zip
you can use it after unzip
the code is
import pydub,librosa
path='12.wav'
y,sr=librosa.load(path,sr=None)
audio=pydub.AudioSegment(data=y,frame_rate=sr,sample_width=2,channels=1)
audio.export('12.mp3',format='mp3')

Note:
**Please Do Not use pydub convert it or export mp3 directly **

Thx

@shidephen
Copy link

Because load returns an ndarray, we can convert y by multipy its bit depth, for example 2**16。Then convert to int like following code

yint = (y * (2**16)).astype(np.int16)
audio = pydub.AudioSegment(yint, ...)

@ucasiggcas
Copy link
Author

Because load returns an ndarray, we can convert y by multipy its bit depth, for example 2**16。Then convert to int like following code

yint = (y * (2**16)).astype(np.int16)
audio = pydub.AudioSegment(yint, ...)

多谢大佬(都是中国人还是汉语直接点)
按理说我的wav是16位深度的,经过您的代码后却是得到32位深度,这让我一脸懵逼,
请教下这是为啥?

图1:wav信息与mp3信息对比,谱图是mp3的,与图2wav的谱图有差别
image

图2:wav谱图
image

请大佬指点一二,多谢!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants