my-music-list/main.py
2024-10-05 08:48:49 +00:00

75 lines
3 KiB
Python

import yt_dlp
import os
musiclist = []
with open('musiclist.txt', 'r') as f:
for line in f:
if line.strip() == '':
continue
musicname, videoid, provider = line.strip().split('---')
musiclist.append({"musicname": musicname.strip(), "videoid": videoid.strip(), "provider": provider.strip()})
if os.path.exists('/mnt/sdcard/Music'):
download_location = '/mnt/sdcard/Music'
elif os.path.exists('/storage/emulated/0/Music'):
download_location = '/storage/emulated/0/Music'
elif os.path.exists('/sdcard/Music'):
download_location = '/sdcard/Music'
elif os.environ.get('HOME') is not None:
if os.path.exists(f'{os.environ["HOME"]}/Music'):
download_location = f'{os.environ["HOME"]}/Music'
else:
os.mkdir(f'{os.environ["HOME"]}/Music')
download_location = f'{os.environ["HOME"]}/Music'
else:
download_location = os.getcwd() + '/music'
print(f'Downloading music to {download_location}')
for music in musiclist:
if music["provider"] == 'YT':
youtube_link = f'https://www.youtube.com/watch?v={music["videoid"]}'
elif music["provider"] == 'YTM':
youtube_link = f'https://music.youtube.com/watch?v={music["videoid"]}'
music_name = music["musicname"]
# find anothor better way to remove not allowed characters and remove this stupid part
music_name = music_name.replace('/', '-')
music_name = music_name.replace('@', '')
music_name = music_name.replace('#', '')
music_name = music_name.replace('%', '')
music_name = music_name.replace('$', '')
music_name = music_name.replace('^', '')
music_name = music_name.replace('?', '')
music_name = music_name.replace('"', '')
music_name = music_name.replace("'", '')
music_name = music_name.replace(':', '')
music_name = music_name.replace('|', '')
music_name = music_name.replace('>', '')
music_name = music_name.replace('<', '')
music_name = music_name.replace('\\', '')
music_name = music_name.replace('&', '')
music_name = music_name.replace('+', '')
music_name = music_name.replace('=', '')
music_name = music_name.replace('~', '')
music_name = music_name.replace('`', '')
music_name = music_name.replace(';', '')
music_name = music_name.replace('*', '')
if os.path.exists(f'{download_location}/{music_name}.opus'):
print(f'{music_name} already exists')
continue
print(f'Downloading {music_name} from {youtube_link}')
yt_opts = {
'format': 'bestaudio/best',
'outtmpl': f'{music_name}.%(ext)s',
'postprocessors': [
{'key': 'FFmpegExtractAudio', 'preferredcodec': 'opus'},
{'key': 'FFmpegMetadata', 'add_metadata': True},
{'key': 'EmbedThumbnail'}
],
'writethumbnail': True,
'quiet': True
}
yt = yt_dlp.YoutubeDL(yt_opts)
yt.download([youtube_link])
print(f'{music_name} downloaded')
os.system(f'mv "{music_name}.opus" "{download_location}/{music_name}.opus"')