-
Notifications
You must be signed in to change notification settings - Fork 0
/
BSTM.py
810 lines (658 loc) · 32.5 KB
/
BSTM.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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
import os
from os import path
import yt_dlp as youtube_dl
import json
import PySimpleGUI as sg
import PIL
from PIL import Image
import urllib
from tinytag import TinyTag
import webbrowser
import subprocess
import librosa
import audioread
import cv2
import requests
from bs4 import BeautifulSoup
# DONE Maybe implement best of three results ytsearch3 -> Not needed
# DONE Make sure the app doesn't download 10 hour videos! -> Limit is now 1.5x duration of the track or if over 900sec
# DONE Generate video.json
# DONE Enable youtubedl
# DONE Make file paths dynamic
# DONE Window references from functions.py don't work -> use global window
# DONE Display duration of the BS track
# DONE Duration seconds are not display with double digits, if seconds are less than 10
# DONE Download files to right folder
# DONE Set maximum character limit for description -> Max is now 106 characters
# DONE Make sure that .mp4 is the only output format with 720p preferred + audio - 6c79, 360b -> Added format
# TODO A lot of code cleaning
# DONE Implement a decent GUI -> Maybe ok now
# DONE Standardize folder format for searches '0000 (trackname - maker)' -> added to readme
# DONE Extra - symbols in folder names are an issue -> Using rfind now to find the last - symbol
# TODO Web interface? Will probably cause issues
# DONE Get rid of the txt files
# DONE Clean images from the folder after closing
# DONE Exception video.json exists, but is empty -> Added try
# DONE Exception info.dat missing, added try
# DONE Exception for missing data in json files (video.json, info.dat) -> Added try
# DONE Exception missing thumbnail from deleted video -> try, except
# DONE Exception Bring It On crashes search: This video requires payment to watch -> Added try
# DONE Quick filter for tracks
# TODO Add delete function
# DONE Easy way for user to read error log -> Using sg.popup
# DONE Add file size display
# TODO Implement proper support for multiple video files
# DONE Video titles with unsupported symbols cause issues during download * / \ ? < > | -> Replace function
# DONE youtubeDL turns " into ' and : into - -> Replace function
# DONE 2df2 crashes auto offset -> Offset tool not very successful, added try
# DONE Listbox chooses lowest option, if empty space clicked -> Feature, not a bug
# TODO Update video.json format to the next MVP version
# TODO Bad url in video.json crashes app, probably a lot more exceptions than just this
# TODO Try to reduce app size
# DONE Download audio and video separately -> Fixed by always merging mp4 with m4a
# TODO Check 3e24, there's some problem with the mp4 file
# DONE HTTP Error 429: Too Many Requests -> Added backup search
# TODO Catch Youtube SSL error CERTIFICATE_VERIFY_FAILED
# DONE Window resizing
# Verify if the browsed CustomLevels folder is valid and write the location to config.ini
def browse_bs_folder():
event, values = window.read(timeout=0)
if values['bs_folder'].find('CustomLevels') == -1:
print('This is not the custom tracks folder!')
window['tracklist'].update('')
window['track_filter'].update(disabled=True)
window['filter'].update(visible=False)
window.Refresh()
return False
else:
f = open('config.ini', 'w', encoding='utf-8')
f.writelines(values['bs_folder'])
f.close()
window['track_filter'].update(disabled=False)
window['filter'].update(visible=True)
check_tracks()
# Check config.ini for the bs folder location
def config_bs_folder():
if not path.exists('config.ini'):
return
f = open('config.ini', 'r', encoding='utf-8')
verifyfolder = f.read()
window['bs_folder'].update(verifyfolder)
f.close()
if verifyfolder.find('CustomLevels') == -1:
print('This is not the custom tracks folder!')
window['tracklist'].update('')
window['track_filter'].update(disabled=True)
window['filter'].update(visible=False)
window.Refresh()
else:
check_tracks()
# Clears displayed info in the GUI
def clear_info(clear):
if not clear:
clear = ['track_name_and_author', 'track_duration', 'cover_image', 'video_name', 'video_duration', 'thumbnail',
'video_size', 'offset']
for i in clear:
window[i].update('')
window['Auto Offset'].update(disabled=True)
# Read and print the CustomLevels folder
def check_tracks():
event, values = window.read(timeout=0)
global tracksWithVideo
global tracksWithNoVideo
global allTracks
tracksWithVideo = []
tracksWithNoVideo = []
allTracks = []
try:
tracks = os.listdir(values['bs_folder'])
for i in tracks:
print(i)
allTracks.append(i)
my_file = values['bs_folder'] + '\\' + i + '\\video.json'
if os.path.isfile(my_file):
print('yes')
tracksWithVideo.append(i)
else:
tracksWithNoVideo.append(i)
# Python doesn't have case/switch statement?!
if values['track_filter'] == 'All tracks':
window['tracklist'].update(allTracks)
elif values['track_filter'] == 'Tracks without video':
window['tracklist'].update(tracksWithNoVideo)
elif values['track_filter'] == 'Tracks with video':
window['tracklist'].update(tracksWithVideo)
except OSError:
print('CustomLevels folder is not valid!')
sg.popup('CustomLevels folder is not valid.\nPlease select the CustomLevels folder from the file menu.')
window.Refresh()
# Replace unsupported symbols from file names, uses same logic as youtubeDl
def replace_symbols(filename):
# These symbols don't work on filenames and also ? " :
ng_symbols = ['*', '/', '\\', '<', '>', '|']
for i in ng_symbols:
filename = filename.replace(i, '_')
filename = filename.replace('?', '')
filename = filename.replace(':', ' -')
filename = filename.replace('\"', '\'')
return filename
# Fetches and updates video.json to the newest format
def fetch_video_json(track_fullPath) -> bool:
if not os.path.isfile(track_fullPath + '/video.json'):
return False
with open(track_fullPath + '/video.json', 'r+', encoding='utf8') as f:
try:
video_json = json.load(f)
# Check if the json is in the new format, if not convert it
# Currently only the active video in the json file is grabbed
if 'videos' not in video_json:
video_json = {'activeVideo': 0, 'videos': [video_json]}
av = video_json['activeVideo'] # Grab only the active video
# Fix lowercase key
if 'videopath' in video_json['videos'][av]:
video_json['videos'][av]['videoPath'] = video_json['videos'][av]['videopath']
del video_json['videos'][av]['videopath']
# Clean unsupported symbols from from videoPath
video_json['videos'][av]['videoPath'] = replace_symbols(video_json['videos'][av]['videoPath'])
# Save video.json with the changes
f.seek(0) # Return to the beginning of the file
json.dump(video_json, f, ensure_ascii=False)
f.truncate() # Clean old data from file
return video_json
# Skip printing video info, if video.json is in weird format or empty
except json.decoder.JSONDecodeError:
print('video.json empty or not in proper format!')
window['video_name'].update('video.json empty or not in proper format!')
return False
# Updates track information when a track is selected from the list
def select_track():
event, values = window.read(timeout=0)
global video_exists
global track_seconds
clear_info('')
if not values['tracklist']:
print('No tracks!')
return
selectedTrack = str(values['tracklist'])
track_path = current_track_fullpath()
try:
with open(track_path + '/info.dat', encoding='utf8') as f:
info_dat = True
track_json = json.load(f)
img = PIL.Image.open(track_path + '/' + track_json['_coverImageFilename'])
img.thumbnail((200, 200))
img.save('cover.png')
f.close()
tag = TinyTag.get(track_path + '/' + track_json['_songFilename'])
track_seconds = tag.duration # Variable to check video length vs track length
# Convert duration into m:ss
track_duration = str(int(tag.duration / 60))
track_duration = track_duration + ':' + str(int(tag.duration % 60)).zfill(2)
# Update track fields
window['cover_image'].update('cover.png')
window['track_name_and_author'].update(
track_json['_songName'] + ' - ' + track_json['_levelAuthorName'])
# window['track_author'].update(track_json['_levelAuthorName'])
window['track_duration'].update(track_duration)
# Reset fields and set duration to 600 seconds, if info.dat is not found
except FileNotFoundError:
info_dat = False
print('Missing info.dat or cover image!')
track_seconds = 600
window['track_name_and_author'].update('info.dat or cover image missing!')
# clear_info(['cover_image', 'track_duration'])
# Reset fields and set duration to 600 seconds, if info.dat is in weird format or empty
except json.decoder.JSONDecodeError:
info_dat = False
print('info.dat empty or not in proper format!')
track_seconds = 600
window['track_name_and_author'].update('info.dat empty or not in proper format!')
# clear_info(['cover_image', 'track_duration'])
# Grab video info
video_json = fetch_video_json(track_path)
if video_json is not False:
av = video_json['activeVideo'] # Grab only the active video
print(video_json['videos'][av]['title'])
print(video_json['videos'][av]['duration'])
print(video_json['videos'][av]['thumbnailURL'])
# Find video thumbnail
try:
urllib.request.urlretrieve(video_json['videos'][av]['thumbnailURL'], 'thumbnail.png')
except urllib.error.HTTPError:
urllib.request.urlretrieve('https://s.ytimg.com/yts/img/no_thumbnail-vfl4t3-4R.jpg', 'thumbnail.png')
except ConnectionResetError:
print('Could not load thumbnail')
thumbnailLoaded = False
try:
img = PIL.Image.open('thumbnail.png')
img.thumbnail((360, 200))
img.save('thumbnail.png')
thumbnailLoaded = True
except (RuntimeError, OSError):
print('Could not load thumbnail')
# Update fields
window['video_name'].update(video_json['videos'][av]['title'])
window['video_duration'].update(video_json['videos'][av]['duration'].replace('.', ':'))
if thumbnailLoaded:
window['thumbnail'].update('thumbnail.png')
window['offset'].update('{}{}'.format('Offset: ', video_json['videos'][av]['offset']), visible=True)
window['Auto Offset'].update(disabled=False)
# Find video in folder and print info
try:
video_path = video_json['videos'][av]['videoPath']
video_size = os.stat(track_path + '/' + video_path).st_size / 1000000
# Grab video height
# TODO This doesn't work on some videos, results show up as 0p
cv2video = cv2.VideoCapture(track_path + '/' + video_path)
video_height = cv2video.get(cv2.CAP_PROP_FRAME_HEIGHT)
cv2.VideoCapture.release(cv2video)
video_exists = True
window['video_size'].update(
'{}{:.2f}{}{:.0f}p'.format('Video downloaded - ', video_size, ' MB - ', video_height), visible=True)
except FileNotFoundError:
print('Video not found!')
video_exists = False
window['video_size'].update('Video file not found in folder', visible=True)
else:
video_exists = False
# If track contains proper info.dat, display track name and author on the search field
if info_dat:
selectedTrack = track_json['_songName'] + ' ' + track_json['_songAuthorName']
# Otherwise cut the excess text from the folder name, based on ( and - symbols. Display on search field
else:
selectedTrack = selectedTrack[selectedTrack.find('(') + 1:selectedTrack.rfind('-') - 1]
window['search_field'].update(selectedTrack)
window['Download'].update(disabled=True)
window.Refresh()
# Run search with the search term, only grabs the 1st result
def search_youtube() -> bool:
event, values = window.read(timeout=0)
clear_info(['video_size', 'offset'])
global info
global video_path
global data_set
if not values['search_field']:
print('Search field empty!')
sg.popup('Please input a search term.\n')
return False
if not values['tracklist']:
print('No track selected!')
sg.popup('Please select a track from the list.\n')
return False
try:
# Run youtube search with youtubeDl
ydl = youtube_dl.YoutubeDL({'outtmpl': '%(id)s.%(ext)s'})
# In some cases users might get HTTP Error 429 if they download too much
# Since ytsearch doesn't support cookies this will result in a download error
info = ydl.extract_info('ytsearch:' + values['search_field'], download=False)
if len(info['entries']) > 0:
info = info['entries'][0] # Grab only the first entry, in case the result is a playlist
else:
print("No video found")
return False
# This error should occur when video is not found or accessible, for example error 429
except youtube_dl.utils.DownloadError:
try:
# If user has cookies file, run another search with beautiful soup, this can workaround the 429 error
if os.path.exists('cookies.txt'):
print('Cookies are available, running another search with soup')
page = requests.get('https://www.youtube.com/results?search_query=' + values['search_field'].replace(' ', '+'))
soup = BeautifulSoup(page.content, 'html.parser')
soup = soup.find_all('script')
# Grab the video id from the search page
text = str(soup) # Convert tag object in string, result 26 in tag object contains the video id
video_id = text.find('watch?') # Finding the first video result
video_id = text[video_id + 8:video_id + 19]
print('https://www.youtube.com/watch?v=' + video_id)
# Run normal youtubedl url extractor, this one supports cookies
ydl = youtube_dl.YoutubeDL({'outtmpl': '%(id)s.%(ext)s', 'cookiefile': 'cookies.txt'})
info = ydl.extract_info('https://www.youtube.com/watch?v=' + video_id, download=False)
else:
print('No video found or unable to download video!')
sg.popup('No video found or unable to download video\n')
return False
except youtube_dl.utils.DownloadError:
print('No video found or unable to download video!')
sg.popup('No video found or unable to download video\n')
return False
except ConnectionResetError:
return False
# Print out the extracted video information for debug purposes
print(info)
print(info['title'])
print(info['uploader'])
# print(info['description'])
print(info['duration']) # Printed in seconds, convert this
print(info['webpage_url'])
print(info['thumbnail'])
# Convert duration into mm:ss
# Youtube seems to be off by a second from youtubedl, good enough I guess
video_duration = str(int(info['duration'] / 60))
video_duration = video_duration + ':' + str(info['duration'] % 60).zfill(2)
# Shorten webpage url
webpage_url = info['webpage_url'][info['webpage_url'].find('/watch'):-1]
try:
urllib.request.urlretrieve(info['thumbnail'], 'thumbnail.png')
except ConnectionResetError:
return False
img = PIL.Image.open('thumbnail.png')
img = img.resize((360, 200))
img.save('thumbnail.png')
window['thumbnail'].update('thumbnail.png')
window['video_name'].update(info['title'])
window['video_duration'].update(video_duration)
window.Refresh()
# Build video.json, "loop":false fixed in a hacky way- :D, limited description to 106 characters
# Unsupported symbols in Windows cause issues, running function to clean those up
video_path = replace_symbols(info['title'])
# Remove everything from thumbnail url starting from ? symbol
thumbnail_url = info['thumbnail']
if thumbnail_url.find('?') != -1:
thumbnail_url = thumbnail_url[0:thumbnail_url.find('?')]
print(thumbnail_url)
# Dataset for video.json
data_set = {'activeVideo': 0, 'videos': [
{'title': info['title'], 'author': info['uploader'],
'description': info['description'][0:106] + ' ...',
'duration': video_duration, 'URL': webpage_url, 'thumbnailURL': thumbnail_url,
'loop': 'f' + 'alse', 'offset': 0, 'videoPath': video_path + '.mp4'}], 'Count': 1}
# video.json debug
print(json.dumps(data_set, ensure_ascii=False))
window['Download'].update(disabled=False)
# TODO Fix file size grab and add resolution information
# print(info)
# video_size = int(info['formats'][0]['filesize']) #/ 1000000
# window['video_size'].update('{:.2f}{}'.format(video_size, ' MB'), visible=True)
window.Refresh()
return True
# Download the video, when download button is pressed
def download_video(track_fullPath) -> bool:
event, values = window.read(timeout=0)
av = data_set['activeVideo']
if not values['tracklist']:
print('No track selected!')
return False
if not info:
print('No video selected!')
return False
# Delete previous video
try:
if video_exists and os.path.isfile(track_fullPath + '/' + data_set['videos'][av]['videoPath']):
os.remove(track_fullPath + '/' + data_set['videos'][av]["videoPath"])
except IOError:
print('Could not delete previous video.')
sg.popup('Could not delete previous video.\nPlease delete it manually from the folder.')
download = info['webpage_url']
print(download)
# Display warning, if video is 1.5 times longer than the bs track
# TODO Add a confirmation window here
# If info.dat is missing, the limit is 900 seconds
if info['duration'] > track_seconds * 1.5:
print('Video is way longer than the track!')
window['video_size'].update('Video is way longer than the track.', visible=True)
window.Refresh()
return False
# Save video.json, encoding in utf8, otherwise there will be problems with MVP
with open(track_fullPath + '/video.json', 'w', encoding='utf8') as outfile:
json.dump(data_set, outfile, ensure_ascii=False, indent=4)
# Download the video
# TODO See if there is a way to overwrite previous video
try:
youtube_dl.YoutubeDL({'format': 'mp4[height>=480][height<1080]+bestaudio[ext=m4a]',
'cookiefile':'cookies.txt',
'outtmpl': track_fullPath + '/%(title)s.%(ext)s'}).download(
[download]) # Outputs title + extension
except youtube_dl.utils.DownloadError:
print('Unable to download video format, lowering requirements.')
youtube_dl.YoutubeDL({'format': 'mp4[height<=720]+bestaudio[ext=m4a]',
'outtmpl': track_fullPath + '/%(title)s.%(ext)s'}).download(
[download])
try:
av = data_set['activeVideo']
#video_path = data_set['videos'][av]['videoPath']
# Patchwork fix for bad symbols in filenames
# The symbol replacement function is not enough in itself
files = os.listdir(track_fullPath + '/')
for filename in files:
if not filename.endswith('.mp4'):
continue
with open(track_fullPath + '/video.json', 'r', encoding='UTF-8') as f:
trackData = json.load(f)
if trackData['videos'][av]['videoPath'] != filename:
trackData['videos'][av]['videoPath'] = filename
with open(track_fullPath + '/video.json', 'w', encoding='UTF-8') as f:
json.dump(trackData, f)
video_size = os.stat(track_fullPath + '/' + trackData['videos'][av]['videoPath']).st_size / 1000000
cv2video = cv2.VideoCapture(track_fullPath + '/' + trackData['videos'][av]['videoPath'])
video_height = cv2video.get(cv2.CAP_PROP_FRAME_HEIGHT)
cv2.VideoCapture.release(cv2video)
window['video_size'].update(
'{}{:.2f}{}{:.0f}p'.format('Video downloaded - ', video_size, ' MB - ', video_height),
visible=True)
window['Auto Offset'].update(disabled=False)
window['offset'].update('{}{}'.format('Offset: ', data_set['videos'][0]['offset']),
visible=True)
return True
except OSError:
print('Could not grab video file size, probably because of a weird symbol in filename')
window['video_size'].update('Video downloaded - File size not available', visible=True)
return True
# Calculate audio/video offset using MVP's tools or librosa
def run_auto_offset(offsetMethod: int, trackFullPath: str):
event, values = window.read(timeout=0)
if not os.path.exists(trackFullPath) or not os.path.isfile(trackFullPath + '/video.json') or not os.path.isfile(
trackFullPath + '/info.dat'):
print('Track folder is not valid!')
return
# Offset using MVP
if offsetMethod == 0:
window['offset'].update('Calculating offset...', visible=True)
window.Refresh()
offset_tool = values['bs_folder'][0:values['bs_folder'].find('Beat Saber_Data')]
offset_tool = '\"' + offset_tool + 'Youtube-dl/SyncVideoWithAudio/SyncVideoWithAudio.exe' + '\"' + ' offset'
with open(trackFullPath + '/info.dat', 'r', encoding='utf8') as outfile:
track_json = json.load(outfile)
offset_audio = ' \"' + trackFullPath + '/' + track_json['_songFilename'] + '\"'
print(offset_audio)
with open(trackFullPath + '/video.json', 'r', encoding='utf8') as outfile:
video_json = json.load(outfile)
av = video_json['activeVideo']
offset_video = ' \"' + trackFullPath + '/' + video_json['videos'][av]['videoPath'] + '\"'
print(offset_video)
try:
offset = subprocess.run(offset_tool + offset_audio + offset_video, shell=True, capture_output=True,
check=True, timeout=15)
# Convert results to str and search the string for the result. There's probably an easier way for this.
offset = str(offset)
print(offset)
offset = offset[offset.rfind('Results: ') + 8:offset.find(',', offset.rfind('Results: '))]
print(offset)
offset = int(float(offset))
offset *= -1 # Values are flipped in video.json
window['offset'].update('{}{}'.format('Offset: ', offset), visible=True)
with open(trackFullPath + '/video.json', 'r+', encoding='utf8') as outfile:
json_offset = json.load(outfile)
json_offset['videos'][av]['offset'] = offset
outfile.seek(0) # Return to the beginning of the file
json.dump(json_offset, outfile, ensure_ascii=False)
outfile.truncate() # Clean old data from file
except subprocess.CalledProcessError:
window['offset'].update('Could not calculate offset', visible=True)
except subprocess.TimeoutExpired:
window['offset'].update('Could not calculate offset', visible=True)
except ValueError:
window['offset'].update('Could not calculate offset', visible=True)
# Offset with librosa, usually faster
elif offsetMethod == 1:
with open(trackFullPath + '/info.dat', 'r', encoding='utf8') as outfile:
track_name = json.load(outfile)
track_name = trackFullPath + '/' + track_name['_songFilename']
with open(trackFullPath + '/video.json', 'r', encoding='utf8') as outfile:
track_video = json.load(outfile)
av = track_video['activeVideo']
track_video = trackFullPath + '/' + track_video['videos'][av]['videoPath']
try:
y, sr = librosa.load(track_name, duration=8)
track_onset = librosa.frames_to_time(librosa.onset.onset_detect(y=y))
y, sr = librosa.load(track_video, duration=8)
video_onset = librosa.frames_to_time(librosa.onset.onset_detect(y=y))
print(track_onset[0] - video_onset[0])
offset = int(round((track_onset[0] - video_onset[0]) * -1, 3) * 1000)
print(offset)
window['offset'].update('{}{}'.format('Offset: ', offset), visible=True)
with open(trackFullPath + '/video.json', 'r+', encoding='utf8') as outfile:
json_offset = json.load(outfile)
json_offset['videos'][av]['offset'] = offset
outfile.seek(0) # Return to the beginning of the file
json.dump(json_offset, outfile, ensure_ascii=False)
outfile.truncate() # Clean old data from file
except audioread.exceptions.NoBackendError:
print('No audio track found in video. Are you missing ffmpeg.exe?')
window['offset'].update('Video has no audio track.', visible=True)
except IndexError:
print('No onset found on audio track. Onset analyze duration might be too low.')
window['offset'].update('No audio found.', visible=True)
except FileNotFoundError:
print('No video found!.')
window['offset'].update('No video found.', visible=True)
def current_track_fullpath() -> str:
event, values = window.read(timeout=0)
trackName = str(values['tracklist']) # Chosen track
if not trackName:
return ''
return values['bs_folder'] + '/' + trackName.replace('[\'', '').replace('\']', '').replace('"]', '').replace('["', '')
# GUI loop
def create_GUI():
sg.theme('Dark Teal 11')
menu_def = [['File', ['Select CustomLevels Folder', 'Exit']],
['Settings', ['Use Normal Auto Offset', 'Use Fast Auto Offset X']],
['Help', 'About'], ]
menu_elem = sg.Menu(menu_def)
col1 = [
[sg.Text(size=(38,2), key='track_name_and_author')],
[sg.Text(size=(38,1), key='track_duration')],
[sg.Image(key='cover_image')],
[sg.Text('', size=(38, 1))], # Empty line
[sg.Text('', key='video_name', size=(38, 2))],
[sg.Text('', key='video_duration', size=(38, 1))],
[sg.Image(r'', key='thumbnail')],
[sg.Text('Video Downloaded - ', size=(38, 1), visible=False, key='video_size')],
[sg.Text('Offset ', size=(38, 1), visible=False, key='offset')]
]
layout = [
[menu_elem],
[sg.Combo(values=('All tracks', 'Tracks without video', 'Tracks with video'),
default_value='All tracks', enable_events=True, key='track_filter', size=(30, 1)),
sg.Text('Filter'), sg.InputText(size=(10, 1), key='filter', enable_events=True)],
[sg.Listbox(values='', size=(50, 34), enable_events=True, key='tracklist',
right_click_menu=['&Right', ['Search Youtube', 'Open track folder', 'Automate']]), sg.Column(col1, vertical_alignment="top")],
[sg.Text('Youtube Search Term:')],
[sg.Input('', size=(50, 1), key='search_field')],
[sg.Button('Search Youtube', bind_return_key=True),
sg.Button('Download', disabled=True), sg.InputText(enable_events=True, key='bs_folder', visible=False),
sg.Button('Auto Offset', disabled=True)]
]
global window
windowSize = (953, 783) # Default window size
window = sg.Window('Beat Saber Track Manager', layout, font='Courier 12', icon='BSTM.ico', size=(windowSize), resizable=True).finalize()
# Check for config.ini
config_bs_folder()
offsetMethod = 1 # Use fast auto offset as default
while True:
event, values = window.read(timeout=1000)
if event == sg.WIN_CLOSED or event == 'Exit': # if user closes window or clicks cancel
if os.path.exists('cover.png'):
os.remove('cover.png')
if os.path.exists('thumbnail.png'):
os.remove('thumbnail.png')
break
# Grab the text from the chosen listbox choice and paste it into searchbox in readable format.
# Also grabs the data from the track's info.dat
if event == 'tracklist':
#info = None # Clear video info from previous searches
clear_info('')
select_track()
# Run search with the search term, only grabs the 1st result
if event == 'Search Youtube':
search_youtube()
# Download the video, when download button is pressed
if event == 'Download':
download_video(current_track_fullpath())
# Calculate audio/video offset using MVP's tools or librosa
if event == 'Auto Offset':
run_auto_offset(offsetMethod, current_track_fullpath())
if event == 'bs_folder':
browse_bs_folder()
if event == 'track_filter':
check_tracks()
if event == 'Open track folder':
try:
trackFullPath = current_track_fullpath()
print(trackFullPath)
os.startfile(trackFullPath)
except UnboundLocalError:
print('No track selected')
except FileNotFoundError:
print('No track selected')
if event == 'Automate':
current_track = current_track_fullpath()
if not search_youtube():
continue
print('search success')
if not download_video(current_track_fullpath()):
continue
print('dl success')
run_auto_offset(offsetMethod, current_track)
if event == 'Select CustomLevels Folder':
trackFolder = sg.popup_get_folder('', title='Select CustomLevels folder',
no_window=True, modal=True, keep_on_top=True)
if not trackFolder:
print('No folder selected!')
else:
clear_info('')
window['bs_folder'].update(trackFolder)
browse_bs_folder()
if event == 'filter':
results = []
if values['track_filter'] == 'All tracks':
filter_list = allTracks
elif values['track_filter'] == 'Tracks without video':
filter_list = tracksWithNoVideo
elif values['track_filter'] == 'Tracks with video':
filter_list = tracksWithVideo
else:
filter_list = allTracks
if len(values['filter']) < 2:
window['tracklist'].update(filter_list)
# Don't filter, if the filter has less than 3 characters
if len(values['filter']) < 3:
continue
clear_info('')
for i in filter_list:
if (values['filter']) in i.lower():
results.append(i)
window['tracklist'].update(results)
window.Refresh()
if event == 'Use Normal Auto Offset':
offsetMethod = 0
menu_def = [['File', ['Select CustomLevels Folder', 'Exit']],
['Settings', ['Use Normal Auto Offset X', 'Use Fast Auto Offset']],
['Help', 'About'], ]
menu_elem.Update(menu_def)
if event == 'Use Fast Auto Offset':
offsetMethod = 1
menu_def = [['File', ['Select CustomLevels Folder', 'Exit']],
['Settings', ['Use Normal Auto Offset', 'Use Fast Auto Offset X']],
['Help', 'About'], ]
menu_elem.Update(menu_def)
if event == 'About':
print(window.size)
webbrowser.open('https://github.com/CuriousCod/BeatSaberTrackManager/tree/master')
if windowSize != window.size:
print(window.size)
CurrentWindowSize = window.size
TracksElementSize = (int(CurrentWindowSize[0] * 0.1) - 45, int(CurrentWindowSize[1] * 0.044))
print(TracksElementSize)
window['tracklist'].set_size(TracksElementSize)
windowSize = window.size
window.close()