Compare commits
18
Commits
gpu-unload
...
ci-pip
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c0343edb64 | ||
|
|
060669fda7 | ||
|
|
15f01c50eb | ||
|
|
2eea510784 | ||
|
|
895c65bb4e | ||
|
|
e3a1bb4809 | ||
|
|
c1368505c2 | ||
|
|
d484d6f1d2 | ||
|
|
6f5e273c6c | ||
|
|
4272189bca | ||
|
|
936641e13c | ||
|
|
793817ce42 | ||
|
|
f3125370d3 | ||
|
|
6d297a8bfc | ||
|
|
4af78fdd1d | ||
|
|
cb485d3c6b | ||
|
|
22c7f6d03f | ||
|
|
7c4359eb39 |
@@ -0,0 +1,6 @@
|
||||
deb.debian.org
|
||||
security.debian.org
|
||||
pypi.org
|
||||
files.pythonhosted.org
|
||||
download.pytorch.org
|
||||
download-r2.pytorch.org
|
||||
@@ -0,0 +1,86 @@
|
||||
name: tests
|
||||
|
||||
# `pull_request_target` reads this file from the base branch, so a pull
|
||||
# request cannot change what runs or where (including `runs-on:`). The run's
|
||||
# ref is therefore the base branch, and the checkout has to name the head.
|
||||
on:
|
||||
pull_request_target:
|
||||
|
||||
jobs:
|
||||
test:
|
||||
# No Docker socket, and egress limited to .dejima/allowed-domains on main.
|
||||
runs-on: dejima-confined
|
||||
|
||||
# Node for actions/checkout; Debian bookworm's python3 is the 3.11 the
|
||||
# Dockerfile ships.
|
||||
container:
|
||||
image: node:22-bookworm
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
ref: ${{ github.event.pull_request.head.sha }}
|
||||
|
||||
# Inline rather than a script in the repository: the files come from the
|
||||
# branch under test, so a script would be missing from older branches.
|
||||
- name: Check the hosts this job fetches from are reachable
|
||||
run: |
|
||||
set -u
|
||||
bad=""
|
||||
for host in deb.debian.org security.debian.org pypi.org \
|
||||
files.pythonhosted.org download.pytorch.org \
|
||||
download-r2.pytorch.org; do
|
||||
resolved=""
|
||||
for attempt in 1 2 3 4; do
|
||||
getent hosts "$host" >/dev/null 2>&1 && { resolved=yes; break; }
|
||||
sleep 2
|
||||
done
|
||||
if [ -z "$resolved" ]; then
|
||||
echo "no $host - does not resolve"; bad="$bad $host(dns)"; continue
|
||||
fi
|
||||
# Any HTTP answer proves the connection; curl reports 000 for none.
|
||||
code="$(curl -sS -o /dev/null --max-time 20 --retry 2 -w '%{http_code}' "https://$host/" 2>/dev/null || true)"
|
||||
case "${code:-000}" in
|
||||
000) echo "no $host - resolves, but nothing answered over https"
|
||||
bad="$bad $host(net)" ;;
|
||||
*) echo "ok $host (http $code)" ;;
|
||||
esac
|
||||
done
|
||||
[ -z "$bad" ] && exit 0
|
||||
{
|
||||
echo
|
||||
echo "Unreachable:$bad"
|
||||
echo
|
||||
echo "The confined tier reaches only what .dejima/allowed-domains on"
|
||||
echo "main declares, rendered onto the runner separately from the"
|
||||
echo "merge. On the runner:"
|
||||
echo " sudo dejima-ci-allowlist show - is the name listed?"
|
||||
echo " sudo dejima-ci-allowlist check - is a change pending?"
|
||||
echo " sudo dejima-ci-allowlist render - apply it now"
|
||||
echo "A (dns) failure is the resolver; a (net) one is the packet"
|
||||
echo "filter, which sudo dejima-ci-firewall reapplies."
|
||||
echo
|
||||
echo "download.pytorch.org redirects its wheels to"
|
||||
echo "download-r2.pytorch.org, so both have to be allowed."
|
||||
} >&2
|
||||
exit 1
|
||||
|
||||
- name: Install Python
|
||||
run: |
|
||||
apt-get update
|
||||
apt-get install -y --no-install-recommends python3 python3-venv
|
||||
python3 -m venv /opt/venv
|
||||
# bookworm's pip 23.0 rejects the PyTorch index's typing_extensions
|
||||
# wheel over its name's spelling, then cannot build it from source
|
||||
/opt/venv/bin/pip install --retries 5 --upgrade pip
|
||||
echo /opt/venv/bin >> "$GITHUB_PATH"
|
||||
|
||||
# The CPU build first: torch from PyPI is the CUDA build, several GB of
|
||||
# libraries no test uses. The install below then finds torch satisfied.
|
||||
- name: Install CPU-only torch
|
||||
run: pip install --retries 5 torch torchaudio --index-url https://download.pytorch.org/whl/cpu
|
||||
|
||||
- name: Install songtrace
|
||||
run: pip install --retries 5 -e '.[dev]'
|
||||
|
||||
- run: pytest
|
||||
@@ -34,6 +34,27 @@ printing the low-confidence regions it found (e.g.
|
||||
editor such as [Yass](https://www.yass-along.com/) or
|
||||
[USDX](https://usdx.eu/) and touch those spots up before playing.
|
||||
|
||||
## Reviewing a song
|
||||
|
||||
The web UI plays the finished song folder back against the review, so you can
|
||||
check the timing without loading it into a game first. Pick a song in the
|
||||
queue and the review panel gets the packaged video with a transport above the
|
||||
lyrics; as it plays, the line being sung is highlighted, words light up as
|
||||
they are reached, and a playhead sweeps each line against its notes. Click any
|
||||
line or word to play from there.
|
||||
|
||||
Playback is modelled on the game: the `#MP3` is the audio and the clock, and
|
||||
the `#VIDEO` is muted background slaved to it. It also uses the same seconds
|
||||
the notes were generated from, so a word that lights up late in the browser is
|
||||
late in the game too.
|
||||
|
||||
The packaged video has its audio track stripped for the same reason. It is the
|
||||
same recording as the `#MP3`, so a player that does not mute the video plays
|
||||
both at once, a few frames apart, which is heard as an echo. Songs built
|
||||
before this change still have it — check with `ffprobe <song>.mp4` for an
|
||||
audio stream, and rebuild with **Restart from** set to `package` to clear it
|
||||
without re-downloading.
|
||||
|
||||
## Configuration
|
||||
|
||||
Config is TOML. Resolution order:
|
||||
@@ -53,7 +74,7 @@ See `config.example.toml` for every available field (`whisper_url`,
|
||||
`whisper_api_key`, `whisper_model`, `whisper_api`, `output_dir`, `work_dir`,
|
||||
`creator`, `demucs_model`, `crepe_model`, `device`, `bind_host`, `port`,
|
||||
`ytdlp_clients`, `ytdlp_cookies`, `ytdlp_impersonate`,
|
||||
`ytdlp_pot_provider_url`).
|
||||
`ytdlp_pot_provider_url`, `ytdlp_version`).
|
||||
|
||||
`whisper_api` selects the wire format: `"openai"` (default) speaks the
|
||||
OpenAI-compatible `POST {whisper_url}/audio/transcriptions` API; `"asr-webservice"`
|
||||
@@ -146,29 +167,45 @@ Valid stages, in pipeline order: `acquire`, `separate`, `transcribe`,
|
||||
In the web UI this is what selecting a queue entry does: its title, artist,
|
||||
lyrics and the rest come back up in the form, so you can adjust them and hit
|
||||
REBUILD SONG. Only the stages your edit actually invalidates are rerun —
|
||||
fixing a line of lyrics realigns without re-downloading or re-separating. NEW
|
||||
SONG INSTEAD clears the form to start a fresh song.
|
||||
fixing a line of lyrics realigns without re-downloading or re-separating.
|
||||
CREATE SONG, at the top of the queue, clears the page to start a fresh one.
|
||||
|
||||
That planning works off the fields you changed, which cannot see a change in
|
||||
songtrace itself: after an upgrade, a rebuild of an older song finds every
|
||||
stage cached and does nothing. **Restart from** on the rebuild form is the way
|
||||
to redo a stage anyway — pick `package` to repackage without re-downloading.
|
||||
It can only widen what is rerun, so an edit that invalidates an earlier stage
|
||||
still wins.
|
||||
|
||||
**REBUILD ALL FROM…**, under CREATE SONG, does the same for every song at once:
|
||||
pick the stage an update improved and each song is rebuilt from there with the
|
||||
inputs it has now. Every song starts ticked; untick any already rebuilt by hand
|
||||
(each shows when it was last built). Songs still in progress are skipped, as
|
||||
are songs whose uploaded video is gone when the stage is `acquire`.
|
||||
|
||||
## YouTube download failures
|
||||
|
||||
`HTTP Error 403: Forbidden` from the acquire stage means YouTube served the
|
||||
page but refused the media URL. In rough order of likelihood:
|
||||
|
||||
1. **yt-dlp is stale.** YouTube changes its defences every few weeks and the
|
||||
version baked into the image does not. Hotfix a running container, then
|
||||
rebuild so it sticks:
|
||||
1. **yt-dlp is stale.** YouTube changes its defences every few weeks, so
|
||||
songtrace keeps yt-dlp current itself. Once a day, and again whenever a
|
||||
download fails outright, it checks PyPI for a newer stable release,
|
||||
installs it under `$XDG_CACHE_HOME/songtrace/` (`/data/models/cache` in the
|
||||
container), and tries it before the copy installed with songtrace. That
|
||||
copy is never modified and is always the fallback, so a release that breaks
|
||||
compatibility costs a retry rather than the download. The job log names
|
||||
the yt-dlp version behind each attempt.
|
||||
|
||||
```sh
|
||||
docker compose exec songtrace pip install -U 'yt-dlp[default,curl-cffi,deno]'
|
||||
docker compose restart songtrace
|
||||
docker compose build --pull songtrace # permanent
|
||||
```
|
||||
`ytdlp_version` overrides this: `"bundled"` uses only the installed copy,
|
||||
and a release number such as `"2026.08.19"` pins that release instead of
|
||||
the newest.
|
||||
|
||||
2. **Missing JS runtime.** yt-dlp needs `yt-dlp-ejs` plus a JavaScript engine
|
||||
to solve YouTube's player challenges; without them it silently falls back to
|
||||
formats that 403. The `[default,curl-cffi,deno]` extras in `pyproject.toml`
|
||||
cover this. Check with
|
||||
`docker compose exec songtrace yt-dlp -v --simulate <url>` — the debug
|
||||
cover this, for both the installed copy and the ones fetched at runtime.
|
||||
Check with `docker compose exec songtrace yt-dlp -v --simulate <url>` — the debug
|
||||
header must show a `JS runtimes:` line that is not `none`.
|
||||
|
||||
3. **The IP is rate-limited or flagged.** Datacenter and VPN ranges get
|
||||
@@ -192,7 +229,9 @@ rather than starting over.
|
||||
## Regenerating the web UI screenshot
|
||||
|
||||
`scripts/screenshot_webui.py` seeds the real app with a fixed set of jobs
|
||||
covering every status and screenshots it to `docs/images/webui.png`:
|
||||
covering every status and screenshots it to `docs/images/webui.png`. It uses
|
||||
`ffmpeg` to give the selected song a playable folder so the shot includes the
|
||||
player; without `ffmpeg` on `PATH` it still runs, just without it.
|
||||
|
||||
```sh
|
||||
pip install -e '.[dev]'
|
||||
|
||||
@@ -18,3 +18,6 @@ bind_host = "0.0.0.0"
|
||||
# ytdlp_cookies = "/config/youtube-cookies.txt" # Netscape cookie jar
|
||||
# ytdlp_impersonate = "chrome" # TLS fingerprint to present
|
||||
# ytdlp_pot_provider_url = "http://bgutil-provider:4416"
|
||||
# Newer yt-dlp releases are fetched from PyPI at runtime and tried before the
|
||||
# bundled one; "bundled" turns that off, a release number pins one.
|
||||
# ytdlp_version = "auto"
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 111 KiB After Width: | Height: | Size: 84 KiB |
@@ -7,6 +7,8 @@ One-time setup: pip install -e '.[dev]' && playwright install chromium
|
||||
Run: python scripts/screenshot_webui.py
|
||||
"""
|
||||
import json
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
import threading
|
||||
import time
|
||||
@@ -110,6 +112,7 @@ SONGS = [
|
||||
("Vampire in the Corner", "Magdalena Bay", "queued", {}),
|
||||
]
|
||||
SELECTED = "Greensleeves"
|
||||
REVIEW_HEADING = "REVIEW"
|
||||
|
||||
running_event = threading.Event()
|
||||
|
||||
@@ -157,14 +160,42 @@ def make_runner():
|
||||
return runner
|
||||
|
||||
|
||||
def start_server(tmp_path: Path) -> str:
|
||||
def start_server(tmp_path: Path) -> tuple[str, Config]:
|
||||
cfg = Config(whisper_url="http://localhost:9000/v1", output_dir=tmp_path / "out",
|
||||
work_dir=tmp_path / "work", bind_host="127.0.0.1", port=8811)
|
||||
app = create_app(cfg, runner=make_runner())
|
||||
server = uvicorn.Server(uvicorn.Config(app, host=cfg.bind_host, port=cfg.port,
|
||||
log_level="warning"))
|
||||
threading.Thread(target=server.run, daemon=True).start()
|
||||
return f"http://{cfg.bind_host}:{cfg.port}"
|
||||
return f"http://{cfg.bind_host}:{cfg.port}", cfg
|
||||
|
||||
|
||||
def install_media(cfg: Config) -> bool:
|
||||
"""Give the selected song a playable folder, so the shot shows the player.
|
||||
|
||||
A flat fill rather than a test pattern: the point is the transport and the
|
||||
lyric tracking underneath it, not the picture. Skipped without ffmpeg.
|
||||
"""
|
||||
if shutil.which("ffmpeg") is None:
|
||||
print("ffmpeg not found; screenshotting without the player")
|
||||
return False
|
||||
artist, title = next((a, t) for t, a, _, _ in SONGS if t == SELECTED)
|
||||
slug = f"{artist} - {title}"
|
||||
out = cfg.output_dir / slug
|
||||
out.mkdir(parents=True, exist_ok=True)
|
||||
subprocess.run(
|
||||
["ffmpeg", "-y", "-loglevel", "error", "-f", "lavfi",
|
||||
"-i", "color=c=0x1A1430:s=640x360:d=60", "-c:v", "libx264",
|
||||
"-pix_fmt", "yuv420p", "-preset", "ultrafast", "-an",
|
||||
str(out / f"{slug}.mp4")], check=True)
|
||||
subprocess.run(
|
||||
["ffmpeg", "-y", "-loglevel", "error", "-f", "lavfi",
|
||||
"-i", "anullsrc=r=44100:cl=stereo:d=60", "-b:a", "64k",
|
||||
str(out / f"{slug}.mp3")], check=True)
|
||||
(out / f"{slug}.txt").write_text(
|
||||
f"#TITLE:{title}\n#ARTIST:{artist}\n#MP3:{slug}.mp3\n#VIDEO:{slug}.mp4\n"
|
||||
f"#BPM:240\n#GAP:10000\n: 0 4 16 Alas,\nE\n", encoding="utf-8")
|
||||
return True
|
||||
|
||||
|
||||
def seed_jobs(base_url: str) -> None:
|
||||
@@ -187,13 +218,25 @@ def seed_jobs(base_url: str) -> None:
|
||||
time.sleep(0.05)
|
||||
|
||||
|
||||
def capture(base_url: str) -> None:
|
||||
def capture(base_url: str, with_player: bool) -> None:
|
||||
with sync_playwright() as p:
|
||||
browser = p.chromium.launch()
|
||||
page = browser.new_page(viewport={"width": 1280, "height": 900})
|
||||
page = browser.new_page(viewport={"width": 1280, "height": 1000})
|
||||
page.goto(base_url, wait_until="networkidle")
|
||||
page.get_by_text(SELECTED, exact=True).click()
|
||||
page.wait_for_timeout(300)
|
||||
page.wait_for_timeout(500)
|
||||
if with_player:
|
||||
# paused mid-line, so the shot carries the playhead and the sung /
|
||||
# current word colouring rather than an untouched transport
|
||||
page.evaluate("document.getElementById('player-media').currentTime = 11.6")
|
||||
page.wait_for_timeout(400)
|
||||
# scrolled to the review: the player pins to the top of #main and
|
||||
# the tracked lines sit under it, which is the panel in use
|
||||
page.evaluate("""() => {
|
||||
const main = document.getElementById('main');
|
||||
main.scrollTo(0, document.getElementById('review').offsetTop - 12);
|
||||
}""")
|
||||
page.wait_for_timeout(400)
|
||||
OUT_IMAGE.parent.mkdir(parents=True, exist_ok=True)
|
||||
page.screenshot(path=str(OUT_IMAGE))
|
||||
browser.close()
|
||||
@@ -201,10 +244,11 @@ def capture(base_url: str) -> None:
|
||||
|
||||
def main() -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
base_url = start_server(Path(tmp))
|
||||
base_url, cfg = start_server(Path(tmp))
|
||||
time.sleep(1)
|
||||
seed_jobs(base_url)
|
||||
capture(base_url)
|
||||
with_player = install_media(cfg)
|
||||
capture(base_url, with_player)
|
||||
print(f"wrote {OUT_IMAGE}")
|
||||
|
||||
|
||||
|
||||
@@ -24,6 +24,9 @@ class Config:
|
||||
ytdlp_cookies: Path | None = None
|
||||
ytdlp_impersonate: str | None = None
|
||||
ytdlp_pot_provider_url: str | None = None
|
||||
# "auto" keeps yt-dlp current from PyPI, "bundled" uses only the copy
|
||||
# installed with songtrace, and a release such as "2026.08.19" pins that.
|
||||
ytdlp_version: str = "auto"
|
||||
|
||||
|
||||
_PATH_FIELDS = {"output_dir", "work_dir", "ytdlp_cookies"}
|
||||
|
||||
+2
-10
@@ -84,19 +84,11 @@ def build_notes(rows: list[dict], times, f0, conf, language: str | None,
|
||||
) -> tuple[list[TimedNote], list[float], list[str]]:
|
||||
notes: list[TimedNote] = []
|
||||
breaks: list[float] = []
|
||||
prev_line, prev_end, last_seen_line = None, 0.0, None
|
||||
prev_line, prev_end = None, 0.0
|
||||
for row in rows:
|
||||
# last_seen_line tracks every row (skipped or not) so a run of
|
||||
# skipped rows can't trigger a duplicate break on the next row of
|
||||
# the same still-unseen line; prev_line/prev_end only advance on
|
||||
# rows that actually emit notes, so a skipped row can't corrupt
|
||||
# line_initial or break timing for its own line's later words.
|
||||
if last_seen_line is not None and row["line_index"] > last_seen_line:
|
||||
if prev_line is not None and row["line_index"] > prev_line:
|
||||
breaks.append((prev_end + row["start"]) / 2)
|
||||
last_seen_line = row["line_index"]
|
||||
line_initial = row["line_index"] != prev_line
|
||||
if any(a <= row["start"] < b for a, b in missing_spans):
|
||||
continue
|
||||
prev_line, prev_end = row["line_index"], row["end"]
|
||||
segs = segment_span(times, f0, conf, row["start"], row["end"])
|
||||
if not segs:
|
||||
|
||||
@@ -38,6 +38,17 @@ def plan(old: SongInput, new: SongInput) -> str | None:
|
||||
return min((FIELD_STAGE[f] for f in changed), key=STAGE_ORDER.index)
|
||||
|
||||
|
||||
def earliest(*stages: str | None) -> str | None:
|
||||
"""The earliest of the stages given, ignoring None ("nothing forced").
|
||||
|
||||
An operator's explicit choice is combined with plan()'s rather than
|
||||
replacing it: the planner's stage is the latest that is still correct for
|
||||
the edit, so a choice may only ever redo more.
|
||||
"""
|
||||
named = [s for s in stages if s]
|
||||
return min(named, key=STAGE_ORDER.index) if named else None
|
||||
|
||||
|
||||
def relocate_work(old: SongInput, new: SongInput, cfg: Config) -> bool:
|
||||
"""Move the cached work dir when title/artist changed its slug.
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ import json
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
|
||||
from .. import ytdlp
|
||||
from ..config import Config
|
||||
from ..media import ffmpeg
|
||||
from ..pipeline import Ctx, PipelineError, Stage
|
||||
@@ -22,67 +23,50 @@ def _classify_downloads(dest: Path) -> tuple[Path, Path | None]:
|
||||
return video, thumbs[0] if thumbs else None
|
||||
|
||||
|
||||
class _YdlLog:
|
||||
"""Routes yt-dlp's own messages into the job log, minus progress noise."""
|
||||
|
||||
def __init__(self, log):
|
||||
self._log = log
|
||||
|
||||
def debug(self, msg: str) -> None:
|
||||
if not msg.startswith("[debug] "):
|
||||
self._log(msg)
|
||||
|
||||
def warning(self, msg: str) -> None:
|
||||
self._log(msg)
|
||||
|
||||
error = warning
|
||||
|
||||
|
||||
def _ydl_opts(cfg: Config, dest: Path, client: str, log) -> dict:
|
||||
extractor_args: dict[str, dict[str, list[str]]] = {}
|
||||
def _ydl_args(cfg: Config, dest: Path, client: str, url: str) -> list[str]:
|
||||
args = [
|
||||
"--output", str(dest / "source_video.%(ext)s"),
|
||||
"--format", "bestvideo*+bestaudio/best",
|
||||
"--write-thumbnail", "--no-playlist", "--no-progress",
|
||||
"--retries", "10", "--fragment-retries", "10",
|
||||
"--extractor-retries", "5",
|
||||
]
|
||||
if client != "default":
|
||||
extractor_args["youtube"] = {"player_client": [client]}
|
||||
args += ["--extractor-args", f"youtube:player_client={client}"]
|
||||
if cfg.ytdlp_pot_provider_url:
|
||||
extractor_args["youtubepot-bgutilhttp"] = {
|
||||
"base_url": [cfg.ytdlp_pot_provider_url]}
|
||||
opts = {
|
||||
"outtmpl": str(dest / "source_video.%(ext)s"),
|
||||
"format": "bestvideo*+bestaudio/best",
|
||||
"writethumbnail": True,
|
||||
"noplaylist": True,
|
||||
"noprogress": True,
|
||||
"logger": _YdlLog(log),
|
||||
"retries": 10,
|
||||
"fragment_retries": 10,
|
||||
"extractor_retries": 5,
|
||||
"extractor_args": extractor_args,
|
||||
}
|
||||
args += ["--extractor-args",
|
||||
f"youtubepot-bgutilhttp:base_url={cfg.ytdlp_pot_provider_url}"]
|
||||
if cfg.ytdlp_cookies:
|
||||
opts["cookiefile"] = str(cfg.ytdlp_cookies)
|
||||
args += ["--cookies", str(cfg.ytdlp_cookies)]
|
||||
if cfg.ytdlp_impersonate:
|
||||
from yt_dlp.networking.impersonate import ImpersonateTarget
|
||||
opts["impersonate"] = ImpersonateTarget.from_str(cfg.ytdlp_impersonate)
|
||||
return opts
|
||||
args += ["--impersonate", cfg.ytdlp_impersonate]
|
||||
return [*args, "--", url]
|
||||
|
||||
|
||||
def download_youtube(url: str, dest: Path, cfg: Config,
|
||||
log=print) -> tuple[Path, Path | None]:
|
||||
import yt_dlp
|
||||
clients = list(cfg.ytdlp_clients) or ["default"]
|
||||
tried: list[str] = []
|
||||
last: Exception | None = None
|
||||
for client in clients:
|
||||
try:
|
||||
with yt_dlp.YoutubeDL(_ydl_opts(cfg, dest, client, log)) as ydl:
|
||||
ydl.extract_info(url, download=True)
|
||||
return _classify_downloads(dest)
|
||||
except Exception as e:
|
||||
last = e
|
||||
log(f"yt-dlp player_client={client} failed: {e}")
|
||||
# If every copy fails, look for a release newer than the daily check found.
|
||||
for refresh in (False, True):
|
||||
for ydl in ytdlp.candidates(cfg, log, refresh):
|
||||
if ydl.version in tried:
|
||||
continue
|
||||
tried.append(ydl.version)
|
||||
for client in clients:
|
||||
try:
|
||||
ydl.run(_ydl_args(cfg, dest, client, url), log)
|
||||
return _classify_downloads(dest)
|
||||
except PipelineError as e:
|
||||
last = e
|
||||
log(f"yt-dlp {ydl.version} player_client={client} "
|
||||
f"failed: {e}")
|
||||
raise PipelineError(
|
||||
f"yt-dlp {yt_dlp.version.__version__} could not download {url} after "
|
||||
f"trying player_client={', '.join(clients)}. Last error: {last}. "
|
||||
"YouTube blocks change often: upgrade yt-dlp, then see the yt-dlp "
|
||||
"options in config.toml (cookies, impersonation, PO token provider).")
|
||||
f"yt-dlp {', '.join(tried)} could not download {url} after trying "
|
||||
f"player_client={', '.join(clients)}. Last error: {last}. "
|
||||
"See the yt-dlp options in config.toml (cookies, impersonation, "
|
||||
"PO token provider).")
|
||||
|
||||
|
||||
def _run(ctx: Ctx) -> None:
|
||||
|
||||
@@ -51,11 +51,15 @@ def _run(ctx: Ctx) -> None:
|
||||
meta = json.loads((ctx.work / "meta.json").read_text())
|
||||
src_video = ctx.work / meta["video_file"]
|
||||
fmt, codec = ffprobe_codec(src_video)
|
||||
# The video is background only: the song's audio is #MP3, and the two are
|
||||
# the same recording. A player that does not mute the video plays both at
|
||||
# once, which is heard as an echo, so the audio track is dropped here.
|
||||
if (fmt, codec) in GOOD_VIDEO:
|
||||
shutil.copyfile(src_video, out / f"{slug}{src_video.suffix}")
|
||||
ffmpeg("-i", str(src_video), "-c", "copy", "-an",
|
||||
str(out / f"{slug}{src_video.suffix}"))
|
||||
else:
|
||||
ffmpeg("-i", str(src_video), "-c:v", "libx264", "-crf", "20",
|
||||
"-preset", "medium", "-c:a", "aac", str(out / f"{slug}.mp4"))
|
||||
"-preset", "medium", "-an", str(out / f"{slug}.mp4"))
|
||||
_set_header(txt, "VIDEO", f"{slug}.mp4")
|
||||
cover_src = ctx.inp.cover_path
|
||||
if cover_src is None and meta.get("thumbnail"):
|
||||
|
||||
@@ -6,6 +6,11 @@ import numpy as np
|
||||
from ..models import on_release
|
||||
from ..pipeline import Ctx, Stage
|
||||
|
||||
# Separated vocal stems keep faint bleed that crepe still finds a pitch in, so
|
||||
# frames this far below the song's loud vocal level count as silence. Relative
|
||||
# rather than absolute, so it holds however loud the song was mastered.
|
||||
SILENCE_BELOW_LOUD_DB = 25.0
|
||||
|
||||
|
||||
@on_release
|
||||
def unload_model() -> None:
|
||||
@@ -18,6 +23,11 @@ def unload_model() -> None:
|
||||
delattr(torchcrepe.infer, attr)
|
||||
|
||||
|
||||
def mute_silence(conf: np.ndarray, loudness: np.ndarray) -> None:
|
||||
"""Zero the confidence of frames quiet enough to be bleed, in place."""
|
||||
conf[loudness < np.quantile(loudness, 0.9) - SILENCE_BELOW_LOUD_DB] = 0.0
|
||||
|
||||
|
||||
def track_pitch(vocals_path: Path, model: str, device: str):
|
||||
import torchaudio
|
||||
import torchcrepe
|
||||
@@ -33,6 +43,7 @@ def track_pitch(vocals_path: Path, model: str, device: str):
|
||||
return_periodicity=True, batch_size=512)
|
||||
f0 = f0[0].cpu().numpy()
|
||||
conf = periodicity[0].cpu().numpy()
|
||||
mute_silence(conf, torchcrepe.loudness.a_weighted(waveform, 16000, 160)[0].cpu().numpy())
|
||||
times = np.arange(len(f0)) * 0.01
|
||||
return times, f0, conf
|
||||
|
||||
|
||||
@@ -47,9 +47,8 @@ def classify_lyrics_match(segments, ref: RefLyrics, pad: float = 1.0
|
||||
dropped_lines: reference line indices covered only by 'insert' opcodes
|
||||
(present in the lyrics, never heard). missing_spans: the padded, merged
|
||||
bounds of the heard segments behind 'delete' opcodes (heard, but matching
|
||||
no reference word) — scoped tightly to the unmatched audio itself, not
|
||||
the whole vocal section it falls in, so matched lines sharing a section
|
||||
with an ad-lib keep their notes.
|
||||
no reference word). A segment can also hold matched lines, so a span
|
||||
marks where to look, not words to discard.
|
||||
"""
|
||||
matcher, seg_of, line_of = match_lyrics(segments, ref)
|
||||
covered = [False] * len(line_of)
|
||||
|
||||
+435
-20
@@ -111,6 +111,62 @@ body {
|
||||
}
|
||||
|
||||
#sidebar .eyebrow, #jobs .empty-note { padding: 0 18px; }
|
||||
#create-song { display: block; width: calc(100% - 36px); margin: 0 18px 20px; padding: 9px 12px; }
|
||||
|
||||
#rebuild-all { display: block; margin: -8px 18px 20px; }
|
||||
#rebuild-all-note { margin: -12px 18px 20px; }
|
||||
|
||||
#rebuild-all-dialog {
|
||||
width: min(480px, calc(100vw - 32px));
|
||||
padding: 26px 28px;
|
||||
color: var(--text);
|
||||
background: var(--panel);
|
||||
border: 1px solid var(--border);
|
||||
}
|
||||
#rebuild-all-dialog::backdrop { background: rgb(10 10 15 / 0.75); }
|
||||
#rebuild-all-dialog h2 {
|
||||
margin: 0 0 14px;
|
||||
font-family: var(--font-display);
|
||||
font-weight: 700;
|
||||
font-size: 0.8rem;
|
||||
letter-spacing: 0.22em;
|
||||
color: var(--text-muted);
|
||||
}
|
||||
#rebuild-all-form { display: grid; gap: 16px; }
|
||||
#rebuild-all-form p { margin: 0; font-size: 0.88rem; line-height: 1.5; }
|
||||
#rebuild-all-count { color: var(--warn); }
|
||||
#rebuild-all-songs { margin: 0; padding: 0; border: 0; min-width: 0; }
|
||||
#rebuild-all-songs legend {
|
||||
margin-bottom: 6px;
|
||||
padding: 0;
|
||||
font-family: var(--font-display);
|
||||
font-weight: 600;
|
||||
font-size: 0.68rem;
|
||||
letter-spacing: 0.14em;
|
||||
text-transform: uppercase;
|
||||
color: var(--text-muted);
|
||||
}
|
||||
#rebuild-all-list {
|
||||
max-height: 40vh;
|
||||
overflow-y: auto;
|
||||
border-top: 1px solid var(--border);
|
||||
border-bottom: 1px solid var(--border);
|
||||
}
|
||||
.rebuild-all-row {
|
||||
display: grid;
|
||||
grid-template-columns: auto 1fr;
|
||||
gap: 1px 10px;
|
||||
align-items: baseline;
|
||||
padding: 7px 4px;
|
||||
font-size: 0.88rem;
|
||||
cursor: pointer;
|
||||
}
|
||||
.rebuild-all-row input { accent-color: var(--purple); }
|
||||
.rebuild-all-row:has(input:disabled) { cursor: default; color: var(--text-muted); }
|
||||
.rebuild-all-row .meta { grid-column: 2; font-size: 0.76rem; color: var(--text-muted); }
|
||||
.rebuild-all-every { padding-top: 0; color: var(--text-muted); }
|
||||
#rebuild-all-error { min-height: 1.2em; color: var(--fail); }
|
||||
#rebuild-all-error:empty { display: none; }
|
||||
|
||||
#jobs { display: flex; flex-direction: column; gap: 1px; margin-top: 8px; }
|
||||
|
||||
@@ -199,6 +255,8 @@ body {
|
||||
.song-form-lyrics textarea { height: 100%; flex: 1 1 auto; }
|
||||
.pair { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
|
||||
.field { display: flex; flex-direction: column; gap: 6px; }
|
||||
/* [hidden] is only display:none, which the rule above would otherwise win */
|
||||
.field[hidden], .keep-note[hidden] { display: none; }
|
||||
.field > span {
|
||||
font-family: var(--font-display);
|
||||
font-weight: 600;
|
||||
@@ -320,8 +378,27 @@ button { cursor: pointer; }
|
||||
}
|
||||
#review h3 span { color: var(--text-muted); font-weight: 400; }
|
||||
|
||||
/* sticks to the top of #main so the video stays in frame as the lyrics scroll */
|
||||
#player { position: sticky; top: 0; z-index: 2; margin: 0 0 14px; padding: 6px 0 10px; background: var(--bg); }
|
||||
#player-video {
|
||||
display: block;
|
||||
width: 100%;
|
||||
max-height: 30vh;
|
||||
background: #000;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 2px;
|
||||
}
|
||||
#player-video[hidden] { display: none; }
|
||||
/* the native transport renders light otherwise, against everything around it */
|
||||
#player-media { display: block; width: 100%; margin-top: 8px; color-scheme: dark; }
|
||||
|
||||
#lyrics-review { display: flex; flex-direction: column; gap: 10px; }
|
||||
.review-line { padding: 8px 12px; background: var(--panel); border: 1px solid var(--border); }
|
||||
.review-line-active { background: var(--panel-raised); border-color: var(--aqua); }
|
||||
.review-word-sung { color: var(--text-muted); }
|
||||
.review-word-active { color: var(--aqua); font-weight: 600; }
|
||||
.review-playhead { position: absolute; top: 0; bottom: 0; width: 2px; margin-left: -1px; background: var(--aqua); }
|
||||
.review-seek { cursor: pointer; }
|
||||
.review-line-text { display: block; font-size: 0.9rem; }
|
||||
.review-line-dropped { opacity: 0.55; }
|
||||
.review-line-dropped .review-line-text { text-decoration: line-through; }
|
||||
@@ -355,7 +432,7 @@ button { cursor: pointer; }
|
||||
border: 1px solid var(--border);
|
||||
}
|
||||
.output-path code { flex: 1; overflow-wrap: anywhere; font-family: var(--font-mono); font-size: 0.82rem; }
|
||||
#copy-path, .action, #cancel-rebuild {
|
||||
#copy-path, .action {
|
||||
flex: none;
|
||||
padding: 5px 12px;
|
||||
font-family: var(--font-display);
|
||||
@@ -368,7 +445,7 @@ button { cursor: pointer; }
|
||||
border: 1px solid var(--control);
|
||||
border-radius: 2px;
|
||||
}
|
||||
#copy-path:hover, .action:hover, #cancel-rebuild:hover { border-color: var(--text-muted); }
|
||||
#copy-path:hover, .action:hover { border-color: var(--text-muted); }
|
||||
|
||||
.form-actions { display: flex; align-items: center; gap: 14px; }
|
||||
.keep-note { margin: -8px 0 0; font-size: 0.78rem; color: var(--aqua); }
|
||||
@@ -495,11 +572,40 @@ button { cursor: pointer; }
|
||||
<div class="kana" aria-hidden="true">カラオケ</div>
|
||||
</div>
|
||||
<div>
|
||||
<button type="button" id="create-song" class="primary">CREATE SONG</button>
|
||||
<button type="button" id="rebuild-all" class="action">REBUILD ALL FROM…</button>
|
||||
<p id="rebuild-all-note" class="empty-note" role="status" hidden></p>
|
||||
<h2 class="eyebrow">QUEUE</h2>
|
||||
<div id="jobs"></div>
|
||||
</div>
|
||||
</aside>
|
||||
|
||||
<dialog id="rebuild-all-dialog" aria-labelledby="rebuild-all-heading">
|
||||
<form id="rebuild-all-form" method="dialog">
|
||||
<h2 id="rebuild-all-heading">REBUILD ALL SONGS</h2>
|
||||
<p>Reruns the songs ticked below from the chosen stage onwards with the
|
||||
inputs they have now, for when a songtrace update improves that stage.</p>
|
||||
<label class="field">
|
||||
<span>Restart from</span>
|
||||
<select name="force_from" required></select>
|
||||
</label>
|
||||
<fieldset id="rebuild-all-songs">
|
||||
<legend>Songs</legend>
|
||||
<label class="rebuild-all-row rebuild-all-every">
|
||||
<input type="checkbox" id="rebuild-all-toggle" checked>
|
||||
<span>All songs</span>
|
||||
</label>
|
||||
<div id="rebuild-all-list"></div>
|
||||
</fieldset>
|
||||
<p id="rebuild-all-count"></p>
|
||||
<p id="rebuild-all-error" role="alert"></p>
|
||||
<div class="form-actions">
|
||||
<button type="submit" class="primary">REBUILD SELECTED</button>
|
||||
<button type="button" class="action" id="rebuild-all-cancel">CANCEL</button>
|
||||
</div>
|
||||
</form>
|
||||
</dialog>
|
||||
|
||||
<main id="main">
|
||||
<section id="new-song">
|
||||
<h2>NEW SONG</h2>
|
||||
@@ -513,6 +619,10 @@ button { cursor: pointer; }
|
||||
<label class="field"><span>YouTube URL</span><input name="youtube_url"></label>
|
||||
<label class="field"><span>Video file</span><input type="file" name="video"></label>
|
||||
<p class="keep-note" data-keep="video" hidden></p>
|
||||
<label class="field" id="restart-field" hidden>
|
||||
<span>Restart from</span>
|
||||
<select name="force_from"></select>
|
||||
</label>
|
||||
<details>
|
||||
<summary>Advanced</summary>
|
||||
<div class="stack">
|
||||
@@ -533,7 +643,6 @@ button { cursor: pointer; }
|
||||
</div>
|
||||
<div class="form-actions">
|
||||
<button type="submit" class="primary">CREATE SONG</button>
|
||||
<button type="button" id="cancel-rebuild" hidden>NEW SONG INSTEAD</button>
|
||||
<p id="form-lock" hidden></p>
|
||||
</div>
|
||||
</form>
|
||||
@@ -545,6 +654,10 @@ button { cursor: pointer; }
|
||||
<h3 id="review-title"></h3>
|
||||
<p id="review-lineage" hidden></p>
|
||||
<p id="review-error" class="review-error" hidden></p>
|
||||
<div id="player" hidden>
|
||||
<video id="player-video" muted playsinline preload="metadata"></video>
|
||||
<audio id="player-media" controls preload="metadata"></audio>
|
||||
</div>
|
||||
<div id="lyrics-review"><p class="empty-note">no songs queued yet — nothing to review</p></div>
|
||||
<div id="output-path"></div>
|
||||
<div id="review-actions"></div>
|
||||
@@ -575,12 +688,15 @@ const nowTitle = document.getElementById("now-title");
|
||||
const stageTrack = document.getElementById("stage-track");
|
||||
const reviewActions = document.getElementById("review-actions");
|
||||
const reviewLineage = document.getElementById("review-lineage");
|
||||
const cancelRebuild = document.getElementById("cancel-rebuild");
|
||||
const createSong = document.getElementById("create-song");
|
||||
const formLock = document.getElementById("form-lock");
|
||||
const formHeading = document.querySelector("#new-song h2");
|
||||
const submitButton = form.querySelector("button[type=submit]");
|
||||
let rebuildOf = null;
|
||||
let formDirty = false;
|
||||
// set by CREATE SONG: the page is a blank sheet on purpose, so the poll must
|
||||
// not quietly select the newest song back into it
|
||||
let composing = false;
|
||||
let JOBS = [];
|
||||
let selectedId = null;
|
||||
let STAGES = [];
|
||||
@@ -615,10 +731,16 @@ form.addEventListener("submit", async (e) => {
|
||||
if (!resp.ok) {
|
||||
const body = await resp.json().catch(() => ({}));
|
||||
formError.textContent = body.detail || `error: ${resp.status}`;
|
||||
return; // before the reset below, so a rejected submission is never retyped
|
||||
return;
|
||||
}
|
||||
selectedId = (await resp.json()).id;
|
||||
exitRebuild();
|
||||
composing = false;
|
||||
// the fields stay as submitted: the song just sent is the obvious starting
|
||||
// point for the next one, and nothing has to be retyped to correct it. The
|
||||
// queue's CREATE SONG button is what empties the form.
|
||||
rebuildOf = null;
|
||||
formDirty = false;
|
||||
setFormMode(null, null);
|
||||
refreshJobs();
|
||||
});
|
||||
|
||||
@@ -647,7 +769,110 @@ for (const input of form.querySelectorAll('input[type="file"]')) {
|
||||
// anything of the operator's in there worth warning about first
|
||||
form.addEventListener("input", () => { formDirty = true; });
|
||||
|
||||
cancelRebuild.addEventListener("click", exitRebuild);
|
||||
// Always available, and the form is usually holding a selected song by now, so
|
||||
// this clears the whole page down to a blank sheet — fields, review, player and
|
||||
// queue selection — and brings the form back into view from wherever playback
|
||||
// scrolled the panel to. Leaving another song's review under an empty form
|
||||
// reads as though the two belong together.
|
||||
createSong.addEventListener("click", () => {
|
||||
if (formDirty && !confirm("Discard your unsaved edits to this form?")) return;
|
||||
composing = true;
|
||||
selectedId = null;
|
||||
for (const card of jobsDiv.querySelectorAll(".job")) card.removeAttribute("aria-current");
|
||||
exitRebuild();
|
||||
clearDetail("nothing selected");
|
||||
document.getElementById("new-song").scrollIntoView({ block: "start", behavior: "smooth" });
|
||||
form.elements.title.focus();
|
||||
});
|
||||
|
||||
const rebuildAllButton = document.getElementById("rebuild-all");
|
||||
const rebuildAllNote = document.getElementById("rebuild-all-note");
|
||||
const rebuildAllDialog = document.getElementById("rebuild-all-dialog");
|
||||
const rebuildAllForm = document.getElementById("rebuild-all-form");
|
||||
const rebuildAllCount = document.getElementById("rebuild-all-count");
|
||||
const rebuildAllError = document.getElementById("rebuild-all-error");
|
||||
|
||||
const rebuildAllList = document.getElementById("rebuild-all-list");
|
||||
const rebuildAllToggle = document.getElementById("rebuild-all-toggle");
|
||||
const rebuildAllSubmit = rebuildAllForm.querySelector("button[type=submit]");
|
||||
|
||||
function rebuildAllBoxes() {
|
||||
return [...rebuildAllList.querySelectorAll("input:not(:disabled)")];
|
||||
}
|
||||
|
||||
function updateRebuildAllCount() {
|
||||
const boxes = rebuildAllBoxes();
|
||||
const chosen = boxes.filter(b => b.checked).length;
|
||||
rebuildAllToggle.checked = chosen === boxes.length && chosen > 0;
|
||||
rebuildAllToggle.indeterminate = chosen > 0 && chosen < boxes.length;
|
||||
rebuildAllCount.textContent = chosen === 1 ? "1 song will be rebuilt."
|
||||
: `${chosen} songs will be rebuilt.`;
|
||||
// with nothing ticked no ids are sent, which the API reads as every song
|
||||
rebuildAllSubmit.disabled = chosen === 0;
|
||||
}
|
||||
|
||||
function rebuildAllRow(song) {
|
||||
const busy = song.status === "queued" || song.status === "running";
|
||||
const row = document.createElement("label");
|
||||
row.className = "rebuild-all-row";
|
||||
const box = document.createElement("input");
|
||||
box.type = "checkbox";
|
||||
box.name = "ids";
|
||||
box.value = song.id;
|
||||
box.checked = !busy;
|
||||
box.disabled = busy;
|
||||
const name = document.createElement("span");
|
||||
name.textContent = `${song.title} · ${song.artist}`;
|
||||
const meta = document.createElement("span");
|
||||
meta.className = "meta";
|
||||
const when = song.finished_at && new Date(song.finished_at * 1000).toLocaleString(
|
||||
undefined, { dateStyle: "medium", timeStyle: "short" });
|
||||
meta.textContent = busy ? "in progress, skipped"
|
||||
: `${song.status === "failed" ? "failed" : song.rebuilt_from ? "rebuilt" : "built"} ${when || ""}`.trim();
|
||||
row.append(box, name, meta);
|
||||
return row;
|
||||
}
|
||||
|
||||
rebuildAllButton.addEventListener("click", async () => {
|
||||
rebuildAllError.textContent = "";
|
||||
rebuildAllForm.reset();
|
||||
const resp = await fetch("/api/rebuild-all");
|
||||
const songs = resp.ok ? await resp.json() : [];
|
||||
rebuildAllList.replaceChildren(...songs.map(rebuildAllRow));
|
||||
updateRebuildAllCount();
|
||||
rebuildAllDialog.showModal();
|
||||
});
|
||||
|
||||
rebuildAllList.addEventListener("change", updateRebuildAllCount);
|
||||
rebuildAllToggle.addEventListener("change", () => {
|
||||
for (const box of rebuildAllBoxes()) box.checked = rebuildAllToggle.checked;
|
||||
updateRebuildAllCount();
|
||||
});
|
||||
|
||||
document.getElementById("rebuild-all-cancel").addEventListener(
|
||||
"click", () => rebuildAllDialog.close());
|
||||
|
||||
rebuildAllForm.addEventListener("submit", async (e) => {
|
||||
e.preventDefault();
|
||||
const stage = rebuildAllForm.elements.force_from.value;
|
||||
const resp = await fetch("/api/rebuild-all", {
|
||||
method: "POST", body: new FormData(rebuildAllForm) });
|
||||
if (!resp.ok) {
|
||||
const body = await resp.json().catch(() => ({}));
|
||||
rebuildAllError.textContent = body.detail || `error: ${resp.status}`;
|
||||
return;
|
||||
}
|
||||
const { ids, skipped } = await resp.json();
|
||||
rebuildAllDialog.close();
|
||||
let note = `Queued ${ids.length} rebuild${ids.length === 1 ? "" : "s"} from ${stage}.`;
|
||||
if (skipped.length) {
|
||||
const reasons = [...new Set(skipped.map(s => s.reason))].join("; ");
|
||||
note += ` Skipped ${skipped.length}: ${reasons}.`;
|
||||
}
|
||||
rebuildAllNote.textContent = note;
|
||||
rebuildAllNote.hidden = false;
|
||||
refreshJobs();
|
||||
});
|
||||
|
||||
function jobCard(job) {
|
||||
const card = document.createElement("button");
|
||||
@@ -722,6 +947,13 @@ function renderJobs(jobs) {
|
||||
async function loadStages() {
|
||||
const resp = await fetch("/api/stages");
|
||||
STAGES = (await resp.json()).stages;
|
||||
// a rebuild redoes only what the edit invalidated, which cannot see a change
|
||||
// in songtrace itself — this is how you redo a stage anyway
|
||||
const select = form.elements.force_from;
|
||||
select.replaceChildren(new Option("only what changed", ""),
|
||||
...STAGES.map(s => new Option(s, s)));
|
||||
rebuildAllForm.elements.force_from.replaceChildren(
|
||||
new Option("choose a stage", ""), ...STAGES.map(s => new Option(s, s)));
|
||||
}
|
||||
|
||||
function renderStageTrack(job) {
|
||||
@@ -784,11 +1016,11 @@ async function refreshJobs() {
|
||||
// deliberately not via selectJob: the auto-selection on load fills the review
|
||||
// panel but leaves the form blank, so landing on the page is still the way to
|
||||
// start a new song. Clicking a queue entry is what loads its inputs.
|
||||
if (selectedId === null && jobs.length) selectedId = jobs[0].id;
|
||||
if (!composing && selectedId === null && jobs.length) selectedId = jobs[0].id;
|
||||
renderJobs(jobs);
|
||||
renderNowBar(jobs);
|
||||
if (selectedId) refreshDetail();
|
||||
else clearDetail();
|
||||
else clearDetail(composing ? "nothing selected" : undefined);
|
||||
schedulePoll(jobs);
|
||||
}
|
||||
|
||||
@@ -796,6 +1028,7 @@ async function selectJob(id) {
|
||||
// the form already holds this job; reloading it would drop edits in progress
|
||||
const reload = id !== rebuildOf;
|
||||
if (reload && formDirty && !confirm("Discard your unsaved edits to this form?")) return;
|
||||
composing = false;
|
||||
selectedId = id;
|
||||
for (const card of jobsDiv.querySelectorAll(".job")) {
|
||||
if (card.dataset.id === id) card.setAttribute("aria-current", "true");
|
||||
@@ -873,6 +1106,10 @@ function lineTimeline(words, notes, minPitch, span) {
|
||||
}
|
||||
box.append(wordsRow);
|
||||
if (notes.length) box.append(pianoRoll(notes, minPitch, span, t0, t1));
|
||||
const playhead = document.createElement("div");
|
||||
playhead.className = "review-playhead";
|
||||
playhead.hidden = true;
|
||||
box.append(playhead);
|
||||
return box;
|
||||
}
|
||||
|
||||
@@ -906,6 +1143,21 @@ function lineNode(line, minPitch, span) {
|
||||
return row;
|
||||
}
|
||||
|
||||
// What playback needs from a rendered line: the time range it was laid out
|
||||
// over, the word labels along it, and the bar that sweeps across it.
|
||||
function lineEntry(row, line) {
|
||||
const laid = line.words.length ? line.words : line.notes;
|
||||
if (!laid.length || line.dropped) return null;
|
||||
const labels = [...row.querySelectorAll(".review-word")];
|
||||
return {
|
||||
row,
|
||||
t0: laid[0].start,
|
||||
t1: Math.max(...laid.map(x => x.end)),
|
||||
words: line.words.map((w, i) => ({ el: labels[i], start: w.start, end: w.end })),
|
||||
playhead: row.querySelector(".review-playhead"),
|
||||
};
|
||||
}
|
||||
|
||||
function buildLyricsReview(data) {
|
||||
const allNotes = data.lines.flatMap(l => l.notes);
|
||||
const pitches = allNotes.map(n => n.pitch);
|
||||
@@ -914,6 +1166,7 @@ function buildLyricsReview(data) {
|
||||
const span = Math.max(1, maxPitch - minPitch);
|
||||
const spans = [...data.missing_spans].sort((a, b) => a.start - b.start);
|
||||
const nodes = [];
|
||||
const entries = [];
|
||||
let spanIdx = 0;
|
||||
for (const line of data.lines) {
|
||||
const lineStart = line.notes.length ? Math.min(...line.notes.map(n => n.start)) : null;
|
||||
@@ -921,24 +1174,172 @@ function buildLyricsReview(data) {
|
||||
nodes.push(missingSpanNode(spans[spanIdx]));
|
||||
spanIdx++;
|
||||
}
|
||||
nodes.push(lineNode(line, minPitch, span));
|
||||
const row = lineNode(line, minPitch, span);
|
||||
const entry = lineEntry(row, line);
|
||||
if (entry) entries.push(entry);
|
||||
nodes.push(row);
|
||||
}
|
||||
while (spanIdx < spans.length) {
|
||||
nodes.push(missingSpanNode(spans[spanIdx]));
|
||||
spanIdx++;
|
||||
}
|
||||
return nodes;
|
||||
return { nodes, entries };
|
||||
}
|
||||
|
||||
// The song folder's own media, played against the same seconds the review was
|
||||
// built from, so the highlighted line is the one you are hearing.
|
||||
//
|
||||
// Modelled on the game: #MP3 is the audio and the clock, the video is muted
|
||||
// background slaved to it. Playing the video's own audio alongside the mp3 is
|
||||
// the same recording twice, a few frames apart, which is heard as an echo.
|
||||
const player = document.getElementById("player");
|
||||
const media = document.getElementById("player-media");
|
||||
const video = document.getElementById("player-video");
|
||||
const DRIFT = 0.25; // seconds before the picture is worth re-seeking
|
||||
|
||||
function slaveVideo(force) {
|
||||
if (video.hidden || !video.getAttribute("src")) return;
|
||||
if (force || Math.abs(video.currentTime - media.currentTime) > DRIFT) {
|
||||
video.currentTime = media.currentTime;
|
||||
}
|
||||
}
|
||||
|
||||
for (const event of ["seeked", "ratechange"]) {
|
||||
media.addEventListener(event, () => {
|
||||
video.playbackRate = media.playbackRate;
|
||||
slaveVideo(true);
|
||||
});
|
||||
}
|
||||
media.addEventListener("pause", () => video.pause());
|
||||
let timeline = [];
|
||||
let activeEntry = null;
|
||||
let syncFrame = null;
|
||||
|
||||
// the first line that has not finished yet: the one being sung, or the one
|
||||
// coming up while the track is between lines
|
||||
function entryAt(t) {
|
||||
return timeline.find(e => t <= e.t1) ?? timeline[timeline.length - 1] ?? null;
|
||||
}
|
||||
|
||||
function paintEntry(entry, t) {
|
||||
for (const w of entry.words) {
|
||||
w.el.classList.toggle("review-word-sung", t >= w.end);
|
||||
w.el.classList.toggle("review-word-active", t >= w.start && t < w.end);
|
||||
}
|
||||
if (!entry.playhead) return;
|
||||
const inside = t >= entry.t0 && t <= entry.t1;
|
||||
entry.playhead.hidden = !inside;
|
||||
if (inside) {
|
||||
const frac = (t - entry.t0) / Math.max(entry.t1 - entry.t0, 0.001);
|
||||
entry.playhead.style.left = `${frac * 100}%`;
|
||||
}
|
||||
}
|
||||
|
||||
function clearEntry(entry) {
|
||||
entry.row.classList.remove("review-line-active");
|
||||
if (entry.playhead) entry.playhead.hidden = true;
|
||||
for (const w of entry.words) {
|
||||
w.el.classList.remove("review-word-sung", "review-word-active");
|
||||
}
|
||||
}
|
||||
|
||||
function syncPlayback() {
|
||||
const t = media.currentTime;
|
||||
const entry = entryAt(t);
|
||||
if (entry !== activeEntry) {
|
||||
if (activeEntry) clearEntry(activeEntry);
|
||||
activeEntry = entry;
|
||||
if (entry) {
|
||||
entry.row.classList.add("review-line-active");
|
||||
focusActive();
|
||||
}
|
||||
}
|
||||
if (entry) paintEntry(entry, t);
|
||||
}
|
||||
|
||||
// centred so there is context either side, like a scrolling lyric sheet; only
|
||||
// while running, so it does not fight a reader who has scrolled away to look
|
||||
// at something else
|
||||
function focusActive() {
|
||||
if (!activeEntry || media.paused) return;
|
||||
activeEntry.row.scrollIntoView({ block: "center", behavior: "smooth" });
|
||||
}
|
||||
|
||||
function pumpPlayback() {
|
||||
syncPlayback();
|
||||
slaveVideo(false);
|
||||
syncFrame = media.paused ? null : requestAnimationFrame(pumpPlayback);
|
||||
}
|
||||
|
||||
// timeupdate fires about four times a second, too coarse for a playhead
|
||||
media.addEventListener("play", () => {
|
||||
if (syncFrame === null) syncFrame = requestAnimationFrame(pumpPlayback);
|
||||
if (!video.hidden && video.getAttribute("src")) video.play().catch(() => {});
|
||||
focusActive(); // the line may already be active from a seek while paused
|
||||
});
|
||||
for (const event of ["pause", "seeked", "loadedmetadata"]) {
|
||||
media.addEventListener(event, syncPlayback);
|
||||
}
|
||||
|
||||
function bindTimeline(entries) {
|
||||
if (activeEntry) clearEntry(activeEntry);
|
||||
activeEntry = null;
|
||||
timeline = entries;
|
||||
for (const entry of entries) {
|
||||
entry.row.classList.add("review-seek");
|
||||
entry.row.title = "click to play from here";
|
||||
entry.row.addEventListener("click", (e) => {
|
||||
const word = entry.words.find(w => w.el === e.target);
|
||||
media.currentTime = word ? word.start : entry.t0;
|
||||
if (media.paused) syncPlayback();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Returns true when the folder gave us something to play. #MP3 is required —
|
||||
// it is the audio the game uses, so it is the one the review has to track.
|
||||
function setPlayerSource(job, available) {
|
||||
const src = available?.audio ? `/api/jobs/${job.id}/media/audio` : "";
|
||||
player.hidden = !src;
|
||||
if (media.dataset.src === src) return !!src;
|
||||
media.dataset.src = src;
|
||||
if (src) media.src = src;
|
||||
else { media.removeAttribute("src"); media.load(); }
|
||||
|
||||
video.hidden = !(src && available.video);
|
||||
video.poster = available?.cover ? `/api/jobs/${job.id}/media/cover` : "";
|
||||
if (!video.hidden) video.src = `/api/jobs/${job.id}/media/video`;
|
||||
else { video.removeAttribute("src"); video.load(); }
|
||||
return !!src;
|
||||
}
|
||||
|
||||
function resetPlayer() {
|
||||
// the 3s poll reaches here for every running job; reloading a cleared
|
||||
// element each time would be pure churn
|
||||
if (player.hidden && !timeline.length) return;
|
||||
bindTimeline([]);
|
||||
player.hidden = true;
|
||||
media.pause();
|
||||
video.pause();
|
||||
media.dataset.src = "";
|
||||
for (const el of [media, video]) {
|
||||
el.removeAttribute("src");
|
||||
el.removeAttribute("poster");
|
||||
el.load();
|
||||
}
|
||||
}
|
||||
|
||||
async function renderLyricsReview(job) {
|
||||
const box = document.getElementById("lyrics-review");
|
||||
if (job.status === "failed") {
|
||||
delete box.dataset.jobId;
|
||||
resetPlayer();
|
||||
box.replaceChildren(emptyNote("job failed — nothing to review"));
|
||||
return;
|
||||
}
|
||||
if (job.status !== "done") {
|
||||
delete box.dataset.jobId;
|
||||
resetPlayer();
|
||||
box.replaceChildren(emptyNote("waiting for this song to finish…"));
|
||||
return;
|
||||
}
|
||||
@@ -946,6 +1347,7 @@ async function renderLyricsReview(job) {
|
||||
const resp = await fetch(`/api/jobs/${job.id}/review`);
|
||||
if (job.id !== selectedId) return;
|
||||
if (!resp.ok) {
|
||||
resetPlayer();
|
||||
box.replaceChildren(emptyNote("no review data available"));
|
||||
return;
|
||||
}
|
||||
@@ -953,10 +1355,17 @@ async function renderLyricsReview(job) {
|
||||
if (job.id !== selectedId) return;
|
||||
box.dataset.jobId = job.id;
|
||||
if (!data.lines.length) {
|
||||
resetPlayer();
|
||||
box.replaceChildren(emptyNote("no lyrics to review"));
|
||||
return;
|
||||
}
|
||||
box.replaceChildren(...buildLyricsReview(data));
|
||||
const { nodes, entries } = buildLyricsReview(data);
|
||||
box.replaceChildren(...nodes);
|
||||
// with nothing to play, a highlighted line and a click-to-seek cursor would
|
||||
// both be promising something the song folder cannot deliver
|
||||
const playable = setPlayerSource(job, data.media);
|
||||
bindTimeline(playable ? entries : []);
|
||||
if (playable) syncPlayback();
|
||||
}
|
||||
|
||||
async function copyPath(path, button) {
|
||||
@@ -1016,19 +1425,23 @@ function renderOutputPath(job) {
|
||||
outputPathDiv.replaceChildren(wrap);
|
||||
}
|
||||
|
||||
function clearDetail() {
|
||||
function clearDetail(why = "no songs queued yet") {
|
||||
reviewTitle.replaceChildren();
|
||||
reviewError.hidden = true;
|
||||
reviewError.textContent = "";
|
||||
document.getElementById("warnings").replaceChildren(
|
||||
emptyNote("no songs queued yet — nothing to review"));
|
||||
resetPlayer();
|
||||
// the review box, not an element that no longer exists: getting this wrong
|
||||
// threw partway through and left the rest of the panel showing the old song
|
||||
const box = document.getElementById("lyrics-review");
|
||||
delete box.dataset.jobId;
|
||||
box.replaceChildren(emptyNote(`${why} — nothing to review`));
|
||||
outputPathDiv.replaceChildren();
|
||||
reviewActions.replaceChildren();
|
||||
delete reviewActions.dataset.jobId;
|
||||
reviewLineage.replaceChildren();
|
||||
reviewLineage.hidden = true;
|
||||
delete reviewLineage.dataset.jobId;
|
||||
detail.textContent = "no songs queued yet — nothing to log";
|
||||
detail.textContent = `${why} — nothing to log`;
|
||||
}
|
||||
|
||||
// a rebuild is refused with 409 until the job settles, so the form says so
|
||||
@@ -1043,8 +1456,10 @@ function setSubmitLock(status) {
|
||||
function setFormMode(inp, status) {
|
||||
formHeading.textContent = rebuildOf ? "REBUILD" : "NEW SONG";
|
||||
submitButton.textContent = rebuildOf ? "REBUILD SONG" : "CREATE SONG";
|
||||
cancelRebuild.hidden = !rebuildOf;
|
||||
setSubmitLock(status);
|
||||
// only a rebuild has stages to restart from; a new song always runs them all
|
||||
document.getElementById("restart-field").hidden = !rebuildOf;
|
||||
if (!rebuildOf) form.elements.force_from.value = "";
|
||||
for (const [field, key] of [["video", "video_filename"], ["cover", "cover_filename"]]) {
|
||||
const note = form.querySelector(`[data-keep="${field}"]`);
|
||||
const kept = rebuildOf && inp ? inp[key] : null;
|
||||
@@ -1052,9 +1467,9 @@ function setFormMode(inp, status) {
|
||||
note.hidden = !kept;
|
||||
}
|
||||
// the advanced fields carry into a rebuild whether or not the section is open,
|
||||
// so a job that has any must not hide them behind a closed summary
|
||||
form.querySelector("details").open = !!inp && (!!inp.cover_filename ||
|
||||
["genre", "year", "language", "bpm"].some(n => form.elements[n].value));
|
||||
// so anything actually set must not sit hidden behind a closed summary
|
||||
form.querySelector("details").open = !!inp?.cover_filename ||
|
||||
["genre", "year", "language", "bpm"].some(n => form.elements[n].value);
|
||||
}
|
||||
|
||||
function exitRebuild() {
|
||||
|
||||
+97
-9
@@ -19,9 +19,12 @@ from .config import Config
|
||||
from .lyrics import normalize_lyrics
|
||||
from .pipeline import (STAGE_ORDER, PipelineError, SongInput, read_warnings,
|
||||
run_pipeline, song_slug)
|
||||
from .ultrastar import get_header, parse_ultrastar
|
||||
|
||||
STATIC = Path(__file__).parent / "static"
|
||||
STAGE_LINE = re.compile(r"^\[(\w+)\] (?:running|cached, skipping)")
|
||||
# the song folder names its own media, exactly as the game reads it
|
||||
MEDIA_HEADERS = {"audio": "MP3", "video": "VIDEO", "cover": "COVER"}
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -137,18 +140,18 @@ def create_app(cfg: Config, runner=run_pipeline) -> FastAPI:
|
||||
|
||||
def _prepare_rebuild(job: Job, log) -> str | None:
|
||||
"""Relocate cached work and report the stage this rebuild restarts from."""
|
||||
if job.force_stage:
|
||||
log(f"[rebuild] restarting from {job.force_stage} (forced rebuild from scratch)")
|
||||
return job.force_stage
|
||||
if not job.rebuilt_from:
|
||||
return None
|
||||
return job.force_stage
|
||||
with lock:
|
||||
parent = jobs.get(job.rebuilt_from)
|
||||
if parent is None:
|
||||
return None
|
||||
force_from = rebuild_policy.plan(parent.inp, job.inp)
|
||||
return job.force_stage
|
||||
planned = rebuild_policy.plan(parent.inp, job.inp)
|
||||
changed = rebuild_policy.changed_fields(parent.inp, job.inp)
|
||||
rebuild_policy.relocate_work(parent.inp, job.inp, cfg)
|
||||
# a stage the operator asked for can only widen what plan() decided,
|
||||
# which is how "redo the packaging" reaches work nothing invalidated
|
||||
force_from = rebuild_policy.earliest(planned, job.force_stage)
|
||||
log(f"[rebuild] restarting from {force_from or 'the first incomplete stage'}"
|
||||
f" ({', '.join(changed) if changed else 'nothing'} changed)")
|
||||
return force_from
|
||||
@@ -245,6 +248,7 @@ def create_app(cfg: Config, runner=run_pipeline) -> FastAPI:
|
||||
cover: UploadFile | None = File(None)):
|
||||
youtube_url, genre, language = (youtube_url or None, genre or None,
|
||||
language or None)
|
||||
lyrics = lyrics.replace("\r\n", "\n")
|
||||
has_video = bool(video and video.filename)
|
||||
if bool(youtube_url) == has_video:
|
||||
raise HTTPException(400, "provide exactly one of youtube_url / video")
|
||||
@@ -290,13 +294,20 @@ def create_app(cfg: Config, runner=run_pipeline) -> FastAPI:
|
||||
lyrics: str = Form(...), youtube_url: str | None = Form(None),
|
||||
genre: str | None = Form(None), year: int | None = Form(None),
|
||||
language: str | None = Form(None), bpm: float | None = Form(None),
|
||||
force_from: str | None = Form(None),
|
||||
video: UploadFile | None = File(None),
|
||||
cover: UploadFile | None = File(None)):
|
||||
parent = _finished_job(job_id)
|
||||
force_from = force_from or None
|
||||
if force_from is not None and force_from not in STAGE_ORDER:
|
||||
raise HTTPException(400, f"unknown stage {force_from!r}")
|
||||
# a cleared field arrives as "" from some clients; "" and None must not
|
||||
# look like different inputs to plan()
|
||||
youtube_url, genre, language = (youtube_url or None, genre or None,
|
||||
language or None)
|
||||
# a browser submits a textarea with CRLF; without this every rebuild
|
||||
# from the page looks like a lyrics edit and redoes the alignment
|
||||
lyrics = lyrics.replace("\r\n", "\n")
|
||||
has_video = bool(video and video.filename)
|
||||
if youtube_url and has_video:
|
||||
raise HTTPException(400, "provide exactly one of youtube_url / video")
|
||||
@@ -314,8 +325,54 @@ def create_app(cfg: Config, runner=run_pipeline) -> FastAPI:
|
||||
youtube_url=youtube_url, video_path=video_path,
|
||||
genre=genre, year=year, language=language,
|
||||
cover_path=cover_path, bpm_override=bpm)
|
||||
_require_source(parent.inp, inp, rebuild_policy.plan(parent.inp, inp))
|
||||
return _enqueue(new_id, inp, rebuilt_from=job_id)
|
||||
_require_source(parent.inp, inp, rebuild_policy.earliest(
|
||||
rebuild_policy.plan(parent.inp, inp), force_from))
|
||||
return _enqueue(new_id, inp, rebuilt_from=job_id, force_stage=force_from)
|
||||
|
||||
def _current_songs() -> list[Job]:
|
||||
"""The job that stands for each song now, newest first."""
|
||||
with lock:
|
||||
# a job that was rebuilt is history: its rebuild is the song now,
|
||||
# possibly under another slug
|
||||
rebuilt = {j.rebuilt_from for j in jobs.values()}
|
||||
latest = {song_slug(j.inp): j for j in (jobs[i] for i in order)
|
||||
if j.id not in rebuilt}
|
||||
position = {job_id: n for n, job_id in enumerate(order)}
|
||||
return sorted(latest.values(), key=lambda j: position[j.id], reverse=True)
|
||||
|
||||
@app.get("/api/rebuild-all")
|
||||
def rebuild_all_candidates():
|
||||
return [{"id": j.id, "title": j.title, "artist": j.artist,
|
||||
"status": j.status, "rebuilt_from": j.rebuilt_from,
|
||||
"finished_at": j.finished_at}
|
||||
for j in _current_songs()]
|
||||
|
||||
@app.post("/api/rebuild-all", status_code=201)
|
||||
def rebuild_all(force_from: str = Form(...), ids: list[str] | None = Form(None)):
|
||||
"""Rebuild every song, or the ones in `ids`, from `force_from` on, for
|
||||
when songtrace itself improved that stage. Each is an ordinary rebuild
|
||||
with unchanged inputs."""
|
||||
if force_from not in STAGE_ORDER:
|
||||
raise HTTPException(400, f"unknown stage {force_from!r}")
|
||||
songs = _current_songs()
|
||||
queued, skipped = [], []
|
||||
if ids is not None:
|
||||
current = {j.id for j in songs}
|
||||
skipped = [{"id": i, "reason": "no longer the current job for its song"}
|
||||
for i in ids if i not in current]
|
||||
songs = [j for j in songs if j.id in set(ids)]
|
||||
for job in songs:
|
||||
if job.status in ("queued", "running"):
|
||||
skipped.append({"id": job.id, "reason": "still in progress"})
|
||||
continue
|
||||
try:
|
||||
_require_source(job.inp, job.inp, force_from)
|
||||
except HTTPException as e:
|
||||
skipped.append({"id": job.id, "reason": e.detail})
|
||||
continue
|
||||
queued.append(_enqueue(uuid.uuid4().hex, job.inp, rebuilt_from=job.id,
|
||||
force_stage=force_from)["id"])
|
||||
return {"ids": queued, "skipped": skipped}
|
||||
|
||||
@app.get("/api/stages")
|
||||
def list_stages():
|
||||
@@ -376,6 +433,35 @@ def create_app(cfg: Config, runner=run_pipeline) -> FastAPI:
|
||||
shutil.rmtree(cfg.work_dir / slug, ignore_errors=True)
|
||||
shutil.rmtree(cfg.output_dir / slug, ignore_errors=True)
|
||||
|
||||
def _resolve_media(job: Job, header: str) -> Path | None:
|
||||
"""The file a song folder's #MP3/#VIDEO/#COVER header points at."""
|
||||
slug = song_slug(job.inp)
|
||||
out = (cfg.output_dir / slug).resolve()
|
||||
txt = out / f"{slug}.txt"
|
||||
if not txt.exists():
|
||||
return None
|
||||
name = get_header(parse_ultrastar(txt.read_text(encoding="utf-8")), header)
|
||||
if not name:
|
||||
return None
|
||||
# a header names a file in the song folder and nothing else
|
||||
path = (out / name).resolve()
|
||||
if path.parent != out or not path.is_file():
|
||||
return None
|
||||
return path
|
||||
|
||||
@app.get("/api/jobs/{job_id}/media/{kind}")
|
||||
def get_media(job_id: str, kind: str):
|
||||
header = MEDIA_HEADERS.get(kind)
|
||||
if header is None:
|
||||
raise HTTPException(404)
|
||||
job = _finished_job(job_id)
|
||||
if job.status != "done":
|
||||
raise HTTPException(404)
|
||||
path = _resolve_media(job, header)
|
||||
if path is None:
|
||||
raise HTTPException(404)
|
||||
return FileResponse(path)
|
||||
|
||||
@app.get("/api/jobs/{job_id}/review")
|
||||
def get_review(job_id: str):
|
||||
job = _finished_job(job_id)
|
||||
@@ -406,7 +492,9 @@ def create_app(cfg: Config, runner=run_pipeline) -> FastAPI:
|
||||
lines = [{"index": i, "text": " ".join(words), "dropped": i in dropped,
|
||||
"notes": by_line.get(i, []), "words": words_by_line.get(i, [])}
|
||||
for i, words in enumerate(ref.lines)]
|
||||
return {"lines": lines, "missing_spans": missing_spans}
|
||||
media = {kind: _resolve_media(job, header) is not None
|
||||
for kind, header in MEDIA_HEADERS.items()}
|
||||
return {"lines": lines, "missing_spans": missing_spans, "media": media}
|
||||
|
||||
return app
|
||||
|
||||
|
||||
@@ -0,0 +1,185 @@
|
||||
"""The one place songtrace touches yt-dlp.
|
||||
|
||||
YouTube breaks old yt-dlp releases faster than songtrace is released, so
|
||||
besides the copy installed with songtrace (the bundled one), newer stable
|
||||
releases are fetched from PyPI into a cache dir at runtime and tried first,
|
||||
with the bundled copy as the fallback. yt-dlp runs in a subprocess and is
|
||||
driven through its command line, its most stable interface, so a cached
|
||||
release never mixes with anything songtrace has imported.
|
||||
"""
|
||||
import importlib.metadata
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import threading
|
||||
import time
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
|
||||
import httpx
|
||||
|
||||
from .config import Config
|
||||
from .pipeline import PipelineError
|
||||
|
||||
# The extras are what make YouTube work; see pyproject.toml.
|
||||
REQUIREMENT = "yt-dlp[default,curl-cffi,deno]"
|
||||
CHECK_INTERVAL = 24 * 3600
|
||||
_lock = threading.Lock()
|
||||
|
||||
|
||||
class YtDlpError(PipelineError):
|
||||
pass
|
||||
|
||||
|
||||
def _key(version: str) -> tuple[int, ...]:
|
||||
return tuple(int(p) for p in version.split("."))
|
||||
|
||||
|
||||
def cache_dir() -> Path:
|
||||
base = os.environ.get("XDG_CACHE_HOME") or Path.home() / ".cache"
|
||||
# Installs hold compiled wheels, so they are only good for one Python.
|
||||
return Path(base) / "songtrace" / f"yt-dlp-py{sys.version_info[0]}{sys.version_info[1]}"
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class YtDlp:
|
||||
version: str
|
||||
path: Path | None = None # None for the bundled copy
|
||||
|
||||
def _env(self) -> dict[str, str]:
|
||||
env = dict(os.environ)
|
||||
if self.path:
|
||||
env["PYTHONPATH"] = os.pathsep.join(
|
||||
filter(None, [str(self.path), env.get("PYTHONPATH")]))
|
||||
env["PATH"] = os.pathsep.join(
|
||||
[str(self.path / "bin"), env.get("PATH", "")])
|
||||
return env
|
||||
|
||||
def run(self, args: list[str], log) -> None:
|
||||
"""Runs yt-dlp with `args`, passing each line it prints to `log`."""
|
||||
proc = subprocess.Popen(
|
||||
[sys.executable, "-P", "-m", "yt_dlp", "--ignore-config", *args],
|
||||
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True,
|
||||
errors="replace", env=self._env())
|
||||
last = error = ""
|
||||
for line in proc.stdout:
|
||||
last = line.rstrip()
|
||||
log(last)
|
||||
if last.startswith("ERROR:"):
|
||||
error = last
|
||||
if proc.wait() == 0:
|
||||
return
|
||||
if proc.returncode == 2:
|
||||
raise YtDlpError(f"yt-dlp {self.version} rejected songtrace's "
|
||||
f"options, so it may be incompatible: {last}")
|
||||
raise YtDlpError(error or f"yt-dlp exited with status {proc.returncode}")
|
||||
|
||||
|
||||
def bundled() -> YtDlp:
|
||||
return YtDlp(importlib.metadata.version("yt-dlp"))
|
||||
|
||||
|
||||
def _latest_release() -> str:
|
||||
r = httpx.get("https://pypi.org/pypi/yt-dlp/json", timeout=15,
|
||||
follow_redirects=True)
|
||||
r.raise_for_status()
|
||||
return r.json()["info"]["version"]
|
||||
|
||||
|
||||
def _pip_install(target: Path, version: str) -> None:
|
||||
proc = subprocess.run(
|
||||
[sys.executable, "-m", "pip", "install", "--quiet", "--no-input",
|
||||
"--disable-pip-version-check", "--target", str(target),
|
||||
f"{REQUIREMENT}=={version}"],
|
||||
capture_output=True, text=True)
|
||||
if proc.returncode:
|
||||
raise YtDlpError(f"pip could not install yt-dlp {version}: "
|
||||
f"{proc.stderr.strip()[-500:]}")
|
||||
|
||||
|
||||
def _install(version: str, log) -> YtDlp:
|
||||
"""Installs `version` into the cache unless it is already there.
|
||||
|
||||
The install is checked to run before it is moved into place, so a
|
||||
directory in the cache is always a usable yt-dlp."""
|
||||
dest = cache_dir() / version
|
||||
if dest.is_dir():
|
||||
return YtDlp(version, dest)
|
||||
dest.parent.mkdir(parents=True, exist_ok=True)
|
||||
log(f"installing yt-dlp {version}")
|
||||
tmp = Path(tempfile.mkdtemp(dir=dest.parent, prefix=".tmp-"))
|
||||
try:
|
||||
_pip_install(tmp, version)
|
||||
proc = subprocess.run(
|
||||
[sys.executable, "-P", "-m", "yt_dlp", "--version"],
|
||||
capture_output=True, text=True, env=YtDlp(version, tmp)._env())
|
||||
got = proc.stdout.strip()
|
||||
if proc.returncode or _key(got) != _key(version):
|
||||
raise YtDlpError(f"yt-dlp {version} failed its check after install: "
|
||||
f"{(got or proc.stderr.strip())[-500:]}")
|
||||
try:
|
||||
tmp.rename(dest)
|
||||
except OSError:
|
||||
if not dest.is_dir():
|
||||
raise
|
||||
finally:
|
||||
shutil.rmtree(tmp, ignore_errors=True)
|
||||
return YtDlp(version, dest)
|
||||
|
||||
|
||||
def _cached() -> list[YtDlp]:
|
||||
root = cache_dir()
|
||||
if not root.is_dir():
|
||||
return []
|
||||
found = [YtDlp(p.name, p) for p in root.iterdir()
|
||||
if p.is_dir() and re.fullmatch(r"\d+(\.\d+)*", p.name)]
|
||||
return sorted(found, key=lambda y: _key(y.version))
|
||||
|
||||
|
||||
def _newest(base: YtDlp, refresh: bool, log) -> YtDlp | None:
|
||||
"""The newest cached release newer than `base`, checking PyPI for a newer
|
||||
one first if that has not been done for a day or `refresh` is set."""
|
||||
marker = cache_dir() / ".checked"
|
||||
if (refresh or not marker.exists()
|
||||
or time.time() - marker.stat().st_mtime > CHECK_INTERVAL):
|
||||
# Recorded even on failure, so a broken release is not reinstalled
|
||||
# for every job; a failed download refreshes regardless.
|
||||
marker.parent.mkdir(parents=True, exist_ok=True)
|
||||
marker.touch()
|
||||
try:
|
||||
latest = _latest_release()
|
||||
if _key(latest) > _key(base.version):
|
||||
_install(latest, log)
|
||||
except Exception as e:
|
||||
log(f"could not update yt-dlp: {e}")
|
||||
newer = [y for y in _cached() if _key(y.version) > _key(base.version)]
|
||||
for old in newer[:-1]:
|
||||
shutil.rmtree(old.path, ignore_errors=True)
|
||||
return newer[-1] if newer else None
|
||||
|
||||
|
||||
def candidates(cfg: Config, log, refresh: bool = False) -> list[YtDlp]:
|
||||
"""The yt-dlp copies to try, in order: the newest release (or the one
|
||||
`cfg.ytdlp_version` pins), then the bundled copy."""
|
||||
base = bundled()
|
||||
want = cfg.ytdlp_version
|
||||
if want == "bundled":
|
||||
return [base]
|
||||
with _lock:
|
||||
if want == "auto":
|
||||
first = _newest(base, refresh, log)
|
||||
elif not re.fullmatch(r"\d+(\.\d+)*", want):
|
||||
raise YtDlpError(f"ytdlp_version must be \"auto\", \"bundled\" or a "
|
||||
f"release such as \"2026.08.19\", not {want!r}")
|
||||
elif _key(want) == _key(base.version):
|
||||
first = None
|
||||
else:
|
||||
try:
|
||||
first = _install(want, log)
|
||||
except Exception as e:
|
||||
log(f"could not install the pinned yt-dlp {want}: {e}")
|
||||
first = None
|
||||
return [first, base] if first else [base]
|
||||
Vendored
+36
@@ -0,0 +1,36 @@
|
||||
#TITLE:Placeholder Chart
|
||||
#ARTIST:Test Artist
|
||||
#LANGUAGE:English
|
||||
#YEAR:2000
|
||||
#CREATOR:songtrace tests
|
||||
#GENRE:Test
|
||||
#COVER:Test Artist - Placeholder Chart [CO].jpg
|
||||
#MP3:Test Artist - Placeholder Chart.mp3
|
||||
#VIDEO:Test Artist - Placeholder Chart.mp4
|
||||
#BPM:217,98
|
||||
#GAP:4880
|
||||
#MEDLEYSTARTBEAT:113
|
||||
#MEDLEYENDBEAT:240
|
||||
: 0 3 16 ah
|
||||
: 5 3 20 ah
|
||||
: 11 3 23 la
|
||||
: 17 3 16 la
|
||||
: 22 28 15 ah
|
||||
: 51 2 16 doo
|
||||
: 54 31 16 ~
|
||||
- 95
|
||||
F 113 3 18 la
|
||||
F 117 3 21 la
|
||||
F 121 3 21 la
|
||||
* 125 3 21 doo
|
||||
* 129 12 19 ~
|
||||
- 150
|
||||
: 155 4 12 Na
|
||||
R 160 4 0 na
|
||||
G 165 4 0 na
|
||||
: 170 6 14 la
|
||||
- 190
|
||||
: 194 3 16 La
|
||||
: 198 3 18 la
|
||||
* 202 10 20 ah
|
||||
E
|
||||
Vendored
+21
@@ -0,0 +1,21 @@
|
||||
#TITLE:Café Placeholder
|
||||
#ARTIST:Tést Artist
|
||||
#LANGUAGE:English
|
||||
#GENRE:Test
|
||||
#YEAR:2000
|
||||
#MP3:Tést Artist - Café Placeholder.mp3
|
||||
#CREATOR:songtrace tests
|
||||
#BPM:256
|
||||
#GAP:49673
|
||||
: 0 4 2 Doo
|
||||
: 6 4 4 doo
|
||||
: 12 8 5 laé
|
||||
- 28 30
|
||||
: 32 4 0 Na
|
||||
: 38 4 -2 na
|
||||
* 44 12 -9 ah
|
||||
- 64
|
||||
F 70 4 3 La
|
||||
: 76 6 -5 la
|
||||
: 84 10 -9 ~
|
||||
E
|
||||
+79
-54
@@ -91,87 +91,112 @@ def test_classify_ignores_partials_left_by_a_failed_attempt(tmp_path):
|
||||
video, _ = acquire._classify_downloads(tmp_path)
|
||||
assert video.name == "source_video.webm"
|
||||
|
||||
def test_ydl_opts_default_client_adds_no_player_client(tmp_path):
|
||||
def test_ydl_args_default_client_adds_no_player_client(tmp_path):
|
||||
cfg = Config(whisper_url="http://x/v1")
|
||||
opts = acquire._ydl_opts(cfg, tmp_path, "default", print)
|
||||
assert "youtube" not in opts["extractor_args"]
|
||||
assert opts["retries"] and opts["fragment_retries"] and opts["extractor_retries"]
|
||||
args = acquire._ydl_args(cfg, tmp_path, "default", "https://yt/x")
|
||||
assert "--extractor-args" not in args
|
||||
assert "--retries" in args and "--fragment-retries" in args
|
||||
assert args[-2:] == ["--", "https://yt/x"]
|
||||
|
||||
def test_ydl_opts_named_client_and_pot_provider(tmp_path):
|
||||
def test_ydl_args_named_client_pot_provider_and_cookies(tmp_path):
|
||||
cfg = Config(whisper_url="http://x/v1",
|
||||
ytdlp_pot_provider_url="http://pot:4416",
|
||||
ytdlp_cookies=tmp_path / "cookies.txt")
|
||||
opts = acquire._ydl_opts(cfg, tmp_path, "tv", print)
|
||||
assert opts["extractor_args"]["youtube"] == {"player_client": ["tv"]}
|
||||
assert opts["extractor_args"]["youtubepot-bgutilhttp"] == {
|
||||
"base_url": ["http://pot:4416"]}
|
||||
assert opts["cookiefile"] == str(tmp_path / "cookies.txt")
|
||||
ytdlp_cookies=tmp_path / "cookies.txt",
|
||||
ytdlp_impersonate="chrome")
|
||||
args = acquire._ydl_args(cfg, tmp_path, "tv", "https://yt/x")
|
||||
joined = " ".join(args)
|
||||
assert "--extractor-args youtube:player_client=tv" in joined
|
||||
assert ("--extractor-args youtubepot-bgutilhttp:base_url=http://pot:4416"
|
||||
in joined)
|
||||
assert f"--cookies {tmp_path / 'cookies.txt'}" in joined
|
||||
assert "--impersonate chrome" in joined
|
||||
|
||||
def _fake_yt_dlp(monkeypatch, behaviour):
|
||||
"""Install a stub yt_dlp whose download calls `behaviour(opts)`."""
|
||||
import sys
|
||||
import types
|
||||
def test_ydl_args_parse_with_real_yt_dlp(tmp_path):
|
||||
"""Every option songtrace passes must exist in the bundled yt-dlp."""
|
||||
import pytest
|
||||
pytest.importorskip("yt_dlp")
|
||||
from yt_dlp import parse_options
|
||||
cfg = Config(whisper_url="http://x/v1", ytdlp_pot_provider_url="http://p:1",
|
||||
ytdlp_cookies=tmp_path / "c.txt")
|
||||
parse_options(acquire._ydl_args(cfg, tmp_path, "tv", "https://yt/x"))
|
||||
|
||||
class FakeYtDlp:
|
||||
def __init__(self, version, behaviour, seen):
|
||||
self.version, self._behaviour, self._seen = version, behaviour, seen
|
||||
def run(self, args, log):
|
||||
client = next((a for a in args if a.startswith("youtube:")), None)
|
||||
self._seen.append((self.version, client))
|
||||
self._behaviour(self.version, client)
|
||||
|
||||
def _fake_candidates(monkeypatch, behaviour, versions=("2026.09.01", "2026.08.19"),
|
||||
refreshed=None):
|
||||
"""Stub ytdlp.candidates; `refreshed` is what a refresh turns up."""
|
||||
seen = []
|
||||
|
||||
class FakeYoutubeDL:
|
||||
def __init__(self, opts):
|
||||
self.opts = opts
|
||||
def __enter__(self):
|
||||
return self
|
||||
def __exit__(self, *exc):
|
||||
return False
|
||||
def extract_info(self, url, download):
|
||||
seen.append(self.opts["extractor_args"].get("youtube"))
|
||||
behaviour(self.opts)
|
||||
|
||||
mod = types.ModuleType("yt_dlp")
|
||||
mod.YoutubeDL = FakeYoutubeDL
|
||||
mod.version = types.SimpleNamespace(__version__="2026.08.19")
|
||||
monkeypatch.setitem(sys.modules, "yt_dlp", mod)
|
||||
def candidates(cfg, log, refresh=False):
|
||||
vs = refreshed if refresh and refreshed else versions
|
||||
return [FakeYtDlp(v, behaviour, seen) for v in vs]
|
||||
monkeypatch.setattr(acquire.ytdlp, "candidates", candidates)
|
||||
return seen
|
||||
|
||||
def test_download_falls_through_to_the_next_client(tmp_path, monkeypatch):
|
||||
def behaviour(opts):
|
||||
client = opts["extractor_args"].get("youtube")
|
||||
from songtrace.ytdlp import YtDlpError
|
||||
def behaviour(version, client):
|
||||
if client is None:
|
||||
raise RuntimeError("HTTP Error 403: Forbidden")
|
||||
raise YtDlpError("ERROR: HTTP Error 403: Forbidden")
|
||||
(tmp_path / "source_video.webm").write_bytes(b"v")
|
||||
seen = _fake_yt_dlp(monkeypatch, behaviour)
|
||||
seen = _fake_candidates(monkeypatch, behaviour)
|
||||
cfg = Config(whisper_url="http://x/v1", ytdlp_clients=["default", "tv"])
|
||||
logged = []
|
||||
video, thumb = acquire.download_youtube("https://yt/x", tmp_path, cfg,
|
||||
logged.append)
|
||||
assert video.name == "source_video.webm"
|
||||
assert thumb is None
|
||||
assert seen == [None, {"player_client": ["tv"]}]
|
||||
assert seen == [("2026.09.01", None),
|
||||
("2026.09.01", "youtube:player_client=tv")]
|
||||
assert any("403" in m for m in logged)
|
||||
|
||||
def test_download_reports_version_and_clients_when_all_fail(tmp_path, monkeypatch):
|
||||
def test_download_falls_back_to_the_bundled_copy(tmp_path, monkeypatch):
|
||||
from songtrace.ytdlp import YtDlpError
|
||||
def behaviour(version, client):
|
||||
if version == "2026.09.01":
|
||||
raise YtDlpError("rejected songtrace's options")
|
||||
(tmp_path / "source_video.webm").write_bytes(b"v")
|
||||
seen = _fake_candidates(monkeypatch, behaviour)
|
||||
cfg = Config(whisper_url="http://x/v1", ytdlp_clients=["default"])
|
||||
acquire.download_youtube("https://yt/x", tmp_path, cfg, lambda m: None)
|
||||
assert [v for v, _ in seen] == ["2026.09.01", "2026.08.19"]
|
||||
|
||||
def test_download_retries_with_a_release_found_after_failing(tmp_path, monkeypatch):
|
||||
from songtrace.ytdlp import YtDlpError
|
||||
def behaviour(version, client):
|
||||
if version != "2026.10.01":
|
||||
raise YtDlpError("ERROR: HTTP Error 403: Forbidden")
|
||||
(tmp_path / "source_video.webm").write_bytes(b"v")
|
||||
seen = _fake_candidates(monkeypatch, behaviour,
|
||||
refreshed=("2026.10.01", "2026.08.19"))
|
||||
cfg = Config(whisper_url="http://x/v1", ytdlp_clients=["default"])
|
||||
acquire.download_youtube("https://yt/x", tmp_path, cfg, lambda m: None)
|
||||
assert [v for v, _ in seen] == ["2026.09.01", "2026.08.19", "2026.10.01"]
|
||||
|
||||
def test_download_reports_versions_and_clients_when_all_fail(tmp_path, monkeypatch):
|
||||
import pytest
|
||||
from songtrace.pipeline import PipelineError
|
||||
def behaviour(opts):
|
||||
raise RuntimeError("HTTP Error 403: Forbidden")
|
||||
_fake_yt_dlp(monkeypatch, behaviour)
|
||||
from songtrace.ytdlp import YtDlpError
|
||||
def behaviour(version, client):
|
||||
raise YtDlpError("ERROR: HTTP Error 403: Forbidden")
|
||||
seen = _fake_candidates(monkeypatch, behaviour)
|
||||
cfg = Config(whisper_url="http://x/v1", ytdlp_clients=["default", "tv"])
|
||||
with pytest.raises(PipelineError) as e:
|
||||
acquire.download_youtube("https://yt/x", tmp_path, cfg, lambda m: None)
|
||||
assert "2026.08.19" in str(e.value)
|
||||
assert "2026.09.01, 2026.08.19" in str(e.value)
|
||||
assert "default, tv" in str(e.value)
|
||||
assert "403" in str(e.value)
|
||||
assert len(seen) == 4
|
||||
|
||||
def test_download_with_no_configured_clients_still_tries_default(tmp_path, monkeypatch):
|
||||
seen = _fake_yt_dlp(
|
||||
monkeypatch, lambda opts: (tmp_path / "source_video.mp4").write_bytes(b"v"))
|
||||
seen = _fake_candidates(
|
||||
monkeypatch,
|
||||
lambda v, c: (tmp_path / "source_video.mp4").write_bytes(b"v"))
|
||||
cfg = Config(whisper_url="http://x/v1", ytdlp_clients=[])
|
||||
acquire.download_youtube("https://yt/x", tmp_path, cfg, lambda m: None)
|
||||
assert seen == [None]
|
||||
|
||||
def test_ydl_log_drops_debug_lines():
|
||||
logged = []
|
||||
log = acquire._YdlLog(logged.append)
|
||||
log.debug("[debug] internal detail")
|
||||
log.debug("[youtube] Extracting URL")
|
||||
log.warning("nsig extraction failed")
|
||||
log.error("HTTP Error 403: Forbidden")
|
||||
assert logged == ["[youtube] Extracting URL", "nsig extraction failed",
|
||||
"HTTP Error 403: Forbidden"]
|
||||
assert seen == [("2026.09.01", None)]
|
||||
|
||||
+14
-27
@@ -78,7 +78,7 @@ def test_syllabify():
|
||||
assert syllabify("reminds", "English") == ["re", "minds"]
|
||||
assert syllabify("a", "English") == ["a"]
|
||||
assert syllabify("xyzzy", None) == ["xyzzy"]
|
||||
assert syllabify("She's", "English") == ["She's"]
|
||||
assert syllabify("It's", "English") == ["It's"]
|
||||
|
||||
|
||||
def test_syllabify_preserves_word_characters():
|
||||
@@ -96,12 +96,12 @@ def test_syllabify_language_without_pyphen_dict_is_not_mishandled_as_english():
|
||||
def test_build_notes_basic_line():
|
||||
times, f0, conf = flat_pitch()
|
||||
rows = [
|
||||
{"word": "She's", "start": 1.0, "end": 1.5, "score": 0.9, "line_index": 0},
|
||||
{"word": "It's", "start": 1.0, "end": 1.5, "score": 0.9, "line_index": 0},
|
||||
{"word": "got", "start": 1.6, "end": 2.0, "score": 0.9, "line_index": 0},
|
||||
{"word": "Now", "start": 5.0, "end": 5.5, "score": 0.9, "line_index": 1},
|
||||
]
|
||||
notes, breaks, warnings = build_notes(rows, times, f0, conf, "English")
|
||||
assert notes[0].text == "She's" # line-initial: no leading space
|
||||
assert notes[0].text == "It's" # line-initial: no leading space
|
||||
assert notes[1].text == " got" # mid-line: leading space
|
||||
assert notes[2].text == "Now" # new line: no leading space
|
||||
assert len(breaks) == 1 and 2.0 <= breaks[0] <= 5.0
|
||||
@@ -278,7 +278,7 @@ def test_notes_stage_writes_json(tmp_path):
|
||||
times, f0, conf = flat_pitch()
|
||||
np.savez(work / "pitch.npz", times=times, f0=f0, conf=conf)
|
||||
(work / "alignment.json").write_text(json.dumps([
|
||||
{"word": "She's", "start": 1.0, "end": 1.5, "score": 0.9, "line_index": 0},
|
||||
{"word": "It's", "start": 1.0, "end": 1.5, "score": 0.9, "line_index": 0},
|
||||
{"word": "gone", "start": 3.0, "end": 3.5, "score": 0.9, "line_index": 1},
|
||||
]))
|
||||
ctx = Ctx(inp=SongInput(title="T", artist="A", lyrics_text="x", language="English"),
|
||||
@@ -287,7 +287,7 @@ def test_notes_stage_writes_json(tmp_path):
|
||||
assert stage.outputs(ctx) == [work / "notes_sec.json"]
|
||||
stage.run(ctx)
|
||||
data = json.loads((work / "notes_sec.json").read_text())
|
||||
assert [n["text"] for n in data["notes"]] == ["She's", "gone"]
|
||||
assert [n["text"] for n in data["notes"]] == ["It's", "gone"]
|
||||
assert data["breaks"] == [2.25]
|
||||
assert data["warnings"] == []
|
||||
assert TimedNote(**data["notes"][0]).pitch == 0
|
||||
@@ -297,15 +297,19 @@ def test_notes_stage_registered():
|
||||
assert "notes" in [s.name for s in all_stages()]
|
||||
|
||||
|
||||
def test_build_notes_skips_notes_within_missing_span():
|
||||
def test_build_notes_keeps_matched_words_within_missing_span():
|
||||
# A missing span covers whole Whisper segments, so it can hold matched
|
||||
# lines alongside the unmatched singing; those lines must keep their notes.
|
||||
times, f0, conf = flat_pitch()
|
||||
rows = [
|
||||
{"word": "keep", "start": 1.0, "end": 1.4, "score": 0.9, "line_index": 0},
|
||||
{"word": "skip", "start": 5.0, "end": 5.4, "score": 0.9, "line_index": 1},
|
||||
{"word": "held", "start": 5.0, "end": 5.4, "score": 0.9, "line_index": 1},
|
||||
]
|
||||
notes, _, _ = build_notes(rows, times, f0, conf, "English",
|
||||
missing_spans=[(4.5, 6.0)])
|
||||
assert [n.text for n in notes] == ["keep"]
|
||||
notes, breaks, warnings = build_notes(rows, times, f0, conf, "English",
|
||||
missing_spans=[(4.5, 6.0)])
|
||||
assert [n.text for n in notes] == ["keep", "held"]
|
||||
assert breaks == [(1.4 + 5.0) / 2]
|
||||
assert warnings == ["missing lyrics near 0:04–0:06"]
|
||||
|
||||
|
||||
def test_warnings_report_dropped_lines():
|
||||
@@ -325,23 +329,6 @@ def test_warnings_report_missing_spans():
|
||||
assert warnings == ["missing lyrics near 1:21–1:29"]
|
||||
|
||||
|
||||
def test_build_notes_skipped_row_mid_line_does_not_corrupt_spacing_or_breaks():
|
||||
# "gone" falls inside a missing span and is skipped; "away" is the same
|
||||
# line's next word and must still get its leading space, and the break
|
||||
# before the next line must use the last *emitted* word's end (3.4, from
|
||||
# "away"), not the skipped row's end (2.4).
|
||||
times, f0, conf = flat_pitch()
|
||||
rows = [
|
||||
{"word": "keep", "start": 1.0, "end": 1.4, "score": 0.9, "line_index": 0},
|
||||
{"word": "gone", "start": 2.0, "end": 2.4, "score": 0.9, "line_index": 0},
|
||||
{"word": "away", "start": 3.0, "end": 3.4, "score": 0.9, "line_index": 0},
|
||||
{"word": "next", "start": 5.0, "end": 5.4, "score": 0.9, "line_index": 1},
|
||||
]
|
||||
notes, breaks, _ = build_notes(rows, times, f0, conf, "English",
|
||||
missing_spans=[(1.9, 2.5)])
|
||||
assert [n.text for n in notes] == ["keep", " away", "next"]
|
||||
assert breaks == [(3.4 + 5.0) / 2]
|
||||
|
||||
def test_notes_carry_line_index():
|
||||
times, f0, conf = flat_pitch()
|
||||
rows = [{"word": "hey", "start": 1.0, "end": 1.4, "score": 0.9, "line_index": 3}]
|
||||
|
||||
+21
-3
@@ -49,19 +49,37 @@ def make_pipeline_inputs(tmp_path, cover=None):
|
||||
return inp, cfg
|
||||
|
||||
|
||||
def test_package_copies_compatible_video(tmp_path, monkeypatch):
|
||||
monkeypatch.setattr(package, "ffmpeg", lambda *a: Path(a[-1]).write_bytes(b"m"))
|
||||
def test_package_remuxes_compatible_video_without_reencoding(tmp_path, monkeypatch):
|
||||
calls = []
|
||||
monkeypatch.setattr(package, "ffmpeg",
|
||||
lambda *a: (calls.append(a), Path(a[-1]).write_bytes(b"m")))
|
||||
monkeypatch.setattr(package, "ffprobe_codec", lambda p: ("webm", "vp9"))
|
||||
ctx = make_ctx(tmp_path, cover=make_cover(tmp_path))
|
||||
package.package_stage().run(ctx)
|
||||
out = tmp_path / "out" / "A - T"
|
||||
assert (out / "A - T.txt").exists()
|
||||
assert (out / "A - T.mp3").exists()
|
||||
assert (out / "A - T.webm").read_bytes() == b"v"
|
||||
assert (out / "A - T.webm").exists()
|
||||
video_call = next(c for c in calls if c[-1].endswith("A - T.webm"))
|
||||
assert "-c" in video_call and "copy" in video_call, "the video was re-encoded"
|
||||
img = Image.open(out / "A - T.jpg")
|
||||
assert img.width == img.height
|
||||
|
||||
|
||||
@pytest.mark.parametrize("probe,name", [(("webm", "vp9"), "A - T.webm"),
|
||||
(("avi", "mpeg4"), "A - T.mp4")])
|
||||
def test_package_strips_the_videos_audio_track(tmp_path, monkeypatch, probe, name):
|
||||
"""#MP3 is the song's audio; a video that keeps its own plays as an echo."""
|
||||
calls = []
|
||||
monkeypatch.setattr(package, "ffmpeg",
|
||||
lambda *a: (calls.append(a), Path(a[-1]).write_bytes(b"m")))
|
||||
monkeypatch.setattr(package, "ffprobe_codec", lambda p: probe)
|
||||
ctx = make_ctx(tmp_path, cover=make_cover(tmp_path))
|
||||
package.package_stage().run(ctx)
|
||||
video_call = next(c for c in calls if c[-1].endswith(name))
|
||||
assert "-an" in video_call
|
||||
|
||||
|
||||
def test_package_reencodes_and_rewrites_video_header(tmp_path, monkeypatch):
|
||||
monkeypatch.setattr(package, "ffmpeg", lambda *a: Path(a[-1]).write_bytes(b"m"))
|
||||
monkeypatch.setattr(package, "ffprobe_codec", lambda p: ("avi", "mpeg4"))
|
||||
|
||||
@@ -17,3 +17,21 @@ def test_pitch_stage_writes_npz(tmp_path, monkeypatch):
|
||||
data = np.load(work / "pitch.npz")
|
||||
assert np.allclose(data["f0"], 220.0)
|
||||
assert data["times"].shape == data["conf"].shape
|
||||
|
||||
|
||||
def test_mute_silence_drops_bleed_but_keeps_quiet_singing():
|
||||
# 60 frames of singing around -30 dB, one quiet sung frame at -50 dB, and
|
||||
# 40 frames of bleed at -60 dB: only the bleed is more than 25 dB below
|
||||
# the song's loud level.
|
||||
loudness = np.array([-30.0] * 60 + [-50.0] + [-60.0] * 40)
|
||||
conf = np.full_like(loudness, 0.8)
|
||||
pitch.mute_silence(conf, loudness)
|
||||
assert (conf[:61] == 0.8).all()
|
||||
assert (conf[61:] == 0.0).all()
|
||||
|
||||
|
||||
def test_mute_silence_is_relative_to_the_songs_level():
|
||||
loudness = np.array([-45.0] * 60 + [-75.0] * 40)
|
||||
conf = np.full_like(loudness, 0.8)
|
||||
pitch.mute_silence(conf, loudness)
|
||||
assert (conf[:60] == 0.8).all() and (conf[60:] == 0.0).all()
|
||||
|
||||
+12
-1
@@ -5,7 +5,7 @@ import pytest
|
||||
|
||||
from songtrace.config import Config
|
||||
from songtrace.pipeline import STAGE_ORDER, PipelineError, SongInput
|
||||
from songtrace.rebuild import (FIELD_STAGE, changed_fields, plan,
|
||||
from songtrace.rebuild import (FIELD_STAGE, changed_fields, earliest, plan,
|
||||
relocate_work, superseded_output)
|
||||
|
||||
|
||||
@@ -124,3 +124,14 @@ def test_superseded_output_refuses_a_path_outside_the_output_root(tmp_path):
|
||||
def test_superseded_output_refuses_the_output_root_itself(tmp_path):
|
||||
root = tmp_path / "out"
|
||||
assert superseded_output(str(root), str(root / "A - T"), root) is None
|
||||
|
||||
|
||||
def test_earliest_picks_the_earlier_stage():
|
||||
assert earliest("package", "separate") == "separate"
|
||||
assert earliest("separate", "package") == "separate"
|
||||
|
||||
|
||||
def test_earliest_ignores_unset_stages():
|
||||
assert earliest(None, "grid") == "grid"
|
||||
assert earliest("grid", None) == "grid"
|
||||
assert earliest(None, None) is None
|
||||
|
||||
@@ -53,7 +53,7 @@ def test_classify_lyrics_match_keeps_partially_matched_lines():
|
||||
def test_classify_lyrics_match_scopes_missing_span_to_adlib_not_whole_section():
|
||||
# matched line and an unmatched ad-lib close enough together that
|
||||
# vocal_sections would merge them into one section; missing_spans must
|
||||
# stay tight around the ad-lib so the matched line keeps its notes.
|
||||
# stay tight around the ad-lib so the review marks the right place.
|
||||
ref = normalize_lyrics("it's one two three\nfour five six seven eight")
|
||||
segs = [seg(1, 4, "it's one two three four five six seven eight"),
|
||||
seg(6, 8, "completely unrelated ad lib")]
|
||||
|
||||
@@ -10,8 +10,8 @@ def load(name):
|
||||
return (FIXTURES / name).read_text(encoding="utf-8", errors="replace")
|
||||
|
||||
def test_parse_headers_and_notes():
|
||||
song = parse_ultrastar(load("sanctuary.txt"))
|
||||
assert get_header(song, "artist") == "Utada Hikaru"
|
||||
song = parse_ultrastar(load("chart_crlf.txt"))
|
||||
assert get_header(song, "artist") == "Test Artist"
|
||||
assert parse_bpm(get_header(song, "BPM")) == 217.98
|
||||
first = song.body[0]
|
||||
assert first == Note(kind=":", beat=0, length=3, pitch=16, text="ah")
|
||||
@@ -19,16 +19,16 @@ def test_parse_headers_and_notes():
|
||||
assert any(n.kind == "F" for n in song.body if isinstance(n, Note))
|
||||
|
||||
def test_note_text_keeps_leading_space():
|
||||
song = parse_ultrastar(load("sanctuary.txt"))
|
||||
song = parse_ultrastar(load("chart_crlf.txt"))
|
||||
notes = [n for n in song.body if isinstance(n, Note)]
|
||||
assert notes[1].text == " ah"
|
||||
|
||||
def test_negative_pitch():
|
||||
song = parse_ultrastar(load("sweet_child.txt"))
|
||||
song = parse_ultrastar(load("chart_latin1.txt"))
|
||||
assert any(n.pitch == -9 for n in song.body if isinstance(n, Note))
|
||||
|
||||
def test_round_trip():
|
||||
for name in ("sweet_child.txt", "sanctuary.txt"):
|
||||
for name in ("chart_latin1.txt", "chart_crlf.txt"):
|
||||
song = parse_ultrastar(load(name))
|
||||
again = parse_ultrastar(serialize_ultrastar(song))
|
||||
assert again == song
|
||||
|
||||
+471
-4
@@ -584,11 +584,19 @@ def _submit_body(html):
|
||||
return html[start:html.index("\n});\n", start)]
|
||||
|
||||
|
||||
def _listener_body(html, prefix):
|
||||
start = html.index(prefix)
|
||||
return html[start:html.index("\n});\n", start)]
|
||||
|
||||
|
||||
def _rebuild_flaws(html):
|
||||
"""Ways rebuild mode could quietly stop working."""
|
||||
flaws, submit = [], _submit_body(html)
|
||||
if "rebuildOf ?" not in submit or "/rebuild" not in submit:
|
||||
flaws.append("the form always posts to /api/jobs, so edits create a new song")
|
||||
if "form.reset()" in submit or "exitRebuild()" in submit:
|
||||
flaws.append("a successful submission empties the form, so correcting "
|
||||
"or varying the song just sent means retyping it")
|
||||
|
||||
actions = _fn_body(html, "renderActions")
|
||||
if not re.search(r"if \(.*dataset\.jobId === job\.id.*\) return;", actions):
|
||||
@@ -609,6 +617,20 @@ def _rebuild_flaws(html):
|
||||
if "if (rebuildOf === id) exitRebuild();" not in _fn_body(html, "deleteJob"):
|
||||
flaws.append("deleting the loaded job leaves the form pointed at it")
|
||||
|
||||
# selecting a queue entry loads it, so the sidebar button is now the only
|
||||
# way back to a blank form
|
||||
create = _listener_body(html, 'createSong.addEventListener("click"')
|
||||
if "exitRebuild()" not in create:
|
||||
flaws.append("the queue's CREATE SONG button does not clear the form")
|
||||
if "formDirty" not in create or "confirm(" not in create:
|
||||
flaws.append("the queue's CREATE SONG button discards edits without asking")
|
||||
if "clearDetail(" not in create:
|
||||
flaws.append("CREATE SONG empties the form but leaves the previous "
|
||||
"song's review and player sitting under it")
|
||||
if "composing" not in create or "composing" not in _fn_body(html, "refreshJobs"):
|
||||
flaws.append("the blank page CREATE SONG made is selected back over by "
|
||||
"the next poll of the queue")
|
||||
|
||||
mode = _fn_body(html, "setFormMode")
|
||||
if 'querySelector("details").open' not in mode:
|
||||
flaws.append("prefilled genre/year/language/bpm stay hidden behind the "
|
||||
@@ -647,7 +669,7 @@ def test_rebuild_full_and_delete_controls_are_wired_up():
|
||||
|
||||
def _empty_state_flaws(html):
|
||||
refresh = _fn_body(html, "refreshJobs")
|
||||
if "clearDetail()" not in refresh:
|
||||
if "clearDetail(" not in refresh:
|
||||
return ["refreshJobs never resets the panel when no job is selected"]
|
||||
return []
|
||||
|
||||
@@ -656,6 +678,54 @@ def test_review_panel_resets_when_no_job_is_selected():
|
||||
assert _empty_state_flaws(_index_html()) == []
|
||||
|
||||
|
||||
def _playback_flaws(html):
|
||||
"""Ways the review could stop tracking the media element."""
|
||||
flaws = []
|
||||
source = _fn_body(html, "setPlayerSource")
|
||||
if "media/audio" not in source:
|
||||
flaws.append("the player is not pointed at the job's packaged audio, "
|
||||
"which is the file the game plays and the notes track")
|
||||
if "media/video" not in source:
|
||||
flaws.append("the packaged video is never shown alongside the audio")
|
||||
|
||||
# the mp3 and the video carry the same recording; playing both is an echo
|
||||
if not re.search(r"<video[^>]*\bmuted\b", html):
|
||||
flaws.append("the background video is not muted, so its own audio "
|
||||
"plays on top of the mp3")
|
||||
pump = _fn_body(html, "pumpPlayback")
|
||||
if "slaveVideo(" not in pump:
|
||||
flaws.append("the picture is never pulled back onto the audio clock, "
|
||||
"so it drifts out of sync over a long song")
|
||||
|
||||
if "requestAnimationFrame" not in _fn_body(html, "pumpPlayback"):
|
||||
flaws.append("sync is not driven by the frame loop, so the playhead "
|
||||
"steps at timeupdate's ~4Hz instead of following the audio")
|
||||
|
||||
if "media.paused" not in _fn_body(html, "focusActive"):
|
||||
flaws.append("the view is scrolled even while paused, fighting a reader "
|
||||
"who scrolled away to look at something else")
|
||||
|
||||
sync = _fn_body(html, "syncPlayback")
|
||||
if "clearEntry(" not in sync:
|
||||
flaws.append("the line the playhead left keeps its highlight")
|
||||
clear = _fn_body(html, "clearEntry")
|
||||
for name in ("review-word-sung", "review-word-active", "review-line-active"):
|
||||
if name not in clear:
|
||||
flaws.append(f"{name} is never taken off a line the playhead left")
|
||||
|
||||
# every branch that gives up on rendering a review has to unload the media
|
||||
# too, or the previous song stays loaded and playable under the new one
|
||||
render = _fn_body(html, "renderLyricsReview")
|
||||
if render.count("resetPlayer()") < render.count("emptyNote("):
|
||||
flaws.append("a review the page cannot render leaves the previous "
|
||||
"song's media loaded in the player")
|
||||
return flaws
|
||||
|
||||
|
||||
def test_review_playback_tracks_the_media_element():
|
||||
assert _playback_flaws(_index_html()) == []
|
||||
|
||||
|
||||
# each mutation is a plausible regression; the checker above must reject all of them
|
||||
MUTATIONS = [
|
||||
(_focus_flaws, "queue: card moved unconditionally",
|
||||
@@ -700,8 +770,40 @@ MUTATIONS = [
|
||||
'form.querySelector("details").open =', "const unusedAdvancedOpen ="),
|
||||
(_rebuild_flaws, "prefill: an in-progress job can be submitted for rebuild anyway",
|
||||
"submitButton.disabled = locked;", ""),
|
||||
(_rebuild_flaws, "submit: a successful submission empties the form again",
|
||||
" rebuildOf = null;\n formDirty = false;\n setFormMode(null, null);\n refreshJobs();",
|
||||
" exitRebuild();\n refreshJobs();"),
|
||||
(_rebuild_flaws, "create: the queue button no longer clears the form",
|
||||
" exitRebuild();\n clearDetail(\"nothing selected\");\n", ""),
|
||||
(_rebuild_flaws, "create: the review is left behind under a blank form",
|
||||
' clearDetail("nothing selected");\n', ""),
|
||||
(_rebuild_flaws, "create: the next poll selects a song back into the blank page",
|
||||
" composing = true;\n", ""),
|
||||
(_rebuild_flaws, "create: the queue button discards edits without asking",
|
||||
' if (formDirty && !confirm("Discard your unsaved edits to this form?")) return;\n composing = true;',
|
||||
" composing = true;"),
|
||||
(_playback_flaws, "playback: sync falls back to the timeupdate event's ~4Hz",
|
||||
"syncFrame = media.paused ? null : requestAnimationFrame(pumpPlayback);",
|
||||
"syncFrame = null;"),
|
||||
(_playback_flaws, "playback: the view is scrolled even while paused",
|
||||
"if (!activeEntry || media.paused) return;", "if (!activeEntry) return;"),
|
||||
(_playback_flaws, "playback: the line the playhead left keeps its highlight",
|
||||
" if (activeEntry) clearEntry(activeEntry);\n", ""),
|
||||
(_playback_flaws, "playback: a word stays lit after the playhead moves on",
|
||||
'w.el.classList.remove("review-word-sung", "review-word-active");',
|
||||
'w.el.classList.remove("review-word-sung");'),
|
||||
(_playback_flaws, "playback: an unrenderable review leaves the old song loaded",
|
||||
" resetPlayer();\n", ""),
|
||||
(_playback_flaws, "playback: the player is not pointed at the packaged audio",
|
||||
'const src = available?.audio ? `/api/jobs/${job.id}/media/audio` : "";',
|
||||
'const src = "";'),
|
||||
(_playback_flaws, "playback: the background video plays its own audio too",
|
||||
'<video id="player-video" muted playsinline',
|
||||
'<video id="player-video" playsinline'),
|
||||
(_playback_flaws, "playback: the picture is never re-synced to the audio",
|
||||
" slaveVideo(false);\n", ""),
|
||||
(_empty_state_flaws, "empty state: clearDetail no longer wired into refreshJobs",
|
||||
"if (selectedId) refreshDetail();\n else clearDetail();",
|
||||
"if (selectedId) refreshDetail();\n else clearDetail(composing ? \"nothing selected\" : undefined);",
|
||||
"if (selectedId) refreshDetail();"),
|
||||
]
|
||||
|
||||
@@ -732,13 +834,13 @@ def test_narrow_stage_labels_stay_in_the_accessibility_tree():
|
||||
|
||||
def test_rebuild_mode_reuses_the_one_form():
|
||||
html = _index_html()
|
||||
assert html.count("<form") == 1
|
||||
assert html.count('name="lyrics"') == 1 # one song form, not a rebuild copy
|
||||
assert _rebuild_flaws(html) == []
|
||||
|
||||
|
||||
def test_index_has_rebuild_hooks():
|
||||
html = _index_html()
|
||||
for hook in ("review-actions", "review-lineage", "cancel-rebuild",
|
||||
for hook in ("review-actions", "review-lineage", "create-song",
|
||||
'data-keep="video"', 'data-keep="cover"'):
|
||||
assert hook in html, hook
|
||||
|
||||
@@ -1385,3 +1487,368 @@ def test_review_missing_when_pipeline_never_wrote_notes(tmp_path):
|
||||
client, _ = make_client(tmp_path, ok_runner)
|
||||
job_id = make_done_job(client)
|
||||
assert client.get(f"/api/jobs/{job_id}/review").status_code == 404
|
||||
|
||||
|
||||
# --- review playback -------------------------------------------------------
|
||||
|
||||
MEDIA_FILES = {"A - T.mp3": b"ID3audio", "A - T.mp4": b"\0\0\0\x18ftypvideo",
|
||||
"A - T.jpg": b"\xff\xd8cover"}
|
||||
MEDIA_HEADERS = [("TITLE", "T"), ("ARTIST", "A"), ("MP3", "A - T.mp3"),
|
||||
("COVER", "A - T.jpg"), ("VIDEO", "A - T.mp4")]
|
||||
|
||||
|
||||
def media_runner_factory(files=None, headers=None, write_txt=True):
|
||||
"""A run that leaves behind a song folder, the way package does."""
|
||||
def runner(inp, cfg, log=print, force_from=None, stages=None):
|
||||
slug = song_slug(inp)
|
||||
out = cfg.output_dir / slug
|
||||
out.mkdir(parents=True, exist_ok=True)
|
||||
for name, body in (MEDIA_FILES if files is None else files).items():
|
||||
(out / name).write_bytes(body)
|
||||
if write_txt:
|
||||
rows = [f"#{k}:{v}" for k, v in
|
||||
(MEDIA_HEADERS if headers is None else headers)]
|
||||
(out / f"{slug}.txt").write_text("\n".join(rows) + "\n: 0 4 0 la\nE\n",
|
||||
encoding="utf-8")
|
||||
return PipelineResult(output_dir=out)
|
||||
return runner
|
||||
|
||||
|
||||
def test_media_serves_the_files_the_song_txt_names(tmp_path):
|
||||
client, _ = make_client(tmp_path, media_runner_factory())
|
||||
job_id = make_done_job(client)
|
||||
for kind, name in (("audio", "A - T.mp3"), ("video", "A - T.mp4"),
|
||||
("cover", "A - T.jpg")):
|
||||
resp = client.get(f"/api/jobs/{job_id}/media/{kind}")
|
||||
assert resp.status_code == 200, kind
|
||||
assert resp.content == MEDIA_FILES[name], kind
|
||||
|
||||
|
||||
def test_media_serves_whatever_the_header_was_rewritten_to(tmp_path):
|
||||
"""package transcodes to .mp4 and rewrites #VIDEO; the extension is not fixed."""
|
||||
files = {**MEDIA_FILES, "A - T.webm": b"transcoded"}
|
||||
headers = [(k, "A - T.webm" if k == "VIDEO" else v) for k, v in MEDIA_HEADERS]
|
||||
client, _ = make_client(tmp_path, media_runner_factory(files, headers))
|
||||
job_id = make_done_job(client)
|
||||
assert client.get(f"/api/jobs/{job_id}/media/video").content == b"transcoded"
|
||||
|
||||
|
||||
def test_media_supports_range_requests(tmp_path):
|
||||
"""Without 206 the browser cannot seek, which is the point of the player."""
|
||||
client, _ = make_client(tmp_path, media_runner_factory())
|
||||
job_id = make_done_job(client)
|
||||
resp = client.get(f"/api/jobs/{job_id}/media/video", headers={"Range": "bytes=0-3"})
|
||||
assert resp.status_code == 206
|
||||
assert resp.headers["content-range"] == f"bytes 0-3/{len(MEDIA_FILES['A - T.mp4'])}"
|
||||
assert resp.content == MEDIA_FILES["A - T.mp4"][:4]
|
||||
|
||||
|
||||
def test_media_of_an_unknown_kind_is_404(tmp_path):
|
||||
client, _ = make_client(tmp_path, media_runner_factory())
|
||||
job_id = make_done_job(client)
|
||||
assert client.get(f"/api/jobs/{job_id}/media/instrumental").status_code == 404
|
||||
|
||||
|
||||
def test_media_refuses_a_header_pointing_outside_the_song_folder(tmp_path):
|
||||
secret = tmp_path / "secret.txt"
|
||||
secret.write_text("not yours")
|
||||
headers = [(k, "../../secret.txt" if k == "MP3" else v) for k, v in MEDIA_HEADERS]
|
||||
client, _ = make_client(tmp_path, media_runner_factory(headers=headers))
|
||||
job_id = make_done_job(client)
|
||||
assert client.get(f"/api/jobs/{job_id}/media/audio").status_code == 404
|
||||
|
||||
|
||||
def test_media_refuses_an_absolute_header_path(tmp_path):
|
||||
secret = tmp_path / "secret.txt"
|
||||
secret.write_text("not yours")
|
||||
headers = [(k, str(secret) if k == "MP3" else v) for k, v in MEDIA_HEADERS]
|
||||
client, _ = make_client(tmp_path, media_runner_factory(headers=headers))
|
||||
job_id = make_done_job(client)
|
||||
assert client.get(f"/api/jobs/{job_id}/media/audio").status_code == 404
|
||||
|
||||
|
||||
def test_media_404s_when_the_named_file_is_missing(tmp_path):
|
||||
client, _ = make_client(tmp_path, media_runner_factory(files={}))
|
||||
job_id = make_done_job(client)
|
||||
assert client.get(f"/api/jobs/{job_id}/media/audio").status_code == 404
|
||||
|
||||
|
||||
def test_media_404s_when_the_header_was_dropped(tmp_path):
|
||||
"""package strips #COVER when there is no usable cover image."""
|
||||
headers = [(k, v) for k, v in MEDIA_HEADERS if k != "COVER"]
|
||||
client, _ = make_client(tmp_path, media_runner_factory(headers=headers))
|
||||
job_id = make_done_job(client)
|
||||
assert client.get(f"/api/jobs/{job_id}/media/cover").status_code == 404
|
||||
assert client.get(f"/api/jobs/{job_id}/media/audio").status_code == 200
|
||||
|
||||
|
||||
def test_media_404s_when_there_is_no_song_txt(tmp_path):
|
||||
client, _ = make_client(tmp_path, media_runner_factory(write_txt=False))
|
||||
job_id = make_done_job(client)
|
||||
assert client.get(f"/api/jobs/{job_id}/media/audio").status_code == 404
|
||||
|
||||
|
||||
def test_media_of_a_failed_job_is_404(tmp_path):
|
||||
def bad_runner(*a, **kw):
|
||||
raise PipelineError("boom")
|
||||
client, _ = make_client(tmp_path, bad_runner)
|
||||
job_id = client.post("/api/jobs", data={
|
||||
"title": "T", "artist": "A", "lyrics": "x", "youtube_url": "http://y"}).json()["id"]
|
||||
wait_status(client, job_id, "failed")
|
||||
assert client.get(f"/api/jobs/{job_id}/media/audio").status_code == 404
|
||||
|
||||
|
||||
def test_media_of_a_running_job_is_409(tmp_path):
|
||||
release = threading.Event()
|
||||
|
||||
def blocking(inp, cfg, log=print, force_from=None, stages=None):
|
||||
release.wait(5)
|
||||
return PipelineResult(output_dir=cfg.output_dir / "X")
|
||||
|
||||
client, _ = make_client(tmp_path, blocking)
|
||||
job_id = client.post("/api/jobs", data={
|
||||
"title": "T", "artist": "A", "lyrics": "x", "youtube_url": "http://y"}).json()["id"]
|
||||
try:
|
||||
wait_status(client, job_id, "running")
|
||||
assert client.get(f"/api/jobs/{job_id}/media/audio").status_code == 409
|
||||
finally:
|
||||
release.set()
|
||||
wait_status(client, job_id, "done")
|
||||
|
||||
|
||||
def test_media_of_an_unknown_job_is_404(tmp_path):
|
||||
client, _ = make_client(tmp_path, media_runner_factory())
|
||||
assert client.get("/api/jobs/nope/media/audio").status_code == 404
|
||||
|
||||
|
||||
def test_review_reports_which_media_the_player_can_load(tmp_path):
|
||||
notes = [{"start": 1.0, "duration": 0.4, "pitch": 0, "text": "hi",
|
||||
"low_confidence": False, "line_index": 0}]
|
||||
review = review_runner_factory(notes)
|
||||
media = media_runner_factory(headers=[(k, v) for k, v in MEDIA_HEADERS
|
||||
if k != "COVER"])
|
||||
|
||||
def runner(inp, cfg, **kw):
|
||||
media(inp, cfg, **kw)
|
||||
return review(inp, cfg, **kw)
|
||||
|
||||
client, _ = make_client(tmp_path, runner)
|
||||
job_id = make_done_job(client, lyrics="hi")
|
||||
body = client.get(f"/api/jobs/{job_id}/review").json()
|
||||
assert body["media"] == {"audio": True, "video": True, "cover": False}
|
||||
|
||||
|
||||
# --- restarting a rebuild from a chosen stage ------------------------------
|
||||
|
||||
def force_from_runner_factory(seen):
|
||||
def runner(inp, cfg, log=print, force_from=None, stages=None):
|
||||
seen.append(force_from)
|
||||
out = cfg.output_dir / song_slug(inp)
|
||||
out.mkdir(parents=True, exist_ok=True)
|
||||
return PipelineResult(output_dir=out)
|
||||
return runner
|
||||
|
||||
|
||||
def rebuild_with(client, job_id, **extra):
|
||||
data = {"title": "T", "artist": "A", "lyrics": "x", "youtube_url": "http://y"}
|
||||
data.update(extra)
|
||||
return client.post(f"/api/jobs/{job_id}/rebuild", data=data)
|
||||
|
||||
|
||||
def test_rebuild_without_edits_restarts_from_the_stage_asked_for(tmp_path):
|
||||
"""The point of the control: a change in songtrace invalidates no field."""
|
||||
seen = []
|
||||
client, _ = make_client(tmp_path, force_from_runner_factory(seen))
|
||||
parent = make_done_job(client)
|
||||
resp = rebuild_with(client, parent, force_from="package")
|
||||
assert resp.status_code == 201
|
||||
wait_status(client, resp.json()["id"], "done")
|
||||
assert seen[-1] == "package"
|
||||
|
||||
|
||||
def test_rebuild_without_a_stage_still_plans_from_the_edit(tmp_path):
|
||||
seen = []
|
||||
client, _ = make_client(tmp_path, force_from_runner_factory(seen))
|
||||
parent = make_done_job(client)
|
||||
resp = rebuild_with(client, parent, lyrics="different words")
|
||||
wait_status(client, resp.json()["id"], "done")
|
||||
assert seen[-1] == "align"
|
||||
|
||||
|
||||
def test_an_edit_wins_when_it_invalidates_more_than_the_stage_asked_for(tmp_path):
|
||||
"""A choice may widen what is redone, never narrow it below correctness."""
|
||||
seen = []
|
||||
client, _ = make_client(tmp_path, force_from_runner_factory(seen))
|
||||
parent = make_done_job(client)
|
||||
resp = rebuild_with(client, parent, lyrics="different words",
|
||||
force_from="package")
|
||||
wait_status(client, resp.json()["id"], "done")
|
||||
assert seen[-1] == "align"
|
||||
|
||||
|
||||
def test_the_stage_asked_for_wins_when_it_is_earlier_than_the_edit(tmp_path):
|
||||
seen = []
|
||||
client, _ = make_client(tmp_path, force_from_runner_factory(seen))
|
||||
parent = make_done_job(client)
|
||||
resp = rebuild_with(client, parent, title="Renamed", force_from="separate")
|
||||
wait_status(client, resp.json()["id"], "done")
|
||||
assert seen[-1] == "separate"
|
||||
|
||||
|
||||
def test_rebuild_rejects_a_stage_that_is_not_in_the_pipeline(tmp_path):
|
||||
client, _ = make_client(tmp_path, ok_runner)
|
||||
parent = make_done_job(client)
|
||||
resp = rebuild_with(client, parent, force_from="deechoing")
|
||||
assert resp.status_code == 400
|
||||
assert "deechoing" in resp.json()["detail"]
|
||||
|
||||
|
||||
def test_a_blank_stage_is_the_same_as_not_asking(tmp_path):
|
||||
seen = []
|
||||
client, _ = make_client(tmp_path, force_from_runner_factory(seen))
|
||||
parent = make_done_job(client)
|
||||
resp = rebuild_with(client, parent, force_from="")
|
||||
wait_status(client, resp.json()["id"], "done")
|
||||
assert seen[-1] is None
|
||||
|
||||
|
||||
def test_rebuild_from_scratch_still_restarts_at_the_first_stage(tmp_path):
|
||||
seen = []
|
||||
client, _ = make_client(tmp_path, force_from_runner_factory(seen))
|
||||
parent = make_done_job(client)
|
||||
resp = client.post(f"/api/jobs/{parent}/rebuild-full")
|
||||
wait_status(client, resp.json()["id"], "done")
|
||||
assert seen[-1] == STAGE_ORDER[0]
|
||||
|
||||
|
||||
def test_the_restart_control_offers_every_stage():
|
||||
html = _index_html()
|
||||
assert 'name="force_from"' in html
|
||||
stages = _fn_body(html, "loadStages")
|
||||
assert "force_from" in stages and "STAGES.map" in stages
|
||||
mode = _fn_body(html, "setFormMode")
|
||||
assert "restart-field" in mode, "the control is shown outside rebuild mode"
|
||||
|
||||
|
||||
def test_a_textareas_crlf_does_not_read_as_a_lyrics_edit(tmp_path):
|
||||
"""Browsers submit textareas with CRLF; the stored lyrics use LF."""
|
||||
seen = []
|
||||
client, _ = make_client(tmp_path, force_from_runner_factory(seen))
|
||||
parent = make_done_job(client, lyrics="one\ntwo")
|
||||
resp = rebuild_with(client, parent, lyrics="one\r\ntwo")
|
||||
wait_status(client, resp.json()["id"], "done")
|
||||
assert seen[-1] is None, "the rebuild redid the alignment for nothing"
|
||||
body = client.get(f"/api/jobs/{resp.json()['id']}/input").json()
|
||||
assert body["lyrics"] == "one\ntwo"
|
||||
|
||||
|
||||
def test_submitting_crlf_lyrics_stores_them_with_lf(tmp_path):
|
||||
client, _ = make_client(tmp_path, ok_runner)
|
||||
job_id = make_done_job(client, lyrics="one\r\ntwo")
|
||||
assert client.get(f"/api/jobs/{job_id}/input").json()["lyrics"] == "one\ntwo"
|
||||
|
||||
|
||||
def test_rebuild_all_rebuilds_every_song_from_the_stage(tmp_path):
|
||||
seen = []
|
||||
client, _ = make_client(tmp_path, capture_runner_factory(seen))
|
||||
first = make_done_job(client, title="One")
|
||||
second = make_done_job(client, title="Two")
|
||||
resp = client.post("/api/rebuild-all", data={"force_from": "pitch"})
|
||||
assert resp.status_code == 201
|
||||
body = resp.json()
|
||||
assert len(body["ids"]) == 2 and body["skipped"] == []
|
||||
jobs = [wait_status(client, i, "done") for i in body["ids"]]
|
||||
assert {j["rebuilt_from"] for j in jobs} == {first, second}
|
||||
assert [s["force_from"] for s in seen[2:]] == ["pitch", "pitch"]
|
||||
by_title = lambda runs: sorted((r["inp"] for r in runs), key=lambda i: i.title)
|
||||
assert by_title(seen[2:]) == by_title(seen[:2])
|
||||
|
||||
|
||||
def test_rebuild_all_skips_jobs_that_were_already_rebuilt(tmp_path):
|
||||
client, _ = make_client(tmp_path, capture_runner_factory([]))
|
||||
parent = make_done_job(client)
|
||||
renamed = client.post(f"/api/jobs/{parent}/rebuild",
|
||||
data=rebuild_form(title="Renamed")).json()["id"]
|
||||
wait_status(client, renamed, "done")
|
||||
body = client.post("/api/rebuild-all", data={"force_from": "notes"}).json()
|
||||
assert len(body["ids"]) == 1
|
||||
job = wait_status(client, body["ids"][0], "done")
|
||||
assert job["rebuilt_from"] == renamed
|
||||
|
||||
|
||||
def test_rebuild_all_takes_the_latest_job_for_a_song(tmp_path):
|
||||
client, _ = make_client(tmp_path, capture_runner_factory([]))
|
||||
make_done_job(client)
|
||||
latest = make_done_job(client)
|
||||
body = client.post("/api/rebuild-all", data={"force_from": "grid"}).json()
|
||||
assert len(body["ids"]) == 1
|
||||
assert wait_status(client, body["ids"][0], "done")["rebuilt_from"] == latest
|
||||
|
||||
|
||||
def test_rebuild_all_skips_songs_in_progress(tmp_path):
|
||||
gate = threading.Event()
|
||||
|
||||
def blocking_runner(inp, cfg, log=print, force_from=None, stages=None):
|
||||
gate.wait(timeout=5)
|
||||
return PipelineResult(output_dir=cfg.output_dir / song_slug(inp))
|
||||
|
||||
client, _ = make_client(tmp_path, blocking_runner)
|
||||
try:
|
||||
busy = client.post("/api/jobs", data={
|
||||
"title": "T", "artist": "A", "lyrics": "x", "youtube_url": "http://y"}).json()["id"]
|
||||
body = client.post("/api/rebuild-all", data={"force_from": "pitch"}).json()
|
||||
assert body == {"ids": [], "skipped": [{"id": busy, "reason": "still in progress"}]}
|
||||
finally:
|
||||
gate.set()
|
||||
|
||||
|
||||
def test_rebuild_all_from_acquire_skips_a_song_whose_upload_is_gone(tmp_path):
|
||||
client, _ = make_client(tmp_path, capture_runner_factory([]))
|
||||
job_id = client.post("/api/jobs", data={"title": "T", "artist": "A", "lyrics": "x"},
|
||||
files={"video": ("v.mp4", io.BytesIO(b"data"), "video/mp4")}
|
||||
).json()["id"]
|
||||
wait_status(client, job_id, "done")
|
||||
shutil.rmtree(tmp_path / "work" / "uploads", ignore_errors=True)
|
||||
body = client.post("/api/rebuild-all", data={"force_from": "acquire"}).json()
|
||||
assert body["ids"] == []
|
||||
assert body["skipped"][0]["id"] == job_id
|
||||
assert "upload" in body["skipped"][0]["reason"]
|
||||
|
||||
|
||||
def test_rebuild_all_rejects_an_unknown_stage(tmp_path):
|
||||
client, _ = make_client(tmp_path, ok_runner)
|
||||
make_done_job(client)
|
||||
assert client.post("/api/rebuild-all", data={"force_from": "nope"}).status_code == 400
|
||||
assert client.post("/api/rebuild-all", data={}).status_code == 422
|
||||
|
||||
|
||||
def test_rebuild_all_lists_the_current_job_of_each_song(tmp_path):
|
||||
client, _ = make_client(tmp_path, capture_runner_factory([]))
|
||||
one = make_done_job(client, title="One")
|
||||
two = make_done_job(client, title="Two")
|
||||
rerun = client.post(f"/api/jobs/{one}/rerun").json()["id"]
|
||||
wait_status(client, rerun, "done")
|
||||
songs = client.get("/api/rebuild-all").json()
|
||||
assert [(s["id"], s["title"], s["rebuilt_from"]) for s in songs] == [
|
||||
(rerun, "One", one), (two, "Two", None)]
|
||||
assert all(s["status"] == "done" and s["finished_at"] for s in songs)
|
||||
|
||||
|
||||
def test_rebuild_all_rebuilds_only_the_songs_selected(tmp_path):
|
||||
client, _ = make_client(tmp_path, capture_runner_factory([]))
|
||||
make_done_job(client, title="One")
|
||||
two = make_done_job(client, title="Two")
|
||||
resp = client.post("/api/rebuild-all", data={"force_from": "pitch", "ids": [two]})
|
||||
body = resp.json()
|
||||
assert len(body["ids"]) == 1 and body["skipped"] == []
|
||||
assert wait_status(client, body["ids"][0], "done")["rebuilt_from"] == two
|
||||
|
||||
|
||||
def test_rebuild_all_reports_a_selected_job_that_was_since_rebuilt(tmp_path):
|
||||
client, _ = make_client(tmp_path, capture_runner_factory([]))
|
||||
old = make_done_job(client)
|
||||
rerun = client.post(f"/api/jobs/{old}/rerun").json()["id"]
|
||||
wait_status(client, rerun, "done")
|
||||
body = client.post("/api/rebuild-all", data={"force_from": "pitch", "ids": [old]}).json()
|
||||
assert body == {"ids": [], "skipped": [
|
||||
{"id": old, "reason": "no longer the current job for its song"}]}
|
||||
|
||||
@@ -0,0 +1,139 @@
|
||||
import os
|
||||
import time
|
||||
|
||||
import pytest
|
||||
|
||||
from songtrace import ytdlp
|
||||
from songtrace.config import Config
|
||||
from songtrace.ytdlp import YtDlp, YtDlpError
|
||||
|
||||
|
||||
def fake_package(path, version="2026.09.01", body=""):
|
||||
"""A stand-in yt_dlp that answers --version and otherwise runs `body`."""
|
||||
pkg = path / "yt_dlp"
|
||||
pkg.mkdir(parents=True)
|
||||
(pkg / "__init__.py").touch()
|
||||
(pkg / "__main__.py").write_text(
|
||||
"import sys\n"
|
||||
f"if '--version' in sys.argv: print({version!r}); sys.exit()\n"
|
||||
+ body)
|
||||
return path
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def cache(tmp_path, monkeypatch):
|
||||
monkeypatch.setenv("XDG_CACHE_HOME", str(tmp_path / "cache"))
|
||||
monkeypatch.setattr(ytdlp, "bundled", lambda: YtDlp("2026.8.19"))
|
||||
return ytdlp.cache_dir()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def pypi(monkeypatch):
|
||||
"""Stubs PyPI and pip: `latest` is the newest release, `installs` logs pip."""
|
||||
state = {"latest": "2026.9.1", "installs": []}
|
||||
monkeypatch.setattr(ytdlp, "_latest_release", lambda: state["latest"])
|
||||
def pip_install(target, version):
|
||||
state["installs"].append(version)
|
||||
fake_package(target, version)
|
||||
monkeypatch.setattr(ytdlp, "_pip_install", pip_install)
|
||||
return state
|
||||
|
||||
|
||||
def cfg(version="auto"):
|
||||
return Config(whisper_url="http://x/v1", ytdlp_version=version)
|
||||
|
||||
|
||||
def test_run_uses_the_install_on_its_path_and_logs_its_output(tmp_path):
|
||||
path = fake_package(tmp_path, body="print('[youtube] hello', sys.argv[1:])\n")
|
||||
logged = []
|
||||
YtDlp("2026.09.01", path).run(["--", "https://yt/x"], logged.append)
|
||||
assert logged == ["[youtube] hello ['--ignore-config', '--', 'https://yt/x']"]
|
||||
|
||||
|
||||
def test_run_raises_the_error_line(tmp_path):
|
||||
path = fake_package(tmp_path, body=(
|
||||
"print('ERROR: HTTP Error 403: Forbidden', file=sys.stderr)\n"
|
||||
"print('trailing noise')\nsys.exit(1)\n"))
|
||||
with pytest.raises(YtDlpError, match="^ERROR: HTTP Error 403"):
|
||||
YtDlp("2026.09.01", path).run([], lambda m: None)
|
||||
|
||||
|
||||
def test_run_flags_a_usage_error_as_incompatible(tmp_path):
|
||||
path = fake_package(tmp_path, body=(
|
||||
"print('error: no such option: --retries', file=sys.stderr)\n"
|
||||
"sys.exit(2)\n"))
|
||||
with pytest.raises(YtDlpError, match="2026.09.01 rejected.*--retries"):
|
||||
YtDlp("2026.09.01", path).run([], lambda m: None)
|
||||
|
||||
|
||||
def test_auto_installs_a_newer_release_and_tries_it_first(cache, pypi):
|
||||
found = ytdlp.candidates(cfg(), lambda m: None)
|
||||
assert [y.version for y in found] == ["2026.9.1", "2026.8.19"]
|
||||
assert found[0].path == cache / "2026.9.1"
|
||||
assert not [p for p in cache.iterdir() if p.name.startswith(".tmp-")]
|
||||
|
||||
|
||||
def test_auto_checks_pypi_once_a_day(cache, pypi):
|
||||
ytdlp.candidates(cfg(), lambda m: None)
|
||||
pypi["latest"] = "2026.10.1"
|
||||
found = ytdlp.candidates(cfg(), lambda m: None)
|
||||
assert found[0].version == "2026.9.1"
|
||||
found = ytdlp.candidates(cfg(), lambda m: None, refresh=True)
|
||||
assert found[0].version == "2026.10.1"
|
||||
assert not (cache / "2026.9.1").exists()
|
||||
old = time.time() - ytdlp.CHECK_INTERVAL - 1
|
||||
os.utime(cache / ".checked", (old, old))
|
||||
pypi["latest"] = "2026.11.1"
|
||||
assert ytdlp.candidates(cfg(), lambda m: None)[0].version == "2026.11.1"
|
||||
|
||||
|
||||
def test_auto_uses_only_bundled_when_it_is_newest(cache, pypi):
|
||||
pypi["latest"] = "2026.8.19"
|
||||
assert [y.version for y in ytdlp.candidates(cfg(), lambda m: None)] == [
|
||||
"2026.8.19"]
|
||||
assert pypi["installs"] == []
|
||||
|
||||
|
||||
def test_auto_survives_pypi_being_unreachable(cache, pypi, monkeypatch):
|
||||
ytdlp.candidates(cfg(), lambda m: None)
|
||||
def down():
|
||||
raise OSError("network unreachable")
|
||||
monkeypatch.setattr(ytdlp, "_latest_release", down)
|
||||
logged = []
|
||||
found = ytdlp.candidates(cfg(), logged.append, refresh=True)
|
||||
assert [y.version for y in found] == ["2026.9.1", "2026.8.19"]
|
||||
assert any("network unreachable" in m for m in logged)
|
||||
|
||||
|
||||
def test_an_install_that_fails_its_check_is_discarded(cache, pypi, monkeypatch):
|
||||
monkeypatch.setattr(ytdlp, "_pip_install",
|
||||
lambda target, v: fake_package(target, "2026.1.1"))
|
||||
logged = []
|
||||
assert [y.version for y in ytdlp.candidates(cfg(), logged.append)] == [
|
||||
"2026.8.19"]
|
||||
assert any("failed its check" in m for m in logged)
|
||||
assert list(cache.iterdir()) == [cache / ".checked"]
|
||||
|
||||
|
||||
def test_bundled_never_touches_pypi(cache, pypi):
|
||||
pypi["latest"] = None
|
||||
assert [y.version for y in ytdlp.candidates(cfg("bundled"), print)] == [
|
||||
"2026.8.19"]
|
||||
assert not cache.exists()
|
||||
|
||||
|
||||
def test_pin_installs_that_release_even_if_older(cache, pypi):
|
||||
found = ytdlp.candidates(cfg("2026.07.01"), lambda m: None)
|
||||
assert [y.version for y in found] == ["2026.07.01", "2026.8.19"]
|
||||
assert pypi["installs"] == ["2026.07.01"]
|
||||
|
||||
|
||||
def test_pin_matching_bundled_installs_nothing(cache, pypi):
|
||||
found = ytdlp.candidates(cfg("2026.08.19"), lambda m: None)
|
||||
assert [y.version for y in found] == ["2026.8.19"]
|
||||
assert pypi["installs"] == []
|
||||
|
||||
|
||||
def test_pin_must_be_a_release_number(cache, pypi):
|
||||
with pytest.raises(YtDlpError, match="ytdlp_version"):
|
||||
ytdlp.candidates(cfg("latest; rm -rf /"), lambda m: None)
|
||||
Reference in New Issue
Block a user