-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplitaudio.py
More file actions
executable file
·234 lines (197 loc) · 6.9 KB
/
splitaudio.py
File metadata and controls
executable file
·234 lines (197 loc) · 6.9 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/usr/bin/python
import soundfile as sf
#import sounddevice as sd
import os
import os.path
import sys, getopt
import re
version="0.1"
time_pattern = r"((([0-9])?[0-9](:|\.))?([0-9])?[0-9](:|\.)?[0-9][0-9])"
time_pattern2 = r"(([0-9]?[0-9]):)?([0-9]?[0-9]):([0-9][0-9])"
nonValidChars = ".;[]{}#$&/()=~+^"
def sliceAudio(iFilename, names, times, verbose_en):
#open aduio
data, fs = sf.read(iFilename)
times.append(len(data)*fs)
# calculate time laps
for i in range(len(times)-1):
startPoint = times[i]*fs
endPoint = times[i+1]*fs
# write slice audio file
sf.write(names[i]+'.wav', data[startPoint:endPoint], fs)
if verbose_en == True:
print names[i]+'.wav'
def getNamesAndTimes_old(iFile):
times = []
names = []
with open(iFile, 'r') as list_file:
for line in list_file:
line = line.replace('.', '-')
line = line.replace(',', ' ')
line = line.replace(')', '-')
line = line.replace('(', '-')
line = line.strip()
# find the ':' to identify the time
idx = line.find(':')
if len(line)>6: # one leter for the name and 00:00
if idx == -1:
times.append(0)
names.append(line.strip())
else:
if line.count(':')==1:
sec = int(line[idx-2:idx])*60+int(line[idx+1:idx+3])
times.append(sec)
names.append(line[0:idx-2].strip())
elif line.count(':')==2:
sec = int(line[idx-2:idx])*3600+int(line[idx+1:idx+3])*60+int(line[idx+4:idx+7])
times.append(sec)
names.append(line[0:idx-2].strip())
return names,times
def getNamesAndTimes (iFile):
times = []
names = []
with open (iFile, 'r') as list_file:
for line in list_file:
if len(line.strip())==0:
continue
match = re.search(time_pattern, line)
if match==None:
sec = 0
line = line.translate(None, nonValidChars).strip()
else:
line = line.replace(match.group(), "").translate(None, nonValidChars).strip()
time_str = match.group()
match = re.search(time_pattern2, time_str)
none, hours, minutes, seconds = match.groups()
hours = 0 if hours==None else hours
sec = (int(hours)*3600)+(int(minutes)*60)+int(seconds)
times.append(sec)
names.append(line)
return names, times
def convert2fmt(names, fmt, keep_wav, verbose_en):
#Convert to mp3
for name in names:
if verbose_en == True:
cmd = 'avconv -i '+'"'+name+'.wav'+'" '+'"'+name+'.'+fmt+'"'
print cmd
else:
cmd = 'avconv -i '+'"'+name+'.wav'+'" '+'"'+name+'.'+fmt+'"'+' -v quiet'
os.system(cmd)
#print cmd
if verbose_en == True:
print '******************************************************'
print name+'.'+fmt, 'Created'
print '******************************************************'
if keep_wav == False:
cmd = 'rm '+'"'+name+'.wav'+'" '
#print cmd
os.system(cmd)
if verbose_en == True:
print name+'.'+'wav', 'Removed'
print '******************************************************'
def showHelp():
print "----------------------------------------------SPLIT AUDIO-----------------------------------------------"
print "splitaudio is a python program to split audio into tracks"
print "from a list of names and times."
print " "
print "Usage:"
print " splitaudio --input <input_file.wav> --list <list_of_names.txt> ..."
print " "
print "Arguments:"
print ".......Options....... .....Arguments..... .............Description.................................."
print " Long Short"
print "--input -i <input_file.wav> : input file, should be a wav file."
print "--list -l <list_of_names> : a text file with the list of names and times."
print "--format -f <mp3 | aiff | ogg> : the output audio format. default is wav"
print "--keep-wav -k : keep the sliced .wav files if other format is configured"
print "--help -h : display help"
print "--verbose -v : print messages"
print " "
print "Format of the lines in list: "
print " <track number>. <Name> <minutes:seconds>"
print " <##>. <Name> <mm:ss>"
print " Example:"
print " 4. Hello (Buddy) 8:07"
print " "
print " "
#iFilename = 'Man From Another Time - Seasick Steve (full album)-dyb6ymIaWjc.wav'
if __name__ == "__main__":
# identify the arguments
# --input -i <input_file.wav> : input file, should be a wav file.
# --list -l <list_of_names> : a text file with the list of names and times.
# --format -f <mp3 | aiff | ogg> : the output audio format. default is wav
# --keep-wav -k : keep the sliced .wav files if other format is configured
# --help -h <argument> : display help
# --verbose -v : print messages
argv = sys.argv[1:]
short_opt = "i:l:f:khv"
long_opt = ['input=', 'list=', 'format=', 'keep-wav', 'help', 'verbose']
supported_formats = ['mp3', 'aiff', 'ogg']
iaudiofile = ''
ilistfile = ''
au_format = ''
keep_wav_en= False
help_en = False
verbose_en = False
#parse arguments
try:
opts, args = getopt.getopt(argv, short_opt, long_opt)
except getopt.GetoptError:
print "error in command"
print "splitaudio --input <input_file.wav> --list <list_of_names.txt>"
sys.exit(2)
#get configurations form arguments
for opt, arg in opts:
if opt in ("-i", "--input"):
if os.path.isfile(arg) and os.access(arg, os.R_OK):
iaudiofile = arg
else:
print "error when trying to access "+arg
print "Either file is missing or is not readable"
sys.exit(2)
elif opt in ('-l', '--list'):
if os.path.isfile(arg) and os.access(arg, os.R_OK):
ilistfile = arg
else:
print "error when trying to access "+arg
print "Either file is missing or is not readable"
sys.exit(2)
elif opt in ('-f', '--format'):
if arg in supported_formats:
au_format = arg
else:
print "format not supported"
sys.exit(2)
elif opt in ('-k', '--keep-wav'):
keep_wav_en = True
elif opt in ('-h', '--help'):
help_en = True
elif opt in ('-v', '--verbose'):
verbose_en = True
#help
if help_en == True:
showHelp()
if iaudiofile=='' or ilistfile=='':
showHelp()
sys.exit(1)
# files
if verbose_en == True:
print "Input Audio File : "+iaudiofile
print "Output List of Names: "+ilistfile
print "Getting the Names and times form the list..."
names, times = getNamesAndTimes(ilistfile)
if verbose_en == True:
print "Time [sec] Name"
for k in range(0, len(names)):
print " ", str(times[k]).ljust(10), " ", names[k]
print 'Spliting audio and creating .wav files'
sliceAudio(iaudiofile, names, times, verbose_en)
if au_format != '':
print 'Converting .wav to', '.'+au_format, 'format'
convert2fmt(names, au_format, keep_wav_en, verbose_en)
#print 'getting the name list and times'
#names, times = getNamesAndTimes('list.txt')
#print 'slicing audio and crating wav files'
#sliceAudio(iFilename, names, times)
#print 'converting to mp3 and removing wav'
#convert2fmt(names, 'mp3')