-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract_audio.py
More file actions
40 lines (33 loc) · 1.52 KB
/
extract_audio.py
File metadata and controls
40 lines (33 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!python3
# Before you can use this script, you must install pydub & ffmpeg
# Install pydub with "pip install pydub"
# Installing ffmpeg is a bit harder, see https://phoenixnap.com/kb/ffmpeg-windows
import argparse
import csv
import os
from pydub import AudioSegment
parser = argparse.ArgumentParser(prog="extract_audio.py",
description="extract audio from source files as specified by a CSV file")
parser.add_argument("CSVFilename", help="CSV file")
args = parser.parse_args()
PreviousSource = None
print("Processing clips specified in {}".format(args.CSVFilename))
if not os.path.isfile(args.CSVFilename):
print("ERROR: '{}' doesn't exist".format(args.CSVFilename))
else:
#csvfile = open(args.CSVFilename)
#csvreader = csv.reader(csvfile)
csvreader = csv.reader(open(args.CSVFilename))
for clip in csvreader:
if len(clip) == 0:
continue
SourceFilename, StartTime, EndTime, LevelAdjust, OutputFile = \
clip[0], float(clip[1]), float(clip[2]), float(clip[3]), clip[4]
print(SourceFilename, StartTime, EndTime, LevelAdjust, OutputFile)
if not(PreviousSource == SourceFilename):
print("Opening {}".format(SourceFilename))
SourceAudio = AudioSegment.from_file(SourceFilename, "wav")
PreviousSource = SourceFilename
AudioClip = SourceAudio[int(1000*StartTime):int(1000*EndTime)]
#AudioClip = SourceAudio[int(1000*StartTime):int(1000*EndTime)] + LevelAdjust
AudioClip.export(OutputFile, format="wav")