sdm-pycli 1.1.1__tar.gz → 1.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sdm_pycli-1.2.0/PKG-INFO +253 -0
- sdm_pycli-1.2.0/README.md +229 -0
- {sdm_pycli-1.1.1 → sdm_pycli-1.2.0}/pyproject.toml +1 -1
- {sdm_pycli-1.1.1 → sdm_pycli-1.2.0}/sdm/__init__.py +1 -1
- {sdm_pycli-1.1.1 → sdm_pycli-1.2.0}/sdm/cli.py +54 -18
- sdm_pycli-1.2.0/sdm/download.py +535 -0
- {sdm_pycli-1.1.1 → sdm_pycli-1.2.0}/sdm/metadata.py +20 -8
- sdm_pycli-1.2.0/sdm/sync.py +183 -0
- sdm_pycli-1.2.0/sdm_pycli.egg-info/PKG-INFO +253 -0
- sdm_pycli-1.1.1/PKG-INFO +0 -168
- sdm_pycli-1.1.1/README.md +0 -144
- sdm_pycli-1.1.1/sdm/download.py +0 -422
- sdm_pycli-1.1.1/sdm/sync.py +0 -103
- sdm_pycli-1.1.1/sdm_pycli.egg-info/PKG-INFO +0 -168
- {sdm_pycli-1.1.1 → sdm_pycli-1.2.0}/LICENSE +0 -0
- {sdm_pycli-1.1.1 → sdm_pycli-1.2.0}/sdm_pycli.egg-info/SOURCES.txt +0 -0
- {sdm_pycli-1.1.1 → sdm_pycli-1.2.0}/sdm_pycli.egg-info/dependency_links.txt +0 -0
- {sdm_pycli-1.1.1 → sdm_pycli-1.2.0}/sdm_pycli.egg-info/entry_points.txt +0 -0
- {sdm_pycli-1.1.1 → sdm_pycli-1.2.0}/sdm_pycli.egg-info/requires.txt +0 -0
- {sdm_pycli-1.1.1 → sdm_pycli-1.2.0}/sdm_pycli.egg-info/top_level.txt +0 -0
- {sdm_pycli-1.1.1 → sdm_pycli-1.2.0}/setup.cfg +0 -0
sdm_pycli-1.2.0/PKG-INFO
ADDED
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: sdm-pycli
|
|
3
|
+
Version: 1.2.0
|
|
4
|
+
Summary: A fast, zero-config CLI tool to download and sync playlists from Spotify, Apple Music, Tidal, YouTube, and SoundCloud.
|
|
5
|
+
Author: Antares699
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/Antares699/sdm
|
|
8
|
+
Project-URL: Bug Tracker, https://github.com/Antares699/sdm/issues
|
|
9
|
+
Keywords: spotify,apple-music,tidal,youtube,soundcloud,download,sync,music,cli
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Environment :: Console
|
|
14
|
+
Requires-Python: >=3.8
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
License-File: LICENSE
|
|
17
|
+
Requires-Dist: yt-dlp>=2024.03.10
|
|
18
|
+
Requires-Dist: mutagen>=1.47.0
|
|
19
|
+
Requires-Dist: requests>=2.31.0
|
|
20
|
+
Requires-Dist: rich>=13.7.1
|
|
21
|
+
Requires-Dist: typer>=0.12.0
|
|
22
|
+
Requires-Dist: imageio-ffmpeg>=0.4.9
|
|
23
|
+
Dynamic: license-file
|
|
24
|
+
|
|
25
|
+
# SDM - SDM Downloads Music
|
|
26
|
+
|
|
27
|
+
`sdm` is a fast, lightweight, and zero-config CLI tool to download and sync Playlists, Albums, and Tracks from **Spotify, Apple Music, Tidal, YouTube, and SoundCloud**.
|
|
28
|
+
|
|
29
|
+
## Why sdm?
|
|
30
|
+
|
|
31
|
+
Recently, Spotify restricted their Web API, requiring developers to have a Premium subscription, which broke popular open-source tools with `403 Forbidden` errors.
|
|
32
|
+
|
|
33
|
+
`sdm` was built to completely bypass these restrictions. It uses native web scraping to extract flawless metadata directly from streaming services, pairs it with `yt-dlp` to fetch the highest-quality audio, and embeds the tags natively. No API keys required.
|
|
34
|
+
|
|
35
|
+
## Features
|
|
36
|
+
|
|
37
|
+
- **Universal Support:** Spotify, Apple Music, Tidal, YouTube, and SoundCloud.
|
|
38
|
+
- **Smart Syncing:** Two-way mirroring. Run `sdm sync` to fetch new tracks and clean up removed ones.
|
|
39
|
+
- **Multi-Format:** Download in M4A (default), MP3, FLAC, or OPUS with native tags.
|
|
40
|
+
- **Perfect Metadata:** Embeds Track, Artist, Album, Cover Art, Track/Disc Numbers, Genre, and Release Year.
|
|
41
|
+
- **Advanced Processing:** Synced lyrics via LRCLIB, LUFS normalization, and SponsorBlock trimming.
|
|
42
|
+
|
|
43
|
+
## Installation
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
pip install sdm-pycli
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Usage
|
|
50
|
+
|
|
51
|
+
Open a terminal in the folder where you want your music saved, then run the commands below.
|
|
52
|
+
|
|
53
|
+
### Playlist
|
|
54
|
+
|
|
55
|
+
To download a playlist, run
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
sdm download [playlistUrl]
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Example:
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
sdm download https://open.spotify.com/playlist/37i9dQZF1E8NjgPSXnmGkI?si=O0kgUaFcQnatcvlZzS9yJw
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Album
|
|
68
|
+
|
|
69
|
+
To download an album, run
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
sdm download [albumUrl]
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Example:
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
sdm download https://open.spotify.com/album/6eUW0wxWtzkFdaEFsTJto6?si=nYD6g_tZQvuFsMzyKG2sRA
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Track
|
|
82
|
+
|
|
83
|
+
To download a single track, run
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
sdm download [trackUrl]
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Example:
|
|
90
|
+
|
|
91
|
+
```bash
|
|
92
|
+
sdm download https://open.spotify.com/track/4PTG3Z6ehGkBFwjybzWkR8?si=1d83934b63464e9e
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### Apple Music
|
|
96
|
+
|
|
97
|
+
Apple Music albums, playlists, and songs are also supported.
|
|
98
|
+
|
|
99
|
+
```bash
|
|
100
|
+
sdm download https://music.apple.com/us/album/starboy/1440871397
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### Sync
|
|
104
|
+
|
|
105
|
+
`sdm` remembers the source URL for each folder. To update a previously downloaded folder with any changes from the remote playlist, run
|
|
106
|
+
|
|
107
|
+
```bash
|
|
108
|
+
sdm sync [directory]
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Example:
|
|
112
|
+
|
|
113
|
+
```bash
|
|
114
|
+
sdm sync "C:\Users\You\Music\My Playlist"
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### Inject
|
|
118
|
+
|
|
119
|
+
To import a local audio file into your playlist with official metadata and protect it from future sync deletions, run
|
|
120
|
+
|
|
121
|
+
```bash
|
|
122
|
+
sdm inject [filePath] [trackUrl]
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
Example:
|
|
126
|
+
|
|
127
|
+
```bash
|
|
128
|
+
sdm inject "C:\Users\You\Downloads\song.mp3" https://open.spotify.com/track/0VjIjW4GlUZAMYd2vXMi3b
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## Options & Flags
|
|
132
|
+
|
|
133
|
+
| Flag | Description |
|
|
134
|
+
|---|---|
|
|
135
|
+
| `-o, --output` | Save to a specific directory |
|
|
136
|
+
| `-f, --format` | Audio format: `m4a`, `mp3`, `flac`, `opus` (default: `m4a`) |
|
|
137
|
+
| `-w, --workers` | Number of concurrent downloads (default: `3`) |
|
|
138
|
+
| `--lyrics` | Fetch and embed synced lyrics from LRCLIB |
|
|
139
|
+
| `--normalize` | Apply EBU R128 (-14 LUFS) volume normalization |
|
|
140
|
+
| `--sponsor-block` | Trim non-music sections from YouTube sources |
|
|
141
|
+
| `--dry-run` | Simulate a sync/download without making changes |
|
|
142
|
+
| `--no-delete` | Download new tracks but never delete local files |
|
|
143
|
+
| `--refresh-metadata` | Re-tag existing files with the latest metadata |
|
|
144
|
+
| `--cookies` | Pass browser cookies for age-restricted content |
|
|
145
|
+
|
|
146
|
+
Example with flags:
|
|
147
|
+
|
|
148
|
+
```bash
|
|
149
|
+
sdm download https://open.spotify.com/playlist/37i9dQZF1E8UXBoz02kGID -f flac --lyrics --normalize
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## Music Sourcing & Audio Quality
|
|
153
|
+
|
|
154
|
+
`sdm` fetches flawless metadata (tags, cover art, tracklists) directly from Spotify, Apple Music, and Tidal, but sources the actual audio streams from YouTube.
|
|
155
|
+
|
|
156
|
+
Unlike other tools that require your Premium credentials and risk permanent account bans by ripping directly from encrypted servers, `sdm` acts as a safe, unauthenticated metadata matcher. It reliably delivers DRM-free audio (up to 256kbps AAC or Opus) perfectly packaged to match the original release.
|
|
157
|
+
|
|
158
|
+
## Optimization
|
|
159
|
+
|
|
160
|
+
`sdm` v1.2.0 implements nine optimization techniques across five categories to significantly reduce network overhead, disk I/O, startup time, and download accuracy.
|
|
161
|
+
|
|
162
|
+
### 1. Application-Level Caching
|
|
163
|
+
|
|
164
|
+
**Problem:** Every track triggered its own HTTP requests for cover art and iTunes metadata, even when all tracks in an album share the same cover image and genre.
|
|
165
|
+
|
|
166
|
+
**Fix:** Thread-safe in-memory caches (`_cover_cache`, `_itunes_cache`) store results keyed by URL and `(artist, track)` respectively. A cache hit skips the HTTP request entirely.
|
|
167
|
+
|
|
168
|
+
| Metric | Before | After |
|
|
169
|
+
|---|---|---|
|
|
170
|
+
| Cover art downloads (14-track album) | 14 HTTP requests | 1 HTTP request |
|
|
171
|
+
| iTunes metadata lookups (14-track album) | 14 HTTP requests | ~1-2 HTTP requests |
|
|
172
|
+
| Total HTTP requests (14 tracks, lyrics enabled) | ~42 | ~16 |
|
|
173
|
+
|
|
174
|
+
### 2. Efficient Algorithms
|
|
175
|
+
|
|
176
|
+
**A) SQLite sync storage** — Sync state was stored in a flat `.sync.json` file. Every `mark_synced()` call serialized the entire dictionary to disk. SQLite replaces this with atomic per-row `INSERT OR REPLACE` operations, indexed lookups, and WAL journal mode for crash safety. Legacy `.sync.json` files are auto-migrated on first run.
|
|
177
|
+
|
|
178
|
+
| Metric | Before (JSON) | After (SQLite) |
|
|
179
|
+
|---|---|---|
|
|
180
|
+
| Write cost per track | O(n) full serialization | O(1) single row INSERT |
|
|
181
|
+
| Total write cost (500 tracks) | O(n^2) | O(n) |
|
|
182
|
+
| Crash safety | Data loss if killed mid-write | WAL mode, zero data loss |
|
|
183
|
+
| Startup load (10,000 tracks) | Parse entire JSON into memory | No upfront load |
|
|
184
|
+
| New dependencies | — | None (sqlite3 is stdlib) |
|
|
185
|
+
|
|
186
|
+
**B) Smart YouTube match scoring** — Instead of blindly trying the first 3 YouTube search results in order, each result is scored against the target track's metadata (duration match, title similarity, artist/channel match, keyword analysis). Results are sorted by score so the best match is downloaded first.
|
|
187
|
+
|
|
188
|
+
| Metric | Before | After |
|
|
189
|
+
|---|---|---|
|
|
190
|
+
| Correct track on first attempt | ~60-70% | ~90%+ |
|
|
191
|
+
| Wrong track downloaded (remix/bootleg) | Common | Rare |
|
|
192
|
+
| Average download attempts per track | ~1.5 | ~1.1 |
|
|
193
|
+
|
|
194
|
+
**C) Fixed duplicate FFmpeg postprocessor** — When `--normalize` was enabled, a second identical `FFmpegExtractAudio` postprocessor was appended, causing yt-dlp to transcode the audio twice per track. Removed the duplicate; normalization is correctly applied via `postprocessor_args`.
|
|
195
|
+
|
|
196
|
+
| Metric | Before | After |
|
|
197
|
+
|---|---|---|
|
|
198
|
+
| FFmpeg transcodes per track (`--normalize`) | 2 (double transcode) | 1 |
|
|
199
|
+
|
|
200
|
+
**D) Cached FFmpeg path** — `imageio_ffmpeg.get_ffmpeg_exe()` performs a filesystem scan to locate the ffmpeg binary. Previously called once per track. Now called once and cached globally.
|
|
201
|
+
|
|
202
|
+
| Metric | Before | After |
|
|
203
|
+
|---|---|---|
|
|
204
|
+
| Filesystem lookups (50-track playlist) | 50 | 1 |
|
|
205
|
+
|
|
206
|
+
### 3. HTTP Connection Pooling (`requests.Session`)
|
|
207
|
+
|
|
208
|
+
**Problem:** Every `requests.get()` call created a new TCP connection with a full TCP/TLS handshake. No HTTP keep-alive, no connection reuse.
|
|
209
|
+
|
|
210
|
+
**Fix:** A shared `requests.Session()` is used across all HTTP calls (Spotify embed scraping, Apple Music scraping, cover art downloads, iTunes API, LrcLib lyrics). This enables HTTP keep-alive and connection pooling. A default 10-second timeout was also added to all requests that previously had none, preventing the CLI from hanging on unresponsive servers.
|
|
211
|
+
|
|
212
|
+
| Metric | Before | After |
|
|
213
|
+
|---|---|---|
|
|
214
|
+
| TCP handshakes (50 tracks, lyrics) | ~150+ | ~4-5 (one per unique host) |
|
|
215
|
+
| Requests without timeout | 3 code paths | 0 |
|
|
216
|
+
|
|
217
|
+
### 4. Lazy Loading
|
|
218
|
+
|
|
219
|
+
**A) Deferred CLI imports** — `subprocess` and `imageio_ffmpeg` were imported at module level in the CLI entry point, adding startup overhead to every command — even though they are only used by the `inject` command. These imports are deferred to inside the function body.
|
|
220
|
+
|
|
221
|
+
**B) Lazy-loaded mutagen submodules** — All 5 mutagen submodules (`mutagen.mp4`, `mutagen.mp3`, `mutagen.id3`, `mutagen.flac`, `mutagen.oggopus`) were imported at module level, even though only one format is used per run. Now each submodule is imported inside its format-specific branch of `embed_metadata()`. A `--format flac` run never loads the MP4 atom parser or the 11 ID3 frame classes.
|
|
222
|
+
|
|
223
|
+
| Metric | Before | After |
|
|
224
|
+
|---|---|---|
|
|
225
|
+
| Mutagen submodules loaded (`--format flac`) | 5 submodules (17 classes) | 2 submodules (FLAC + Picture) |
|
|
226
|
+
| Modules loaded on `sdm download` | `subprocess` + `imageio_ffmpeg` | Not loaded |
|
|
227
|
+
|
|
228
|
+
### 5. Minimize Dependencies / Reduce Overhead
|
|
229
|
+
|
|
230
|
+
**A) System FFmpeg detection** — `_get_ffmpeg_path()` now checks `shutil.which("ffmpeg")` first. Users who already have ffmpeg installed system-wide skip the bundled `imageio-ffmpeg` binary entirely, getting a faster PATH lookup. New users without system ffmpeg see zero difference — the bundled binary kicks in automatically as a fallback.
|
|
231
|
+
|
|
232
|
+
| Metric | Before | After |
|
|
233
|
+
|---|---|---|
|
|
234
|
+
| FFmpeg resolution (system ffmpeg installed) | Filesystem scan via imageio_ffmpeg | `shutil.which()` PATH lookup |
|
|
235
|
+
| Install experience (no system ffmpeg) | Unchanged | Unchanged (bundled fallback) |
|
|
236
|
+
|
|
237
|
+
**B) Reuse yt-dlp instances per worker** — `YoutubeDL.__init__()` compiles format selectors, loads the extractor registry, and sets up the downloader framework. Previously, a fresh instance was created for every single track. Now one instance is created per worker thread and reused across all tracks assigned to that worker.
|
|
238
|
+
|
|
239
|
+
| Metric | Before (50-track album, 3 workers) | After |
|
|
240
|
+
|---|---|---|
|
|
241
|
+
| `YoutubeDL()` instantiations | 50 | 3 |
|
|
242
|
+
| Format selector compilations | 50 | 3 |
|
|
243
|
+
| Postprocessor setup cycles | 50 | 3 |
|
|
244
|
+
|
|
245
|
+
## Legal Disclaimer
|
|
246
|
+
|
|
247
|
+
*Users are responsible for their actions. We do not support unauthorized downloading of copyrighted material. `sdm` is open-source (MIT License) for educational and personal use only.*
|
|
248
|
+
|
|
249
|
+
---
|
|
250
|
+
|
|
251
|
+
**Important Notes:**
|
|
252
|
+
- **Spotify Playlists** must be set to **Public**. Private playlists and personalized mixes will not work.
|
|
253
|
+
- **Explicit Content:** If YouTube blocks downloads, pass browser cookies with `--cookies firefox`. Firefox is recommended.
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
# SDM - SDM Downloads Music
|
|
2
|
+
|
|
3
|
+
`sdm` is a fast, lightweight, and zero-config CLI tool to download and sync Playlists, Albums, and Tracks from **Spotify, Apple Music, Tidal, YouTube, and SoundCloud**.
|
|
4
|
+
|
|
5
|
+
## Why sdm?
|
|
6
|
+
|
|
7
|
+
Recently, Spotify restricted their Web API, requiring developers to have a Premium subscription, which broke popular open-source tools with `403 Forbidden` errors.
|
|
8
|
+
|
|
9
|
+
`sdm` was built to completely bypass these restrictions. It uses native web scraping to extract flawless metadata directly from streaming services, pairs it with `yt-dlp` to fetch the highest-quality audio, and embeds the tags natively. No API keys required.
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- **Universal Support:** Spotify, Apple Music, Tidal, YouTube, and SoundCloud.
|
|
14
|
+
- **Smart Syncing:** Two-way mirroring. Run `sdm sync` to fetch new tracks and clean up removed ones.
|
|
15
|
+
- **Multi-Format:** Download in M4A (default), MP3, FLAC, or OPUS with native tags.
|
|
16
|
+
- **Perfect Metadata:** Embeds Track, Artist, Album, Cover Art, Track/Disc Numbers, Genre, and Release Year.
|
|
17
|
+
- **Advanced Processing:** Synced lyrics via LRCLIB, LUFS normalization, and SponsorBlock trimming.
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pip install sdm-pycli
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
Open a terminal in the folder where you want your music saved, then run the commands below.
|
|
28
|
+
|
|
29
|
+
### Playlist
|
|
30
|
+
|
|
31
|
+
To download a playlist, run
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
sdm download [playlistUrl]
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Example:
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
sdm download https://open.spotify.com/playlist/37i9dQZF1E8NjgPSXnmGkI?si=O0kgUaFcQnatcvlZzS9yJw
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Album
|
|
44
|
+
|
|
45
|
+
To download an album, run
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
sdm download [albumUrl]
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Example:
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
sdm download https://open.spotify.com/album/6eUW0wxWtzkFdaEFsTJto6?si=nYD6g_tZQvuFsMzyKG2sRA
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Track
|
|
58
|
+
|
|
59
|
+
To download a single track, run
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
sdm download [trackUrl]
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Example:
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
sdm download https://open.spotify.com/track/4PTG3Z6ehGkBFwjybzWkR8?si=1d83934b63464e9e
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Apple Music
|
|
72
|
+
|
|
73
|
+
Apple Music albums, playlists, and songs are also supported.
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
sdm download https://music.apple.com/us/album/starboy/1440871397
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### Sync
|
|
80
|
+
|
|
81
|
+
`sdm` remembers the source URL for each folder. To update a previously downloaded folder with any changes from the remote playlist, run
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
sdm sync [directory]
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Example:
|
|
88
|
+
|
|
89
|
+
```bash
|
|
90
|
+
sdm sync "C:\Users\You\Music\My Playlist"
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### Inject
|
|
94
|
+
|
|
95
|
+
To import a local audio file into your playlist with official metadata and protect it from future sync deletions, run
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
sdm inject [filePath] [trackUrl]
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Example:
|
|
102
|
+
|
|
103
|
+
```bash
|
|
104
|
+
sdm inject "C:\Users\You\Downloads\song.mp3" https://open.spotify.com/track/0VjIjW4GlUZAMYd2vXMi3b
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## Options & Flags
|
|
108
|
+
|
|
109
|
+
| Flag | Description |
|
|
110
|
+
|---|---|
|
|
111
|
+
| `-o, --output` | Save to a specific directory |
|
|
112
|
+
| `-f, --format` | Audio format: `m4a`, `mp3`, `flac`, `opus` (default: `m4a`) |
|
|
113
|
+
| `-w, --workers` | Number of concurrent downloads (default: `3`) |
|
|
114
|
+
| `--lyrics` | Fetch and embed synced lyrics from LRCLIB |
|
|
115
|
+
| `--normalize` | Apply EBU R128 (-14 LUFS) volume normalization |
|
|
116
|
+
| `--sponsor-block` | Trim non-music sections from YouTube sources |
|
|
117
|
+
| `--dry-run` | Simulate a sync/download without making changes |
|
|
118
|
+
| `--no-delete` | Download new tracks but never delete local files |
|
|
119
|
+
| `--refresh-metadata` | Re-tag existing files with the latest metadata |
|
|
120
|
+
| `--cookies` | Pass browser cookies for age-restricted content |
|
|
121
|
+
|
|
122
|
+
Example with flags:
|
|
123
|
+
|
|
124
|
+
```bash
|
|
125
|
+
sdm download https://open.spotify.com/playlist/37i9dQZF1E8UXBoz02kGID -f flac --lyrics --normalize
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## Music Sourcing & Audio Quality
|
|
129
|
+
|
|
130
|
+
`sdm` fetches flawless metadata (tags, cover art, tracklists) directly from Spotify, Apple Music, and Tidal, but sources the actual audio streams from YouTube.
|
|
131
|
+
|
|
132
|
+
Unlike other tools that require your Premium credentials and risk permanent account bans by ripping directly from encrypted servers, `sdm` acts as a safe, unauthenticated metadata matcher. It reliably delivers DRM-free audio (up to 256kbps AAC or Opus) perfectly packaged to match the original release.
|
|
133
|
+
|
|
134
|
+
## Optimization
|
|
135
|
+
|
|
136
|
+
`sdm` v1.2.0 implements nine optimization techniques across five categories to significantly reduce network overhead, disk I/O, startup time, and download accuracy.
|
|
137
|
+
|
|
138
|
+
### 1. Application-Level Caching
|
|
139
|
+
|
|
140
|
+
**Problem:** Every track triggered its own HTTP requests for cover art and iTunes metadata, even when all tracks in an album share the same cover image and genre.
|
|
141
|
+
|
|
142
|
+
**Fix:** Thread-safe in-memory caches (`_cover_cache`, `_itunes_cache`) store results keyed by URL and `(artist, track)` respectively. A cache hit skips the HTTP request entirely.
|
|
143
|
+
|
|
144
|
+
| Metric | Before | After |
|
|
145
|
+
|---|---|---|
|
|
146
|
+
| Cover art downloads (14-track album) | 14 HTTP requests | 1 HTTP request |
|
|
147
|
+
| iTunes metadata lookups (14-track album) | 14 HTTP requests | ~1-2 HTTP requests |
|
|
148
|
+
| Total HTTP requests (14 tracks, lyrics enabled) | ~42 | ~16 |
|
|
149
|
+
|
|
150
|
+
### 2. Efficient Algorithms
|
|
151
|
+
|
|
152
|
+
**A) SQLite sync storage** — Sync state was stored in a flat `.sync.json` file. Every `mark_synced()` call serialized the entire dictionary to disk. SQLite replaces this with atomic per-row `INSERT OR REPLACE` operations, indexed lookups, and WAL journal mode for crash safety. Legacy `.sync.json` files are auto-migrated on first run.
|
|
153
|
+
|
|
154
|
+
| Metric | Before (JSON) | After (SQLite) |
|
|
155
|
+
|---|---|---|
|
|
156
|
+
| Write cost per track | O(n) full serialization | O(1) single row INSERT |
|
|
157
|
+
| Total write cost (500 tracks) | O(n^2) | O(n) |
|
|
158
|
+
| Crash safety | Data loss if killed mid-write | WAL mode, zero data loss |
|
|
159
|
+
| Startup load (10,000 tracks) | Parse entire JSON into memory | No upfront load |
|
|
160
|
+
| New dependencies | — | None (sqlite3 is stdlib) |
|
|
161
|
+
|
|
162
|
+
**B) Smart YouTube match scoring** — Instead of blindly trying the first 3 YouTube search results in order, each result is scored against the target track's metadata (duration match, title similarity, artist/channel match, keyword analysis). Results are sorted by score so the best match is downloaded first.
|
|
163
|
+
|
|
164
|
+
| Metric | Before | After |
|
|
165
|
+
|---|---|---|
|
|
166
|
+
| Correct track on first attempt | ~60-70% | ~90%+ |
|
|
167
|
+
| Wrong track downloaded (remix/bootleg) | Common | Rare |
|
|
168
|
+
| Average download attempts per track | ~1.5 | ~1.1 |
|
|
169
|
+
|
|
170
|
+
**C) Fixed duplicate FFmpeg postprocessor** — When `--normalize` was enabled, a second identical `FFmpegExtractAudio` postprocessor was appended, causing yt-dlp to transcode the audio twice per track. Removed the duplicate; normalization is correctly applied via `postprocessor_args`.
|
|
171
|
+
|
|
172
|
+
| Metric | Before | After |
|
|
173
|
+
|---|---|---|
|
|
174
|
+
| FFmpeg transcodes per track (`--normalize`) | 2 (double transcode) | 1 |
|
|
175
|
+
|
|
176
|
+
**D) Cached FFmpeg path** — `imageio_ffmpeg.get_ffmpeg_exe()` performs a filesystem scan to locate the ffmpeg binary. Previously called once per track. Now called once and cached globally.
|
|
177
|
+
|
|
178
|
+
| Metric | Before | After |
|
|
179
|
+
|---|---|---|
|
|
180
|
+
| Filesystem lookups (50-track playlist) | 50 | 1 |
|
|
181
|
+
|
|
182
|
+
### 3. HTTP Connection Pooling (`requests.Session`)
|
|
183
|
+
|
|
184
|
+
**Problem:** Every `requests.get()` call created a new TCP connection with a full TCP/TLS handshake. No HTTP keep-alive, no connection reuse.
|
|
185
|
+
|
|
186
|
+
**Fix:** A shared `requests.Session()` is used across all HTTP calls (Spotify embed scraping, Apple Music scraping, cover art downloads, iTunes API, LrcLib lyrics). This enables HTTP keep-alive and connection pooling. A default 10-second timeout was also added to all requests that previously had none, preventing the CLI from hanging on unresponsive servers.
|
|
187
|
+
|
|
188
|
+
| Metric | Before | After |
|
|
189
|
+
|---|---|---|
|
|
190
|
+
| TCP handshakes (50 tracks, lyrics) | ~150+ | ~4-5 (one per unique host) |
|
|
191
|
+
| Requests without timeout | 3 code paths | 0 |
|
|
192
|
+
|
|
193
|
+
### 4. Lazy Loading
|
|
194
|
+
|
|
195
|
+
**A) Deferred CLI imports** — `subprocess` and `imageio_ffmpeg` were imported at module level in the CLI entry point, adding startup overhead to every command — even though they are only used by the `inject` command. These imports are deferred to inside the function body.
|
|
196
|
+
|
|
197
|
+
**B) Lazy-loaded mutagen submodules** — All 5 mutagen submodules (`mutagen.mp4`, `mutagen.mp3`, `mutagen.id3`, `mutagen.flac`, `mutagen.oggopus`) were imported at module level, even though only one format is used per run. Now each submodule is imported inside its format-specific branch of `embed_metadata()`. A `--format flac` run never loads the MP4 atom parser or the 11 ID3 frame classes.
|
|
198
|
+
|
|
199
|
+
| Metric | Before | After |
|
|
200
|
+
|---|---|---|
|
|
201
|
+
| Mutagen submodules loaded (`--format flac`) | 5 submodules (17 classes) | 2 submodules (FLAC + Picture) |
|
|
202
|
+
| Modules loaded on `sdm download` | `subprocess` + `imageio_ffmpeg` | Not loaded |
|
|
203
|
+
|
|
204
|
+
### 5. Minimize Dependencies / Reduce Overhead
|
|
205
|
+
|
|
206
|
+
**A) System FFmpeg detection** — `_get_ffmpeg_path()` now checks `shutil.which("ffmpeg")` first. Users who already have ffmpeg installed system-wide skip the bundled `imageio-ffmpeg` binary entirely, getting a faster PATH lookup. New users without system ffmpeg see zero difference — the bundled binary kicks in automatically as a fallback.
|
|
207
|
+
|
|
208
|
+
| Metric | Before | After |
|
|
209
|
+
|---|---|---|
|
|
210
|
+
| FFmpeg resolution (system ffmpeg installed) | Filesystem scan via imageio_ffmpeg | `shutil.which()` PATH lookup |
|
|
211
|
+
| Install experience (no system ffmpeg) | Unchanged | Unchanged (bundled fallback) |
|
|
212
|
+
|
|
213
|
+
**B) Reuse yt-dlp instances per worker** — `YoutubeDL.__init__()` compiles format selectors, loads the extractor registry, and sets up the downloader framework. Previously, a fresh instance was created for every single track. Now one instance is created per worker thread and reused across all tracks assigned to that worker.
|
|
214
|
+
|
|
215
|
+
| Metric | Before (50-track album, 3 workers) | After |
|
|
216
|
+
|---|---|---|
|
|
217
|
+
| `YoutubeDL()` instantiations | 50 | 3 |
|
|
218
|
+
| Format selector compilations | 50 | 3 |
|
|
219
|
+
| Postprocessor setup cycles | 50 | 3 |
|
|
220
|
+
|
|
221
|
+
## Legal Disclaimer
|
|
222
|
+
|
|
223
|
+
*Users are responsible for their actions. We do not support unauthorized downloading of copyrighted material. `sdm` is open-source (MIT License) for educational and personal use only.*
|
|
224
|
+
|
|
225
|
+
---
|
|
226
|
+
|
|
227
|
+
**Important Notes:**
|
|
228
|
+
- **Spotify Playlists** must be set to **Public**. Private playlists and personalized mixes will not work.
|
|
229
|
+
- **Explicit Content:** If YouTube blocks downloads, pass browser cookies with `--cookies firefox`. Firefox is recommended.
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "sdm-pycli"
|
|
7
|
-
version = "1.
|
|
7
|
+
version = "1.2.0"
|
|
8
8
|
description = "A fast, zero-config CLI tool to download and sync playlists from Spotify, Apple Music, Tidal, YouTube, and SoundCloud."
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
authors = [
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
from pathlib import Path
|
|
2
2
|
from concurrent.futures import ThreadPoolExecutor, as_completed
|
|
3
|
-
import subprocess
|
|
4
|
-
import imageio_ffmpeg
|
|
5
3
|
import typer
|
|
6
4
|
from enum import Enum
|
|
7
5
|
|
|
@@ -17,7 +15,13 @@ from rich.table import Table
|
|
|
17
15
|
from rich.prompt import Confirm
|
|
18
16
|
|
|
19
17
|
from sdm.metadata import fetch_tracks
|
|
20
|
-
from sdm.download import
|
|
18
|
+
from sdm.download import (
|
|
19
|
+
download_and_tag,
|
|
20
|
+
sanitize_filename,
|
|
21
|
+
embed_metadata,
|
|
22
|
+
_get_ffmpeg_path,
|
|
23
|
+
build_ydl_opts,
|
|
24
|
+
)
|
|
21
25
|
from sdm.sync import SyncManager
|
|
22
26
|
|
|
23
27
|
app = typer.Typer(
|
|
@@ -123,6 +127,15 @@ def execute_sync_or_download(
|
|
|
123
127
|
f"{action_verb} {len(tracks_to_download)} tracks using {workers} workers..."
|
|
124
128
|
)
|
|
125
129
|
|
|
130
|
+
import threading
|
|
131
|
+
import yt_dlp
|
|
132
|
+
|
|
133
|
+
shared_ydl_opts = build_ydl_opts(
|
|
134
|
+
cookies, format.value, sponsor_block, normalize
|
|
135
|
+
)
|
|
136
|
+
worker_ydl_instances = {}
|
|
137
|
+
_ydl_lock = threading.Lock()
|
|
138
|
+
|
|
126
139
|
def worker(index, track, is_synced):
|
|
127
140
|
title = track.get("name", "Unknown")
|
|
128
141
|
if dry_run:
|
|
@@ -132,7 +145,7 @@ def execute_sync_or_download(
|
|
|
132
145
|
return "dry_run_refresh", "Simulated refresh"
|
|
133
146
|
|
|
134
147
|
if is_synced:
|
|
135
|
-
filename = sync_manager.
|
|
148
|
+
filename = sync_manager.get_filename(track["id"])
|
|
136
149
|
if not filename:
|
|
137
150
|
return "error", "Filename not found in sync index"
|
|
138
151
|
filepath = output_dir / filename
|
|
@@ -143,16 +156,23 @@ def execute_sync_or_download(
|
|
|
143
156
|
else:
|
|
144
157
|
return "error", "Failed to refresh metadata"
|
|
145
158
|
else:
|
|
159
|
+
# Get or create a per-worker ydl instance
|
|
160
|
+
tid = threading.current_thread().ident
|
|
161
|
+
with _ydl_lock:
|
|
162
|
+
if tid not in worker_ydl_instances:
|
|
163
|
+
worker_ydl_instances[tid] = yt_dlp.YoutubeDL(
|
|
164
|
+
dict(shared_ydl_opts)
|
|
165
|
+
)
|
|
166
|
+
ydl_instance = worker_ydl_instances[tid]
|
|
167
|
+
|
|
146
168
|
return download_and_tag(
|
|
147
169
|
track,
|
|
148
170
|
output_dir,
|
|
149
171
|
index,
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
normalize,
|
|
155
|
-
lyrics,
|
|
172
|
+
format_flag=format.value,
|
|
173
|
+
fallback=False,
|
|
174
|
+
fetch_lyrics=lyrics,
|
|
175
|
+
ydl=ydl_instance,
|
|
156
176
|
)
|
|
157
177
|
|
|
158
178
|
with Progress(
|
|
@@ -238,6 +258,16 @@ def execute_sync_or_download(
|
|
|
238
258
|
)
|
|
239
259
|
|
|
240
260
|
progress.advance(main_task)
|
|
261
|
+
|
|
262
|
+
# Flush any remaining batched sync data to disk
|
|
263
|
+
sync_manager.flush()
|
|
264
|
+
|
|
265
|
+
# Clean up per-worker yt-dlp instances
|
|
266
|
+
for ydl_inst in worker_ydl_instances.values():
|
|
267
|
+
try:
|
|
268
|
+
ydl_inst.close()
|
|
269
|
+
except Exception:
|
|
270
|
+
pass
|
|
241
271
|
else:
|
|
242
272
|
console.print("[green]All tracks are already up to date.[/green]")
|
|
243
273
|
|
|
@@ -314,12 +344,12 @@ def execute_sync_or_download(
|
|
|
314
344
|
track,
|
|
315
345
|
output_dir,
|
|
316
346
|
index,
|
|
317
|
-
cookies,
|
|
318
|
-
format.value,
|
|
319
|
-
True,
|
|
320
|
-
sponsor_block,
|
|
321
|
-
normalize,
|
|
322
|
-
lyrics,
|
|
347
|
+
cookies_source=cookies,
|
|
348
|
+
format_flag=format.value,
|
|
349
|
+
fallback=True,
|
|
350
|
+
sponsor_block=sponsor_block,
|
|
351
|
+
normalize=normalize,
|
|
352
|
+
fetch_lyrics=lyrics,
|
|
323
353
|
)
|
|
324
354
|
if f_status in ["fallback_success", "success"]:
|
|
325
355
|
sync_manager.mark_synced(track["id"], f_message)
|
|
@@ -343,6 +373,9 @@ def execute_sync_or_download(
|
|
|
343
373
|
f' sdm inject "your_file.mp3" "<track_url>" -o "{output_dir}"'
|
|
344
374
|
)
|
|
345
375
|
|
|
376
|
+
# Flush any sync data from DRM fallback downloads
|
|
377
|
+
sync_manager.flush()
|
|
378
|
+
|
|
346
379
|
if not dry_run:
|
|
347
380
|
m3u_path = output_dir / "_playlist.m3u"
|
|
348
381
|
try:
|
|
@@ -350,7 +383,7 @@ def execute_sync_or_download(
|
|
|
350
383
|
f.write("#EXTM3U\n")
|
|
351
384
|
for t in tracks:
|
|
352
385
|
t_id = t.get("id")
|
|
353
|
-
fname = sync_manager.
|
|
386
|
+
fname = sync_manager.get_filename(t_id)
|
|
354
387
|
if fname:
|
|
355
388
|
f.write(f"{fname}\n")
|
|
356
389
|
except Exception as e:
|
|
@@ -603,7 +636,10 @@ def inject(
|
|
|
603
636
|
|
|
604
637
|
console.print(f"[cyan]Converting and importing {filename_template}...[/cyan]")
|
|
605
638
|
|
|
606
|
-
|
|
639
|
+
import subprocess
|
|
640
|
+
|
|
641
|
+
# Use the shared cached ffmpeg path (with system PATH detection)
|
|
642
|
+
cmd = [_get_ffmpeg_path(), "-y", "-i", str(file)]
|
|
607
643
|
|
|
608
644
|
codec = {"m4a": "aac", "mp3": "libmp3lame", "flac": "flac", "opus": "libopus"}[
|
|
609
645
|
format.value
|