Skip to content

Instantly share code, notes, and snippets.

@andreinechaev
Created July 19, 2018 13:52
Show Gist options
  • Save andreinechaev/10a82852457c4173b7c4b6ee2075b8f2 to your computer and use it in GitHub Desktop.
Save andreinechaev/10a82852457c4173b7c4b6ee2075b8f2 to your computer and use it in GitHub Desktop.
An example how to chunk audio with Python and pydub
from pydub import AudioSegment
from pydub.utils import make_chunks
import csv
import os
from pprint import pprint
chunk_s = 2000
natives = {}
with open('speakers_all.csv', 'r') as f:
reader = csv.reader(f)
next(reader) # skipting header
for row in reader:
native = row[4]
filename = row[3]
fn = 'recordings/' + filename + '.wav'
if native in natives:
natives[native].append(fn)
else:
natives[native] = [fn]
# pprint(natives)
for k, v in natives.items():
fp = 'recordings/' + k
os.mkdir(fp)
for fn in v:
audio = AudioSegment.from_wav(fn)
chunks = make_chunks(audio, chunk_s)
l = len(chunks)
for i, ch in enumerate(chunks):
if i == 0 or i == (l - 1):
continue
ch.export(fp + '/' + k + str(i) + '.wav', format='wav')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment