Commit Graph

534 Commits

Author SHA1 Message Date
imSp4rky
982f821f19 fix(dash): deduplicate multi-period SegmentBase segments
Multi-period DASH manifests using SegmentBase with shared BaseURLs were downloading the entire file once per period. Deduplicate identical segments across periods so each file is only downloaded once. Also demote multi-period log message from info to debug.
2026-04-13 16:44:35 +00:00
imSp4rky
c56a92ed0c feat(track): add optional per-track session parameter (#96)
Originally authored by panitan103 — adds optional session parameter to Track class allowing services to pass per-track sessions with different headers/cookies/auth.

Changes for dev branch integration:
- Fix type hints to support both requests.Session and RnetSession
- Fix session fallback in dl.py: track.session or service.session
- Remove redundant `session or None` assignment

Co-Authored-By: panitan103 <panitan103@users.noreply.github.com>
2026-04-12 16:41:43 -06:00
imSp4rky
cb3535215d fix(dl): always report full error trace for download worker failures
Previously, unexpected errors only showed a generic message without the actual exception details or traceback. Simplify the error handler to always include the exception type/message and print the full trace.
2026-04-12 22:32:23 +00:00
imSp4rky
4179b9045e fix(tags): prevent metadata lookup failures from skipping group tag
Wrapped metadata provider lookup in try/except so custom tags (Group) are always applied even when IMDB/TMDB lookups fail. Also log mkvpropedit errors instead of silently discarding them.
2026-04-12 22:21:02 +00:00
imSp4rky
2e7fc1720d fix(drm): handle non-UTF-8 output from shaka-packager stderr
Shaka-packager can emit non-UTF-8 bytes in its log output, causing UnicodeDecodeError when reading stderr in text mode. Use explicit errors="replace" encoding. Also harden try_ensure_utf8 fallback paths to always return valid UTF-8 instead of raw bytes.
2026-04-12 22:19:44 +00:00
imSp4rky
8bdb942234 feat(dl): add download resume support via HTTP Range headers
Partial downloads are now preserved across interruptions and retries. When a control file and partial data exist, the downloader sends a Range header to resume from the last byte. Falls back to full re-download if the server doesn't support Range requests (no 206).
2026-04-12 11:40:15 -06:00
imSp4rky
8f4f947d0d fix(api): sync REST API download endpoint with updated dl command (#98)
The REST API download endpoint was broken after recent dl command changes.

- Add missing vbitrate_range, abitrate_range, and worst parameters to the API call and DEFAULT_DOWNLOAD_PARAMS
- Convert wanted episode strings (S01E01) to internal SxE format via SeasonRange so episode filtering works correctly
- Track completed output files via dl.completed_files instead of returning an empty list

Closes #98
2026-04-08 23:04:58 -06:00
imSp4rky
50d2b127ec style: apply ruff linting fixes across codebase 2026-04-08 22:09:19 -06:00
imSp4rky
725edd59e0 fix(gluetun): support WireGuard VPN ready detection
WireGuard is stateless and never emits the OpenVPN-specific "initialization sequence completed" log line, causing the readiness check to always time out. Also accept "public ip address is" which gluetun logs once the WireGuard tunnel is up.

Closes #99
2026-04-08 22:05:56 -06:00
imSp4rky
c5aa57c9db fix(dash): add SIDX parsing for SegmentBase manifests and deduplicate multi-period segments
Multi-period DASH manifests using SegmentBase with shared BaseURLs were downloading the entire file once per period, causing massive file size inflation. Parse the SIDX box to extract proper per-segment byte ranges and deduplicate identical segments across periods.
2026-04-06 02:45:17 +00:00
imSp4rky
fef68202e9 fix(dl): preserve per-language video selection in quality step
The video quality Cartesian product (resolution × range × codec) only picked the first matching track, collapsing multi-language selections back to a single language. Add language as a product dimension when -l best/all or -vl with multiple languages is used.
2026-04-04 09:56:20 -06:00
imSp4rky
c051d9df23 ci(security): add Bandit pre-commit hook 2026-04-02 13:23:26 -06:00
imSp4rky
e10dbeed94 fix(api): sanitize user-provided values in log statements to prevent log injection
Addresses 12 CodeQL high-severity log injection alerts (CWE-117) by stripping newline and control characters from user-provided values before logging.
2026-04-02 12:10:46 -06:00
imSp4rky
0ebf9278d1 fix(deps): bump aiohttp and pygments to resolve 11 security vulnerabilities 2026-04-02 11:44:54 -06:00
imSp4rky
bb0a800ab6 docs(api): update --export from string path to boolean flag
Update API docs, Swagger schema, handlers, and example config to reflect --export as a boolean flag that auto-generates export files in the configurable exports directory.
2026-04-02 11:27:39 -06:00
imSp4rky
fabc96ba1b feat(dl): change --export flag with manifest URL, subtitles, and track info
changed --export flag to export decryption keys, manifest URLs, subtitle URLs, and track info to a JSON file in the configurable exports directory. Manifest URLs are captured from DASH, ISM, and HLS parsers and propagated through the Tracks system via a new manifest_url attribute. Deduplicate Widevine/PlayReady export logic into a shared _write_export helper with dedicated EXPORT_LOCK. Add Tracks.filter() method that preserves metadata when filtering tracks by predicate.
2026-04-02 10:29:22 -06:00
imSp4rky
655e4197c3 fix(session): native rnet proxy support and cookie compat layer
Proxies now use rnet's native `proxies` parameter (`List[rnet.Proxy]`) with in-place `client.update()` for live proxy changes. Client is created lazily on first request, allowing headers, cookies, and proxies to be configured freely before any connection is established.

Cookie adapter supports RequestsCookieJar-compatible methods (jar, get_dict, clear) for seamless interop with cookie persistence.
2026-04-01 17:08:25 -06:00
imSp4rky
3aaca77c48 feat(serve): add service allowlist for global and per-user access control
Allow server operators to restrict which services are exposed via the API using serve.services (global) and serve.users.<key>.services (per-user).
Effective access is the intersection of both when both are set. Unlisted services return the same error as non-existent ones to prevent enumeration.
2026-03-31 23:06:07 -06:00
imSp4rky
8a714d6455 fix(template): detect folder spacer from template separators, not raw string
The previous heuristic checked the raw template string for dots, which could match dots inside variable names or title content, causing
Plex-friendly folder names to incorrectly use dots as spacers. Now strips template variables first and checks only the separators between them to determine user intent.
2026-03-31 09:13:44 -06:00
imSp4rky
5e801580a3 fix(hybrid): read actual HDR metadata for HDR10+ to DV conversion
The L6 metadata in convert_hdr10plus_to_dv was hardcoded to 1000 nits max mastering display luminance. Now probes the source stream via ffprobe to extract the real mastering display and content light level values, preserving accurate luminance for sources mastered above 1000 nits.
2026-03-30 17:02:06 -06:00
imSp4rky
47b3390bd0 feat(dl): allow --slow to accept custom delay range
--slow now supports an optional MIN-MAX range (e.g., --slow 20-40) for custom delay between title downloads. Bare --slow retains the original 60-120s default. Minimum delay enforced at 20 seconds.
2026-03-30 16:25:29 -06:00
imSp4rky
c7fd2a904c Merge branch 'dev' of https://github.com/unshackle-dl/unshackle into dev 2026-03-29 23:46:28 -06:00
imSp4rky
13ebdddaf4 Merge branch 'main' into dev 2026-03-29 23:45:53 -06:00
imSp4rky
d37a1a514f chore(gitignore): ignore binary files in unshackle/binaries/ 2026-03-30 05:27:37 +00:00
imSp4rky
ccc494be06 Merge branch 'feat/unified-downloader' into dev
# Conflicts:
#	unshackle/core/manifests/hls.py
2026-03-30 05:24:09 +00:00
imSp4rky
d3594ca67c fix(remote): forward track selection params to server and improve error display
Forward range, vcodec, quality, and best_available from client to server so services fetch the correct manifests (e.g. HDR10 instead of SDR).
Exit with error when no video tracks match the requested range filter.
Use Rich logger for remote API errors instead of plain click.ClickException.
2026-03-30 05:20:32 +00:00
imSp4rky
1a6f2c5b7e fix(serve): allow remote-only mode without output_template and fix CORS/auth for Cloudflare
Move output_template validation from config init to dl command so serve, search, and other non-download commands work without it configured. Fix CORS header to use X-Secret-Key (matching actual auth header). Exempt /api/health from auth for Cloudflare tunnel health checks.
2026-03-30 04:36:23 +00:00
imSp4rky
3d5e46a2e3 feat(hls): probe TS segments for resolution and codec when master playlist lacks RESOLUTION/CODECS tags
Some HLS services serve master playlists without RESOLUTION or CODECS attributes, leaving video tracks with no width/height/codec info which causes crashes in by_resolutions() quality selection.

After parsing tracks in to_tracks(), any video track missing resolution or codec is now probed by fetching the first 8KB of its first TS segment and parsing the H.264/H.265 SPS NAL unit to extract the actual width, height, and codec. This approach mirrors how cat-catch/hls.js determines resolution from the media data rather than relying on playlist metadata.
2026-03-29 15:55:53 -06:00
Andy
5a3ac81ff9 feat(session): translate requests 'data' kwarg to rnet equivalents for compatibility 2026-03-26 16:36:35 -06:00
Andy
e323f6f3b3 feat(template): add configurable folder naming via output_template.folder (#94)
Adds an optional `folder` key under `output_template` to customize output folder names using the same template variables as file naming.
2026-03-25 21:42:47 -06:00
Andy
10cca7d0ea fix(sanitize): restore parentheses stripping in filename sanitization (#93)
Commit 6ce7b6c accidentally removed () from the unsafe-characters regex
2026-03-25 19:46:00 -06:00
Andy
7358619a40 fix(deps): bump PyJWT minimum to 2.12.0 for CVE-2026-32597
PyJWT <= 2.11.0 accepts unknown `crit` header extensions in violation of RFC 7515 §4.1.11. Bump lower bound to 2.12.0 which includes the fix.
2026-03-25 15:16:21 -06:00
Andy
b524585d78 fix(drm): add zero-KID fallback for mp4decrypt and clear HLS track.drm after download
mp4decrypt silently copies files unchanged when the tenc box default KID is all zeros, since none of the real KID:KEY pairs match. Add zero-KID fallback entries to both Widevine and PlayReady mp4decrypt methods, matching what Shaka Packager already does.

Also clear track.drm after HLS download when decryption was performed, preventing unnecessary double-decryption. DASH and URL descriptors already did this.
2026-03-25 15:06:41 -06:00
Andy
fe1ccd085c Revert "fix(drm): add track ID fallback for mp4decrypt CBCS zero-KID content"
This reverts commit 23466cae8b.
2026-03-25 14:39:08 -06:00
Andy
23466cae8b fix(drm): add track ID fallback for mp4decrypt CBCS zero-KID content
Some CBCS-encrypted content has an all-zeros default_KID in the tenc box while the real KID is only in the PSSH boxes. mp4decrypt matches keys against the tenc KID, so it silently skips decryption when the provided KID doesn't match. This adds a track ID-based key fallback when a zero KID is detected, matching the existing shaka-packager zero-KID fallback behavior.
2026-03-25 14:36:26 -06:00
Andy
c930abc6fd fix(subtitle): decompress gzip/zlib responses for subtitle downloads
The requests downloader used decode_content=False on raw socket reads, which skipped HTTP content-encoding decompression. Subtitle files served with Content-Encoding: gzip were saved as raw compressed bytes, then mangled by try_ensure_utf8 falling back to CP1252 decoding.

Remove decode_content=False from the raw read path — the speed gain comes from raw.read() itself, not from skipping decompression. Also add gzip/zlib magic byte detection in try_ensure_utf8 as a safety net for any edge cases where compressed data reaches encoding detection.
2026-03-24 17:44:23 -06:00
Andy
99be88dc08 feat(session): replace curl_cffi with rnet for TLS-fingerprinted HTTP
Replace CurlSession (curl_cffi) with RnetSession powered by rnet (Rust/BoringSSL). Benchmarks show 3.5x faster segmented downloads (1.06 GB/s vs 304 MB/s) and 16% faster single-file downloads with near-zero TLS fingerprinting overhead.

- Add RnetSession wrapper with requests-compatible API (headers, cookies, proxies, retry logic, prepared requests)
- Add RnetResponse wrapper normalizing rnet quirks (status_code as int, text as property, bytes-to-str headers, iter_content re-chunking)
- Replace CurlSession isinstance checks across manifests, tracks, DRM
- Update downloader with rnet native streaming path and byte-based progress tracking for accurate Rich progress bars
- Add speed display column to Rich progress bar (DASH/HLS/URL prefix)
- Add rnet dependency, services use exact preset names (e.g. OkHttp4_12)
2026-03-24 10:08:17 -06:00
Andy
6840944738 perf(downloader): optimize hot loop and threading efficiency
Replace list.pop(0) with deque.popleft() for O(1) speed tracker eviction, skip urllib3 decode chain with decode_content=False on raw reads, use running total instead of sum() for progress reporting, add explicit stream.close() on CurlSession path, replace busy-poll loop with concurrent.futures.wait(FIRST_COMPLETED), skip ThreadPoolExecutor for single-URL downloads, and DRY up duplicated raw/iter_content progress logic into a unified chunk iterator.
2026-03-23 18:17:12 -06:00
Andy
006d080416 feat(downloader): optimize download throughput with Queue-based threading and raw reads
Fix critical bug where ThreadPoolExecutor was not actually parallelizing downloads (generator functions returned instantly, I/O ran on main thread).

Performance improvements:
Queue-based event dispatch: workers consume generators in threads, push events to a thread-safe Queue for truly parallel segment downloads
Raw socket reads (resp.raw.read) for requests.Session — 30-35% faster than iter_content, with iter_content fallback for CurlSession
File pre-allocation via truncate when Content-Length is known
Hot loop caching: time.time, f.write, stream.raw.read cached as locals
HTTPAdapter connection pooling mounted on passed sessions for reuse
2026-03-23 17:20:26 -06:00
Andy
732709d3a9 feat(remote): interactive auth handshake, server CDM, cache round-trip, and serve remote-only mode
Add InputBridge for interactive client-server authentication (OTP, device codes, PINs) with async auth via asyncio.to_thread, prompt polling endpoints, and cancellation support.
Server CDM mode detects CDM type from config.cdm per-service, resolves keys server-side, and returns DRM type to client for correct display. Cache files round-trip between client and server on session create/delete. Vault loading fixed for server-side key caching.
HLS/ISM/DASH DRM extraction in license handler. Serve --remote-only mode exposes only session endpoints. Clean connection error handling for unreachable servers.
2026-03-22 22:44:36 -06:00
Andy
1ad226fbcf feat(remote): server vault lookups, service CDM mapping, key display, and service param forwarding 2026-03-20 21:13:56 -06:00
Andy
51d6921eaf fix(drm): include external KID in PSSH when it differs from existing KIDs
When the DASH manifest provides a cenc:default_KID that differs from the PSSH's embedded KIDs, the external KID must be added to the PSSH so the license server returns keys for both. Previously, the PSSH was only modified when all existing KIDs were placeholders, causing a CEKNotFound error when the track's actual encryption KID wasn't in the license response.
2026-03-20 12:48:49 -06:00
Andy
dc197af29e feat(dash): refactor segment extraction and add content period validation 2026-03-20 12:47:49 -06:00
Andy
561a609040 fix(audio): support 'xheaac' profile 2026-03-20 10:34:49 -06:00
Andy
a21c32df5d feat(dl): add --vbitrate-range and --abitrate-range options for bitrate range selection
Allow users to specify a bitrate range (e.g., --abitrate-range 300-400) to filter tracks within that range, with downstream selection picking the highest per language. Mutually exclusive with the existing --vbitrate/--abitrate exact match options.
2026-03-20 10:34:26 -06:00
Andy
2f721266f0 Merge branch 'main' into dev 2026-03-19 20:36:55 -06:00
Andy
faaaf08bd5 fix(drm): add zero-KID fallback for mp4decrypt and clear HLS track.drm after download
mp4decrypt silently copies files unchanged when the tenc box default KID is all zeros, since none of the real KID:KEY pairs match. Add zero-KID fallback entries to both Widevine and PlayReady mp4decrypt methods, matching what Shaka Packager already does.

Also clear track.drm after HLS download when decryption was performed, preventing unnecessary double-decryption. DASH and URL descriptors already did this.
2026-03-19 18:43:43 -06:00
Andy
c323db9481 feat(downloader): consolidate into unified requests-based downloader
Replace 4 separate downloaders (requests, curl_impersonate, aria2c, n_m3u8dl_re) with a single optimized requests downloader with adaptive chunk sizing and session passthrough for TLS fingerprinting support.

- Adaptive chunk sizing (512KB-4MB) based on content length, up from fixed 1KB
- Buffered writes (1MB buffer) for improved I/O throughput
- Session passthrough: accepts both requests.Session and CurlSession
- Per-call speed tracking with rolling window (fixes cross-track speed bleed)
- Worker count default capped at 16
- Removed all downloader.__name__ special-casing from manifest parsers
- Removed aria2c/curl_impersonate/n_m3u8dl_re downloader modules
- Deprecated downloader config key in unshackle.yaml
2026-03-19 18:13:43 -06:00
Andy
4c55f7af5b docs: update API and configuration documentation with example service tags 2026-03-19 12:55:39 -06:00
Andy
e9dbe3f0ac Merge branch 'dev' of https://github.com/unshackle-dl/unshackle into dev 2026-03-19 12:38:40 -06:00