# Sanctuary Commander Sanctuary Commander will be the successor to Video Commander. With built in functionality for preparing dynamically submitted content, rather than working from a static configuration file. The end goal is to have web based control of TV power, volume, input, and displayed content from a single location, while having the ability to pre-download Youtube video content, upload videos and pictures for display later, and keep the interface as simple as possible for the less technically adept. Simple Goal List - HomeAssistant Integration for TV Power, Volume, and Input Control - FFPlay based playback of video and audio - Ahead of time downloading of Youtube video content - Ahead of time uploading of pictures and videos from external devices - USB media awareness/playback ## YouTube download worker Install `yt-dlp` and make sure its command is on `PATH`. Install FFmpeg as well for downloads that need video/audio merging or other postprocessing. The worker uses Python's standard library; it does not need Flask. From the project directory, initialize or upgrade the database, then start the worker: ```sh python3 sc_database_generator.py python3 youtube_downloader.py ``` The initializer requires Python 3.12 or newer (the SQLite wrapper uses `Connection.autocommit`). It preserves existing jobs when adding the media-name and retry-count columns and upgrading the allowed statuses. Existing jobs start with a retry count of zero. Stop running workers before upgrading the database. The worker polls every five seconds when idle or after a failed attempt; use `--poll-interval 10` to change that delay, or `--config /path/to/config.json` to select another configuration file. `config.json` includes `"youtube_download_location": "Videos"`. The folder is created automatically. The worker resolves relative storage and database paths against the configuration file's directory. Downloaded files are named `job-.` to avoid collisions and keep display names out of file paths. The default `Videos/` folder is excluded from Git. ### Web interface Start the Flask app with `python3 sc_webserver.py` and use the home page's **Get Video from YouTube** link, or visit `/youtube/download`. Enter a media name and a YouTube video URL to queue a job. Invalid submissions show an error without creating a job; successful submissions redirect to `/youtube/jobs`. The jobs page shows pending, downloading, and failed jobs by default, including each job's name, URL, status, and failed-attempt count. Check **Include completed jobs** and click **Update List** to also show downloaded jobs. Refresh the page to see worker updates. Both pages are linked from the home screen. The download worker must run separately from Flask to process requests; submitting a form only adds a pending job. As with the existing upload endpoints, keep this interface on a trusted network until access controls are implemented. Alternatively, queue a job using SQL against `scdb.db`: ```sql INSERT INTO youtube_download_job ( youtube_download_job_url, youtube_download_job_media_name ) VALUES ( 'https://www.youtube.com/watch?v=VIDEO_ID', 'Desired button name' ); ``` Jobs default to `pending`. The worker claims the oldest pending job by ID and marks it `downloading` before invoking `yt-dlp` without a shell interpreter. After a successful download and postprocessing, it checks that the reported file exists inside storage, then atomically inserts a `media` record and marks the job `downloaded`. The media record contains the requested display name and the final absolute file path. For older jobs or blank names, the video's title is used. No playback-option records are created automatically. `config.json` sets `"youtube_download_max_retries": 3`. This must be a non-negative integer; zero allows only the initial attempt. Each failed worker attempt increments the job's persistent `youtube_download_job_retry_count`. While the count is at or below the limit, the job returns to `pending` for retry after the polling delay. When the count exceeds the limit, its status becomes `failed` and the worker ignores it, allowing later jobs to run. With the default limit, this allows one initial attempt plus three retries (four failed attempts total). These are worker attempts; yt-dlp may also retry network requests internally. Successful downloads and Ctrl+C interruptions do not increment the counter. To manually retry a failed job with a fresh budget: ```sql UPDATE youtube_download_job SET youtube_download_job_status = 'pending', youtube_download_job_retry_count = 0 WHERE youtube_download_job_id = 123 AND youtube_download_job_status = 'failed'; ``` Ctrl+C releases an in-flight job, but a forced termination or power loss can leave it `downloading`. Once you have confirmed no worker is still handling that job, reset it manually (this preserves its retry count): ```sql UPDATE youtube_download_job SET youtube_download_job_status = 'pending' WHERE youtube_download_job_id = 123 AND youtube_download_job_status = 'downloading'; ``` Run the database and worker tests without downloading any videos: ```sh python3 -m unittest test.youtube_download_job_test test.youtube_download_web_test -v ```