-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmeta_getter.py
89 lines (72 loc) · 2.46 KB
/
meta_getter.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
"""
Will keep running in background even if main.py closes
until meta data for all files have been extracted saved
"""
import os
import sys
import json
import subprocess as sp
from url_validate import url_is_valid
ARGS = sys.argv[1:]
cur_dir = os.path.dirname(os.path.realpath(__file__))
os.chdir(cur_dir)
'''
< (less than)
> (greater than)
: (colon - sometimes works, but is actually NTFS Alternate Data Streams)
" (double quote)
/ (forward slash)
\ (backslash)
| (vertical bar or pipe)
? (question mark)
* (asterisk)
'''
illegal_path_chars = list('<>:"/\\|?*')
def get_meta(supported_file_types):
"""
media_list: a list of absolute file paths to all media
supported_file_types: a list of supported file types (usually taken from "main.py")
"""
os.chdir(cur_dir)
valid_medias_list = []
with open('data/snd_files.json', 'r', encoding='utf-8') as fp:
media_list = json.load(fp)
if type(media_list) == str:
try:
if type(eval(media_list)) == list:
media_list = eval(media_list)
except Exception:
pass
for media in media_list:
if os.path.isfile(media):
if media.endswith(tuple(supported_file_types)):
valid_medias_list.append(media)
elif url_is_valid(media):
valid_medias_list.append(media)
for _, media in enumerate(valid_medias_list):
meta_info = sp.run(['ffprobe',
'-v',
'quiet',
'-print_format',
'json',
'-show_format',
'-show_streams',
media],
capture_output=True,
text=True).stdout
meta_info = json.loads(meta_info)
meta_info['format'].update({'bpm': 120})
media = ''.join(['~' if chr in illegal_path_chars else chr for chr in media])
with open(f'data/mediameta_{media}.json', 'w', encoding='utf-8') as fp:
json.dump(meta_info, fp, indent=2)
return valid_medias_list
if __name__ == "__main__":
ARGS = sys.argv[1:]
if len(ARGS) == 1:
try:
get_meta(ARGS[0])
sys.exit(0)
except Exception:
sys.exit(1)
else:
sys.exit(1)