opencode-media-panel 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +188 -2
- package/package.json +65 -4
- package/src/frames.ts +351 -0
- package/src/names.ts +8 -0
- package/src/options.ts +94 -0
- package/src/tui.tsx +252 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Single Barrel Labs
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,189 @@
|
|
|
1
|
-
#
|
|
1
|
+
# opencode-media-panel
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Play GIFs and video inside the OpenCode terminal UI, in a session panel.
|
|
4
|
+
|
|
5
|
+
OpenCode renders one still frame for an image in a tool result: its transcript
|
|
6
|
+
has no animation loop. This plugin adds one. It extracts frames with ffmpeg,
|
|
7
|
+
then swaps them in a `<image>` renderable on a timer, so a clip plays next to
|
|
8
|
+
your conversation instead of opening a second window.
|
|
9
|
+
|
|
10
|
+
Verified against OpenCode 2.3.2 on macOS with ffmpeg 9.
|
|
11
|
+
|
|
12
|
+
## Requirements
|
|
13
|
+
|
|
14
|
+
- OpenCode 2.3 or newer (TUI plugins and `session.panel` slots).
|
|
15
|
+
- `ffmpeg` on `PATH`, or an absolute path in the `ffmpeg` option. Frames are
|
|
16
|
+
extracted locally; nothing is uploaded.
|
|
17
|
+
|
|
18
|
+
## Install
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
opencode plugin add opencode-media-panel
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
A package with only a TUI part is configured in `cli.json`, not
|
|
25
|
+
`opencode.json`. `plugin add` writes a bare package name; add options by
|
|
26
|
+
editing the entry:
|
|
27
|
+
|
|
28
|
+
```json
|
|
29
|
+
{
|
|
30
|
+
"$schema": "https://opencode.ai/v2/cli.json",
|
|
31
|
+
"plugins": [
|
|
32
|
+
{
|
|
33
|
+
"package": "opencode-media-panel",
|
|
34
|
+
"options": {
|
|
35
|
+
"source": "/home/user/clips/demo.gif",
|
|
36
|
+
"fps": 12
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
]
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
With `source` set and `autoplay` left on, the panel opens by itself once a
|
|
44
|
+
session is on screen.
|
|
45
|
+
|
|
46
|
+
## Example
|
|
47
|
+
|
|
48
|
+
Make a clip to try it with, so no media of your own is needed:
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
ffmpeg -f lavfi -i "testsrc=size=320x240:rate=24:duration=4" -pix_fmt yuv420p /tmp/demo.mp4
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Point `cli.json` at it, as in the install example, with `"source": "/tmp/demo.mp4"`,
|
|
55
|
+
then start OpenCode and open a session. The panel opens on its own:
|
|
56
|
+
|
|
57
|
+
```text
|
|
58
|
+
demo.mp4 · 12 fps · 7/48 · space pause · f fullscreen · esc close
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
48 frames, because the request is 12 frames per second over 4 seconds; the source
|
|
62
|
+
clip's own frame rate does not change that. The line above the frames is the whole
|
|
63
|
+
interface: the file name, the frame rate in use, the current frame, and the keys.
|
|
64
|
+
`space` pauses, `f` takes the full window, `esc` closes the panel and returns focus
|
|
65
|
+
to the composer.
|
|
66
|
+
|
|
67
|
+
Without a `source`, ask for the file instead:
|
|
68
|
+
|
|
69
|
+
```text
|
|
70
|
+
/media /tmp/demo.mp4
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
The panel belongs to a session, so opening a file from the home screen reports
|
|
74
|
+
"Open a session first" and opens nothing.
|
|
75
|
+
|
|
76
|
+
## Use
|
|
77
|
+
|
|
78
|
+
| Action | How |
|
|
79
|
+
| -------------------------- | ----------------------------------------------------- |
|
|
80
|
+
| Play a file | `/media /home/user/clips/demo.mp4` |
|
|
81
|
+
| Play the configured source | `/media` |
|
|
82
|
+
| Close | `esc`, or the palette command "Close the media panel" |
|
|
83
|
+
| Pause and resume | `space` |
|
|
84
|
+
| Full screen | `f` |
|
|
85
|
+
|
|
86
|
+
Paths with spaces are fine: the whole argument is the path. Remote URLs work
|
|
87
|
+
too, as long as ffmpeg can read the protocol.
|
|
88
|
+
|
|
89
|
+
## Options
|
|
90
|
+
|
|
91
|
+
| Option | Default | Meaning |
|
|
92
|
+
| ---------------- | ----------- | ----------------------------------------------- |
|
|
93
|
+
| `source` | none | File path or URL opened on start. |
|
|
94
|
+
| `autoplay` | `true` | Open the panel on start when a source is known. |
|
|
95
|
+
| `remember` | `false` | Reopen the last clip played on start. |
|
|
96
|
+
| `fps` | `12` | Frames played per second, 1 to 30. |
|
|
97
|
+
| `width` | `44` | Panel width in cells, 4 pixels each. |
|
|
98
|
+
| `loop` | `true` | Restart after the last frame. |
|
|
99
|
+
| `maxFrames` | `300` | Cap on extracted frames, 1 to 2000. |
|
|
100
|
+
| `ffmpeg` | `ffmpeg` | Binary name or absolute path. |
|
|
101
|
+
| `cacheDirectory` | system temp | Root directory for extracted frames. |
|
|
102
|
+
| `presentation` | `panel` | `panel` or `fullscreen`. |
|
|
103
|
+
|
|
104
|
+
Frame height follows the source aspect ratio.
|
|
105
|
+
|
|
106
|
+
## How it works
|
|
107
|
+
|
|
108
|
+
1. `ffmpeg -i <source> -an -vf fps=<fps>,scale=<pixels>:-2` writes numbered
|
|
109
|
+
PNGs.
|
|
110
|
+
2. The PNGs are read into memory as data URIs.
|
|
111
|
+
3. A timer advances the frame index, and the panel redraws its `<image>` with
|
|
112
|
+
the next entry.
|
|
113
|
+
|
|
114
|
+
Extraction is cached per source path, size, modification time, and the settings
|
|
115
|
+
that change the pixels. A restart reuses the previous frames; editing the clip
|
|
116
|
+
re-extracts. Each source gets its own directory under the system temp
|
|
117
|
+
directory, created owner-only. A failed extraction leaves nothing behind, and a
|
|
118
|
+
staging directory left by a killed run is swept an hour later.
|
|
119
|
+
|
|
120
|
+
A remote source is cached by its URL alone and is never refreshed. Delete its
|
|
121
|
+
directory under `cacheDirectory` to fetch it again.
|
|
122
|
+
|
|
123
|
+
## Limits
|
|
124
|
+
|
|
125
|
+
- No audio. A terminal image renderable cannot play sound.
|
|
126
|
+
- Settings are fixed per clip. `fps` decides the frame rate, not the source
|
|
127
|
+
file's own timing; a 60 fps clip at `fps: 12` plays at 12.
|
|
128
|
+
- Frames are held in memory as base64. Measured at 44 cells wide, frames ran
|
|
129
|
+
about 34 KB each, so the default 300-frame cap is roughly 15 MB. A 64 MB
|
|
130
|
+
budget on the stored base64 stops the load past that: the clip plays the
|
|
131
|
+
frames that fit and the panel marks the count `capped`.
|
|
132
|
+
- Extraction blocks the TUI while ffmpeg runs. A long clip is slow on first
|
|
133
|
+
play and fast afterwards. A remote input that stops responding aborts after
|
|
134
|
+
30 seconds instead of hanging.
|
|
135
|
+
- One panel per plugin instance. Playing another file replaces the current clip.
|
|
136
|
+
|
|
137
|
+
## Troubleshooting
|
|
138
|
+
|
|
139
|
+
| Symptom | Cause |
|
|
140
|
+
| ----------------------------- | --------------------------------------------------------------- |
|
|
141
|
+
| Toast: `Could not run ffmpeg` | ffmpeg is not on the TUI's `PATH`; set `ffmpeg` to a full path. |
|
|
142
|
+
| Toast: `Open a session first` | The panel only exists inside a session. |
|
|
143
|
+
| Toast: `ffmpeg failed on ...` | The file is not media ffmpeg can decode. |
|
|
144
|
+
| No autoplay | No `source` set and `remember` off, or no session is open yet. |
|
|
145
|
+
|
|
146
|
+
## Development
|
|
147
|
+
|
|
148
|
+
```bash
|
|
149
|
+
npm install
|
|
150
|
+
npm run check # typecheck, lint, tests
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
`npm run check` needs ffmpeg; the extraction tests skip themselves without it.
|
|
154
|
+
|
|
155
|
+
To try a local checkout, point `cli.json` at the directory:
|
|
156
|
+
|
|
157
|
+
```json
|
|
158
|
+
{
|
|
159
|
+
"plugins": [
|
|
160
|
+
{
|
|
161
|
+
"package": "/home/user/code/opencode-media-panel",
|
|
162
|
+
"options": { "source": "/home/user/clip.gif" }
|
|
163
|
+
}
|
|
164
|
+
]
|
|
165
|
+
}
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
## Releases (maintainers)
|
|
169
|
+
|
|
170
|
+
CI in `.github/workflows/ci.yml` owns publishing.
|
|
171
|
+
|
|
172
|
+
- Merge to `main` publishes the next patch to npm `latest`, pushes a `vX.Y.Z`
|
|
173
|
+
tag, and creates a GitHub release.
|
|
174
|
+
- A `v*-beta*` tag publishes the version in the tag name to the `beta`
|
|
175
|
+
dist-tag; a manual workflow dispatch publishes the next `-beta.X`.
|
|
176
|
+
- Every pull request publishes `<version>-beta.<run_number>` to `beta` once it
|
|
177
|
+
is out of draft, so each PR is installable.
|
|
178
|
+
- Publishing is skipped when every changed path is non-shipping:
|
|
179
|
+
`package-lock.json`, `.github/**`, `CHANGELOG.md`, `.gitignore`, `test/**`,
|
|
180
|
+
`tsconfig.json`, `biome.json`, `.markdownlint-cli2.jsonc`, and a
|
|
181
|
+
`package.json` whose only diffs are the version or a toolchain devDependency.
|
|
182
|
+
Any other path publishes.
|
|
183
|
+
- Auth is npm trusted publishing over OIDC. The trusted publisher names this
|
|
184
|
+
repository and the workflow file `ci.yml`; renaming the file breaks it. The
|
|
185
|
+
first publish of a new package cannot use OIDC and is done interactively.
|
|
186
|
+
|
|
187
|
+
## License
|
|
188
|
+
|
|
189
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,67 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opencode-media-panel",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "OpenCode v2 TUI plugin: play GIFs and video inside a session panel.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
"./tui": "./src/tui.tsx"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"src",
|
|
11
|
+
"README.md",
|
|
12
|
+
"LICENSE"
|
|
13
|
+
],
|
|
14
|
+
"keywords": [
|
|
15
|
+
"opencode",
|
|
16
|
+
"opencode-plugin",
|
|
17
|
+
"opencode-v2",
|
|
18
|
+
"tui",
|
|
19
|
+
"gif",
|
|
20
|
+
"video",
|
|
21
|
+
"animation",
|
|
22
|
+
"ffmpeg",
|
|
23
|
+
"terminal"
|
|
24
|
+
],
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"author": "Single Barrel Labs",
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "git+https://github.com/singlebarrellabs/opencode-media-panel.git"
|
|
30
|
+
},
|
|
31
|
+
"bugs": {
|
|
32
|
+
"url": "https://github.com/singlebarrellabs/opencode-media-panel/issues"
|
|
33
|
+
},
|
|
34
|
+
"homepage": "https://github.com/singlebarrellabs/opencode-media-panel#readme",
|
|
35
|
+
"engines": {
|
|
36
|
+
"node": ">=22"
|
|
37
|
+
},
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public"
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"typecheck": "tsc --noEmit",
|
|
43
|
+
"lint": "biome check . && markdownlint-cli2 \"**/*.md\" \"#node_modules/**\" \"#dist/**\" \"#.git/**\"",
|
|
44
|
+
"lint:fix": "biome check --write . && markdownlint-cli2 --fix \"**/*.md\" \"#node_modules/**\" \"#dist/**\" \"#.git/**\"",
|
|
45
|
+
"test": "vitest run",
|
|
46
|
+
"check": "npm run typecheck && npm run lint && npm test"
|
|
47
|
+
},
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"@opencode/plugin": "^2.3.2"
|
|
50
|
+
},
|
|
51
|
+
"peerDependencies": {
|
|
52
|
+
"@opentui/core": ">=0.8.4",
|
|
53
|
+
"@opentui/solid": ">=0.8.4",
|
|
54
|
+
"solid-js": ">=1.9.0"
|
|
55
|
+
},
|
|
56
|
+
"devDependencies": {
|
|
57
|
+
"@biomejs/biome": "^2.6.7",
|
|
58
|
+
"@opencode/plugin": "^2.3.2",
|
|
59
|
+
"@opentui/core": "^0.8.4",
|
|
60
|
+
"@opentui/solid": "^0.8.4",
|
|
61
|
+
"@types/node": "^24.0.0",
|
|
62
|
+
"markdownlint-cli2": "^0.24.0",
|
|
63
|
+
"solid-js": "^1.9.0",
|
|
64
|
+
"typescript": "^5.9.0",
|
|
65
|
+
"vitest": "^3.2.0"
|
|
66
|
+
}
|
|
67
|
+
}
|
package/src/frames.ts
ADDED
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turns a GIF, video, or image into ordered PNG frames on disk, then loads them
|
|
3
|
+
* as data URIs the TUI can draw.
|
|
4
|
+
*
|
|
5
|
+
* Frames are cached per source and setting combination so a restart reuses the
|
|
6
|
+
* previous extraction instead of paying for it again.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { spawnSync } from "node:child_process";
|
|
10
|
+
import { createHash } from "node:crypto";
|
|
11
|
+
import {
|
|
12
|
+
existsSync,
|
|
13
|
+
mkdirSync,
|
|
14
|
+
readdirSync,
|
|
15
|
+
readFileSync,
|
|
16
|
+
renameSync,
|
|
17
|
+
rmSync,
|
|
18
|
+
statSync,
|
|
19
|
+
} from "node:fs";
|
|
20
|
+
import { tmpdir } from "node:os";
|
|
21
|
+
import { join, resolve } from "node:path";
|
|
22
|
+
import { fileURLToPath } from "node:url";
|
|
23
|
+
import { frameFile, framePattern } from "./names";
|
|
24
|
+
import { type MediaOptions, pixelsWide } from "./options";
|
|
25
|
+
|
|
26
|
+
export interface FrameSource {
|
|
27
|
+
/** Ordered `data:image/png;base64,...` URIs. */
|
|
28
|
+
readonly frames: readonly string[];
|
|
29
|
+
/** Frame width in pixels. */
|
|
30
|
+
readonly width: number;
|
|
31
|
+
/** Frame height in pixels. */
|
|
32
|
+
readonly height: number;
|
|
33
|
+
/** True when the frames came from an existing cache directory. */
|
|
34
|
+
readonly cached: boolean;
|
|
35
|
+
/** Directory holding the PNG files. */
|
|
36
|
+
readonly directory: string;
|
|
37
|
+
/** True when the frame list was cut short to stay inside the memory budget. */
|
|
38
|
+
readonly truncated: boolean;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export class FrameError extends Error {
|
|
42
|
+
override name = "FrameError";
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export const DEFAULT_CACHE_ROOT = "opencode-media-panel";
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Ceiling on the bytes held in memory. Counted as the stored base64 string, not
|
|
49
|
+
* the PNG on disk, because the base64 form is what stays resident. 64 MB is
|
|
50
|
+
* roughly 1400 frames of the size these clips produce.
|
|
51
|
+
*/
|
|
52
|
+
export const FRAME_BUDGET_BYTES = 64 * 1024 * 1024;
|
|
53
|
+
|
|
54
|
+
const DATA_URI_PREFIX = "data:image/png;base64,";
|
|
55
|
+
|
|
56
|
+
/** A staging directory this old belongs to a run that died without cleaning up. */
|
|
57
|
+
const STAGING_TTL_MS = 60 * 60 * 1000;
|
|
58
|
+
|
|
59
|
+
/** Remote reads stop after 30 s instead of blocking the TUI forever. */
|
|
60
|
+
const REMOTE_TIMEOUT_SECONDS = 30;
|
|
61
|
+
|
|
62
|
+
/** Protocols ffmpeg may use for a remote source. Local files are not remote. */
|
|
63
|
+
const REMOTE_PROTOCOLS = "http,https,tcp,tls,crypto";
|
|
64
|
+
|
|
65
|
+
const SCHEME = /^[a-z][a-z0-9+.-]*:\/\//i;
|
|
66
|
+
const FILE_URL = /^file:\/\//i;
|
|
67
|
+
|
|
68
|
+
const isRemote = (source: string): boolean => SCHEME.test(source) && !FILE_URL.test(source);
|
|
69
|
+
|
|
70
|
+
/** Drops credentials and query strings from a URL before it reaches a message. */
|
|
71
|
+
export function redact(source: string): string {
|
|
72
|
+
if (!isRemote(source)) return source;
|
|
73
|
+
try {
|
|
74
|
+
const url = new URL(source);
|
|
75
|
+
url.username = "";
|
|
76
|
+
url.password = "";
|
|
77
|
+
url.search = "";
|
|
78
|
+
return url.toString();
|
|
79
|
+
} catch {
|
|
80
|
+
return "(remote source)";
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Removes the source from text this code did not build, such as ffmpeg's own
|
|
86
|
+
* stderr, where it can reappear with a signed query string still attached.
|
|
87
|
+
*/
|
|
88
|
+
export function scrub(text: string, source: string): string {
|
|
89
|
+
return text.split(source).join(redact(source));
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Local paths are resolved; `file://` URLs are converted, not treated as remote. */
|
|
93
|
+
function localPath(source: string): string {
|
|
94
|
+
if (!FILE_URL.test(source)) return resolve(source);
|
|
95
|
+
try {
|
|
96
|
+
return fileURLToPath(source);
|
|
97
|
+
} catch {
|
|
98
|
+
throw new FrameError(`Not a usable file URL: ${redact(source)}`);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const ownerOnly = (directory: string): void => {
|
|
103
|
+
mkdirSync(directory, { recursive: true, mode: 0o700 });
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* A predictable cache path in a shared temp directory could be pre-created by
|
|
108
|
+
* another local user. Refuse a directory this process does not own.
|
|
109
|
+
*/
|
|
110
|
+
function assertOwned(directory: string): void {
|
|
111
|
+
const getuid = process.getuid;
|
|
112
|
+
if (typeof getuid !== "function") return;
|
|
113
|
+
const info = statSync(directory);
|
|
114
|
+
if (info.uid !== getuid.call(process)) {
|
|
115
|
+
throw new FrameError(`${directory} is owned by another user; set a private cacheDirectory`);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Removes staging directories left by a run that was killed mid-extraction. A
|
|
121
|
+
* live extraction is younger than the TTL and is left alone.
|
|
122
|
+
*/
|
|
123
|
+
function sweepStaging(root: string): void {
|
|
124
|
+
for (const entry of readdirSync(root)) {
|
|
125
|
+
if (!entry.includes(".partial-")) continue;
|
|
126
|
+
const path = join(root, entry);
|
|
127
|
+
try {
|
|
128
|
+
if (Date.now() - statSync(path).mtimeMs < STAGING_TTL_MS) continue;
|
|
129
|
+
rmSync(path, { recursive: true, force: true });
|
|
130
|
+
} catch {
|
|
131
|
+
// Another process is using it. Leave it for the next sweep.
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Local files are identified by path, size, and modification time, so editing a
|
|
138
|
+
* clip produces new frames. A remote source is identified by its URL alone and
|
|
139
|
+
* is never refreshed; clear the cache directory to fetch it again.
|
|
140
|
+
*/
|
|
141
|
+
function sourceStamp(source: string): string {
|
|
142
|
+
if (isRemote(source)) return source;
|
|
143
|
+
const path = localPath(source);
|
|
144
|
+
let info: ReturnType<typeof statSync>;
|
|
145
|
+
try {
|
|
146
|
+
info = statSync(path);
|
|
147
|
+
} catch (error) {
|
|
148
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
149
|
+
throw new FrameError(`Cannot read ${path}: ${reason}`);
|
|
150
|
+
}
|
|
151
|
+
if (info.isDirectory()) throw new FrameError(`${path} is a directory, not a media file`);
|
|
152
|
+
return `${path}:${info.size}:${Math.round(info.mtimeMs)}`;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function cacheRoot(
|
|
156
|
+
options: MediaOptions,
|
|
157
|
+
fallback = join(tmpdir(), DEFAULT_CACHE_ROOT),
|
|
158
|
+
): string {
|
|
159
|
+
return options.cacheDirectory ?? fallback;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export function cacheDirectory(source: string, options: MediaOptions, root?: string): string {
|
|
163
|
+
const key = [sourceStamp(source), options.fps, pixelsWide(options.width), options.maxFrames].join(
|
|
164
|
+
"|",
|
|
165
|
+
);
|
|
166
|
+
const hash = createHash("sha1").update(key).digest("hex").slice(0, 16);
|
|
167
|
+
return join(cacheRoot(options, root), hash);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function listFrames(directory: string): string[] {
|
|
171
|
+
if (!existsSync(directory)) return [];
|
|
172
|
+
return readdirSync(directory)
|
|
173
|
+
.filter((entry) => frameFile(entry))
|
|
174
|
+
.sort()
|
|
175
|
+
.map((entry) => join(directory, entry));
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function pngSize(buffer: Buffer, path: string): { width: number; height: number } {
|
|
179
|
+
if (buffer.length < 24 || buffer.toString("ascii", 1, 4) !== "PNG") {
|
|
180
|
+
throw new FrameError(`ffmpeg wrote ${path} but it is not a PNG`);
|
|
181
|
+
}
|
|
182
|
+
return { width: buffer.readUInt32BE(16), height: buffer.readUInt32BE(20) };
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Reads frames until the byte budget is reached. The first frame always loads,
|
|
187
|
+
* so a single oversized frame still renders.
|
|
188
|
+
*/
|
|
189
|
+
function load(directory: string, paths: string[], cached: boolean, source: string): FrameSource {
|
|
190
|
+
const first = paths[0];
|
|
191
|
+
if (first === undefined) throw new FrameError(`No frames extracted from ${redact(source)}`);
|
|
192
|
+
|
|
193
|
+
const frames: string[] = [];
|
|
194
|
+
let size: { width: number; height: number } | undefined;
|
|
195
|
+
let bytes = 0;
|
|
196
|
+
let truncated = false;
|
|
197
|
+
|
|
198
|
+
for (const path of paths) {
|
|
199
|
+
const buffer = readFileSync(path);
|
|
200
|
+
const stored = DATA_URI_PREFIX.length + Math.ceil(buffer.byteLength / 3) * 4;
|
|
201
|
+
if (size !== undefined && bytes + stored > FRAME_BUDGET_BYTES) {
|
|
202
|
+
truncated = true;
|
|
203
|
+
break;
|
|
204
|
+
}
|
|
205
|
+
bytes += stored;
|
|
206
|
+
size ??= pngSize(buffer, path);
|
|
207
|
+
frames.push(`${DATA_URI_PREFIX}${buffer.toString("base64")}`);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if (size === undefined) throw new FrameError(`No frames extracted from ${redact(source)}`);
|
|
211
|
+
|
|
212
|
+
return { frames, width: size.width, height: size.height, cached, directory, truncated };
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const discard = (directory: string) => rmSync(directory, { recursive: true, force: true });
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Moves the staging directory into place. Losing a race against another
|
|
219
|
+
* process is not an error: whoever published a complete set wins.
|
|
220
|
+
*/
|
|
221
|
+
function publish(staging: string, directory: string, source: string): FrameSource {
|
|
222
|
+
const published = listFrames(directory);
|
|
223
|
+
if (published.length > 0) {
|
|
224
|
+
discard(staging);
|
|
225
|
+
return load(directory, published, true, source);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// A directory with no frames cannot be renamed onto, and cannot be anything
|
|
229
|
+
// but debris: an extraction that never finished, or a hand-made directory.
|
|
230
|
+
if (existsSync(directory)) discard(directory);
|
|
231
|
+
|
|
232
|
+
try {
|
|
233
|
+
renameSync(staging, directory);
|
|
234
|
+
} catch {
|
|
235
|
+
const winner = listFrames(directory);
|
|
236
|
+
discard(staging);
|
|
237
|
+
if (winner.length === 0) {
|
|
238
|
+
throw new FrameError(`Could not store the extracted frames for ${redact(source)}`);
|
|
239
|
+
}
|
|
240
|
+
return load(directory, winner, true, source);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
return load(directory, listFrames(directory), false, source);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Extracts frames with ffmpeg, or reuses a completed cache directory.
|
|
248
|
+
*
|
|
249
|
+
* ffmpeg writes into a staging directory that is renamed into place only on a
|
|
250
|
+
* clean exit with at least one frame, so no reader sees a half-written frame
|
|
251
|
+
* set. Staging directories from a killed run are swept once they are old
|
|
252
|
+
* enough to be unowned.
|
|
253
|
+
*
|
|
254
|
+
* `-nostdin` keeps ffmpeg away from the terminal's input stream. The `fps`
|
|
255
|
+
* filter normalizes variable-delay GIFs and constant-rate video alike, so one
|
|
256
|
+
* playback timer drives both.
|
|
257
|
+
*/
|
|
258
|
+
export function extractFrames(source: string, options: MediaOptions): FrameSource {
|
|
259
|
+
const directory = cacheDirectory(source, options);
|
|
260
|
+
const root = cacheRoot(options);
|
|
261
|
+
|
|
262
|
+
ownerOnly(root);
|
|
263
|
+
assertOwned(root);
|
|
264
|
+
sweepStaging(root);
|
|
265
|
+
|
|
266
|
+
const cachedFrames = listFrames(directory);
|
|
267
|
+
if (cachedFrames.length > 0) {
|
|
268
|
+
assertOwned(directory);
|
|
269
|
+
try {
|
|
270
|
+
return load(directory, cachedFrames, true, source);
|
|
271
|
+
} catch (error) {
|
|
272
|
+
// A cache entry that cannot be read would fail forever. Drop it and
|
|
273
|
+
// extract again.
|
|
274
|
+
if (!(error instanceof FrameError)) throw error;
|
|
275
|
+
discard(directory);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const staging = `${directory}.partial-${process.pid}-${Date.now()}`;
|
|
280
|
+
ownerOnly(staging);
|
|
281
|
+
|
|
282
|
+
const remote = isRemote(source);
|
|
283
|
+
const inputFlags = remote
|
|
284
|
+
? [
|
|
285
|
+
"-rw_timeout",
|
|
286
|
+
String(REMOTE_TIMEOUT_SECONDS * 1_000_000),
|
|
287
|
+
"-protocol_whitelist",
|
|
288
|
+
REMOTE_PROTOCOLS,
|
|
289
|
+
]
|
|
290
|
+
: [];
|
|
291
|
+
|
|
292
|
+
const result = spawnSync(
|
|
293
|
+
options.ffmpeg,
|
|
294
|
+
[
|
|
295
|
+
"-hide_banner",
|
|
296
|
+
"-nostdin",
|
|
297
|
+
"-loglevel",
|
|
298
|
+
"error",
|
|
299
|
+
...inputFlags,
|
|
300
|
+
"-i",
|
|
301
|
+
remote ? source : localPath(source),
|
|
302
|
+
"-an",
|
|
303
|
+
"-vf",
|
|
304
|
+
`fps=${options.fps},scale=${pixelsWide(options.width)}:-2:flags=lanczos`,
|
|
305
|
+
"-frames:v",
|
|
306
|
+
String(options.maxFrames),
|
|
307
|
+
"-y",
|
|
308
|
+
join(staging, framePattern()),
|
|
309
|
+
],
|
|
310
|
+
{ encoding: "utf8" },
|
|
311
|
+
);
|
|
312
|
+
|
|
313
|
+
if (result.error) {
|
|
314
|
+
discard(staging);
|
|
315
|
+
throw new FrameError(
|
|
316
|
+
`Could not run ${options.ffmpeg}: ${result.error.message}. Install ffmpeg and make sure it is on PATH.`,
|
|
317
|
+
);
|
|
318
|
+
}
|
|
319
|
+
if (result.status !== 0) {
|
|
320
|
+
const detail = scrub(
|
|
321
|
+
String(result.stderr ?? "")
|
|
322
|
+
.trim()
|
|
323
|
+
.split("\n")
|
|
324
|
+
.slice(-3)
|
|
325
|
+
.join(" "),
|
|
326
|
+
source,
|
|
327
|
+
);
|
|
328
|
+
discard(staging);
|
|
329
|
+
throw new FrameError(
|
|
330
|
+
`ffmpeg failed on ${redact(source)}: ${detail || `exit code ${result.status}`}`,
|
|
331
|
+
);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
const extracted = listFrames(staging);
|
|
335
|
+
const first = extracted[0];
|
|
336
|
+
if (first === undefined) {
|
|
337
|
+
discard(staging);
|
|
338
|
+
throw new FrameError(`ffmpeg produced no frames for ${redact(source)}`);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// Validate the output before it can become a cache entry, so a bad extraction
|
|
342
|
+
// is never published.
|
|
343
|
+
try {
|
|
344
|
+
pngSize(readFileSync(first), first);
|
|
345
|
+
} catch (error) {
|
|
346
|
+
discard(staging);
|
|
347
|
+
throw error;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
return publish(staging, directory, source);
|
|
351
|
+
}
|
package/src/names.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** ffmpeg output naming. Kept apart so tests and the extractor share one rule. */
|
|
2
|
+
|
|
3
|
+
const PATTERN = "f%05d.png";
|
|
4
|
+
const FILE = /^f\d{5}\.png$/;
|
|
5
|
+
|
|
6
|
+
export const framePattern = (): string => PATTERN;
|
|
7
|
+
|
|
8
|
+
export const frameFile = (entry: string): boolean => FILE.test(entry);
|
package/src/options.ts
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Plugin options are untyped on the host side (`Readonly<Record<string, any>>`),
|
|
3
|
+
* so every value is validated here and clamped to a range the panel can render.
|
|
4
|
+
* Invalid values fall back to the default instead of throwing: a bad option
|
|
5
|
+
* must not stop the TUI from starting.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export type PanelPresentation = "panel" | "fullscreen";
|
|
9
|
+
|
|
10
|
+
export interface MediaOptions {
|
|
11
|
+
/** File path or URL played on start. Without it the panel opens from a command. */
|
|
12
|
+
readonly source: string | undefined;
|
|
13
|
+
/** Frames played per second. */
|
|
14
|
+
readonly fps: number;
|
|
15
|
+
/** Panel width in terminal cells. Pixel width is four times this. */
|
|
16
|
+
readonly width: number;
|
|
17
|
+
/** Restart at the first frame after the last one. */
|
|
18
|
+
readonly loop: boolean;
|
|
19
|
+
/** Upper bound on extracted frames, so a long clip cannot exhaust memory. */
|
|
20
|
+
readonly maxFrames: number;
|
|
21
|
+
/** Open the panel on start when a source is known. */
|
|
22
|
+
readonly autoplay: boolean;
|
|
23
|
+
/** Remember the last clip played and reopen it on start. */
|
|
24
|
+
readonly remember: boolean;
|
|
25
|
+
/** ffmpeg binary name or absolute path. */
|
|
26
|
+
readonly ffmpeg: string;
|
|
27
|
+
/** Root directory for extracted frames. Defaults to the system temp directory. */
|
|
28
|
+
readonly cacheDirectory: string | undefined;
|
|
29
|
+
/** Initial presentation when the panel opens. */
|
|
30
|
+
readonly presentation: PanelPresentation;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export const DEFAULT_MEDIA_OPTIONS: MediaOptions = {
|
|
34
|
+
source: undefined,
|
|
35
|
+
fps: 12,
|
|
36
|
+
width: 44,
|
|
37
|
+
loop: true,
|
|
38
|
+
maxFrames: 300,
|
|
39
|
+
autoplay: true,
|
|
40
|
+
remember: false,
|
|
41
|
+
ffmpeg: "ffmpeg",
|
|
42
|
+
cacheDirectory: undefined,
|
|
43
|
+
presentation: "panel",
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/** Terminal cell geometry used to size the image. */
|
|
47
|
+
export const CELL_WIDTH_PX = 4;
|
|
48
|
+
export const CELL_HEIGHT_PX = 8;
|
|
49
|
+
|
|
50
|
+
export const pixelsWide = (widthCells: number): number => widthCells * CELL_WIDTH_PX;
|
|
51
|
+
|
|
52
|
+
export const cellsFor = (pixels: { readonly width: number; readonly height: number }) => ({
|
|
53
|
+
width: Math.max(1, Math.round(pixels.width / CELL_WIDTH_PX)),
|
|
54
|
+
height: Math.max(1, Math.round(pixels.height / CELL_HEIGHT_PX)),
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
const clamp = (value: number, min: number, max: number): number =>
|
|
58
|
+
Math.min(max, Math.max(min, Math.round(value)));
|
|
59
|
+
|
|
60
|
+
const numberOr = (value: unknown, fallback: number, min: number, max: number): number =>
|
|
61
|
+
typeof value === "number" && Number.isFinite(value) ? clamp(value, min, max) : fallback;
|
|
62
|
+
|
|
63
|
+
const textOr = (value: unknown, fallback: string): string => {
|
|
64
|
+
const text = typeof value === "string" ? value.trim() : "";
|
|
65
|
+
return text === "" ? fallback : text;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const booleanOr = (value: unknown, fallback: boolean): boolean =>
|
|
69
|
+
typeof value === "boolean" ? value : fallback;
|
|
70
|
+
|
|
71
|
+
export function normalizeOptions(raw: Readonly<Record<string, unknown>> | undefined): MediaOptions {
|
|
72
|
+
const input = raw ?? {};
|
|
73
|
+
const source =
|
|
74
|
+
typeof input.source === "string" && input.source.trim() !== ""
|
|
75
|
+
? input.source.trim()
|
|
76
|
+
: undefined;
|
|
77
|
+
const cacheDirectory =
|
|
78
|
+
typeof input.cacheDirectory === "string" && input.cacheDirectory.trim() !== ""
|
|
79
|
+
? input.cacheDirectory.trim()
|
|
80
|
+
: undefined;
|
|
81
|
+
|
|
82
|
+
return {
|
|
83
|
+
source,
|
|
84
|
+
fps: numberOr(input.fps, DEFAULT_MEDIA_OPTIONS.fps, 1, 30),
|
|
85
|
+
width: numberOr(input.width, DEFAULT_MEDIA_OPTIONS.width, 8, 120),
|
|
86
|
+
loop: booleanOr(input.loop, DEFAULT_MEDIA_OPTIONS.loop),
|
|
87
|
+
maxFrames: numberOr(input.maxFrames, DEFAULT_MEDIA_OPTIONS.maxFrames, 1, 2000),
|
|
88
|
+
autoplay: booleanOr(input.autoplay, DEFAULT_MEDIA_OPTIONS.autoplay),
|
|
89
|
+
remember: booleanOr(input.remember, DEFAULT_MEDIA_OPTIONS.remember),
|
|
90
|
+
ffmpeg: textOr(input.ffmpeg, DEFAULT_MEDIA_OPTIONS.ffmpeg),
|
|
91
|
+
cacheDirectory,
|
|
92
|
+
presentation: input.presentation === "fullscreen" ? "fullscreen" : "panel",
|
|
93
|
+
};
|
|
94
|
+
}
|
package/src/tui.tsx
ADDED
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import { Plugin, usePlugin } from "@opencode/plugin/tui";
|
|
2
|
+
import type { PanelInput } from "@opencode/plugin/tui/context";
|
|
3
|
+
import { createSignal, onCleanup, Show } from "solid-js";
|
|
4
|
+
import { extractFrames, redact } from "./frames";
|
|
5
|
+
import { cellsFor, normalizeOptions } from "./options";
|
|
6
|
+
|
|
7
|
+
/** Opening name for this plugin's panel. Shared selection value, not a registry key. */
|
|
8
|
+
const PANEL = "media.panel";
|
|
9
|
+
const STORAGE = "media";
|
|
10
|
+
|
|
11
|
+
// File names and ffmpeg messages reach the renderer as text. Replace control
|
|
12
|
+
// characters so neither can inject a terminal escape sequence.
|
|
13
|
+
const clean = (text: string): string => {
|
|
14
|
+
let out = "";
|
|
15
|
+
for (const char of text) {
|
|
16
|
+
const code = char.codePointAt(0) ?? 0;
|
|
17
|
+
out += code < 0x20 || (code >= 0x7f && code <= 0x9f) ? " " : char;
|
|
18
|
+
}
|
|
19
|
+
return out;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
interface Clip {
|
|
23
|
+
readonly id: number;
|
|
24
|
+
readonly source: string;
|
|
25
|
+
readonly frames: readonly string[];
|
|
26
|
+
readonly width: number;
|
|
27
|
+
readonly height: number;
|
|
28
|
+
readonly fps: number;
|
|
29
|
+
readonly loop: boolean;
|
|
30
|
+
readonly truncated: boolean;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function MediaPanel(props: {
|
|
34
|
+
readonly panel: PanelInput;
|
|
35
|
+
readonly clip: Clip;
|
|
36
|
+
readonly index: number;
|
|
37
|
+
readonly paused: boolean;
|
|
38
|
+
readonly togglePause: () => void;
|
|
39
|
+
readonly onDispose?: (id: number) => void;
|
|
40
|
+
}) {
|
|
41
|
+
const context = usePlugin();
|
|
42
|
+
|
|
43
|
+
// The host disposes panel contributions when the panel closes, so this is
|
|
44
|
+
// where playback stops: a closed panel must not keep a timer alive. The clip
|
|
45
|
+
// id keeps a late cleanup from stopping the timer of the clip that replaced
|
|
46
|
+
// it.
|
|
47
|
+
onCleanup(() => props.onDispose?.(props.clip.id));
|
|
48
|
+
|
|
49
|
+
context.keymap.layer(() => ({
|
|
50
|
+
commands: [
|
|
51
|
+
{ id: "media.panel.close", bind: "escape", run: () => props.panel.close() },
|
|
52
|
+
{ id: "media.panel.fullscreen", bind: "f", run: () => props.panel.toggleFullscreen() },
|
|
53
|
+
{ id: "media.panel.pause", bind: "space", run: () => props.togglePause() },
|
|
54
|
+
],
|
|
55
|
+
}));
|
|
56
|
+
|
|
57
|
+
const cells = () => cellsFor({ width: props.clip.width, height: props.clip.height });
|
|
58
|
+
const total = () => props.clip.frames.length;
|
|
59
|
+
const position = () => Math.min(props.index + 1, total());
|
|
60
|
+
// The title line is visible in screenshots, so it shows the redacted source:
|
|
61
|
+
// a signed query string must not be drawn.
|
|
62
|
+
const name = () => clean(redact(props.clip.source).split("/").pop() ?? props.clip.source);
|
|
63
|
+
const frame = () => props.clip.frames[props.index] ?? props.clip.frames[0] ?? "";
|
|
64
|
+
const count = () =>
|
|
65
|
+
props.clip.truncated ? `${position()}/${total()} capped` : `${position()}/${total()}`;
|
|
66
|
+
|
|
67
|
+
return (
|
|
68
|
+
<box flexDirection="column" alignItems="center">
|
|
69
|
+
<text>
|
|
70
|
+
{name()} · {props.paused ? "paused" : `${props.clip.fps} fps`} · {count()} · space pause · f
|
|
71
|
+
fullscreen · esc close
|
|
72
|
+
</text>
|
|
73
|
+
<image
|
|
74
|
+
source={frame()}
|
|
75
|
+
fit="fit"
|
|
76
|
+
protocol="auto"
|
|
77
|
+
width={cells().width}
|
|
78
|
+
height={cells().height}
|
|
79
|
+
/>
|
|
80
|
+
</box>
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export default Plugin.define({
|
|
85
|
+
id: PANEL,
|
|
86
|
+
setup(context) {
|
|
87
|
+
const options = normalizeOptions(context.options);
|
|
88
|
+
const [saved, save] = context.storage.store(STORAGE, { initial: { source: "" } });
|
|
89
|
+
const [clip, setClip] = createSignal<Clip>();
|
|
90
|
+
const [index, setIndex] = createSignal(0);
|
|
91
|
+
const [paused, setPaused] = createSignal(false);
|
|
92
|
+
const togglePause = () => setPaused((value) => !value);
|
|
93
|
+
|
|
94
|
+
let ticker: ReturnType<typeof setInterval> | undefined;
|
|
95
|
+
let generation = 0;
|
|
96
|
+
|
|
97
|
+
function stopTicker() {
|
|
98
|
+
if (ticker) clearInterval(ticker);
|
|
99
|
+
ticker = undefined;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Stops playback only if the clip that asked for it is still the current one. */
|
|
103
|
+
function stopClip(id: number) {
|
|
104
|
+
if (id === generation) stopTicker();
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function play(next: Clip) {
|
|
108
|
+
stopTicker();
|
|
109
|
+
generation = next.id;
|
|
110
|
+
setClip(next);
|
|
111
|
+
setIndex(0);
|
|
112
|
+
setPaused(false);
|
|
113
|
+
ticker = setInterval(() => {
|
|
114
|
+
if (paused()) return;
|
|
115
|
+
setIndex((current) => {
|
|
116
|
+
const following = current + 1;
|
|
117
|
+
if (following < next.frames.length) return following;
|
|
118
|
+
return next.loop ? 0 : current;
|
|
119
|
+
});
|
|
120
|
+
}, 1000 / next.fps);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function open(source: string): boolean {
|
|
124
|
+
let extracted: ReturnType<typeof extractFrames>;
|
|
125
|
+
try {
|
|
126
|
+
extracted = extractFrames(source, options);
|
|
127
|
+
} catch (error) {
|
|
128
|
+
context.ui.toast.show({
|
|
129
|
+
variant: "error",
|
|
130
|
+
title: "Media panel",
|
|
131
|
+
message: clean(error instanceof Error ? error.message : String(error)),
|
|
132
|
+
duration: 8000,
|
|
133
|
+
});
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
play({
|
|
138
|
+
id: generation + 1,
|
|
139
|
+
source,
|
|
140
|
+
frames: extracted.frames,
|
|
141
|
+
width: extracted.width,
|
|
142
|
+
height: extracted.height,
|
|
143
|
+
fps: options.fps,
|
|
144
|
+
loop: options.loop,
|
|
145
|
+
truncated: extracted.truncated,
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
const opened = context.ui.panel.open(PANEL, { presentation: options.presentation });
|
|
149
|
+
if (!opened) {
|
|
150
|
+
stopTicker();
|
|
151
|
+
context.ui.toast.show({
|
|
152
|
+
variant: "warning",
|
|
153
|
+
title: "Media panel",
|
|
154
|
+
message: "Open a session first: the panel only exists inside one.",
|
|
155
|
+
});
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (options.remember) {
|
|
160
|
+
void save((draft) => {
|
|
161
|
+
draft.source = source;
|
|
162
|
+
}).catch(() => {
|
|
163
|
+
context.ui.toast.show({
|
|
164
|
+
variant: "warning",
|
|
165
|
+
title: "Media panel",
|
|
166
|
+
message: "Could not save the last clip.",
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
return true;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
context.keymap.layer(() => ({
|
|
174
|
+
mode: "global",
|
|
175
|
+
commands: [
|
|
176
|
+
{
|
|
177
|
+
id: "media.open",
|
|
178
|
+
title: "Play a GIF or video in a panel",
|
|
179
|
+
description: "Extracts frames with ffmpeg and loops them in the session panel.",
|
|
180
|
+
group: "Media",
|
|
181
|
+
palette: true,
|
|
182
|
+
slash: { name: "media", aliases: ["anim"], arguments: true },
|
|
183
|
+
run: (input) => {
|
|
184
|
+
const given = input?.trim() ?? "";
|
|
185
|
+
const source = given === "" ? options.source : given;
|
|
186
|
+
if (source === undefined) {
|
|
187
|
+
void context.ui.dialog.alert({
|
|
188
|
+
title: "Media panel",
|
|
189
|
+
message: "Pass a file path or URL, for example: /media /home/user/clip.mp4",
|
|
190
|
+
});
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
open(source);
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
{
|
|
197
|
+
id: "media.close",
|
|
198
|
+
title: "Close the media panel",
|
|
199
|
+
group: "Media",
|
|
200
|
+
palette: true,
|
|
201
|
+
run: () => context.ui.panel.close(),
|
|
202
|
+
},
|
|
203
|
+
],
|
|
204
|
+
}));
|
|
205
|
+
|
|
206
|
+
const unregister = context.ui.slot({
|
|
207
|
+
append: "session.panel",
|
|
208
|
+
render: (panel) => (
|
|
209
|
+
<Show when={panel.name === PANEL ? clip() : undefined} keyed>
|
|
210
|
+
{(active: Clip) => (
|
|
211
|
+
<MediaPanel
|
|
212
|
+
panel={panel}
|
|
213
|
+
clip={active}
|
|
214
|
+
index={index()}
|
|
215
|
+
paused={paused()}
|
|
216
|
+
togglePause={togglePause}
|
|
217
|
+
onDispose={stopClip}
|
|
218
|
+
/>
|
|
219
|
+
)}
|
|
220
|
+
</Show>
|
|
221
|
+
),
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
const remembered = options.remember ? saved.source.trim() : "";
|
|
225
|
+
const initial = options.source ?? (remembered === "" ? undefined : remembered);
|
|
226
|
+
let autoplay: ReturnType<typeof setInterval> | undefined;
|
|
227
|
+
|
|
228
|
+
if (options.autoplay && initial !== undefined) {
|
|
229
|
+
let attempts = 0;
|
|
230
|
+
autoplay = setInterval(() => {
|
|
231
|
+
attempts += 1;
|
|
232
|
+
if (context.ui.router.current().type === "session") {
|
|
233
|
+
if (autoplay) clearInterval(autoplay);
|
|
234
|
+
autoplay = undefined;
|
|
235
|
+
open(initial);
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
// Give up after two minutes: no session was opened.
|
|
239
|
+
if (attempts > 240 && autoplay) {
|
|
240
|
+
clearInterval(autoplay);
|
|
241
|
+
autoplay = undefined;
|
|
242
|
+
}
|
|
243
|
+
}, 500);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
return () => {
|
|
247
|
+
stopTicker();
|
|
248
|
+
if (autoplay) clearInterval(autoplay);
|
|
249
|
+
unregister();
|
|
250
|
+
};
|
|
251
|
+
},
|
|
252
|
+
});
|