-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathytmd.py
More file actions
96 lines (83 loc) · 3.59 KB
/
ytmd.py
File metadata and controls
96 lines (83 loc) · 3.59 KB
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
import os
import sys
import subprocess
import shutil
import requests
REPO_URL = "https://github.com/Eleazar4628/ytmd.git"
REPO_API_URL = "https://api.github.com/repos/Eleazar4628/ytmd/commits/main"
VERSION_FILE = os.path.join(os.path.expanduser("~"), ".ytmd_version")
def run_upgrade():
print(f"Checking for updates from {REPO_URL}...")
try:
response = requests.get(REPO_API_URL, timeout=5)
if response.status_code == 200:
latest_sha = response.json()['sha']
subprocess.run([sys.executable, "-m", "pip", "install", "--upgrade", f"git+{REPO_URL}"], check=True)
with open(VERSION_FILE, "w") as f:
f.write(latest_sha)
print("✅ Successfully upgraded to the latest version of ytmd.")
else:
print("❌ Could not connect to GitHub to check for updates.")
except Exception as e:
print(f"❌ Upgrade failed: {e}")
def check_for_updates_silently():
try:
response = requests.get(REPO_API_URL, timeout=2)
if response.status_code == 200:
latest_sha = response.json()['sha']
if os.path.exists(VERSION_FILE):
with open(VERSION_FILE, "r") as f:
if f.read().strip() != latest_sha:
print("💡 A new version is available. Use 'ytmd --upgrade' to update.")
except:
pass
def check_ffmpeg():
if shutil.which("ffmpeg") is None:
print("📦 FFmpeg not found.")
choice = input("Install now? (y/n): ").lower()
if choice == 'y':
if os.name == 'nt':
subprocess.run(["winget", "install", "ffmpeg"], check=True)
else:
subprocess.run(["pkg", "install", "ffmpeg", "-y"], check=True)
print("✅ Installed. Please restart your terminal.")
sys.exit(0)
sys.exit(1)
def main():
if len(sys.argv) < 2:
print("\n🎵 ytmd - YouTube Music Downloader")
print("Usage: ytmd <URL> or ytmd --upgrade")
return
arg = sys.argv[1].lower()
if arg in ["--upgrade", "-u"]:
run_upgrade()
return
check_for_updates_silently()
check_ffmpeg()
url = sys.argv[1]
if os.name == 'nt':
base_path = os.path.join(os.path.expanduser("~"), "Music")
else:
base_path = "/sdcard/Music" if os.path.exists("/sdcard") else os.path.expanduser("~/storage/music")
output_template = os.path.join(base_path, "%(artist,uploader)s", "%(album,playlist_title,Unknown_Album)s", "%(title)s.%(ext)s")
command = [
"yt-dlp", "-f", "ba", "-x", "--audio-format", "mp3", "--audio-quality", "0",
"--embed-metadata", "--embed-thumbnail", "--convert-thumbnails", "jpg",
"--ppa", "ThumbnailsConvertor:-vf crop=ih:ih",
"--parse-metadata", "upload_date:%(date)s",
"--replace-in-metadata", "date", r"(\d{4})(\d{2})(\d{2})", r"\1-\2-\3",
"--parse-metadata", "artist:%(artist)s",
"--replace-in-metadata", "artist", r",.*", "",
"--replace-in-metadata", "artist", r" &.*", "",
"--parse-metadata", "title:%(title)s",
"--replace-in-metadata", "title", r"(?i)\s*([\(\[][^\]\)]*(video|audio|lyrics|official|video oficial|hd)[^\]\)]*[\)\]])", "",
"-o", output_template, url
]
try:
print(f"🚀 Initializing download...")
subprocess.run(command, check=True)
print(f"\n✨ Done! Saved in: {base_path}")
except Exception as e:
print(f"\n❌ Error: {e}")
if __name__ == "__main__":
main()