-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvideos.py
582 lines (516 loc) · 29.1 KB
/
videos.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
# !/usr/bin/env python3
import twitch # pip install python-twitch-client
import yaml # pip install PyYAML
from vosk import Model, KaldiRecognizer, SetLogLevel # pip install vosk
from discord_webhook import DiscordWebhook # pip install discord-webhook
import os
import json
import subprocess
from datetime import datetime
import utils
import time
import shutil
import sys
# importing static-ffmpeg and pre-downloading
import static_ffmpeg
static_ffmpeg.add_paths()
# authentication information
path_base = os.path.dirname(os.path.abspath(__file__))
config_file = path_base + "/config/config.yaml"
with open(config_file) as f:
conf = yaml.load(f, Loader=yaml.FullLoader)
client_id = conf["client_id"]
client_secret = conf["client_secret"]
videos_config = path_base + "/config/videos.yaml"
with open(videos_config) as g:
videos = yaml.load(g, Loader=yaml.FullLoader)
channels = videos["channels"]
max_videos = videos["max_videos"]
render_chat = videos["render_chat"]
render_webvtt = videos["render_webvtt"]
# Check for ffmpeg path as installed by static-ffmpeg and the installed version of python/pip
# A full path is needed for TwitchDownloader
ffmpeg_path = shutil.which('ffmpeg')
# ================================================================
# ================================================================
# paths of the cli and data
tdcli = conf["twitchdownloader"]
path_twitch_cli = path_base + tdcli
path_root = videos["video_downloads"]
badchat_log = videos["video_downloads"] + "badchat.videos"
path_temp = videos["video_temp"]
# Vosk speech recognition models - 'Small' English model selected by default and recommended.
# 'Large' model can be used on a system with enough resources including minimum 6GB RAM
# free or more and a high end processor.
path_model = path_base + "/thirdparty/vosk-model-small-en-us-0.15/"
#path_model = path_base + "/thirdparty/vosk-model-en-us-0.22/"
# ================================================================
# ================================================================
# setup control+c handler
utils.setup_signal_handle()
users = {}
if len(channels) != len(render_chat) or len(channels) != len(render_webvtt):
print('number of channels and chat render settings do not match!!')
print('\tlen(channels) = %d' % len(channels))
print('\tlen(users) = %d' % len(users))
print('\tlen(render_chat) = %d' % len(render_chat))
print('\tlen(render_webvtt) = %d' % len(render_webvtt))
exit(-1)
# convert the usernames to ids (sort so the are in the same order)
client_helix = twitch.TwitchHelix(client_id=client_id, client_secret=client_secret)
client_helix.get_oauth()
users_tmp = client_helix.get_users(login_names=channels)
users = []
render_chat_tmp = []
render_webvtt_tmp = []
for idx, channel in enumerate(channels):
found = False
for user in users_tmp:
if user["login"].lower() == channel.lower():
users.append(user)
render_chat_tmp.append(render_chat[idx])
render_webvtt_tmp.append(render_webvtt[idx])
found = True
break
if not found:
print("streamer %s wasn't found, are they banned???" % channel)
render_chat = render_chat_tmp
render_webvtt = render_webvtt_tmp
# now lets loop through each user and make sure we have downloaded
# their most recent VODs and if we have not, we should download them!
for idx, user in enumerate(users):
# check if we should download any more
if utils.terminated_requested:
print('terminate requested, not looking at any more users...')
break
# check if the directory is created
path_data = path_root + "/" + user["login"].lower() + "/"
if not os.path.exists(path_data):
os.makedirs(path_data)
if not os.path.exists(path_temp):
os.makedirs(path_temp)
# get this stream object, it will have something if the stream is live
client_helix = twitch.TwitchHelix(client_id=client_id, client_secret=client_secret)
client_helix.get_oauth()
stream = client_helix.get_streams(user_ids=[user["id"]])
stream_is_live = (len(stream) == 1)
# get the videos for this specific user
print("getting videos for -> " + user["login"].lower() + " (id " + str(user["id"]) + ")")
vid_iter = client_helix.get_videos(user_id=user["id"], page_size=100)
arr_archive = []
arr_highlight = []
arr_upload = []
ct_added = [0, 0, 0]
seen_first_video = False
for video in vid_iter:
# skip the first VOD is they are live
if not seen_first_video and stream_is_live:
print("skipping video " + video['id'] + " since stream is live...")
seen_first_video = True
continue
seen_first_video = True
# else lets process
# "all", "upload", "archive", "highlight"
if video['type'] == 'archive' and ct_added[0] < max_videos:
arr_archive.append({'helix': video})
ct_added[0] = ct_added[0] + 1
elif video['type'] == 'highlight' and ct_added[1] < max_videos:
arr_highlight.append({'helix': video})
ct_added[1] = ct_added[1] + 1
elif video['type'] == 'upload' and ct_added[2] < max_videos:
arr_upload.append({'helix': video})
ct_added[2] = ct_added[2] + 1
# nice debug print
print("\t- found " + str(len(arr_archive)) + " archives")
print("\t- found " + str(len(arr_highlight)) + " highlights")
print("\t- found " + str(len(arr_upload)) + " uploads")
# loop through each archive/VOD and download
for video in arr_archive:
# check if we should download any more
if utils.terminated_requested:
print('terminate requested, not downloading any more..')
break
# DATA: api data of this vod
t0_start = time.time()
video_data = {
'id': video['helix']['id'],
'user_id': video['helix']['user_id'],
'user_name': video['helix']['user_name'],
'title': video['helix']['title'],
'type': video['helix']['type'],
'duration': video['helix']['duration'],
'url': video['helix']['url'],
'views': video['helix']['view_count'],
'moments': utils.get_vod_moments(video['helix']['id']),
'muted_segments': (video['helix']['muted_segments'] if video['helix']['muted_segments'] != None else []),
'recorded_at': video['helix']['created_at'].strftime('%Y-%m-%dT%H:%M:%SZ'),
'recorded_at_iso': video['helix']['created_at'].strftime('%Y%m%d T%H%M%SZ')
}
# providing a single source for all filename calls in this script, including stripping illegal characters
filename_format = utils.cleanFilename(str(video_data['recorded_at_iso']) + " - " + str(video['helix']['id']) + " - " + str(video['helix']['title']) + "_" + str(video['helix']['type']))
# extract what folder we should save into
# create the folder if it isn't created already
try:
date = datetime.strptime(video_data['recorded_at'], '%Y-%m-%dT%H:%M:%SZ')
export_folder = format(date.year, '02') + "-" + format(date.month, '02') + "/"
except:
export_folder = "unknown/"
if not os.path.exists(path_data + export_folder):
os.makedirs(path_data + export_folder)
# VIDEO: check if the file exists
file_path_info = path_data + export_folder + filename_format + "_info.json"
print("\t- saving video info: " + file_path_info)
if not utils.terminated_requested and not os.path.exists(file_path_info):
with open(file_path_info, 'w', encoding="utf-8") as file:
json.dump(video_data, file, indent=4)
elif not utils.terminated_requested:
print("\t- updating video info: " + file_path_info)
with open(file_path_info) as f:
video_info = json.load(f)
# update moments if failed before
if len(video_info["moments"]) == 0:
moments = utils.get_vod_moments(video['helix']['id'])
if len(moments) != 0:
video_info["moments"] = moments
# finally write to file
with open(file_path_info, 'w', encoding="utf-8") as file:
json.dump(video_info, file, indent=4)
# VIDEO: check if the file exists
file_path = path_data + export_folder + filename_format + ".mp4"
print("\t- download video: " + file_path)
if not utils.terminated_requested and not os.path.exists(file_path):
t0 = time.time()
cmd = path_twitch_cli + ' videodownload' \
+ ' --id ' + str(video['helix']['id']) + ' --ffmpeg-path "' + ffmpeg_path + '"' \
+ ' --temp-path "' + path_temp + '" -o "' + file_path + '"'
# print("CMD: " + str(cmd))
subprocess.Popen(cmd, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).wait()
# subprocess.Popen(cmd, shell=True).wait()
print("\t- done in " + str(time.time() - t0) + " seconds")
# CHAT: check if the file exists
file_path_chat = path_data + export_folder + filename_format + "_chat.json"
file_bad = file_path_chat + ".BAD"
file_path_chat_tmp = path_temp + str(video['helix']['id']) + "_chat.json"
print("\t- download chat: " + file_path_chat)
if not utils.terminated_requested and (not os.path.exists(file_path_chat) or utils.checkBadChat(video['helix']['id'], "clips", badchat_log)):
t0 = time.time()
cmd = path_twitch_cli + ' chatdownload' \
+ ' --id ' + str(video['helix']['id']) + ' -E' \
+ ' -o ' + file_path_chat_tmp
# print("CMD: " + str(cmd))
# Attempt to download chat log. If the first attempt with emojis embedded fails, try again without emojis. TDCLI will error out
# there's issues with downloading emojis. We'd rather download the log without them than fail entirely.
#
# If the no-emoji attempt fails as well, we'll assume there's no chat log at all for this video. We'll write a blank .BAD file
# to satisfy future file-exists checks.
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
proc.wait()
if proc.returncode != 0:
print("ERR: Clip has no chat. Either nothing was said or the source VOD is no longer available. Inserting placeholder.")
with open(badchat_log, 'a') as fp:
fp.write(str(video['helix']['id']))
fp.write('\n')
else:
print("GOOD: File moved")
if os.path.exists(file_path_chat_tmp):
shutil.move(file_path_chat_tmp, file_path_chat)
print("\t- done in " + str(time.time() - t0) + " seconds")
# AUDIO-TO-TEXT: check if file exists
file_path_webvtt = path_data + export_folder + filename_format + ".srt"
if not utils.terminated_requested and os.path.exists(file_path) and not os.path.exists(file_path_webvtt) and render_webvtt[idx]:
print("\t- transcribing: " + file_path_webvtt)
t0 = time.time()
# open the model
SetLogLevel(-1)
sample_rate = 16000
# words_per_line = 7
model = Model(path_model)
rec = KaldiRecognizer(model, sample_rate)
rec.SetWords(True)
with subprocess.Popen([ffmpeg_path, "-loglevel", "quiet", "-i",
file_path,
"-ar", str(sample_rate) , "-ac", "1", "-f", "s16le", "-"],
stdout=subprocess.PIPE).stdout as stream:
with open(file_path_webvtt, 'w') as f:
f.write(rec.SrtResult(stream))
print("\t- done in " + str(time.time() - t0) + " seconds")
# send pushover that this twitch vod is ready to edit
text = video['helix']['user_name'] + " vod " + str(video['helix']['id']) \
+ " ready to edit (" + str(int((time.time() - t0_start)/60.0)) + " min to prepare)"
utils.send_pushover_message(conf, text)
# RENDER: check if the file exists
file_path_chat = path_data + export_folder + filename_format + "_chat.json"
file_path_render = path_data + export_folder + filename_format + "_chat.mp4"
file_path_render_tmp = path_temp + str(video['helix']['id']) + "_chat.mp4"
if not utils.terminated_requested and os.path.exists(file_path_chat) and not os.path.exists(file_path_render) and render_chat[idx]:
print("\t- rendering chat: " + file_path_render)
t0 = time.time()
cmd = path_twitch_cli + ' chatrender' \
+ ' -i ' + file_path_chat + ' --ffmpeg-path "' + ffmpeg_path + '"' \
+ ' -h 926 -w 274 --update-rate 0.1 --framerate 60 --font-size 15' \
+ ' --temp-path "' + path_temp + '" -o ' + file_path_render_tmp
# subprocess.Popen(cmd, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).wait()
subprocess.Popen(cmd, shell=True).wait()
if os.path.exists(file_path_render_tmp):
shutil.move(file_path_render_tmp, file_path_render)
print("\t- done in " + str(time.time() - t0) + " seconds")
# loop through each highlight and download
for video in arr_highlight:
# check if we should download any more
if utils.terminated_requested:
print('terminate requested, not downloading any more..')
break
# DATA: api data of this vod
t0_start = time.time()
video_data = {
'id': video['helix']['id'],
'user_id': video['helix']['user_id'],
'user_name': video['helix']['user_name'],
'title': video['helix']['title'],
'type': video['helix']['type'],
'duration': video['helix']['duration'],
'url': video['helix']['url'],
'views': video['helix']['view_count'],
'moments': utils.get_vod_moments(video['helix']['id']),
'muted_segments': (video['helix']['muted_segments'] if video['helix']['muted_segments'] != None else []),
'recorded_at': video['helix']['created_at'].strftime('%Y-%m-%dT%H:%M:%SZ'),
'recorded_at_iso': video['helix']['created_at'].strftime('%Y%m%d T%H%M%SZ')
}
# providing a single source for all filename calls in this script, including stripping illegal characters
filename_format = utils.cleanFilename(str(video_data['recorded_at_iso']) + " - " + str(video['helix']['id']) + " - " + str(video['helix']['title']) + "_" + str(video['helix']['type']))
# extract what folder we should save into
# create the folder if it isn't created already
try:
date = datetime.strptime(video_data['recorded_at'], '%Y-%m-%dT%H:%M:%SZ')
export_folder = format(date.year, '02') + "-" + format(date.month, '02') + "/"
except:
export_folder = "unknown/"
if not os.path.exists(path_data + export_folder):
os.makedirs(path_data + export_folder)
# VIDEO: check if the file exists
file_path_info = path_data + export_folder + filename_format + "_info.json"
print("\t- saving video info: " + file_path_info)
if not utils.terminated_requested and not os.path.exists(file_path_info):
with open(file_path_info, 'w', encoding="utf-8") as file:
json.dump(video_data, file, indent=4)
elif not utils.terminated_requested:
print("\t- updating video info: " + file_path_info)
with open(file_path_info) as f:
video_info = json.load(f)
# update moments if failed before
if len(video_info["moments"]) == 0:
moments = utils.get_vod_moments(video['helix']['id'])
if len(moments) != 0:
video_info["moments"] = moments
# finally write to file
with open(file_path_info, 'w', encoding="utf-8") as file:
json.dump(video_info, file, indent=4)
# VIDEO: check if the file exists
file_path = path_data + export_folder + filename_format + ".mp4"
print("\t- download video: " + file_path)
if not utils.terminated_requested and not os.path.exists(file_path):
t0 = time.time()
cmd = path_twitch_cli + ' videodownload' \
+ ' --id ' + str(video['helix']['id']) + ' --ffmpeg-path "' + ffmpeg_path + '"' \
+ ' --temp-path "' + path_temp + '" -o "' + file_path + '"'
# print("CMD: " + str(cmd))
subprocess.Popen(cmd, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).wait()
# subprocess.Popen(cmd, shell=True).wait()
print("\t- done in " + str(time.time() - t0) + " seconds")
# CHAT: check if the file exists
file_path_chat = path_data + export_folder + filename_format + "_chat.json"
file_bad = file_path_chat + ".BAD"
file_path_chat_tmp = path_temp + str(video['helix']['id']) + "_chat.json"
print("\t- download chat: " + file_path_chat)
if not utils.terminated_requested and (not os.path.exists(file_path_chat) or utils.checkBadChat(video['helix']['id'], "clips", badchat_log)):
t0 = time.time()
cmd = path_twitch_cli + ' chatdownload' \
+ ' --id ' + str(video['helix']['id']) + ' -E' \
+ ' -o ' + file_path_chat_tmp
# print("CMD: " + str(cmd))
# Attempt to download chat log. If the first attempt with emojis embedded fails, try again without emojis. TDCLI will error out
# there's issues with downloading emojis. We'd rather download the log without them than fail entirely.
#
# If the no-emoji attempt fails as well, we'll assume there's no chat log at all for this video. We'll write a blank .BAD file
# to satisfy future file-exists checks.
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
proc.wait()
if proc.returncode != 0:
print("ERR: Clip has no chat. Either nothing was said or the source VOD is no longer available. Inserting placeholder.")
with open(badchat_log, 'a') as fp:
fp.write(str(video['helix']['id']))
fp.write('\n')
else:
print("GOOD: File moved")
if os.path.exists(file_path_chat_tmp):
shutil.move(file_path_chat_tmp, file_path_chat)
print("\t- done in " + str(time.time() - t0) + " seconds")
# AUDIO-TO-TEXT: check if file exists
file_path_webvtt = path_data + export_folder + filename_format + ".srt"
if not utils.terminated_requested and os.path.exists(file_path) and not os.path.exists(file_path_webvtt) and render_webvtt[idx]:
print("\t- transcribing: " + file_path_webvtt)
t0 = time.time()
# open the model
SetLogLevel(-1)
sample_rate = 16000
# words_per_line = 7
model = Model(path_model)
rec = KaldiRecognizer(model, sample_rate)
rec.SetWords(True)
with subprocess.Popen([ffmpeg_path, "-loglevel", "quiet", "-i",
file_path,
"-ar", str(sample_rate) , "-ac", "1", "-f", "s16le", "-"],
stdout=subprocess.PIPE).stdout as stream:
with open(file_path_webvtt, 'w') as f:
f.write(rec.SrtResult(stream))
print("\t- done in " + str(time.time() - t0) + " seconds")
# send pushover that this twitch vod is ready to edit
text = video['helix']['user_name'] + " vod " + str(video['helix']['id']) \
+ " ready to edit (" + str(int((time.time() - t0_start)/60.0)) + " min to prepare)"
utils.send_pushover_message(conf, text)
# RENDER: check if the file exists
file_path_chat = path_data + export_folder + filename_format + "_chat.json"
file_path_render = path_data + export_folder + filename_format + "_chat.mp4"
file_path_render_tmp = path_temp + str(video['helix']['id']) + "_chat.mp4"
if not utils.terminated_requested and os.path.exists(file_path_chat) and not os.path.exists(file_path_render) and render_chat[idx]:
print("\t- rendering chat: " + file_path_render)
t0 = time.time()
cmd = path_twitch_cli + ' chatrender' \
+ ' -i ' + file_path_chat + ' --ffmpeg-path "' + ffmpeg_path + '"' \
+ ' -h 926 -w 274 --update-rate 0.1 --framerate 60 --font-size 15' \
+ ' --temp-path "' + path_temp + '" -o ' + file_path_render_tmp
# subprocess.Popen(cmd, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).wait()
subprocess.Popen(cmd, shell=True).wait()
if os.path.exists(file_path_render_tmp):
shutil.move(file_path_render_tmp, file_path_render)
print("\t- done in " + str(time.time() - t0) + " seconds")
# loop through each highlight and download
for video in arr_upload:
# check if we should download any more
if utils.terminated_requested:
print('terminate requested, not downloading any more..')
break
# DATA: api data of this vod
t0_start = time.time()
video_data = {
'id': video['helix']['id'],
'user_id': video['helix']['user_id'],
'user_name': video['helix']['user_name'],
'title': video['helix']['title'],
'type': video['helix']['type'],
'duration': video['helix']['duration'],
'url': video['helix']['url'],
'views': video['helix']['view_count'],
'moments': utils.get_vod_moments(video['helix']['id']),
'muted_segments': (video['helix']['muted_segments'] if video['helix']['muted_segments'] != None else []),
'recorded_at': video['helix']['created_at'].strftime('%Y-%m-%dT%H:%M:%SZ'),
'recorded_at_iso': video['helix']['created_at'].strftime('%Y%m%d T%H%M%SZ')
}
# providing a single source for all filename calls in this script, including stripping illegal characters
filename_format = utils.cleanFilename(str(video_data['recorded_at_iso']) + " - " + str(video['helix']['id']) + " - " + str(video['helix']['title']) + "_" + str(video['helix']['type']))
# extract what folder we should save into
# create the folder if it isn't created already
try:
date = datetime.strptime(video_data['recorded_at'], '%Y-%m-%dT%H:%M:%SZ')
export_folder = format(date.year, '02') + "-" + format(date.month, '02') + "/"
except:
export_folder = "unknown/"
if not os.path.exists(path_data + export_folder):
os.makedirs(path_data + export_folder)
# VIDEO: check if the file exists
file_path_info = path_data + export_folder + filename_format + "_info.json"
print("\t- saving video info: " + file_path_info)
if not utils.terminated_requested and not os.path.exists(file_path_info):
with open(file_path_info, 'w', encoding="utf-8") as file:
json.dump(video_data, file, indent=4)
elif not utils.terminated_requested:
print("\t- updating video info: " + file_path_info)
with open(file_path_info) as f:
video_info = json.load(f)
# update moments if failed before
if len(video_info["moments"]) == 0:
moments = utils.get_vod_moments(video['helix']['id'])
if len(moments) != 0:
video_info["moments"] = moments
# finally write to file
with open(file_path_info, 'w', encoding="utf-8") as file:
json.dump(video_info, file, indent=4)
# VIDEO: check if the file exists
file_path = path_data + export_folder + filename_format + ".mp4"
print("\t- download video: " + file_path)
if not utils.terminated_requested and not os.path.exists(file_path):
t0 = time.time()
cmd = path_twitch_cli + ' videodownload' \
+ ' --id ' + str(video['helix']['id']) + ' --ffmpeg-path "' + ffmpeg_path + '"' \
+ ' --temp-path "' + path_temp + '" -o "' + file_path + '"'
# print("CMD: " + str(cmd))
subprocess.Popen(cmd, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).wait()
# subprocess.Popen(cmd, shell=True).wait()
print("\t- done in " + str(time.time() - t0) + " seconds")
# CHAT: check if the file exists
file_path_chat = path_data + export_folder + filename_format + "_chat.json"
file_bad = file_path_chat + ".BAD"
file_path_chat_tmp = path_temp + str(video['helix']['id']) + "_chat.json"
print("\t- download chat: " + file_path_chat)
if not utils.terminated_requested and (not os.path.exists(file_path_chat) or utils.checkBadChat(video['helix']['id'], "clips", badchat_log)):
t0 = time.time()
cmd = path_twitch_cli + ' chatdownload' \
+ ' --id ' + str(video['helix']['id']) + ' -E' \
+ ' -o ' + file_path_chat_tmp
# print("CMD: " + str(cmd))
# Attempt to download chat log. If the first attempt with emojis embedded fails, try again without emojis. TDCLI will error out
# there's issues with downloading emojis. We'd rather download the log without them than fail entirely.
#
# If the no-emoji attempt fails as well, we'll assume there's no chat log at all for this video. We'll write a blank .BAD file
# to satisfy future file-exists checks.
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
proc.wait()
if proc.returncode != 0:
print("ERR: Clip has no chat. Either nothing was said or the source VOD is no longer available. Inserting placeholder.")
with open(badchat_log, 'a') as fp:
fp.write(str(video['helix']['id']))
fp.write('\n')
else:
print("GOOD: File moved")
if os.path.exists(file_path_chat_tmp):
shutil.move(file_path_chat_tmp, file_path_chat)
print("\t- done in " + str(time.time() - t0) + " seconds")
# AUDIO-TO-TEXT: check if file exists
file_path_webvtt = path_data + export_folder + filename_format + ".srt"
if not utils.terminated_requested and os.path.exists(file_path) and not os.path.exists(file_path_webvtt) and render_webvtt[idx]:
print("\t- transcribing: " + file_path_webvtt)
t0 = time.time()
# open the model
SetLogLevel(-1)
sample_rate = 16000
# words_per_line = 7
model = Model(path_model)
rec = KaldiRecognizer(model, sample_rate)
rec.SetWords(True)
with subprocess.Popen([ffmpeg_path, "-loglevel", "quiet", "-i",
file_path,
"-ar", str(sample_rate) , "-ac", "1", "-f", "s16le", "-"],
stdout=subprocess.PIPE).stdout as stream:
with open(file_path_webvtt, 'w') as f:
f.write(rec.SrtResult(stream))
print("\t- done in " + str(time.time() - t0) + " seconds")
# send pushover that this twitch vod is ready to edit
text = video['helix']['user_name'] + " vod " + str(video['helix']['id']) \
+ " ready to edit (" + str(int((time.time() - t0_start)/60.0)) + " min to prepare)"
utils.send_pushover_message(conf, text)
# RENDER: check if the file exists
file_path_chat = path_data + export_folder + filename_format + "_chat.json"
file_path_render = path_data + export_folder + filename_format + "_chat.mp4"
file_path_render_tmp = path_temp + str(video['helix']['id']) + "_chat.mp4"
if not utils.terminated_requested and os.path.exists(file_path_chat) and not os.path.exists(file_path_render) and render_chat[idx]:
print("\t- rendering chat: " + file_path_render)
t0 = time.time()
cmd = path_twitch_cli + ' chatrender' \
+ ' -i ' + file_path_chat + ' --ffmpeg-path "' + ffmpeg_path + '"' \
+ ' -h 926 -w 274 --update-rate 0.1 --framerate 60 --font-size 15' \
+ ' --temp-path "' + path_temp + '" -o ' + file_path_render_tmp
# subprocess.Popen(cmd, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).wait()
subprocess.Popen(cmd, shell=True).wait()
if os.path.exists(file_path_render_tmp):
shutil.move(file_path_render_tmp, file_path_render)
print("\t- done in " + str(time.time() - t0) + " seconds")