-
Notifications
You must be signed in to change notification settings - Fork 2
/
yscrape.py
330 lines (286 loc) · 11.7 KB
/
yscrape.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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
'''
Script: yscrape.py
Author: Jim Schwoebel
This script takes in a template excel sheet and downloads videos from youtube.
After this, the videos are clipped to the desired ranges as annoted by the end user.
This is all done in the current directory that the script is executed.
In this way, we can quickly build custom curated datasets around specific
use cases based on self-reported video bloggers.
Also, labels each output audio file with date, url, length, clipped points,
label, age, gender, accent, and environment (if available in excel sheet).
'''
import os, json, time, wave, ffmpy, shutil, getpass, datetime, sys, time
from tqdm import tqdm
from optparse import OptionParser
import pandas as pd
import soundfile as sf
# download audio with youtube-dl instead of pafy to prevent errors
def download_audio(link):
listdir=os.listdir()
os.system("youtube-dl -f 'bestaudio[ext=m4a]' '%s'"%(link))
listdir2=os.listdir()
filename=''
for i in range(len(listdir2)):
if listdir2[i] not in listdir and listdir2[i].endswith('.m4a'):
filename=listdir2[i]
break
return filename
def load_colunns(loadfile):
labels=list(loadfile)
# -------------------------- #
# get all the curenti nfo in the terminal if its there
curdir=os.getcwd()
# add a very simple file parser here for CLI client
parser = OptionParser()
parser.add_option("-e", "--excel", dest="filename",
help="load in audiofile (.WAV format)", metavar="FILE")
parser.add_option("-f", "--folder", dest="folder",
help="specify the folder", metavar="FOLDER")
(options, args) = parser.parse_args()
# -------------------------- #
# load terminal variables; if they aren't there prompt user for spreadsheets available / which to download
try:
directory=options.folder
filename=options.filename
print(filename)
print(directory)
os.chdir(directory)
except:
os.chdir('spreadsheets')
listdir=os.listdir()
excel_files=list()
for i in range(len(listdir)):
if listdir[i].endswith('.xlsx'):
excel_files.append(listdir[i])
excel_files.append('all')
filename=input('what is the file name? \n\n options: %s \n\n'%(str(excel_files)))
os.chdir(curdir)
# -------------------------- #
if filename == 'all':
# recursively call this script with proper variables if user wants all data
for i in tqdm(range(len(excel_files))):
if excel_files[i] != 'all':
# now iterate through all these
os.chdir(curdir)
os.chdir('spreadsheets')
filename=excel_files[i]
desktop=os.getcwd()+'/'
foldername=filename[0:-5]
destfolder=desktop+foldername+'/'
try:
os.mkdir(foldername)
os.chdir(destfolder)
except:
os.chdir(destfolder)
#move file to destfolder
shutil.copy(desktop+filename,destfolder+filename)
#load xls sheet (and get labels)
loadfile=pd.read_excel(filename)
link=loadfile.iloc[:,0]
length=loadfile.iloc[:,1]
times=loadfile.iloc[:,2]
#initialize lists
links=list()
lengths=list()
start_times=list()
end_times=list()
labels=list()
# headers - use these as the primary labels to put in the .JSON docs
headers_=list(loadfile)
headers=list()
for i in range(len(headers_)):
if i > 2:
headers.append(headers_[i])
print('key labels: ')
print(headers)
#only make links that are in youtube processable
for i in range(len(link)):
if str(link[i]).find('youtube.com/watch') != -1:
links.append(str(link[i]))
lengths.append(str(length[i]))
#find the dash for start/stop times
time_=str(times[i])
index=time_.find('-')
start_time=time_[0:index]
#get start time in seconds
start_minutes=int(start_time[0])
start_seconds=int(start_time[-2:])
start_total=start_minutes*60+start_seconds
#get end time in seconds
end_time=time_[index+1:]
end_minutes=int(end_time[0])
end_seconds=int(end_time[-2:])
end_total=end_minutes*60+end_seconds
#update lists
start_times.append(start_total)
end_times.append(end_total)
#labels
labels.append(foldername)
files=list()
for i in tqdm(range(len(links)), desc=filename+'_files'):
try:
# use YouTube DL to download audio
filename=download_audio(links[i])
extension='.m4a'
start=start_times[i]
end=end_times[i]
#get file extension and convert to .wav for processing later
os.rename(filename,'%s_start_%s_end_%s%s'%(str(i),start,end,extension))
filename='%s_start_%s_end_%s%s'%(str(i),start,end,extension)
if extension not in ['.wav']:
xindex=filename.find(extension)
filename=filename[0:xindex]
ff=ffmpy.FFmpeg(
inputs={filename+extension:None},
outputs={filename+'.wav':None}
)
ff.run()
os.remove(filename+extension)
file=filename+'.wav'
data,samplerate=sf.read(file)
totalframes=len(data)
totalseconds=totalframes/samplerate
startsec=int(start_times[i])
startframe=samplerate*startsec
endsec=int(end_times[i])
endframe=samplerate*endsec
sf.write('snipped'+file, data[startframe:endframe], samplerate)
newfilename='snipped'+file
#can write json too
nfile= dict()
nfile["Date"] = str(datetime.datetime.now())
nfile["URL"] = str(links[i])
nfile["Length"] = str(length[i])
nfile["Clipped points"] = str(times[i])
nfile["Indication"] = str(foldername)
data=dict()
for j in range(len(headers)):
# find index
i1=headers_.index(headers[j])
try:
print(headers[j])
print(loadfile.iloc[:,i1][i])
data[headers[j]] = str(loadfile.iloc[:,i1][i])
except:
print('error loading header')
nfile["labels"] = data
jsonfile=open(newfilename[0:-4]+'.json','w')
json.dump(nfile, jsonfile)
jsonfile.close()
os.remove(file)
except:
print('error fetching video')
# sleep 5 seconds to not overwhelm YouTube's servers
# it's a good practice to be a good internet citizen!! :)
time.sleep(5)
else:
os.chdir('spreadsheets')
desktop=os.getcwd()+'/'
foldername=filename[0:-5]
destfolder=desktop+foldername+'/'
try:
os.mkdir(foldername)
os.chdir(destfolder)
except:
os.chdir(destfolder)
#move file to destfolder
shutil.copy(desktop+filename,destfolder+filename)
#load xls sheet (and get labels)
loadfile=pd.read_excel(filename)
link=loadfile.iloc[:,0]
length=loadfile.iloc[:,1]
times=loadfile.iloc[:,2]
#initialize lists
links=list()
lengths=list()
start_times=list()
end_times=list()
labels=list()
# headers - use these as the primary labels to put in the .JSON docs
headers_=list(loadfile)
headers=list()
for i in range(len(headers_)):
if i > 2:
headers.append(headers_[i])
print('key labels: ')
print(headers)
#only make links that are in youtube processable
for i in range(len(link)):
if str(link[i]).find('youtube.com/watch') != -1:
links.append(str(link[i]))
lengths.append(str(length[i]))
#find the dash for start/stop times
time_=str(times[i])
index=time_.find('-')
start_time=time_[0:index]
#get start time in seconds
start_minutes=int(start_time[0])
start_seconds=int(start_time[-2:])
start_total=start_minutes*60+start_seconds
#get end time in seconds
end_time=time_[index+1:]
end_minutes=int(end_time[0])
end_seconds=int(end_time[-2:])
end_total=end_minutes*60+end_seconds
#update lists
start_times.append(start_total)
end_times.append(end_total)
#labels
labels.append(foldername)
files=list()
for i in tqdm(range(len(links)), desc=filename):
try:
# use YouTube DL to download audio
filename=download_audio(links[i])
extension='.m4a'
start=start_times[i]
end=end_times[i]
#get file extension and convert to .wav for processing later
os.rename(filename,'%s_start_%s_end_%s%s'%(str(i),start,end,extension))
filename='%s_start_%s_end_%s%s'%(str(i),start,end,extension)
if extension not in ['.wav']:
xindex=filename.find(extension)
filename=filename[0:xindex]
ff=ffmpy.FFmpeg(
inputs={filename+extension:None},
outputs={filename+'.wav':None}
)
ff.run()
os.remove(filename+extension)
file=filename+'.wav'
data,samplerate=sf.read(file)
totalframes=len(data)
totalseconds=totalframes/samplerate
startsec=int(start_times[i])
startframe=samplerate*startsec
endsec=int(end_times[i])
endframe=samplerate*endsec
sf.write('snipped'+file, data[startframe:endframe], samplerate)
newfilename='snipped'+file
#can write json too
nfile= dict()
nfile["Date"] = str(datetime.datetime.now())
nfile["URL"] = str(links[i])
nfile["Length"] = str(length[i])
nfile["Clipped points"] = str(times[i])
nfile["Indication"] = str(foldername)
data=dict()
for j in range(len(headers)):
# find index
i1=headers_.index(headers[j])
try:
print(headers[j])
print(loadfile.iloc[:,i1][i])
data[headers[j]] = str(loadfile.iloc[:,i1][i])
except:
print('error loading header')
nfile["labels"] = data
jsonfile=open(newfilename[0:-4]+'.json','w')
json.dump(nfile, jsonfile)
jsonfile.close()
os.remove(file)
except:
print('error fetching video')
# sleep 5 seconds to not overwhelm YouTube's servers
# it's a good practice to be a good internet citizen!! :)
time.sleep(5)