From c84e7e715d44badf695056abd8167abbbc004045 Mon Sep 17 00:00:00 2001 From: jude Date: Mon, 23 Nov 2026 10:59:40 +0000 Subject: [PATCH] Play new songs from YouTube in the browser YouTube's bot checks block yt-dlp from fetching audio on the server, so suggested songs now play in YouTube's embedded player, kept out of sight, in the browser. yt-dlp is only used to search. - GET /api/discover/youtube/ returns candidate video IDs, best first, or the one that played before (?refresh=true searches again). - PUT /api/discover/youtube/ remembers the video that played. - The UI tries the next candidate when a video can't be embedded, searches again when a remembered video is gone, and skips the song otherwise. - Drop the YouTube audio proxy, and Deno from the image. Co-Authored-By: Claude Opus 5.5 --- Cargo.toml | 2 +- Dockerfile | 4 +- README.md | 24 +++-- discover/src/api.ts | 7 +- discover/src/queue.ts | 5 +- discover/src/styles.css | 14 +++ discover/src/usePlayer.ts | 185 ++++++++++++++++++++++++++++------ discover/src/youtubePlayer.ts | 158 +++++++++++++++++++++++++++++ etc/playlistd/playlistd.toml | 2 +- src/web.rs | 173 ++++++++++++++++++------------- src/youtube.rs | 102 +++++++------------ 11 files changed, 495 insertions(+), 181 deletions(-) create mode 100644 discover/src/youtubePlayer.ts diff --git a/Cargo.toml b/Cargo.toml index 2f90b3e..404d575 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,7 +28,7 @@ sha2 = "0.10" [package.metadata.deb] depends = "$auto" -# plays suggested songs from YouTube. Distribution packages may be too old to work with YouTube, +# finds suggested songs on YouTube. Distribution packages may be too old to work with YouTube, # so it's only recommended: a current yt-dlp from elsewhere (e.g. pip) works too recommends = "yt-dlp" assets = [ diff --git a/Dockerfile b/Dockerfile index adf71b9..a465060 100644 --- a/Dockerfile +++ b/Dockerfile @@ -25,14 +25,12 @@ RUN apt-get update \ && apt-get install -y --no-install-recommends ca-certificates libssl3 python3 python3-venv \ && rm -rf /var/lib/apt/lists/* -# yt-dlp plays suggested songs from YouTube. It has to be recent to keep working with YouTube, so +# yt-dlp finds suggested songs on YouTube. It has to be recent to keep working with YouTube, so # the latest version is installed: rebuild the image to update it, or set YTDLP_VERSION to pin one. ARG YTDLP_VERSION= RUN python3 -m venv /opt/yt-dlp \ && /opt/yt-dlp/bin/pip install --no-cache-dir "yt-dlp[default]${YTDLP_VERSION:+==${YTDLP_VERSION}}" \ && ln -s /opt/yt-dlp/bin/yt-dlp /usr/local/bin/yt-dlp -# yt-dlp needs a JavaScript runtime for YouTube; Deno is the one it uses by default -COPY --from=denoland/deno:bin-2.9.8 /deno /usr/local/bin/deno COPY --from=server /src/target/release/playlistd /usr/local/bin/playlistd COPY --from=ui /src/dist /usr/share/playlistd/discover diff --git a/README.md b/README.md index 242d585..8862d5a 100644 --- a/README.md +++ b/README.md @@ -24,8 +24,8 @@ otherwise against the query data in `.sqlx/`. After changing a query, update tha ### Docker Compose -`compose.yaml` runs playlistd with its own Postgres database. The image includes the web UI, -yt-dlp and Deno (which yt-dlp needs for YouTube). +`compose.yaml` runs playlistd with its own Postgres database. The image includes the web UI and +yt-dlp (for finding new songs on YouTube). ```sh cp .env.example .env # then fill in your Subsonic server and a database password @@ -39,8 +39,8 @@ your network. If your Subsonic server runs on the Docker host, use `http://host.docker.internal:4533/rest` as `SUBSONIC_BASE`. -YouTube changes often break older yt-dlp versions, and the image installs the latest one when it's -built, so rebuild it now and then to keep new songs playing: +YouTube changes can break older yt-dlp versions, and the image installs the latest one when it's +built, so rebuild it now and then to keep finding new songs: ```sh docker compose build --pull && docker compose up -d @@ -74,7 +74,7 @@ The package is written to `target/debian/`. It installs the web UI to `/usr/shar which the default config serves at `/`. `cargo deb` refuses to build the package if the UI hasn't been built. -The package recommends `yt-dlp`, for playing suggested songs from YouTube. Distribution packages of +The package recommends `yt-dlp`, for finding suggested songs on YouTube. Distribution packages of yt-dlp are often too old to work with YouTube, so you may want a current one instead, e.g. from pip. ### Logging @@ -191,9 +191,14 @@ Every candidate's MusicBrainz genres are compared with each mix's, and it's sugg it fits best (if it fits well enough), skipping songs already in your library. In the UI, songs from your library stream from Subsonic. New songs are found on YouTube with -[yt-dlp](https://github.com/yt-dlp/yt-dlp) and streamed through playlistd, so yt-dlp needs to be -installed on the server (keep it up to date: YouTube changes often break older versions). You can -like a new song to keep it in "Liked new songs", or hide it so it isn't suggested again. +[yt-dlp](https://github.com/yt-dlp/yt-dlp) on the server (keep it up to date: YouTube changes can +break older versions), and play in the browser, in YouTube's embedded player kept out of sight. +Playing them from the browser, rather than the server, keeps YouTube's bot checks from blocking +them. If the first video found can't be embedded, the next search result is tried. You can like a +new song to keep it in "Liked new songs", or hide it so it isn't suggested again. + +Mobile browsers pause embedded YouTube videos when the screen locks or the tab is in the +background, so new songs only play while the UI is open. "Play mix" plays a mix's new songs in random order with songs you already know (the songs in the mix you've played recently) mixed in evenly, so that `familiar_ratio` of what you hear is familiar. @@ -261,7 +266,8 @@ The UI uses these endpoints under `/api/discover`: | `POST /rules//rebuild` | Rebuild a rule's mixes on the next daemon tick | | `GET /stream/` | Stream a library song from Subsonic | | `GET /cover/` | Library cover art from Subsonic | -| `GET /youtube/` | Stream a suggested song from YouTube | +| `GET /youtube/?refresh=` | YouTube videos for a suggestion, best first (`refresh` searches again) | +| `PUT /youtube/` | Remember the video that played (`video_id`) | and these under `/api/tracked` for tracked playlists: diff --git a/discover/src/api.ts b/discover/src/api.ts index 3b4d93e..1c67d5b 100644 --- a/discover/src/api.ts +++ b/discover/src/api.ts @@ -96,12 +96,17 @@ export const api = { body: JSON.stringify({ status }), }), rebuild: (ruleId: string) => request(`/rules/${ruleId}/rebuild`, { method: "POST" }), + /** Videos to play a suggestion with, best first. `refresh` searches again. */ + youtubeVideos: (suggestionId: string, refresh = false) => + request<{ video_ids: string[]; cached: boolean }>(`/youtube/${suggestionId}?refresh=${refresh}`), + /** The video that played, when it wasn't the first one */ + setYoutubeVideo: (suggestionId: string, videoId: string) => + request(`/youtube/${suggestionId}`, json("PUT", { video_id: videoId })), }; export const coverUrl = (coverArt: string, size = 120) => `/api/discover/cover/${encodeURIComponent(coverArt)}?size=${size}`; export const streamUrl = (songId: string) => `/api/discover/stream/${encodeURIComponent(songId)}`; -export const youtubeUrl = (suggestionId: string) => `/api/discover/youtube/${suggestionId}`; /** "Mix: Pop Punk / Emo" -> "Pop Punk / Emo" */ export const mixTitle = (mix: MixSummary) => diff --git a/discover/src/queue.ts b/discover/src/queue.ts index 464cbc4..44a83e8 100644 --- a/discover/src/queue.ts +++ b/discover/src/queue.ts @@ -1,8 +1,9 @@ -import { coverUrl, Song, streamUrl, Suggestion, youtubeUrl } from "./api"; +import { coverUrl, Song, streamUrl, Suggestion } from "./api"; export type QueueItem = { key: string; source: "library" | "youtube"; + /** Audio to stream, for library songs. YouTube ones play from `suggestionId`. */ src: string; title: string; artist: string; @@ -24,7 +25,7 @@ export const fromSong = (song: Song): QueueItem => ({ export const fromSuggestion = (s: Suggestion): QueueItem => ({ key: `suggestion:${s.id}`, source: "youtube", - src: youtubeUrl(s.id), + src: "", title: s.title, artist: s.artist, album: s.release, diff --git a/discover/src/styles.css b/discover/src/styles.css index 9689bfa..1defaa3 100644 --- a/discover/src/styles.css +++ b/discover/src/styles.css @@ -1125,6 +1125,20 @@ h2 { color: var(--accent); } +/* YouTube's player, for new songs: only the sound is wanted. Not display: none, as browsers + may not start or may throttle players that aren't rendered. */ +.youtube-host { + position: fixed; + left: 0; + bottom: 0; + z-index: -1; + width: 1px; + height: 1px; + overflow: hidden; + opacity: 0; + pointer-events: none; +} + /* ---------- small screens ---------- */ @media (max-width: 800px) { diff --git a/discover/src/usePlayer.ts b/discover/src/usePlayer.ts index e04da4e..79875da 100644 --- a/discover/src/usePlayer.ts +++ b/discover/src/usePlayer.ts @@ -1,16 +1,32 @@ import { useCallback, useEffect, useRef, useState } from "react"; +import { api } from "./api"; import { QueueItem } from "./queue"; +import { HiddenYouTubePlayer, State, YouTubeEvents } from "./youtubePlayer"; export type PlayerStatus = "idle" | "loading" | "playing" | "paused" | "error"; export type Player = ReturnType; +/** The video being tried for the current YouTube item */ +type YouTubeAttempt = { + suggestionId: string; + videoIds: string[]; + at: number; + /** `videoIds` is the one video that played before, so a fresh search might find another */ + cached: boolean; +}; + +/** + * Library songs play in an