Worker: - manage.py run_worker polls the database for pending transcode jobs (ffmpeg) and packaging jobs (Bento4 mp4fragment + mp4hls). Jobs are claimed with a conditional UPDATE, a heartbeat thread marks progress, stale jobs are failed, and SIGTERM returns an interrupted job to the queue. - SQLite runs in WAL mode with IMMEDIATE transactions and a busy timeout so the web server and worker can share the database. - transcode.py exposes a reusable transcode() and the HW->CPU fallback works. - Docker image includes ffmpeg and a checksum-verified Bento4 SDK. Jobs and storage: - TranscodeJob and PackagingJob have slugs, set explicitly on creation; outputs go to transcodes/<slug>/ and packages/<slug>/hls/. Migrations regenerated. - Uploads into transcodes/ and packages/ are refused; the uploader defaults to the videos/raw folder. UI: - New packaging job button on transcode jobs. - Delete views for packaging jobs, transcode jobs, uploaded files and content entries, each removing files before records and refusing while referenced. TranscodeJob.asset is now SET_NULL so deleting a source keeps its jobs. - Streamed zip downloads for completed transcode jobs and packages, rooted at the job slug; the content page lists packages with download buttons. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5.5 KiB
Running and Managing Private Studio
Private Studio is a standard Django project. All administration goes through manage.py, which uses privatestudio.settings.dev unless DJANGO_SETTINGS_MODULE is set.
Setup
python3 -m venv venv
source venv/bin/activate
pip install -e ".[dev]"
python manage.py migrate
Access Model
- Users are standard
django.contrib.authusers. - Studios each own one unique
bucket_nameand point to one Django Group. - A user can access a studio (its S3 bucket, profiles and jobs) when they are in that studio's group. Superusers can access every studio.
- API keys belong to a user and authenticate as that user. Only a SHA256 hash of each key is stored.
Commands
Create users
python manage.py createsuperuser # admin user
python manage.py shell -c "from django.contrib.auth.models import User; User.objects.create_user('alice')"
You can also create users in the admin at /admin/.
create_studio
python manage.py create_studio <bucket_name> --group <group> [--name "Display Name"] [--add-user USER ...]
This creates the group if it doesn't exist, adds the given users to it, and creates the studio. Bucket names must be lowercase letters, digits and hyphens.
create_key
python manage.py create_key <username>
This prints a new API key. The key is shown only once. Users can also create and revoke their own keys through /api/keys.
Run the server
python manage.py runserver 0.0.0.0:8000
In production, set DJANGO_SETTINGS_MODULE=privatestudio.settings.production and serve privatestudio.wsgi:application or privatestudio.asgi:application with your WSGI/ASGI server of choice. See the configuration table in README.md.
cleanup_uploads
python manage.py cleanup_uploads [--days 7] [--dry-run]
This removes unfinished uploads with no activity for --days (default UPLOAD_EXPIRY_DAYS), part directories without an active upload, and temporary files older than a day. Run it daily, for example from cron.
run_worker
python manage.py run_worker [--poll-interval 5] [--once]
Runs pending transcode jobs with ffmpeg, then their packaging jobs with Bento4 (mp4fragment and mp4hls on PATH, or in BENTO4_BIN). Run it as a separate long-lived process next to the web server, on the same database and STORAGE_PATH. --once runs the jobs that are pending now and exits.
- Transcode outputs are stored at
transcodes/<transcode job slug>/in the studio's bucket, and HLS packages atpackages/<packaging job slug>/hls/(playlistmaster.m3u8). Work files are kept inSTORAGE_PATH/.work/while a job runs. - On
SIGTERMorCtrl-C, the worker stops the running tool and returns the interrupted job to the queue. - While a job runs the worker records a heartbeat. A job without one for
WORKER_STALE_SECONDS(the worker crashed or was killed) is marked failed. - With SQLite, the web server and worker share the database file through WAL mode. Keep the file on a local disk (a Docker volume is fine), not a network filesystem.
With Docker, run a second container from the same image and volume:
docker run -d --name privatestudio-worker -v privatestudio-data:/data <image> python manage.py run_worker
Serving large uploads
Uploads and downloads are streamed, but the server in front of Django must allow them:
- WSGI (e.g. gunicorn): request bodies are read straight from the socket. A slow upload occupies a worker thread, so use threaded workers (
-k gthread --threads 8) and a--timeoutlonger than the slowest part and the final join of a large file. A chunked request withoutContent-Length(boto3 over HTTPS) needs a server that setswsgi.input_terminated, as gunicorn does; otherwise it is refused with 411. - ASGI (e.g. uvicorn): Django writes each request body to a temporary file before the view runs. Point
TMPDIRat a disk with room for the largest single upload. Browser parts are 16 MB, but a plain S3PUTof a whole file needs its full size. - nginx: set
client_max_body_sizeabove the part size (or0for large single PUTs), useproxy_request_buffering offfor WSGI so bodies are not buffered twice, and raiseproxy_read_timeoutso completing a large upload is not cut off. - Locking uses
flockon files inSTORAGE_PATH. Run all app servers on the host that owns the storage, or on a filesystem whereflockworks across hosts.
Typical Workflow
python manage.py migrate
python manage.py createsuperuser --username admin
python manage.py shell -c "from django.contrib.auth.models import User; User.objects.create_user('alice')"
python manage.py create_studio demo --name "Demo Studio" --group demo-team --add-user alice
python manage.py create_key alice
python manage.py runserver
curl -H "Authorization: Bearer $KEY" http://localhost:8000/api/userinfo
curl -X PUT -H "Authorization: Bearer $KEY" --data-binary @video.mp4 http://localhost:8000/s3/demo/video.mp4
curl -H "Authorization: Bearer $KEY" http://localhost:8000/s3/demo
Tests
pytest # all tests
pytest privatestudio/studio/tests/test_s3.py -k signature
pytest --cov=privatestudio
Troubleshooting
- 401 Unauthorized: the API key is wrong or revoked, or the S3 credentials have expired (by default they last 1 hour, set by
S3_CREDENTIAL_TTL_HOURS). - 403 on a bucket: the user isn't in the studio's group. Add them in the admin.
- 404 on a bucket: no studio has that
bucket_name.