Plays folders of videos on a loop from boot, with a controller menu, per-video on/off, play settings, a logo boot splash and a nightly YouTube sync. Tested on a Raspberry Pi 4; x86 paths are untested. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
92 lines
3.3 KiB
Python
Executable File
92 lines
3.3 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Download the videos listed in ~/enrichment_videos/youtube.txt.
|
|
|
|
Each line is 'Folder name | link'. Files reach the video folder only once
|
|
complete, so the player never picks up a partial download.
|
|
"""
|
|
|
|
import os
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
HOME = Path.home()
|
|
VIDEOS = HOME / "enrichment_videos"
|
|
SOURCES = VIDEOS / "youtube.txt"
|
|
ARCHIVE = HOME / ".local/state/enrichment_player/youtube_archive.txt"
|
|
TEMP = HOME / ".cache/enrichment_player/downloads" # same disk: the final move is atomic
|
|
YT_DLP = HOME / ".local/bin/yt-dlp"
|
|
|
|
RESERVE_BYTES = 5 * 1024**3 # always leave this much disk free
|
|
|
|
# H.264 up to 1080p first: the Pi plays it reliably and YouTube's 4K versions
|
|
# are far too heavy. The later choices only apply if a video has no H.264.
|
|
FORMAT = ("bv*[vcodec^=avc1][height<=1080]+ba[ext=m4a]"
|
|
"/b[vcodec^=avc1][height<=1080]/bv*[height<=1080]+ba/b[height<=1080]")
|
|
|
|
|
|
def parse_sources(text):
|
|
"""Return [(folder, url)], reporting and skipping lines that make no sense."""
|
|
sources = []
|
|
for number, raw in enumerate(text.splitlines(), start=1):
|
|
line = raw.strip()
|
|
if not line or line.startswith("#"):
|
|
continue
|
|
folder, separator, url = (part.strip() for part in line.partition("|"))
|
|
safe = folder and folder not in (".", "..") and "/" not in folder
|
|
if not (separator and safe and url.startswith("http")):
|
|
print(f"youtube.txt line {number} ignored: {raw!r}", flush=True)
|
|
continue
|
|
sources.append((folder, url))
|
|
return sources
|
|
|
|
|
|
def download(folder, url):
|
|
"""Run yt-dlp for one source. Returns True if it finished cleanly."""
|
|
target = VIDEOS / folder
|
|
budget = shutil.disk_usage(HOME).free - RESERVE_BYTES
|
|
if budget <= 0:
|
|
print(f"skipping {folder!r}: disk is nearly full", flush=True)
|
|
return False
|
|
target.mkdir(parents=True, exist_ok=True)
|
|
print(f"syncing {folder!r}: {url}", flush=True)
|
|
result = subprocess.run([
|
|
str(YT_DLP), url,
|
|
"--format", FORMAT,
|
|
"--format-sort", "proto:https", # far smaller than the HLS copies
|
|
"--merge-output-format", "mp4",
|
|
"--paths", str(target),
|
|
"--paths", f"temp:{TEMP}",
|
|
"--output", "%(title).120B [%(id)s].%(ext)s",
|
|
"--download-archive", str(ARCHIVE),
|
|
"--no-playlist", # a video link stays one video; playlist links still work
|
|
"--match-filters", "!is_live",
|
|
"--max-filesize", str(budget),
|
|
"--ignore-errors",
|
|
"--retries", "20",
|
|
"--newline",
|
|
"--progress-delta", "60",
|
|
], stdin=subprocess.DEVNULL, check=False)
|
|
return result.returncode == 0
|
|
|
|
|
|
def main():
|
|
try:
|
|
sources = parse_sources(SOURCES.read_text(encoding="utf-8"))
|
|
except FileNotFoundError:
|
|
print(f"nothing to do: {SOURCES} does not exist", flush=True)
|
|
return 0
|
|
ARCHIVE.parent.mkdir(parents=True, exist_ok=True)
|
|
TEMP.mkdir(parents=True, exist_ok=True)
|
|
results = [download(folder, url) for folder, url in sources]
|
|
if all(results):
|
|
# Partial files are kept after a failure so the next run can resume.
|
|
shutil.rmtree(TEMP, ignore_errors=True)
|
|
return 0
|
|
return 1
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|