-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaylist_creator.py
66 lines (53 loc) · 2.55 KB
/
playlist_creator.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
import os
def create_playlist_for_lecture(name):
folder_path = os.path.abspath(f'slokas/{name}') # get absolute path of the directory
playlist_path = f"{folder_path}/playlist.m3u"
with open(playlist_path, 'w') as file:
for root, dirs, files in os.walk(folder_path):
files = [os.path.join(root, name) for name in files] # add directory path to filenames
files = [os.path.abspath(name) for name in files] # convert to absolute paths
for name in sorted(files, key=os.path.getctime):
name = os.path.relpath(name, start=folder_path)
if name.endswith('.mp3'):
file.write(name + '\n')
def create_playlists_for_all_lecture():
for top_root, top_dirs, _ in os.walk('slokas_location_in_lecture'):
for top_dir in top_dirs:
for root, _, files in os.walk(os.path.join(top_root, top_dir)):
for file in files:
name = file.split('.')[0]
path = os.path.join(top_dir, name)
create_playlist_for_lecture(path)
def to_digit(text):
try:
return int(text)
except:
return text
def create_playlist_for_slokas(folder_path):
name = folder_path.split('/')[-1].strip()
playlist_path = f"{folder_path}/{name}.m3u"
sorter = lambda x: [to_digit(dig) for dig in x.split('.')[0].split('_')]
with open(playlist_path, 'w') as file:
for root, dirs, files in os.walk(folder_path):
files = [os.path.join(root, name) for name in files] # add directory path to filenames
files = [os.path.abspath(name) for name in files] # convert to absolute paths
for name in sorted(files, key=sorter):
name = os.path.relpath(name, start=folder_path)
if name.endswith('.mp3'):
file.write(name + '\n')
old_playlist = f"{folder_path}/playlist.m3u"
try:
os.remove(old_playlist)
except:
pass
def create_playlists_for_all_slokas():
for top_root, top_dirs, _ in os.walk('/Users/kishoriji/sadhana/audio/slokas mp3'):
for top_dir in top_dirs:
folder_path = os.path.join(top_root, top_dir)
print(f'creating playlist for: {top_dir}')
create_playlist_for_slokas(folder_path)
if __name__ == '__main__':
# create_playlist_for_lecture('Radha Tattva/Radha Tatva 19-03-09')
# create_playlists_for_lecture()
#create_playlist_for_slokas('/Users/kishoriji/sadhana/audio/slokas mp3/brahm sutras')
create_playlists_for_all_slokas()