Skip to content

Instantly share code, notes, and snippets.

@aoirint
Created March 20, 2020 03:44
Show Gist options
  • Save aoirint/583825b528f9013b4e741dff3904afc8 to your computer and use it in GitHub Desktop.
Save aoirint/583825b528f9013b4e741dff3904afc8 to your computer and use it in GitHub Desktop.
from pydub import *
import numpy as np
import time
# https://own-search-and-study.xyz/2017/11/19/numpy%E3%81%AEarray%E3%81%8B%E3%82%89pydub%E3%81%AEaudiosegment%E3%82%92%E4%BD%9C%E6%88%90%E3%81%99%E3%82%8B/
# https://maoudamashii.jokersounds.com/archives/song_kouichi_the_milky_way.html
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment