pydub numpy
from pydub import *
import numpy as np
import time
path = 'song_kouichi_the_milky_way.m4a'
sound = AudioSegment.from_file(path, format='m4a')
samples = np.array(sound.get_array_of_samples())
print(path)
print('Sample width (Num of bytes of a sample):', sound.sample_width)
print('Frame rate (Num of samples per second):', sound.frame_rate)
print('Channels (Stereo/Mono):', sound.channels)
print('Shape (Length):', samples.shape)
print('Type:', samples.dtype)
print('Min/Max:', samples.min(), samples.max())
output = AudioSegment(
samples.astype('int32').tobytes(),
sample_width=4,
frame_rate=44100,
channels=2,
)
ts = time.time()
output.export('output.m4a')
elapsed = time.time() - ts
print('Exported as m4a: %f s' % elapsed)
ts = time.time()
output.export('output.mp3')
elapsed = time.time() - ts
print('Exported as mp3: %f s' % elapsed)
ts = time.time()
output.export('output.wav')
elapsed = time.time() - ts
print('Exported as wav: %f s' % elapsed)
song_kouichi_the_milky_way.m4a
Sample width (Num of bytes of a sample): 2
Frame rate (Num of samples per second): 44100
Channels (Stereo/Mono): 2
Shape (Length): (22339584,)
Type: int16
Min/Max: -32768 32767
Exported as m4a: 6.288757 s
Exported as mp3: 6.194534 s
Exported as wav: 6.064215 s
- 出力時間はフォーマットによって変わらない(誤差の範囲)