-
Notifications
You must be signed in to change notification settings - Fork 2
/
audiocut.py
114 lines (84 loc) · 3.69 KB
/
audiocut.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import os
import errno
import argparse
import scipy.io.wavfile
import re
from timecode import is_timecode
from timecode import timecode_to_samples
from timecode import validate_parameters
# ==== command line arguments and help ====
parser = argparse.ArgumentParser()
parser.add_argument("input", help="wav file that has to be cut", type=str)
parser.add_argument("start_time", help="Timecode in the form of hh:mm:ss:f where f is a float >= 0 and < 1 (e.g. 0.5)",
type=str)
parser.add_argument("end_time", help="Timecode in the form of hh:mm:ss:f where f is a float >= 0 and < 1 (e.g. 0.5)",
type=str)
parser.add_argument("regular_chunk_length",
help="Timecode in the form of hh:mm:ss:f where f is a float >= 0 and < 1 (e.g. 0.5)", type=str)
parser.add_argument("-v", "--verbosity", action="count", help="increase output verbosity")
args = parser.parse_args()
# ==== file handling ====
file_name = args.input
file_name_base = re.findall('(.+).wav', file_name)
try:
file_handler = scipy.io.wavfile.read(file_name)
except OSError:
print('Sorry, file -', file_name, '- could not be opened. Exiting.')
quit()
# ==== parameter testing
start_time = args.start_time
end_time = args.end_time
regular_chunk_length = args.regular_chunk_length
if not is_timecode(start_time) or not is_timecode(end_time) or not is_timecode(regular_chunk_length):
print('Sorry, a Timecode was not well formed. Exiting.')
quit()
sample_rate = file_handler[0]
total_time_of_input_file = len(file_handler[1])
total_time_of_all_chunks = timecode_to_samples(end_time, sample_rate) - timecode_to_samples(start_time, sample_rate)
if total_time_of_input_file - timecode_to_samples(end_time, sample_rate) < 0:
print("Sorry, the end_time seems to be longer than the wav file itself. Exiting.")
quit()
validate_parameters(file_name, file_handler[0], start_time, end_time, regular_chunk_length)
# ==== the algorithm ====
start_time = timecode_to_samples(start_time, sample_rate)
end_time = timecode_to_samples(end_time, sample_rate)
regular_chunk_length = timecode_to_samples(regular_chunk_length, sample_rate)
total_count_of_regular_chunks = int(total_time_of_all_chunks / regular_chunk_length)
chunks = []
first_chunk = (start_time, start_time + regular_chunk_length)
chunks.append(first_chunk)
chunk_count = 1
while chunk_count < total_count_of_regular_chunks:
chunk = (chunks[chunk_count - 1][1], chunks[chunk_count - 1][1] + regular_chunk_length)
chunks.append(chunk)
chunk_count = chunk_count + 1
# if chunks don't fit regularly, calculate the remaining part
if chunks[chunk_count - 1][1] < start_time + total_time_of_all_chunks:
chunk = (chunks[chunk_count - 1][1], start_time + total_time_of_all_chunks)
chunks.append(chunk)
chunk_count = chunk_count + 1
if args.verbosity == 2:
print(chunks)
# ==== write everything to disk ====
def create_sub_folder(path):
try:
os.makedirs(path)
except OSError as exception:
if exception.errno != errno.EEXIST:
raise
return path
sub_folder = create_sub_folder(str(file_name + '-cut'))
for chunk in range(len(chunks)):
start_time = chunks[chunk][0]
end_time = chunks[chunk][1]
out_file_name = '{0}-{1:010d}-{2:010d}.wav'.format(file_name_base[0], start_time, end_time)
out_file_name = os.path.join(sub_folder, out_file_name)
try:
scipy.io.wavfile.write(out_file_name, sample_rate, file_handler[1][start_time:end_time])
if args.verbosity == 1 or args.verbosity == 2:
print(out_file_name)
except OSError:
print('Sorry, file ', out_file_name, ' could not be written.')
quit()
print(chunk_count, 'files have been written.')
quit()