# ytcat `ytcat` concatenates video files without re-encoding and uploads the result to YouTube for archival. Concatenation and upload run as a single stream: ffmpeg writes MPEG-TS to stdout, and `ytcat` feeds that pipe straight into YouTube's resumable upload API. The video data is never written to disk and nothing needs to seek; only a small concat playlist file is placed in the system temp directory. ## Requirements A Nix flake is included. It pins the stable `nixos-26.05` channel and provides `ffmpeg`, `go_1_27` and `golangci-lint`: ```sh nix develop # or, with direnv: direnv allow ``` `ffmpeg` must also be on `PATH` when running `ytcat`, unless `-ffmpeg` is given or `nix` is available (see below). ### Running without installing ffmpeg On a Nix system, `ytcat` can obtain ffmpeg without installing it into the user or system environment. Build it as usual: ```sh go build -o ytcat . ``` At runtime, ffmpeg is resolved in this order: 1. the executable given with `-ffmpeg ` (fails if it does not exist) 2. `ffmpeg` in `PATH` 3. `nix shell nixpkgs#ffmpeg -c ffmpeg` The last step requires `nix` with flakes enabled and fetches ffmpeg from the `nixpkgs` flake registry on first use. ## One-time YouTube setup 1. Create a project in the [Google Cloud Console](https://console.cloud.google.com/). 2. Enable the **YouTube Data API v3**. 3. Configure the OAuth consent screen (an "External" app in testing mode is enough for personal use; add your Google account as a test user). 4. Create an OAuth client ID of type **Desktop app** and download the JSON. 5. Place it at `~/.config/ytcat/client_secret.json` (or pass `-client-secret`). 6. If you want to use `-wait`, add the `https://www.googleapis.com/auth/youtube.readonly` scope to the OAuth consent screen's scopes (Data access). It is a sensitive scope; an app in testing mode with your own account as a test user is sufficient. On the first upload, `ytcat` prints a URL. Open it, approve access, and the token is cached at `~/.config/ytcat/token.json` (override with `-token`). You can run `ytcat -auth` to complete this step without uploading; it requests both the upload and read-only scopes. Existing tokens created before `-wait` support are re-authorized automatically the first time `-wait` needs read access. To force a fresh consent, delete the token file. For backwards compatibility, if a file does not exist under `~/.config/ytcat`, `ytcat` falls back to the pre-rename `~/.config/yup` path for the OAuth client secret and token cache. Existing installations therefore keep working without re-authentication. ### Authenticating from a remote or headless machine The default `-auth` flow listens for the OAuth callback on the machine running `ytcat`. If that machine cannot receive the browser callback, use one of these: 1. **Copy the token (simplest).** Run `ytcat -auth` on a machine with a browser, then copy `~/.config/ytcat/token.json` to the same path on the remote host. The file contains a refresh token, so transfer it securely and keep it mode `0600`. 2. **Paste the redirect URL.** Run `ytcat -auth-manual` on the remote host. Open the printed URL in any browser. After approving, the browser will try to open `http://127.0.0.1:8090/...` and probably fail; copy the full URL from the address bar and paste it back into `ytcat`. Change the port with `-auth-port`. 3. **SSH port forwarding.** Run `ytcat -auth -auth-port 8090` on the remote host and `ssh -L 8090:127.0.0.1:8090 user@remote` from your local machine. Then open the printed URL locally; the callback reaches the remote `ytcat` through the tunnel. ## Usage ```sh ytcat [flags] video1.mp4 video2.mp4 ... ``` Common flags: | Flag | Default | Meaning | | --- | --- | --- | | `-title` | first file name | video title | | `-description` | empty | video description | | `-tags` | empty | comma-separated tags | | `-category` | `22` | YouTube category ID (22 = People & Blogs) | | `-privacy` | `private` | `private`, `unlisted` or `public` | | `-client-secret` | `~/.config/ytcat/client_secret.json` | OAuth client JSON | | `-token` | `~/.config/ytcat/token.json` | OAuth token cache | | `-dry-run` | `false` | write the TS stream to stdout, do not upload | | `-ffmpeg` | from `PATH`, then `nix` | ffmpeg executable; must exist if set | | `-i` | `false` | prompt for title, description, visibility and category | | `-wait` | `false` | wait for YouTube processing to finish (requires `youtube.readonly`) | | `-check` | | poll an existing video ID's processing status and exit | | `-auth` | `false` | authenticate with YouTube and exit | | `-auth-manual` | `false` | authenticate by pasting the redirect URL (no callback) | | `-auth-port` | `0` | callback port (0 = ephemeral; manual defaults to 8090) | | `-quiet` | `false` | suppress ffmpeg output and the progress bar | | `-verbose` | `false` | verbose ffmpeg logging; disables the progress bar | By default, `ytcat` shows a progress bar with the transfer rate and an ETA based on the total input file size. `-verbose` and `-quiet` disable it and are mutually exclusive. With `-i`, `ytcat` prompts for the title, description, visibility and category, using the current values as defaults. The category is selected by number from a table of the standard YouTube categories. With `-wait`, `ytcat` polls `videos.list` until YouTube reports processing as `succeeded` or `failed`, printing processing progress and YouTube's estimated time remaining. This requests the `https://www.googleapis.com/auth/youtube.readonly` scope in addition to `youtube.upload`. `-check ` polls an existing video the same way without uploading or requiring ffmpeg. Examples: ```sh # Upload two files as a private video ytcat -title "Archive 2024-01" -tags "archive,backup" part1.mp4 part2.mp4 # Enter the metadata interactively ytcat -i part1.mp4 part2.mp4 # Check an existing upload ytcat -check dQw4w9WgXcQ # Authenticate without uploading ytcat -auth # Authenticate on a remote host without an HTTP callback ytcat -auth-manual # Inspect the concatenated stream locally ytcat -dry-run part1.mp4 part2.mp4 > combined.ts ``` ## Limitations - The inputs must be compatible for stream copy: same codecs and codec parameters (resolution, pixel format, sample rate, ...). ffmpeg cannot concatenate arbitrary formats without re-encoding. - YouTube receives an MPEG-TS upload (`video/mp2t`) and transcodes it as usual. - A successful upload means YouTube has received the file. Processing (checks, transcoding, visibility) continues asynchronously, so YouTube Studio can show 0% and "checks starting soon" for a while. `ytcat` prints the returned `uploadStatus` (`uploaded` or `processed`). - If ffmpeg fails after YouTube accepted the stream, the upload is not rolled back automatically. ## Development ```sh nix develop golangci-lint run ./... go test ./... ```