-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
113 lines (99 loc) · 3.49 KB
/
main.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
#!/usr/bin/python3
"""
Скрипт скачивает плейлисты с YouTube и вырезает аудиодорожки.
Если какие-то видео уже скачаны, они пропускаются.
"""
import json
import os
import re
import sys
import textwrap
import time
import traceback
import yt_dlp
def read_config():
config_content = """
{
/* Путь до каталога с плейлистами */
"path": "./output/path/",
"format": "original", /* Формат звука */
/* Используйте формат "original", чтобы не менять формат звука
и не зависеть от наличия ffmpeg
Форматы:
"aac", "alac", "flac",
"m4a", "mp3", "opus",
"vorbis", "wav"
*/
"playlists": [ /* Ссылки на плейлисты с музыкальными клипами */
"https://www.youtube.com/playlist?list=PLL_example", /* Пример первый */
"https://www.youtube.com/playlist?list=PLL_example2" /* Пример последний */
]
}"""
config_content = textwrap.dedent(config_content).strip()
config_path = os.path.abspath('config.json')
try:
if not os.path.exists(config_path):
with open(config_path, 'w', encoding='utf-8') as f:
f.write(config_content)
print(f"Edit config at {config_path!r}")
input("And then press Enter...")
with open(config_path, encoding='utf-8') as f:
config_content = f.read()
config_content = re.sub(pattern=r'\/\*[\s\S]*?\*\/', repl='', string=config_content)
return json.loads(config_content)
except:
print("Error on read config:")
raise
def output(text):
print('>>>>>>>>>>>>>>>>', text)
def _main():
config = read_config()
playlists_path = os.path.abspath(config['path'])
audio_format = config['format']
playlists = config['playlists']
download_options = dict(
format='bestaudio',
windowsfilenames=(sys.platform == 'win32'),
no_warnings=True,
compat_opts={
'no-youtube-unavailable-videos'
},
postprocessors=[dict(
key='FFmpegExtractAudio',
preferredcodec=audio_format,
)],
download_archive='playlists.cache',
outtmpl='%(playlist_title)s/%(title)s-%(id)s.%(ext)s'
)
if audio_format == 'original':
del download_options['postprocessors']
os.makedirs(playlists_path, exist_ok=True)
os.chdir(playlists_path)
output('Syncing all playlists')
for i, link in enumerate(playlists, 1):
output(f'Start sync #{i}. {link!r}')
retry_count = 0
success = False
with yt_dlp.YoutubeDL(download_options) as downloader:
while not success:
if retry_count:
output(f'Retry sync #{i}, attempt {retry_count}')
time.sleep(0.25*retry_count//2)
try:
downloader.download([link])
success = True
except Exception as e:
success = False
print(e)
retry_count += 1
output(f'Synced #{i}!')
output('Done syncing playlists')
def main():
try:
_main()
except:
traceback.print_exc()
input("Error...")
input("Done...")
if __name__ == '__main__':
main()