Created
July 19, 2018 13:52
-
-
Save andreinechaev/10a82852457c4173b7c4b6ee2075b8f2 to your computer and use it in GitHub Desktop.
An example how to chunk audio with Python and pydub
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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