The open source AI video agent: turns any coding agent into a video agent, with an editor to change anything it makes.
Exports faster, lighter on disk, true to the page's colors and safe on Windows; DeepSeek Harness; volume in dB (#163)
riverisbuilding0cc33fe34f0e
diff --git a/.changeset/bilingual-burn-once.md b/.changeset/bilingual-burn-once.mdnew file mode 100644index 0000000..0975a4b--- /dev/null+++ b/.changeset/bilingual-burn-once.md@@ -0,0 +1,5 @@+---+"openfilm": patch+---++Export › Burn in subtitles: bilingual subtitles show the original and its translation once each, as in the editor. The translation was drawn twice.diff --git a/.changeset/deepseek-harness-agent.md b/.changeset/deepseek-harness-agent.mdnew file mode 100644index 0000000..4d20690--- /dev/null+++ b/.changeset/deepseek-harness-agent.md@@ -0,0 +1,5 @@+---+'@openfilm/desktop': patch+---++Desktop: DeepSeek Harness is one of the agents the chat can connect, its `dsh` program over ACP like the others, found inside its desktop app too. The chat's menu lists models an agent offers in groups, each with its group's name.diff --git a/.changeset/draft-pages-as-drawn.md b/.changeset/draft-pages-as-drawn.mdnew file mode 100644index 0000000..8aee3ca--- /dev/null+++ b/.changeset/draft-pages-as-drawn.md@@ -0,0 +1,5 @@+---+"openfilm": patch+---++Editing-project drafts are faster and need almost no spare disk space. Each page's transparency is read in every frame as the page is drawn. An opaque page is written straight to H.264 (or ProRes 422 HQ), with no lossless copy first and no second pass. A page with any transparent pixel is drawn again at once in the same browser tab, as lossless PNG (or ProRes 4444), exactly as before. A draft of a music video whose pages were held as gigabytes of PNG while it was made now needs no more room than the draft itself.diff --git a/.changeset/drafts-where-the-app-looks.md b/.changeset/drafts-where-the-app-looks.mdnew file mode 100644index 0000000..ede0138--- /dev/null+++ b/.changeset/drafts-where-the-app-looks.md@@ -0,0 +1,5 @@+---+"openfilm": patch+---++Export › Editing project › 剪映 or CapCut: a draft goes to the drafts folder the app's own Settings › Drafts location names, so it is on the app's home screen even when that folder was moved (it went to the default folder, where the app no longer looks).diff --git a/.changeset/editing-project-browser-pool.md b/.changeset/editing-project-browser-pool.mdnew file mode 100644index 0000000..d5ac18e--- /dev/null+++ b/.changeset/editing-project-browser-pool.md@@ -0,0 +1,10 @@+---+"openfilm": patch+---++Export › Editing project starts its browsers once for all the pages and keeps them steady.+- One process draws every page with the same browsers. Each page opens its own tab and closes it after.+- A browser that crashes or stops answering is replaced at once, and its page is drawn again in new browsers. A page's own error still stops the export and says why.+- A browser is started again after many pages, so memory stays flat.+- Cancel still stops everything and leaves nothing behind.+- A transparent page now comes out the same to the last pixel however many browsers share it. Before, the first two frames each browser drew came out a level or two off.diff --git a/.changeset/editing-project-faster.md b/.changeset/editing-project-faster.mdnew file mode 100644index 0000000..292bd24--- /dev/null+++ b/.changeset/editing-project-faster.md@@ -0,0 +1,5 @@+---+"openfilm": patch+---++Export › Editing project is two to four times faster, with the same files. A 剪映 or CapCut draft's pages are drawn as a quick lossless PNG and written once at the end (H.264, ProRes, or PNG as before): ffmpeg's own PNG settings took more processor time than drawing the page. Pages of every editing project are drawn several at once with the browsers the export already uses, each page's check and re-encode running while the next ones draw. On an M5 Mac a 50-second film of ten full-frame pages went from 153–190 s to about 50 s, and a 95-second music video from 334 s to 145 s.diff --git a/.changeset/editor-export-alpha-codec.md b/.changeset/editor-export-alpha-codec.mdnew file mode 100644index 0000000..aefb51c--- /dev/null+++ b/.changeset/editor-export-alpha-codec.md@@ -0,0 +1,5 @@+---+"openfilm": patch+---++Export › Editing project: a choice of how the pages are written. A 剪映 or CapCut draft is automatic by default: each page is drawn with its transparency, as Studio shows it, and every frame is read; a page with no clear pixel in any frame comes as H.264 (the graphics chip's when there is one), and one with any transparency as lossless PNG, each picture kept once for as long as it shows. A one-minute film of full-frame pages went from 2.5 GB to under 20 MB. All PNG, or ProRes (422 HQ for an opaque page, 4444 for one with transparency), can be picked instead. Premiere Pro and DaVinci Resolve keep ProRes 4444 by default, with PNG to pick; Final Cut Pro takes ProRes 4444.diff --git a/.changeset/export-colors-bt709.md b/.changeset/export-colors-bt709.mdnew file mode 100644index 0000000..da46284--- /dev/null+++ b/.changeset/export-colors-bt709.md@@ -0,0 +1,5 @@+---+"openfilm": patch+---++Exports keep the page's colors in every codec: ProRes, VP9, HEVC and the editing-project pages are converted with BT.709 and tagged so (red came out up to 25 levels off, green up to 39, in Apple's apps), with the graphics chip or without it. HEVC with alpha opens in Apple's apps again: it was copied after encoding, which dropped its alpha layer.diff --git a/.changeset/export-disk-space.md b/.changeset/export-disk-space.mdnew file mode 100644index 0000000..d83a3f2--- /dev/null+++ b/.changeset/export-disk-space.md@@ -0,0 +1,5 @@+---+"openfilm": patch+---++Exports need far less free disk space while they run. A page's frames were on the disk three times while its file was made; now they are there twice at most, and a page drawn by one browser is moved into place instead of copied. This matters most for editing projects, whose pages are drawn as lossless PNG first, so a long page is gigabytes. An export that fills the disk now says so plainly, in your language, instead of showing ffmpeg's log.diff --git a/.changeset/export-pool-windows.md b/.changeset/export-pool-windows.mdnew file mode 100644index 0000000..fd9d12a--- /dev/null+++ b/.changeset/export-pool-windows.md@@ -0,0 +1,5 @@+---+"openfilm": patch+---++An editing project's browser pool and its footage copies stop their whole process tree on Windows when they are ended, and a copy that a virus scanner holds for a moment as it is written is tried again instead of counting as not made.diff --git a/.changeset/export-quit-cleanup.md b/.changeset/export-quit-cleanup.mdnew file mode 100644index 0000000..7ab9a71--- /dev/null+++ b/.changeset/export-quit-cleanup.md@@ -0,0 +1,5 @@+---+"openfilm": patch+---++Quitting Studio while an export runs no longer leaves half-made files behind. Studio now waits a few seconds for the export to stop and clean up after itself. If Studio was ended outright (forced to quit, a crash, the computer shutting down), the next export cleans up what was left: hidden half-made files beside your exports and drafts, and scratch in the project's cache, which could be gigabytes.diff --git a/.changeset/export-tray-stays.md b/.changeset/export-tray-stays.mdnew file mode 100644index 0000000..3e0461c--- /dev/null+++ b/.changeset/export-tray-stays.md@@ -0,0 +1,5 @@+---+"openfilm": patch+---++The export tray stays until it is closed: a finished export's card no longer goes by itself after a few seconds, and it is still there when the project or the page is opened again while Studio runs, so its file is always one click away. A closed card stays closed.diff --git a/.changeset/export-windows-safety.md b/.changeset/export-windows-safety.mdnew file mode 100644index 0000000..7b550e8--- /dev/null+++ b/.changeset/export-windows-safety.md@@ -0,0 +1,5 @@+---+"openfilm": patch+---++Exports are safer on Windows and when something goes wrong: a file is written under a hidden name until it is whole and then renamed, so a stop or a crash never leaves a broken file that looks made; a rename that a virus scanner or the editor watching the folder holds for a moment is tried again; cleaning up after an export that is made never fails it (it used to delete the finished files); a cancel ends the export's whole process tree; a long page's PNG keeps its pictures however many there are; the graphics chip running out of memory or sessions falls back to the processor's encoder; names ending in a dot or a space, or named like a Windows device, are written so Windows can open them; and a draft's files are named with the system's own separators.diff --git a/.changeset/ffmpeg-8-and-9.md b/.changeset/ffmpeg-8-and-9.mdnew file mode 100644index 0000000..21d54b2--- /dev/null+++ b/.changeset/ffmpeg-8-and-9.md@@ -0,0 +1,5 @@+---+"@openfilm/desktop": patch+---++The app's own ffmpeg is FFmpeg 8.1.2 on a Mac and 9.0.2 on Windows (was 6.0 and 6.1.1). FFmpeg 6 wrote ProRes 4444 whose transparency editors read as opaque, so rendered pages came into 剪映 and other editors on black.diff --git a/.changeset/footage-the-browser-cannot-read.md b/.changeset/footage-the-browser-cannot-read.mdnew file mode 100644index 0000000..75cc5bf--- /dev/null+++ b/.changeset/footage-the-browser-cannot-read.md@@ -0,0 +1,5 @@+---+"openfilm": patch+---++Footage the browser cannot play (ProRes, and other codecs it has no decoder for) placed whole, without a trim, plays and exports: its length and picture size come from ffprobe when the browser cannot read them. The film stopped with "no length to trim against" in Studio's preview, in every export and in `openfilm render`.diff --git a/.changeset/mono-sound-level.md b/.changeset/mono-sound-level.mdnew file mode 100644index 0000000..5c9a67f--- /dev/null+++ b/.changeset/mono-sound-level.md@@ -0,0 +1,5 @@+---+"openfilm": patch+---++Export: a mono sound, such as a voice recording, is as loud in the export as in the editor. It was 3 dB quieter.diff --git a/.changeset/paused-preview-exact-frame.md b/.changeset/paused-preview-exact-frame.mdnew file mode 100644index 0000000..61b6c0f--- /dev/null+++ b/.changeset/paused-preview-exact-frame.md@@ -0,0 +1,5 @@+---+"openfilm": patch+---++The paused preview shows the exact frame of a video, as an export draws it. The browser sometimes decodes a frame and never puts it on screen, so the picture could stay a few frames behind after you stepped or stopped. Studio now checks the frame on screen and seeks again if it is not there yet. Scrubbing stays as fast as before.diff --git a/.changeset/playback-menu-no-subtitles.md b/.changeset/playback-menu-no-subtitles.mdnew file mode 100644index 0000000..6adc0dd--- /dev/null+++ b/.changeset/playback-menu-no-subtitles.md@@ -0,0 +1,5 @@+---+"openfilm": patch+---++Viewer › Playback settings no longer has a subtitles switch. Subtitles are shown or hidden only with the CC button, in full screen too.diff --git a/.changeset/seek-copy-once.md b/.changeset/seek-copy-once.mdnew file mode 100644index 0000000..369b4a6--- /dev/null+++ b/.changeset/seek-copy-once.md@@ -0,0 +1,5 @@+---+"openfilm": patch+---++Exports drawing several pages at once make the playable copy of shared footage once. ProRes, HEVC a browser can't play, and slow-seeking footage get that copy. Two pages of one source now wait for the same copy, instead of both writing one file, which could leave a page drawn from a half-written copy or, on Windows, from footage the browser can't play.diff --git a/.changeset/text-fill-color.md b/.changeset/text-fill-color.mdnew file mode 100644index 0000000..e63702f--- /dev/null+++ b/.changeset/text-fill-color.md@@ -0,0 +1,5 @@+---+"openfilm": patch+---++The inspector's text color now changes words whose page gives them a fill of their own (`-webkit-text-fill-color`, often used for gold or gradient words). It shows the color the words are drawn in, and a color you pick takes over that fill, so the words are drawn in it.diff --git a/.changeset/volume-in-db.md b/.changeset/volume-in-db.mdnew file mode 100644index 0000000..5f35577--- /dev/null+++ b/.changeset/volume-in-db.md@@ -0,0 +1,5 @@+---+"openfilm": patch+---++Inspector › Volume is in dB, from silent to +14 dB (500%): 0 dB is the file as it is, and each step sounds as big as the next, quiet or loud.diff --git a/.changeset/volume-to-500.md b/.changeset/volume-to-500.mdnew file mode 100644index 0000000..8a976f5--- /dev/null+++ b/.changeset/volume-to-500.md@@ -0,0 +1,5 @@+---+"openfilm": patch+---++Inspector › Volume goes up to 500%, and Normalize loudness can raise a clip that far, so a quiet voice recording can be as loud as the rest of the film. It stopped at 200%.diff --git a/apps/desktop/chat/src/components/AgentIcons.tsx b/apps/desktop/chat/src/components/AgentIcons.tsxindex af9bcbd..744fb63 100644--- a/apps/desktop/chat/src/components/AgentIcons.tsx+++ b/apps/desktop/chat/src/components/AgentIcons.tsx@@ -2,7 +2,7 @@* The official marks of the coding agents.** Paths come unchanged from simple-icons (the brands' own published SVGs; the data is CC0). Those not in- * simple-icons come from the vendor: Devin's favicon.svg, unchanged; CodeBuddy, Qwen and Kimi's monochrome marks+ * simple-icons come from the vendor: Devin's favicon.svg, unchanged; CodeBuddy, Qwen, Kimi and DeepSeek's monochrome marks* from Lobe Icons (https://github.com/lobehub/lobe-icons, MIT), unchanged. They are other people's trademarks, so* none is redrawn.*@@ -11,7 +11,7 @@* used as our own marks.*/-export type AgentIconId = 'claude' | 'codex' | 'cursor' | 'copilot' | 'opencode' | 'codebuddy' | 'qwen' | 'kimi' | 'minimax' | 'devin';+export type AgentIconId = 'claude' | 'codex' | 'cursor' | 'copilot' | 'opencode' | 'codebuddy' | 'qwen' | 'kimi' | 'minimax' | 'deepseek' | 'devin';/** Codex is an OpenAI product and uses the OpenAI mark. */const PATHS: Record<AgentIconId, string> = {@@ -24,6 +24,7 @@ const PATHS: Record<AgentIconId, string> = {qwen: 'M12.604 1.34c.393.69.784 1.382 1.174 2.075a.18.18 0 00.157.091h5.552c.174 0 .322.11.446.327l1.454 2.57c.19.337.24.478.024.837-.26.43-.513.864-.76 1.3l-.367.658c-.106.196-.223.28-.04.512l2.652 4.637c.172.301.111.494-.043.77-.437.785-.882 1.564-1.335 2.34-.159.272-.352.375-.68.37-.777-.016-1.552-.01-2.327.016a.099.099 0 00-.081.05 575.097 575.097 0 01-2.705 4.74c-.169.293-.38.363-.725.364-.997.003-2.002.004-3.017.002a.537.537 0 01-.465-.271l-1.335-2.323a.09.09 0 00-.083-.049H4.982c-.285.03-.553-.001-.805-.092l-1.603-2.77a.543.543 0 01-.002-.54l1.207-2.12a.198.198 0 000-.197 550.951 550.951 0 01-1.875-3.272l-.79-1.395c-.16-.31-.173-.496.095-.965.465-.813.927-1.625 1.387-2.436.132-.234.304-.334.584-.335a338.3 338.3 0 012.589-.001.124.124 0 00.107-.063l2.806-4.895a.488.488 0 01.422-.246c.524-.001 1.053 0 1.583-.006L11.704 1c.341-.003.724.032.9.34zm-3.432.403a.06.06 0 00-.052.03L6.254 6.788a.157.157 0 01-.135.078H3.253c-.056 0-.07.025-.041.074l5.81 10.156c.025.042.013.062-.034.063l-2.795.015a.218.218 0 00-.2.116l-1.32 2.31c-.044.078-.021.118.068.118l5.716.008c.046 0 .08.02.104.061l1.403 2.454c.046.081.092.082.139 0l5.006-8.76.783-1.382a.055.055 0 01.096 0l1.424 2.53a.122.122 0 00.107.062l2.763-.02a.04.04 0 00.035-.02.041.041 0 000-.04l-2.9-5.086a.108.108 0 010-.113l.293-.507 1.12-1.977c.024-.041.012-.062-.035-.062H9.2c-.059 0-.073-.026-.043-.077l1.434-2.505a.107.107 0 000-.114L9.225 1.774a.06.06 0 00-.053-.031zm6.29 8.02c.046 0 .058.02.034.06l-.832 1.465-2.613 4.585a.056.056 0 01-.05.029.058.058 0 01-.05-.029L8.498 9.841c-.02-.034-.01-.052.028-.054l.216-.012 6.722-.012z',kimi: 'M21.846 0a1.923 1.923 0 110 3.846H20.15a.226.226 0 01-.227-.226V1.923C19.923.861 20.784 0 21.846 0zM11.065 11.199l7.257-7.2c.137-.136.06-.41-.116-.41H14.3a.164.164 0 00-.117.051l-7.82 7.756c-.122.12-.302.013-.302-.179V3.82c0-.127-.083-.23-.185-.23H3.186c-.103 0-.186.103-.186.23V19.77c0 .128.083.23.186.23h2.69c.103 0 .186-.102.186-.23v-3.25c0-.069.025-.135.069-.178l2.424-2.406a.158.158 0 01.205-.023l6.484 4.772a7.677 7.677 0 003.453 1.283c.108.012.2-.095.2-.23v-3.06c0-.117-.07-.212-.164-.227a5.028 5.028 0 01-2.027-.807l-5.613-4.064c-.117-.078-.132-.279-.028-.381z',minimax: 'M11.43 3.92a.86.86 0 1 0-1.718 0v14.236a1.999 1.999 0 0 1-3.997 0V9.022a.86.86 0 1 0-1.718 0v3.87a1.999 1.999 0 0 1-3.997 0V11.49a.57.57 0 0 1 1.139 0v1.404a.86.86 0 0 0 1.719 0V9.022a1.999 1.999 0 0 1 3.997 0v9.134a.86.86 0 0 0 1.719 0V3.92a1.998 1.998 0 1 1 3.996 0v11.788a.57.57 0 1 1-1.139 0zm10.572 3.105a2 2 0 0 0-1.999 1.997v7.63a.86.86 0 0 1-1.718 0V3.923a1.999 1.999 0 0 0-3.997 0v16.16a.86.86 0 0 1-1.719 0V18.08a.57.57 0 1 0-1.138 0v2a1.998 1.998 0 0 0 3.996 0V3.92a.86.86 0 0 1 1.719 0v12.73a1.999 1.999 0 0 0 3.996 0V9.023a.86.86 0 1 1 1.72 0v6.686a.57.57 0 0 0 1.138 0V9.022a2 2 0 0 0-1.998-1.997',+ deepseek: 'M23.748 4.482c-.254-.124-.364.113-.512.234-.051.039-.094.09-.137.136-.372.397-.806.657-1.373.626-.829-.046-1.537.214-2.163.848-.133-.782-.575-1.248-1.247-1.548-.352-.156-.708-.311-.955-.65-.172-.241-.219-.51-.305-.774-.055-.16-.11-.323-.293-.35-.2-.031-.278.136-.356.276-.313.572-.434 1.202-.422 1.84.027 1.436.633 2.58 1.838 3.393.137.093.172.187.129.323-.082.28-.18.552-.266.833-.055.179-.137.217-.329.14a5.526 5.526 0 01-1.736-1.18c-.857-.828-1.631-1.742-2.597-2.458a11.365 11.365 0 00-.689-.471c-.985-.957.13-1.743.388-1.836.27-.098.093-.432-.779-.428-.872.004-1.67.295-2.687.684a3.055 3.055 0 01-.465.137 9.597 9.597 0 00-2.883-.102c-1.885.21-3.39 1.102-4.497 2.623C.082 8.606-.231 10.684.152 12.85c.403 2.284 1.569 4.175 3.36 5.653 1.858 1.533 3.997 2.284 6.438 2.14 1.482-.085 3.133-.284 4.994-1.86.47.234.962.327 1.78.397.63.059 1.236-.03 1.705-.128.735-.156.684-.837.419-.961-2.155-1.004-1.682-.595-2.113-.926 1.096-1.296 2.746-2.642 3.392-7.003.05-.347.007-.565 0-.845-.004-.17.035-.237.23-.256a4.173 4.173 0 001.545-.475c1.396-.763 1.96-2.015 2.093-3.517.02-.23-.004-.467-.247-.588zM11.581 18c-2.089-1.642-3.102-2.183-3.52-2.16-.392.024-.321.471-.235.763.09.288.207.486.371.739.114.167.192.416-.113.603-.673.416-1.842-.14-1.897-.167-1.361-.802-2.5-1.86-3.301-3.307-.774-1.393-1.224-2.887-1.298-4.482-.02-.386.093-.522.477-.592a4.696 4.696 0 011.529-.039c2.132.312 3.946 1.265 5.468 2.774.868.86 1.525 1.887 2.202 2.891.72 1.066 1.494 2.082 2.48 2.914.348.292.625.514.891.677-.802.09-2.14.11-3.054-.614zm1-6.44a.306.306 0 01.415-.287.302.302 0 01.2.288.306.306 0 01-.31.307.303.303 0 01-.304-.308zm3.11 1.596c-.2.081-.399.151-.59.16a1.245 1.245 0 01-.798-.254c-.274-.23-.47-.358-.552-.758a1.73 1.73 0 01.016-.588c.07-.327-.008-.537-.239-.727-.187-.156-.426-.199-.688-.199a.559.559 0 01-.254-.078c-.11-.054-.2-.19-.114-.358.028-.054.16-.186.192-.21.356-.202.767-.136 1.146.016.352.144.618.408 1.001.782.391.451.462.576.685.914.176.265.336.537.445.848.067.195-.019.354-.25.452z',devin: 'M70 159.333V91.3471C70 88.3592 71.594 85.5983 74.1816 84.1044L133.043 50.1205C135.631 48.6265 138.819 48.6265 141.407 50.1205L200.269 84.1044C202.856 85.5983 204.45 88.3592 204.45 91.3471V126.068C204.708 137.606 210.806 148.734 221.531 154.926C232.256 161.117 244.942 160.834 255.063 155.289L285.132 137.929C287.719 136.435 290.907 136.435 293.495 137.929L352.357 171.913C354.944 173.406 356.538 176.167 356.538 179.155V247.123C356.538 250.111 354.944 252.872 352.357 254.366L293.495 288.35C290.907 289.844 287.719 289.844 285.132 288.35L255.306 271.13C245.146 265.456 232.344 265.117 221.534 271.358C210.809 277.55 204.711 288.678 204.453 300.215V334.926C204.453 337.914 202.859 340.675 200.271 342.169L141.41 376.153C138.822 377.647 135.634 377.647 133.046 376.153L74.1845 342.169C71.5969 340.675 70.0028 337.914 70.0028 334.926V266.959C70.0029 263.971 71.5969 261.21 74.1845 259.716L133.046 225.732C135.634 224.238 138.822 224.238 141.41 225.732L171.547 243.132C181.656 248.638 194.306 248.906 205.005 242.729C215.815 236.488 221.922 225.231 222.088 213.595C221.83 202.057 215.732 189.737 205.008 183.545C194.283 177.353 181.597 177.636 171.476 183.181L141.269 200.72C138.67 202.229 135.461 202.228 132.864 200.716L74.1576 166.562C71.5835 165.065 70 162.311 70 159.333Z',};@@ -31,7 +32,7 @@ const PATHS: Record<AgentIconId, string> = {const VIEWBOX: Partial<Record<AgentIconId, string>> = { devin: '48 48 330 330' };/** The Lobe Icons marks fill even-odd (so their cut-outs stay empty). */-const EVEN_ODD = new Set<AgentIconId>(['codebuddy', 'qwen', 'kimi']);+const EVEN_ODD = new Set<AgentIconId>(['codebuddy', 'qwen', 'kimi', 'deepseek']);/** Whether this agent has its own mark (those without, such as OpenFilm's own agent, use the OpenFilm mark). */export const hasAgentIcon = (id: string): id is AgentIconId => Object.hasOwn(PATHS, id);diff --git a/apps/desktop/chat/src/lib/desktop-bridge.ts b/apps/desktop/chat/src/lib/desktop-bridge.tsindex 4da7b14..acb5d4d 100644--- a/apps/desktop/chat/src/lib/desktop-bridge.ts+++ b/apps/desktop/chat/src/lib/desktop-bridge.ts@@ -13,7 +13,7 @@ import type { Balance } from './balance';export type DesktopAgentId = 'claude' | 'codex' | 'cursor' | 'copilot' | 'opencode' | 'codebuddy' | 'devin';/** The chat's agents: the person's own (Codex, Claude Code, Qwen Code, …), and the app's own on their own key. */-export type LocalAgentId = 'claude' | 'codex' | 'copilot' | 'cursor' | 'opencode' | 'codebuddy' | 'qwen' | 'kimi' | 'minimax' | 'byok';+export type LocalAgentId = 'claude' | 'codex' | 'copilot' | 'cursor' | 'opencode' | 'codebuddy' | 'qwen' | 'kimi' | 'minimax' | 'deepseek' | 'byok';/** One agent as the app's roster has it (apps/desktop/src/agent-roster.mjs; Studio's lib/host.ts HostAgent). */export interface RosterAgent {diff --git a/apps/desktop/src/agent-definitions.mjs b/apps/desktop/src/agent-definitions.mjsindex 20f0c5e..8deda9b 100644--- a/apps/desktop/src/agent-definitions.mjs+++ b/apps/desktop/src/agent-definitions.mjs@@ -11,6 +11,7 @@ const definitions = {qwen: { name: 'Qwen Code', install: 'https://github.com/QwenLM/qwen-code' },kimi: { name: 'Kimi', install: 'https://www.kimi.com/code/docs/en/kimi-code-cli/guides/getting-started.html' },minimax: { name: 'MiniMax Code', install: 'https://www.npmjs.com/package/@minimax-ai/code' },+ deepseek: { name: 'DeepSeek Harness', install: 'https://github.com/deepseek-ai/deepseek-harness' },byok: { name: 'Own key', install: null },};diff --git a/apps/desktop/src/agent-installation.mjs b/apps/desktop/src/agent-installation.mjsindex 4968677..5a9b1fe 100644--- a/apps/desktop/src/agent-installation.mjs+++ b/apps/desktop/src/agent-installation.mjs@@ -15,7 +15,8 @@ export const AGENT_DOWNLOADS = Object.freeze(Object.fromEntries(OWN_AGENT_IDS.ma/*** Each agent's program, by the names it installs (the first found counts), and the folders its own installer puts it- * in beside the usual ones (HOME_BIN_CANDIDATES): those are not always on the PATH an app gets.+ * in beside the usual ones (HOME_BIN_CANDIDATES): those are not always on the PATH an app gets. `dirs` is told the+ * home folder, whether this is Windows, and the folders apps are in (a Mac's).*/export const AGENT_PROGRAMS = Object.freeze({claude: { names: ['claude'] },@@ -33,8 +34,16 @@ export const AGENT_PROGRAMS = Object.freeze({kimi: { names: ['kimi'], dirs: (home) => [join(home, '.kimi-code', 'bin')] },/* MiniMax Code's own installer keeps it in ~/.minimax-code/bin */minimax: { names: ['mcode'], dirs: (home) => [join(home, '.minimax-code', 'bin')] },+ /* DeepSeek Harness's desktop app carries the command (its own setting only links it into the PATH): inside+ DeepSeek Harness.app on a Mac, in its install folder's resources on Windows */+ deepseek: { names: ['dsh'], dirs: (home, win, apps = ['/Applications', join(home, 'Applications')]) => (win+ ? [join(localAppData(home), 'Programs', 'DeepSeek Harness', 'resources', 'runtime', 'cli', 'bin')]+ : apps.map((dir) => dirname(join(dir, 'DeepSeek Harness.app', DSH_IN_APP)))) },});+/* where DeepSeek Harness is inside DeepSeek Harness.app, its desktop app (macOS) */+const DSH_IN_APP = 'Contents/Resources/runtime/cli/bin/dsh';+/** %LOCALAPPDATA% (Windows). */const localAppData = (home) => process.env.LOCALAPPDATA || join(home, 'AppData', 'Local');@@ -395,7 +404,7 @@ export function chosenProgram(path, { names = [], win = windows } = {}) {let folder = false;try { folder = statSync(path).isDirectory(); } catch { /* a file, or not there */ }if (folder) {- const inApp = /\.app$/i.test(path) ? [...CODEX_IN_APP, OPENCODE_IN_APP, ...names.map((n) => `Contents/MacOS/${n}`)].map((p) => join(path, p)).find(executableFile) : null;+ const inApp = /\.app$/i.test(path) ? [...CODEX_IN_APP, OPENCODE_IN_APP, DSH_IN_APP, ...names.map((n) => `Contents/MacOS/${n}`)].map((p) => join(path, p)).find(executableFile) : null;return inApp ?? names.map((n) => findExecutable(n, [path, join(path, 'bin')], { PATH: '' }, { win })).find(Boolean) ?? null;}if (win) {@@ -423,7 +432,7 @@ export function detectAgentInstallation(id, { home = homedir(), env = process.enconst install = AGENT_DOWNLOADS[id] ?? null;const picked = chosen ? chosenProgram(chosen, { names: program.names, win }) : null;if (picked && !notTheAgent(id, picked, { chosen, win })) return { installed: true, cli: picked, source: 'chosen', install };- const dirs = [...pathDirs, ...(program.dirs?.(home, win) ?? [])];+ const dirs = [...pathDirs, ...(program.dirs?.(home, win, applicationDirs) ?? [])];let cli = null;for (const name of program.names) {if (cli) break;diff --git a/apps/desktop/src/agent-installation.test.mjs b/apps/desktop/src/agent-installation.test.mjsindex 6d39bcc..6089c9d 100644--- a/apps/desktop/src/agent-installation.test.mjs+++ b/apps/desktop/src/agent-installation.test.mjs@@ -41,6 +41,24 @@ test("an agent is found by any of its names, and in its own installer's folder oassert.equal(detectAgentInstallation('codebuddy', { ...none, pathDirs: [bin] }).cli, join(bin, 'cbc'));});+test("DeepSeek Harness is found inside its desktop app on a Mac, which carries the command, and from the app the person chooses", () => {+ if (process.platform === 'win32') return;+ const home = mkdtempSync(join(tmpdir(), 'of-home-'));+ const apps = join(home, 'Applications');+ const none = { home, env: { PATH: '' }, pathDirs: [], applicationDirs: [apps] };+ assert.equal(detectAgentInstallation('deepseek', none).cli, null);+ const dsh = join(apps, 'DeepSeek Harness.app', 'Contents', 'Resources', 'runtime', 'cli', 'bin', 'dsh');+ mkdirSync(join(dsh, '..'), { recursive: true });+ writeFileSync(dsh, '#!/bin/sh', { mode: 0o755 });+ assert.equal(detectAgentInstallation('deepseek', none).cli, dsh);+ assert.equal(chosenProgram(join(apps, 'DeepSeek Harness.app'), { names: ['dsh'], win: false }), dsh);+ /* one linked into the PATH (its own setting, or npm's @deepseek-ai/dsh) comes first */+ const bin = join(home, 'bin');+ mkdirSync(bin);+ writeFileSync(join(bin, 'dsh'), '', { mode: 0o755 });+ assert.equal(detectAgentInstallation('deepseek', { ...none, env: { PATH: bin } }).cli, join(bin, 'dsh'));+});+/** a file, its folders made */const put = (file, text = '', mode = 0o644) => { mkdirSync(join(file, '..'), { recursive: true }); writeFileSync(file, text, { mode }); return file; };diff --git a/apps/desktop/src/agent-roster.mjs b/apps/desktop/src/agent-roster.mjsindex 51018a6..b2d64bf 100644--- a/apps/desktop/src/agent-roster.mjs+++ b/apps/desktop/src/agent-roster.mjs@@ -1,14 +1,14 @@/*** The agents the chat can use, as Settings → Agents shows them, and the person's choices about them:*- * · codex, claude, copilot, cursor, opencode, codebuddy, qwen, kimi, minimax — the person's own agents (agents.mjs):- * Codex, Claude Code, GitHub Copilot, Cursor, OpenCode, CodeBuddy, Qwen Code, Kimi and MiniMax Code. Each uses- * the account signed in on the computer as it is: connecting opens Terminal for its own sign-in command only when- * it is signed out (never Codex's: it is signed in in Codex, so one found is connected), disconnecting only stops- * OpenFilm using it, and connecting again uses whichever account is signed in then (one switched in the agent or- * its maker's app). `account` says whose it is. Codex and Claude Code are shown in the chat's menu from the start;- * the others once the person connects one or switches it on. Codex is connected from the start, Claude Code once- * the person connects it.+ * · codex, claude, copilot, cursor, opencode, codebuddy, qwen, kimi, minimax, deepseek — the person's own agents+ * (agents.mjs): Codex, Claude Code, GitHub Copilot, Cursor, OpenCode, CodeBuddy, Qwen Code, Kimi, MiniMax Code and+ * DeepSeek Harness. Each uses the account signed in on the computer as it is: connecting opens Terminal for its own+ * sign-in command only when it is signed out (never Codex's or DeepSeek Harness's: each is signed in in its own+ * app, so one found is connected), disconnecting only stops OpenFilm using it, and connecting again uses whichever+ * account is signed in then (one switched in the agent or its maker's app). `account` says whose it is. Codex and+ * Claude Code are shown in the chat's menu from the start; the others once the person connects one or switches it+ * on. Codex is connected from the start, Claude Code once the person connects it.* · byok — the app's own agent (Pi, pi-agent.mjs) on the person's own models, each set up on its own: the name the* chat's menu shows, its service (an address on a standard API in one of three formats: the OpenAI Responses API* (…/v1/responses), the OpenAI Chat Completions API (…/v1/chat/completions) or the Anthropic Messages APIdiff --git a/apps/desktop/src/agent-skills.mjs b/apps/desktop/src/agent-skills.mjsindex ea4e65b..ff43f1a 100644--- a/apps/desktop/src/agent-skills.mjs+++ b/apps/desktop/src/agent-skills.mjs@@ -18,9 +18,9 @@* Copilot OpenFilm's through COPILOT_SKILLS_DIRS and `--disable-mcp-server` for the person's own servers; it* has no way to hide a skill, so it keeps all of the person's.* Pi OpenFilm's only.- * CodeBuddy, Cursor, Kimi and MiniMax Code are not told anything: they have the person's skills and servers as in their- * own terminal, and OpenFilm's skills when named in a message. A system-defaults file the person set themselves is left- * as it is.+ * CodeBuddy, Cursor, Kimi, MiniMax Code and DeepSeek Harness are not told anything: they have the person's skills and+ * servers as in their own terminal, and OpenFilm's skills when named in a message. A system-defaults file the person+ * set themselves is left as it is.** Which are the person's own is read from where each agent keeps them; its maker's are elsewhere or marked (Claude* Code's skills/synced/ from claude.ai and its claude.ai connectors, Codex's .system skills, OpenAI's plugins and thediff --git a/apps/desktop/src/agents.mjs b/apps/desktop/src/agents.mjsindex 17e0594..760a89d 100644--- a/apps/desktop/src/agents.mjs+++ b/apps/desktop/src/agents.mjs@@ -1,7 +1,7 @@/*** The chat's agents: the person's own Claude Code, Codex, GitHub Copilot, Cursor, OpenCode, CodeBuddy, Qwen Code,- * Kimi or MiniMax Code, driven over the Agent Client Protocol (ACP): a child process speaking JSON-RPC over- * stdin/stdout.+ * Kimi, MiniMax Code or DeepSeek Harness, driven over the Agent Client Protocol (ACP): a child process speaking+ * JSON-RPC over stdin/stdout.* The app is the client: initialize, session/new (or session/resume for a conversation from before), session/prompt;* session/update draws the chat, and session/request_permission asks the person in the chat.*@@ -85,6 +85,9 @@ export const AGENTS = Object.freeze({qwen: { label: AGENT_DEFINITIONS.qwen.name, args: ['--acp', '--approval-mode', 'yolo'], login: [], approve: true },kimi: { label: AGENT_DEFINITIONS.kimi.name, args: ['acp'], login: ['login'], approve: true },minimax: { label: AGENT_DEFINITIONS.minimax.name, args: ['acp'], login: ['login'], approve: true },+ /* DeepSeek Harness (`dsh`) speaks ACP as its `acp` profile. It has no sign-in command: the person signs in to their+ DeepSeek account in its desktop app, or sets DEEPSEEK_API_KEY, so one found is connected */+ deepseek: { label: AGENT_DEFINITIONS.deepseek.name, args: ['--profile', 'acp'], login: null, approve: true },});export const isAgentId = (id) => typeof id === 'string' && Object.hasOwn(AGENTS, id);@@ -939,13 +942,18 @@ export function legacyOptions(session) {/*** Config options with each choice once: an agent may list the same value twice (GitHub Copilot's model list has* "Auto" twice), which the menu would show twice, both ticked. The first of each is kept; anything else as it was.+ * Choices in groups (DeepSeek Harness groups its models by where they run: its API, the person's DeepSeek account)+ * are listed one after another, each named with its group's name.*/export function distinctOptions(configOptions) {if (!Array.isArray(configOptions)) return configOptions;return configOptions.map((o) => {if (!Array.isArray(o?.options)) return o;const seen = new Set();- return { ...o, options: o.options.filter((c) => !c || !('value' in c) || (!seen.has(c.value) && seen.add(c.value))) };+ const choices = o.options.flatMap((c) => (Array.isArray(c?.options) && !('value' in c)+ ? c.options.map((g) => (g && typeof c.name === 'string' && c.name ? { ...g, name: `${c.name} · ${g.name}` } : g))+ : [c]));+ return { ...o, options: choices.filter((c) => !c || !('value' in c) || (!seen.has(c.value) && seen.add(c.value))) };});}diff --git a/apps/desktop/src/agents.test.mjs b/apps/desktop/src/agents.test.mjsindex 902dd12..b05ed03 100644--- a/apps/desktop/src/agents.test.mjs+++ b/apps/desktop/src/agents.test.mjs@@ -35,6 +35,22 @@ test('a choice an agent lists twice shows once in the menu', () => {assert.equal(distinctOptions(null), null);});+test("choices an agent lists in groups show one after another, each with its group's name", () => {+ /* DeepSeek Harness 0.2.0 groups its models by where they run */+ const [model, effort] = distinctOptions([+ { id: 'model', currentValue: '["deepseek-official","deepseek-v4-flash"]', options: [+ { group: 'deepseek-official', name: 'DeepSeek', options: [{ value: '["deepseek-official","deepseek-v4-flash"]', name: 'deepseek-v4-flash' }] },+ { group: 'deepseek-account', name: 'DeepSeek Account', options: [{ value: '["deepseek-account","deepseek-flash"]', name: 'DeepSeek-V41-Flash', description: 'Fast' }] },+ ] },+ { id: 'reasoning_effort', currentValue: 'high', options: [{ value: 'high', name: 'High' }] },+ ]);+ assert.deepEqual(model.options, [+ { value: '["deepseek-official","deepseek-v4-flash"]', name: 'DeepSeek · deepseek-v4-flash' },+ { value: '["deepseek-account","deepseek-flash"]', name: 'DeepSeek Account · DeepSeek-V41-Flash', description: 'Fast' },+ ]);+ assert.deepEqual(effort.options, [{ value: 'high', name: 'High' }]);+});+test('whose sign-in it is, as each program says it, never a key', () => {const claude = JSON.stringify({ loggedIn: true, authMethod: 'claude.ai', email: '[email protected]', orgName: 'Studio', subscriptionType: 'max' });assert.deepEqual(readAgentStatus('claude', claude, claude), { signedIn: true, account: { method: 'claude.ai', email: '[email protected]', org: 'Studio', plan: 'max' } });diff --git a/apps/desktop/src/ffmpeg-runtime.mjs b/apps/desktop/src/ffmpeg-runtime.mjsindex fdadfc1..147337e 100644--- a/apps/desktop/src/ffmpeg-runtime.mjs+++ b/apps/desktop/src/ffmpeg-runtime.mjs@@ -2,9 +2,12 @@* ffmpeg and ffprobe: Studio and `openfilm` need them for video pictures, sound and every export. The installer brings* its own (scripts/release.mjs puts them in the app's resources, `runtime/ffmpeg`, with their license), so the app* works without a download. A build without them (from source) downloads them once into its data folder when the- * computer has none on its PATH. Both come from the ffmpeg-static project's release (static builds, GPL-3.0, as- * published there), each checked against the sha256 pinned here. The folder is on the PATH Studio and the agents get- * from the start, so they find the programs the moment they land, without a restart.+ * computer has none on its PATH. Static builds (GPL-3.0, as published), each checked against the sha256 pinned here:+ * on a Mac the Shaka project's (both programs as they are), on Windows gyan.dev's essentials build (a zip, the two+ * programs taken out of it; it has NVIDIA's, Intel's and AMD's encoders, which the Shaka build for Windows lacks).+ * FFmpeg 6 wrote ProRes 4444 whose alpha Apple's decoder reads as opaque (editors showed rendered pages black); 8.1+ * and later write it right. The folder is on the PATH Studio and the agents get from the start, so they find the+ * programs the moment they land, without a restart.** Reads no Electron module, so `node --test` can use it.*/@@ -12,29 +15,34 @@ import { createHash } from 'node:crypto';import { spawnSync } from 'node:child_process';import { chmodSync, existsSync, mkdirSync, renameSync, writeFileSync } from 'node:fs';import { join } from 'node:path';-import { gunzipSync } from 'node:zlib';+import { inflateRawSync } from 'node:zlib';++const SHAKA = 'https://github.com/shaka-project/static-ffmpeg-binaries/releases/download/n8.1.2-1';+const GYAN = 'https://github.com/GyanD/codexffmpeg/releases/download/9.0.2';+/** FFmpeg's own license text, for a build that publishes none beside it. */+const GPL3 = (version) => `https://raw.githubusercontent.com/FFmpeg/FFmpeg/n${version}/COPYING.GPLv3`;-const RELEASE = 'b6.1.1';-const BASE = `https://github.com/eugeneware/ffmpeg-static/releases/download/${RELEASE}`;/**- * The pinned builds: `<program>-<platform>-<arch>.gz` and its sha256 (as the release publishes it), and the FFmpeg- * version each is (as `ffmpeg -version` says), whose source goes beside it for its GPL.+ * The pinned builds: the FFmpeg version each is (as `ffmpeg -version` says), whose source goes beside it for its GPL,+ * and where each program comes from with its sha256: a file of its own, or (`zip`) an entry of one archive.*/const BUILDS = Object.freeze({'darwin-arm64': {- version: '6.0',- ffmpeg: '8923876afa8db5585022d7860ec7e589af192f441c56793971276d450ed3bbfa',- ffprobe: 'd986a8ec7b030899fe66a8a288ed809a3543338705a3ce178cfb85869c5d80be',+ version: '8.1.2',+ by: `the Shaka project's static build n8.1.2-1 (${SHAKA}), made by its scripts at https://github.com/shaka-project/static-ffmpeg-binaries`,+ ffmpeg: { url: `${SHAKA}/ffmpeg-osx-arm64`, sha256: 'e7b9fcd97f95f333512d6e8b8ac24d9dbc08f189f36047695499bd7b57214b22' },+ ffprobe: { url: `${SHAKA}/ffprobe-osx-arm64`, sha256: 'ded4c698b8ff38d0bc1fd30fcc5e768dc46f58bc15a8dfd61f98615ba49cde5c' },},'darwin-x64': {- version: '6.1.1',- ffmpeg: '929b375c1182d956c51f7ac25e0b2b0411fb01f6f407aa15c9758efeb4242106',- ffprobe: 'd4da574d6e2e197bd259b47d69cf262df9e312af24ad960444f6d806d3d4c186',+ version: '8.1.2',+ by: `the Shaka project's static build n8.1.2-1 (${SHAKA}), made by its scripts at https://github.com/shaka-project/static-ffmpeg-binaries`,+ ffmpeg: { url: `${SHAKA}/ffmpeg-osx-x64`, sha256: '62c87854d851f202fc4a29bdda0fe7b6ebcddd37b863482ce1bdc81151b03fe4' },+ ffprobe: { url: `${SHAKA}/ffprobe-osx-x64`, sha256: 'd530823f480a3c7eb6334f18a00197d1e9f1070e86172b9aa89c4bf4022bd879' },},'win32-x64': {- version: '6.1.1',- ffmpeg: '8883a3dffbd0a16cf4ef95206ea05283f78908dbfb118f73c83f4951dcc06d77',- ffprobe: 'f309e6223ad89d2fe54bccd420a7709b66fd27540674e92309578ed491a43c8d',+ version: '9.0.2',+ by: `gyan.dev's essentials build 9.0.2 (${GYAN})`,+ zip: { url: `${GYAN}/ffmpeg-9.0.2-essentials_build.zip`, sha256: '60f467265b1e312373dbcd92200c2618a74850f98d3d078e94296bb3fa2047ba', folder: 'ffmpeg-9.0.2-essentials_build' },},});@@ -45,7 +53,7 @@ const TRIES = 3;const exe = (name, platform = process.platform) => (platform === 'win32' ? `${name}.exe` : name);/** The folder the downloaded programs go in (on the PATH of Studio and the agents, whether they are there yet or not). */-export const ffmpegDir = (dataDir) => join(dataDir, 'runtime', `ffmpeg-${RELEASE}`);+export const ffmpegDir = (dataDir, platform = process.platform, arch = process.arch) => join(dataDir, 'runtime', `ffmpeg-${BUILDS[`${platform}-${arch}`]?.version ?? 'none'}`);/** Whether a folder holds both programs (the installer's, or a download that finished). */export const ffmpegIn = (dir, platform = process.platform) => ['ffmpeg', 'ffprobe'].every((n) => existsSync(join(dir, exe(n, platform))));@@ -75,8 +83,8 @@ export function ensureFfmpeg(dataDir, { onProgress = () => {}, fetch: fetchImpl}/**- * Download one platform's build into `dir`: both programs, and with `license`, its LICENSE, its README and where its- * source is (what the installer ships beside them). A download that stalls is tried again.+ * Download one platform's build into `dir`: both programs, and with `license`, its license, its readme when it has+ * one, and where its source is (what the installer ships beside them). A download that stalls is tried again.* @param {string} dir* @param {{ platform?: string, arch?: string, license?: boolean, onProgress?: (fraction: number) => void, fetch?: typeof fetch, stallMs?: number }} [options]*/@@ -86,36 +94,77 @@ export async function fetchFfmpeg(dir, { platform = process.platform, arch = proif (!build) throw new Error(`There is no ffmpeg to download for ${key}: install it from ffmpeg.org.`);mkdirSync(dir, { recursive: true });const names = /** @type {const} */ (['ffmpeg', 'ffprobe']);- const got = { ffmpeg: 0, ffprobe: 0 };- const total = { ffmpeg: 0, ffprobe: 0 };+ const missing = names.filter((name) => !existsSync(join(dir, exe(name, platform))));+ /** each download's bytes so far and in all, for one fraction across them */+ const parts = new Map();const tell = () => {- const all = total.ffmpeg + total.ffprobe;- if (all) onProgress(Math.min(0.99, (got.ffmpeg + got.ffprobe) / all));+ const all = [...parts.values()].reduce((n, p) => n + p.total, 0);+ if (all) onProgress(Math.min(0.99, [...parts.values()].reduce((n, p) => n + p.got, 0) / all));};- await Promise.all(names.map(async (name) => {+ const pinned = (/** @type {{ url: string, sha256: string }} */ from, /** @type {string} */ what) => retried(async () => {+ const part = { got: 0, total: 0 };+ parts.set(from.url, part);+ const bytes = await download(from.url, fetchImpl, stallMs, (size) => { part.total = size; }, (n) => { part.got += n; tell(); });+ if (createHash('sha256').update(bytes).digest('hex') !== from.sha256) throw new Error(`The ${what} download did not match its checksum; nothing was kept.`);+ return bytes;+ });+ const place = (/** @type {string} */ name, /** @type {Buffer} */ bytes) => {const target = join(dir, exe(name, platform));- if (existsSync(target)) return;- const gz = await retried(() => {- got[name] = 0;- return download(`${BASE}/${name}-${key}.gz`, fetchImpl, stallMs, (size) => { total[name] = size; }, (n) => { got[name] += n; tell(); });- });- if (createHash('sha256').update(gz).digest('hex') !== build[name]) throw new Error(`The ${name} download did not match its checksum; nothing was kept.`);- const part = `${target}.part`;- writeFileSync(part, gunzipSync(gz));- chmodSync(part, 0o755);- renameSync(part, target);- }));+ writeFileSync(`${target}.part`, bytes);+ chmodSync(`${target}.part`, 0o755);+ renameSync(`${target}.part`, target);+ };+ /** the archive's entries, when the build is one */+ let zip = null;+ if ('zip' in build) {+ if (missing.length || license) {+ zip = await pinned(build.zip, 'ffmpeg');+ for (const name of missing) place(name, unzipEntry(zip, `${build.zip.folder}/bin/${exe(name, platform)}`));+ }+ } else {+ await Promise.all(missing.map(async (name) => place(name, await pinned(build[name], name))));+ }if (license) {- for (const [from, to] of [[`${key}.LICENSE`, 'LICENSE'], [`${key}.README`, 'README']]) {- writeFileSync(join(dir, to), await retried(() => download(`${BASE}/${from}`, fetchImpl, stallMs)));+ if (zip) {+ writeFileSync(join(dir, 'LICENSE'), unzipEntry(zip, `${build.zip.folder}/LICENSE`));+ writeFileSync(join(dir, 'README'), unzipEntry(zip, `${build.zip.folder}/README.txt`));+ } else {+ writeFileSync(join(dir, 'LICENSE'), await retried(() => download(GPL3(build.version), fetchImpl, stallMs)));}- writeFileSync(join(dir, 'SOURCE'), `FFmpeg ${build.version}, the static build of ffmpeg-static ${RELEASE} (${BASE}).\n`+ writeFileSync(join(dir, 'SOURCE'), `FFmpeg ${build.version}, ${build.by}.\n`+ `Its source: https://ffmpeg.org/releases/ffmpeg-${build.version}.tar.xz\n`);}onProgress(1);return dir;}+/**+ * One file out of a zip in memory, by its name: found in the central directory, stored or deflated. A zip of+ * programs under 4 GB, so no Zip64.+ * @param {Buffer} zip @param {string} name+ */+export function unzipEntry(zip, name) {+ const end = zip.lastIndexOf(Buffer.from([0x50, 0x4b, 0x05, 0x06]));+ if (end < 0) throw new Error('The ffmpeg download is not a zip.');+ let at = zip.readUInt32LE(end + 16);+ for (let i = zip.readUInt16LE(end + 10); i > 0; i--) {+ const method = zip.readUInt16LE(at + 10);+ const size = zip.readUInt32LE(at + 20);+ const nameLength = zip.readUInt16LE(at + 28);+ const skip = nameLength + zip.readUInt16LE(at + 30) + zip.readUInt16LE(at + 32);+ if (zip.toString('utf8', at + 46, at + 46 + nameLength) === name) {+ const local = zip.readUInt32LE(at + 42);+ const start = local + 30 + zip.readUInt16LE(local + 26) + zip.readUInt16LE(local + 28);+ const data = zip.subarray(start, start + size);+ if (method === 0) return Buffer.from(data);+ if (method === 8) return inflateRawSync(data);+ throw new Error(`${name} is compressed in a way this can't read (method ${method}).`);+ }+ at += 46 + skip;+ }+ throw new Error(`The ffmpeg download has no ${name}.`);+}+/** One file's bytes; one that stops coming for `stallMs` is an error. */async function download(url, fetchImpl, stallMs, onSize = () => {}, onBytes = () => {}) {const stop = new AbortController();diff --git a/apps/desktop/src/ffmpeg-runtime.test.mjs b/apps/desktop/src/ffmpeg-runtime.test.mjsindex 8af3e73..068e6d1 100644--- a/apps/desktop/src/ffmpeg-runtime.test.mjs+++ b/apps/desktop/src/ffmpeg-runtime.test.mjs@@ -3,15 +3,15 @@ import assert from 'node:assert/strict';import { mkdtempSync, readFileSync, writeFileSync } from 'node:fs';import { tmpdir } from 'node:os';import { join } from 'node:path';-import { gzipSync } from 'node:zlib';-import { fetchFfmpeg, ffmpegIn } from './ffmpeg-runtime.mjs';+import { fetchFfmpeg, ffmpegIn, unzipEntry } from './ffmpeg-runtime.mjs';+import { zipFiles } from '../../../packages/openfilm/studio/server/zip.mjs';/** A response whose body comes in one piece. */const answer = (bytes) => new Response(new ReadableStream({ start(c) { c.enqueue(bytes); c.close(); } }), { headers: { 'content-length': String(bytes.length) } });test('a build whose checksum is not the pinned one is not kept', async () => {const dir = mkdtempSync(join(tmpdir(), 'of-ffmpeg-'));- const fetch = async () => answer(gzipSync(Buffer.from('not ffmpeg')));+ const fetch = async () => answer(Buffer.from('not ffmpeg'));await assert.rejects(fetchFfmpeg(dir, { platform: 'darwin', arch: 'arm64', fetch }), /did not match its checksum/);assert.equal(ffmpegIn(dir, 'darwin'), false);});@@ -25,14 +25,14 @@ test('a download that fails is tried again, three times in all', async () => {const asked = [];const fetch = async (url) => { asked.push(url.split('/').pop()); throw new TypeError('fetch failed'); };await assert.rejects(fetchFfmpeg(dir, { platform: 'win32', arch: 'x64', fetch }), /fetch failed/);- assert.deepEqual(asked.filter((n) => n === 'ffmpeg-win32-x64.gz').length, 3);+ assert.deepEqual(asked.filter((n) => n === 'ffmpeg-9.0.2-essentials_build.zip').length, 3, 'one archive, both programs in it');});test('a download that stops coming is stopped and tried again', async () => {const dir = mkdtempSync(join(tmpdir(), 'of-ffmpeg-'));let tries = 0;const fetch = async (url, { signal }) => {- if (url.endsWith('ffmpeg-darwin-x64.gz')) tries++;+ if (url.endsWith('ffmpeg-osx-x64')) tries++;/* fetch ends a body whose request is aborted with an error */return new Response(new ReadableStream({ start(c) { signal.addEventListener('abort', () => c.error(new DOMException('aborted', 'AbortError'))); } }));};@@ -40,15 +40,27 @@ test('a download that stops coming is stopped and tried again', async () => {assert.equal(tries, 3);});-test('the installer\'s copy has the build\'s license, its readme and where its source is', async () => {+test('the installer\'s copy has FFmpeg\'s license and where its source is', async () => {/* the programs themselves are the pinned builds: a stand-in with the pinned checksum is not to be had, so thischecks the license files only, with the programs already there */const dir = mkdtempSync(join(tmpdir(), 'of-ffmpeg-'));- writeFileSync(join(dir, 'ffmpeg.exe'), '');- writeFileSync(join(dir, 'ffprobe.exe'), '');- const fetch = async (url) => answer(Buffer.from(`text of ${url.split('/').pop()}`));- await fetchFfmpeg(dir, { platform: 'win32', arch: 'x64', license: true, fetch });- assert.equal(readFileSync(join(dir, 'LICENSE'), 'utf8'), 'text of win32-x64.LICENSE');- assert.equal(readFileSync(join(dir, 'README'), 'utf8'), 'text of win32-x64.README');- assert.match(readFileSync(join(dir, 'SOURCE'), 'utf8'), /^FFmpeg 6\.1\.1,[^]*ffmpeg-6\.1\.1\.tar\.xz/);+ writeFileSync(join(dir, 'ffmpeg'), '');+ writeFileSync(join(dir, 'ffprobe'), '');+ const asked = [];+ const fetch = async (url) => { asked.push(url); return answer(Buffer.from('GPL v3 text')); };+ await fetchFfmpeg(dir, { platform: 'darwin', arch: 'arm64', license: true, fetch });+ assert.deepEqual(asked, ['https://raw.githubusercontent.com/FFmpeg/FFmpeg/n8.1.2/COPYING.GPLv3'], 'the license of the version it is, nothing else');+ assert.equal(readFileSync(join(dir, 'LICENSE'), 'utf8'), 'GPL v3 text');+ assert.match(readFileSync(join(dir, 'SOURCE'), 'utf8'), /^FFmpeg 8\.1\.2,[^]*ffmpeg-8\.1\.2\.tar\.xz/);+});++test('a program comes out of a zip as it went in', async () => {+ const dir = mkdtempSync(join(tmpdir(), 'of-ffmpeg-'));+ const program = Buffer.from('MZ'.repeat(5000));+ await zipFiles(join(dir, 'build.zip'), [{ name: 'build/LICENSE', data: Buffer.from('license') }, { name: 'build/bin/ffmpeg.exe', data: program }]);+ const zip = readFileSync(join(dir, 'build.zip'));+ assert.deepEqual(unzipEntry(zip, 'build/bin/ffmpeg.exe'), program);+ assert.equal(unzipEntry(zip, 'build/LICENSE').toString(), 'license');+ assert.throws(() => unzipEntry(zip, 'build/bin/ffprobe.exe'), /has no build\/bin\/ffprobe\.exe/);+ assert.throws(() => unzipEntry(Buffer.from('not a zip'), 'x'), /not a zip/);});diff --git a/packages/openfilm/src/browser-pool.mjs b/packages/openfilm/src/browser-pool.mjsnew file mode 100644index 0000000..fd86de3--- /dev/null+++ b/packages/openfilm/src/browser-pool.mjs@@ -0,0 +1,85 @@+// Browsers kept for many renders: Studio's editing-project export draws page after page with the same ones, instead+// of starting a browser for each page. A render borrows some (take) and gives them back (give); each page opens its+// own tab in them and closes it after.+import { launch } from './host.mjs';++/**+ * Pages a browser draws before it is closed and a new one started. A browser's memory measured flat over 20 pages+ * (a new tab each), so this only bounds whatever a page might leave behind in it.+ */+export const PAGES_PER_BROWSER = 24;++export class BrowserPool {+ /** @type {import('playwright-core').Browser[]} */+ #idle = [];+ /** @type {Map<import('playwright-core').Browser, number>} pages each browser has drawn */+ #drawn = new Map();+ #closed = false;+ /** @param {{ start?: () => Promise<import('playwright-core').Browser> }} [opts] how a browser is started (tests) */+ constructor({ start = () => launch() } = {}) {+ this.start = start;+ }++ /** How many browsers are open: lent and idle. */+ get size() { return this.#drawn.size; }++ /**+ * `n` browsers: idle ones first (one that died while idle is dropped), the rest started now, at once.+ * @param {number} n @returns {Promise<import('playwright-core').Browser[]>}+ */+ async take(n) {+ if (this.#closed) throw new Error('cancelled');+ /** @type {import('playwright-core').Browser[]} */+ const out = [];+ while (out.length < n && this.#idle.length) {+ const b = /** @type {import('playwright-core').Browser} */ (this.#idle.pop());+ if (b.isConnected()) out.push(b);+ else this.#drawn.delete(b);+ }+ const started = await Promise.allSettled(Array.from({ length: n - out.length }, () => this.start()));+ for (const s of started) if (s.status === 'fulfilled') { this.#drawn.set(s.value, 0); out.push(s.value); }+ const failed = started.find((s) => s.status === 'rejected');+ if (failed || this.#closed) {+ await this.give(out);+ throw failed ? /** @type {PromiseRejectedResult} */ (failed).reason : new Error('cancelled');+ }+ return out;+ }++ /**+ * Browsers back, a page drawn with each. One that crashed, one the render could not finish with (`retire`: it may+ * hang), and one that has drawn PAGES_PER_BROWSER pages are closed; a new one starts when next asked for.+ * @param {import('playwright-core').Browser[]} browsers @param {{ retire?: boolean }} [opts]+ */+ async give(browsers, { retire = false } = {}) {+ await Promise.allSettled(browsers.map(async (b) => {+ const drawn = (this.#drawn.get(b) ?? 0) + 1;+ if (this.#closed || retire || !b.isConnected() || drawn >= PAGES_PER_BROWSER) {+ this.#drawn.delete(b);+ await b.close();+ return;+ }+ this.#drawn.set(b, drawn);+ this.#idle.push(b);+ }));+ }++ /** Every browser closed, lent or not; take fails from now on. */+ async close() {+ this.#closed = true;+ const all = [...this.#drawn.keys()];+ this.#drawn.clear();+ this.#idle = [];+ await Promise.allSettled(all.map((b) => b.close()));+ }+}++/** What a browser that crashed, or a tab that stopped answering, says: drawn again in new browsers, it may work. */+const BROWSER_FAILED = /browser drawing the page closed|target (page, context or browser has been )?closed|target crashed|page crashed|browser has been closed|browser (has )?disconnected|did not finish within|did not come within/i;++/**+ * Whether a render that failed is worth drawing again in new browsers: its browser crashed or stopped answering. A+ * page's own error (a frame that throws, a file that is missing) or a loop in its code would fail again.+ * @param {unknown} e+ */+export const browserFailed = (e) => e instanceof Error && BROWSER_FAILED.test(e.message) && !/still running code/.test(e.message);diff --git a/packages/openfilm/src/browser-pool.test.mjs b/packages/openfilm/src/browser-pool.test.mjsnew file mode 100644index 0000000..f308ff5--- /dev/null+++ b/packages/openfilm/src/browser-pool.test.mjs@@ -0,0 +1,76 @@+import { test } from 'node:test';+import assert from 'node:assert/strict';+import { BrowserPool, PAGES_PER_BROWSER, browserFailed } from './browser-pool.mjs';++/** A stand-in browser: connected until it is closed or `crash()`ed. */+function fakes() {+ let n = 0;+ const started = [];+ const start = async () => {+ const b = { id: ++n, connected: true, closed: false, isConnected() { return this.connected; }, async close() { this.closed = true; this.connected = false; }, crash() { this.connected = false; } };+ started.push(b);+ return /** @type {any} */ (b);+ };+ return { start, started };+}++test('a pool lends its idle browsers again and starts only what it lacks', async () => {+ const { start, started } = fakes();+ const pool = new BrowserPool({ start });+ const two = await pool.take(2);+ assert.equal(started.length, 2);+ await pool.give(two);+ const three = await pool.take(3);+ assert.equal(started.length, 3, 'the two back, one more started');+ assert.deepEqual(new Set(three.slice(0, 2)), new Set(two));+ assert.equal(pool.size, 3);+ await pool.close();+ assert.ok(started.every((b) => b.closed), 'every browser closed, lent or not');+ await assert.rejects(pool.take(1), /cancelled/);+});++test('a browser that crashed, one that failed a render, and one that drew many pages are not lent again', async () => {+ const { start, started } = fakes();+ const pool = new BrowserPool({ start });+ const [a, b] = await pool.take(2);+ /** @type {any} */ (a).crash();+ await pool.give([a, b], { retire: false });+ assert.equal(pool.size, 1, 'the crashed one dropped');+ const [again] = await pool.take(1);+ assert.equal(again, b);+ await pool.give([again], { retire: true });+ assert.ok(started[1].closed, 'a render that failed: its browser closed');+ assert.equal(pool.size, 0);+ const [c] = await pool.take(1);+ for (let i = 1; i < PAGES_PER_BROWSER; i++) {+ await pool.give([c]);+ assert.equal((await pool.take(1))[0], c);+ }+ await pool.give([c]);+ assert.ok(/** @type {any} */ (c).closed, `closed after ${PAGES_PER_BROWSER} pages`);+ const [fresh] = await pool.take(1);+ assert.notEqual(fresh, c);+ await pool.close();+});++test('a browser that fails to start gives the others back and says why', async () => {+ const { start, started } = fakes();+ let calls = 0;+ const pool = new BrowserPool({ start: () => (++calls === 2 ? Promise.reject(new Error('no browser')) : start()) });+ await assert.rejects(pool.take(3), /no browser/);+ assert.equal(pool.size, 2, 'the two that started are kept');+ assert.equal((await pool.take(2)).length, 2);+ assert.equal(started.length, 2, 'and lent again');+ await pool.close();+});++test('a render is drawn again in new browsers only when its browser failed it', () => {+ assert.ok(browserFailed(new Error('page.evaluate: Target page, context or browser has been closed')));+ assert.ok(browserFailed(new Error('page.evaluate: Target crashed')));+ assert.ok(browserFailed(new Error('the browser drawing the page closed (it crashed, or the system ended it)')));+ assert.ok(browserFailed(new Error('frame(1.2) did not finish within 60 s')));+ assert.ok(browserFailed(new Error('the picture did not come within 30 s')));+ assert.ok(!browserFailed(new Error('frame(1.2) did not finish within 60 s (the page is still running code: a loop that never ends?)')), 'a loop in the page would loop again');+ assert.ok(!browserFailed(new Error('frame(1.2) threw: x is not defined')));+ assert.ok(!browserFailed(new Error('ffmpeg: Unknown encoder')));+});diff --git a/packages/openfilm/src/encoders.mjs b/packages/openfilm/src/encoders.mjsindex 8cc70f4..fbd273f 100644--- a/packages/openfilm/src/encoders.mjs+++ b/packages/openfilm/src/encoders.mjs@@ -83,6 +83,24 @@ export function proresOptions(encoder, alpha) {return { video: ['-c:v', 'prores_ks', '-profile:v', alpha ? '4444' : '3', '-vendor', 'apl0'], pix: alpha ? 'format=yuva444p10le' : 'format=yuv422p10le' };}+/* BT.709 said on the frames: an encoder that takes no notice of the color options (the Mac's VideoToolbox writes no+ primaries or transfer from them, and nothing at all for HEVC with alpha) takes it from the frames */+export const FRAMES_709 = 'setparams=colorspace=bt709:color_primaries=bt709:color_trc=bt709';++/**+ * ProRes of the browser's frames, BT.709 throughout: converted to YUV with it (swscale's own default is BT.601; the+ * Mac's engine takes RGB and converts it itself), said on the frames and to the encoder. Footage converted for an+ * editor keeps its own colors and takes proresOptions as it is.+ * @param {string} encoder @param {boolean} alpha+ */+export function prores709(encoder, alpha) {+ const { video, pix } = proresOptions(encoder, alpha);+ return {+ video: [...video, '-colorspace', 'bt709', '-color_primaries', 'bt709', '-color_trc', 'bt709', '-color_range', 'tv'],+ pix: pix === 'format=bgra' ? `${pix},${FRAMES_709}` : `scale=out_color_matrix=bt709:out_range=tv,${pix},${FRAMES_709}`,+ };+}+/** @type {Map<string, Promise<boolean>>} */const tried = new Map();/**@@ -156,7 +174,13 @@ function decodedMd5(options, sample) {{ stdio: ['pipe', 'pipe', 'ignore'], windowsHide: true });let out = '';proc.stdout.on('data', (d) => { out += d; });- const timer = setTimeout(() => proc.kill('SIGKILL'), 20_000);+ /* a decoder that hangs is not one to wait for: its whole tree ended (an ffmpeg that is a shim runs the real one as+ its child, which would keep the pipe open), and no answer at once */+ const timer = setTimeout(() => {+ if (process.platform === 'win32' && proc.pid) spawn('taskkill', ['/pid', String(proc.pid), '/T', '/F'], { stdio: 'ignore', windowsHide: true }).on('error', () => {});+ else proc.kill('SIGKILL');+ done(null);+ }, 20_000);proc.on('error', () => { clearTimeout(timer); done(null); });proc.on('close', (code) => { clearTimeout(timer); done(code === 0 && /^MD5=\w+$/.test(out.trim()) ? out.trim() : null); });proc.stdin.on('error', () => {});diff --git a/packages/openfilm/src/host.mjs b/packages/openfilm/src/host.mjsindex 050772d..c0ea2db 100644--- a/packages/openfilm/src/host.mjs+++ b/packages/openfilm/src/host.mjs@@ -90,6 +90,15 @@ export function folderFiles(root, { inject = `<script>${HOST_FLAG}</script>`, tielse void frameDuration(file).then(answer);return true;}+ if (path.endsWith('/__film/media-facts')) {+ /* what a video or sound is when the browser cannot read it (ProRes, a codec it has no decoder for): its length+ and picture size as ffprobe reads them (`?of=` the file, from the film's folder) */+ const file = fileInRoot(root, `${path.slice(0, -'/__film/media-facts'.length)}/${query?.get('of') ?? ''}`);+ const answer = (facts) => res.writeHead(200, { ...extraHeaders, 'content-type': 'application/json', 'cache-control': 'no-store' }).end(JSON.stringify(facts));+ if (!file) answer(null);+ else void mediaFacts(file).then(answer);+ return true;+ }let rel;try { rel = decodeURIComponent(path); } catch { res.writeHead(400, extraHeaders).end('bad address'); return true; }/* an address speaks in `/`: a `\` in it (escaped as %5C) would be a separator on Windows, past every check */@@ -175,6 +184,32 @@ export function frameDuration(file) {return /** @type {Promise<number | null>} */ (frameDurations.get(key));}+/** @type {Map<string, Promise<{ duration: number, w: number, h: number } | null>>} */+const mediaFactsOf = new Map();+/**+ * A video's or sound's length (s) and picture size (0 for a sound), as ffprobe reads them, once per version of the file;+ * null when it cannot tell.+ * @param {string} file+ */+export function mediaFacts(file) {+ let key;+ try { const stat = statSync(file); key = `${file}\0${stat.size}\0${stat.mtimeMs}`; } catch { return Promise.resolve(null); }+ if (!mediaFactsOf.has(key)) {+ mediaFactsOf.set(key, new Promise((done) => {+ execFile('ffprobe', ['-v', 'error', '-show_entries', 'format=duration:stream=codec_type,width,height', '-of', 'json', file],+ { timeout: 20_000, windowsHide: true }, (error, stdout) => {+ if (error) { mediaFactsOf.delete(key); done(null); return; }+ let said;+ try { said = JSON.parse(String(stdout)); } catch { /* nothing to read */ }+ const duration = Number(said?.format?.duration);+ const video = said?.streams?.find((s) => s.codec_type === 'video');+ done(duration > 0 ? { duration, w: video?.width ?? 0, h: video?.height ?? 0 } : null);+ });+ }));+ }+ return /** @type {Promise<{ duration: number, w: number, h: number } | null>} */ (mediaFactsOf.get(key));+}+/*** Serve `root`. HTML gets the host flag injected before any page script;* ?film-host=0 serves the page untouched, as a visitor would see it.diff --git a/packages/openfilm/src/render.mjs b/packages/openfilm/src/render.mjsindex 5b03eb6..5ca68bd 100644--- a/packages/openfilm/src/render.mjs+++ b/packages/openfilm/src/render.mjs@@ -1,11 +1,11 @@// film render: the deliverable. Frames are rendered in parallel page instances, averaged// over the shutter for motion blur, encoded with tagged BT.709 color, and muxed with the mix of the film's sounds.-import { spawnSync } from 'node:child_process';-import { mkdirSync, rmSync, writeFileSync } from 'node:fs';+import { spawn, spawnSync } from 'node:child_process';+import { mkdirSync, renameSync, rmSync, writeFileSync } from 'node:fs';import { availableParallelism, freemem, totalmem } from 'node:os';import { basename, dirname, extname, join, resolve } from 'node:path';import { clipKind, FILM_FILE } from './film-doc.mjs';-import { broken, encoders, hardwareEncoder, hardwareOptions, proresOptions } from './encoders.mjs';+import { broken, encoders, FRAMES_709, hardwareEncoder, hardwareOptions, prores709 } from './encoders.mjs';import { FilmPage, launch } from './host.mjs';import { exactRate, ffmpegRate } from './rate.mjs';import { browserCopies, filmVideos } from './seekable.mjs';@@ -17,8 +17,8 @@ const TAG_709 = ['-bsf:v', 'h264_metadata=colour_primaries=1:transfer_characteri/** Output formats: picture codec, pixel format, sound codec. Transparency needs .webm or .mov. */const FORMATS = {'.mp4': { video: ['-c:v', 'libx264', '-preset', 'medium', '-crf', '16', ...BT709], pix: 'scale=out_color_matrix=bt709:out_range=tv,format=yuv420p', audio: ['-c:a', 'aac', '-b:a', '192k'], tag: TAG_709 },- '.webm': { video: ['-c:v', 'libvpx-vp9', '-b:v', '0', '-crf', '20', '-row-mt', '1'], pix: 'format=yuva420p', audio: ['-c:a', 'libopus', '-b:a', '160k'], tag: [] },- '.mov': { video: ['-c:v', 'prores_ks', '-profile:v', '4444', '-vendor', 'apl0'], pix: 'format=yuva444p10le', audio: ['-c:a', 'pcm_s16le'], tag: [] },+ '.webm': { video: ['-c:v', 'libvpx-vp9', '-b:v', '0', '-crf', '20', '-row-mt', '1', ...BT709], pix: `scale=out_color_matrix=bt709:out_range=tv,format=yuva420p,${FRAMES_709}`, audio: ['-c:a', 'libopus', '-b:a', '160k'], tag: [] },+ '.mov': { ...prores709('prores_ks', true), audio: ['-c:a', 'pcm_s16le'], tag: [] },};/** Bits per pixel per frame for the chip's H.264: Studio's "high", with room for screen text (exports.mjs). */@@ -39,11 +39,21 @@ async function chipFormat(ext, size, fps) {}if (ext === '.mov') {const encoder = await hardwareEncoder('prores', have);- return encoder ? { encoder, format: proresOptions(encoder, true) } : null;+ return encoder ? { encoder, format: prores709(encoder, true) } : null;}return null;}+/** `from` renamed `to`, if the system lets it now: not onto another disk, nor a file Windows still holds a moment. @param {string} from @param {string} to */+function moved(from, to) {+ try {+ renameSync(from, to);+ return true;+ } catch {+ return false;+ }+}+/** The encoder a format's picture options name. @param {string[] | undefined} video */const encoderOf = (video = []) => video[video.indexOf('-c:v') + 1] ?? 'ffmpeg';@@ -76,7 +86,18 @@ export async function render(args, opts = {}) {if (clipKind(file) === 'video') videos.add(file);return copies.get(file) ?? null;};- const probe = await open(target, { ...(opts.root ? { root: opts.root } : {}), swap });+ /* `browsers` (a BrowserPool): the render borrows `workers` of its browsers, draws in a tab of its own in each, and+ gives them back; else it starts its own and closes them */+ const pool = opts.browsers ?? null;+ const lent = pool ? await pool.take(Math.max(1, Number(opts.workers ?? 1))) : [];+ let failed = true;+ let probe;+ try {+ probe = await open(target, { ...(opts.root ? { root: opts.root } : {}), swap, ...(pool ? { browser: lent[0] } : {}) });+ } catch (e) {+ await pool?.give(lent, { retire: true });+ throw e;+ }const { entry, dir, name, site, browser, isFilm, root } = probe;const meta = probe.film.meta;await probe.film.close();@@ -86,12 +107,12 @@ export async function render(args, opts = {}) {const fps = rate.fps;/* a page without a duration has no end: it is rendered by a range */const end = meta.duration ?? Infinity;- if (!range && meta.duration == null) { await probe.close(); throw new UsageError('this page has no duration, so it has no end: give a range to render, e.g. openfilm render 0-6'); }+ if (!range && meta.duration == null) { await probe.close(); await pool?.give(lent); throw new UsageError('this page has no duration, so it has no end: give a range to render, e.g. openfilm render 0-6'); }const [from, to] = range ? [range[0], Math.min(range[1], end)] : [0, end];const f0 = Math.round(from * fps);const f1 = Math.round(to * fps);const total = f1 - f0;- if (total <= 0) { await probe.close(); throw new UsageError(`nothing to render between ${fmt(from)} and ${fmt(to)}${meta.duration != null ? `: the film is ${fmt(meta.duration)} long` : ''}`); }+ if (total <= 0) { await probe.close(); await pool?.give(lent); throw new UsageError(`nothing to render between ${fmt(from)} and ${fmt(to)}${meta.duration != null ? `: the film is ${fmt(meta.duration)} long` : ''}`); }if (range && range[1] > end) console.error(`openfilm render: ${fmt(range[0])}-${fmt(range[1])} runs past the film's end (${fmt(meta.duration)}): rendered to there`);const scale = opts.scale ?? (opts['4k'] ? 3840 / meta.width : 1);const samples = Math.max(1, Number(opts.samples ?? (opts.blur ? 8 : 1)));@@ -103,16 +124,22 @@ export async function render(args, opts = {}) {: join(dir, `${name}${alpha ? '.webm' : '.mp4'}`)));mkdirSync(dirname(out), { recursive: true });const container = FORMATS[extname(out).toLowerCase()];- if (!container) { await probe.close(); throw new UsageError(`can't write ${extname(out)}: use .mp4, .webm or .mov`); }- if (alpha && extname(out).toLowerCase() === '.mp4') { await probe.close(); throw new UsageError('mp4 has no transparency: write .webm or .mov with --alpha'); }+ if (!container) { await probe.close(); await pool?.give(lent); throw new UsageError(`can't write ${extname(out)}: use .mp4, .webm or .mov`); }+ if (alpha && extname(out).toLowerCase() === '.mp4') { await probe.close(); await pool?.give(lent); throw new UsageError('mp4 has no transparency: write .webm or .mov with --alpha'); }/* a host may encode the container its own way (Studio's export: another codec, a quality, a frame around the film),and gets exactly that; else the picture goes through the graphics chip when it can */const own = { ...container, ...opts.format };+ /* `clear` (with `alpha`): the format for a page that lets anything through. The page is drawn with `format` while+ every frame is opaque, each frame's alpha read as it is drawn; at the first pixel that shows through it is drawn+ again with `clear`, in the same tabs (an editing project's page: H.264 when it can, PNG with its alpha when not).+ The result says which (`clear`) */+ const clearFormat = alpha && opts.clear ? { ...container, ...opts.clear } : null;+ let clear = false;const size = { w: Math.round(meta.width * scale), h: Math.round(meta.height * scale) };const chip = opts.format || opts.hardware === false ? null : await chipFormat(extname(out).toLowerCase(), size, fps);let format = chip ? { ...own, ...chip.format } : own;let encoder = chip?.encoder ?? encoderOf(own.video);- const workers = Math.max(1, Math.min(Number(opts.workers ?? defaultWorkers(machine(), total)), total));+ const workers = Math.max(1, Math.min(pool ? lent.length : Number(opts.workers ?? defaultWorkers(machine(), total)), total));/* the videos get copies this browser plays: those it does not decode here (ProRes; HEVC without a chip for it), andfor a render of more than a moment those that seek slowly (seekable.mjs). A film's video clips load only whentheir time comes, so they are read from film.html (a page drawn around the project's film, as a burned-in@@ -120,10 +147,17 @@ export async function render(args, opts = {}) {if (opts.seekable !== false) {const film = isFilm ? entry : opts.root ? FILM_FILE : null;const files = [...new Set([...videos, ...(film ? filmVideos(root, film) : [])])];- copies = await browserCopies(browser, site.url, root, files, { signal: opts.signal, slow: total >= fps * 2 });+ try {+ copies = await browserCopies(browser, site.url, root, files, { signal: opts.signal, slow: total >= fps * 2 });+ } catch (e) {+ await probe.close();+ await pool?.give(lent, { retire: true });+ throw e;+ }}if (!process.stderr.isTTY && !opts.onProgress) process.stderr.write(` rendering ${total} frames with ${workers} browser${workers > 1 ? 's' : ''}…\n`);+ /* a pool's process draws several renders at once, each of its own film folder (Studio's export makes one per page) */const work = join(outDir(dir, 'render'), `.parts-${process.pid}`);mkdirSync(work, { recursive: true });let done = 0;@@ -150,72 +184,114 @@ export async function render(args, opts = {}) {cancelled.catch(() => {});/** @type {import('playwright-core').Browser[]} */const browsers = [];+ /** @type {FilmPage[]} the tabs drawing, closed at the end (a pool's browsers stay open) */+ const tabs = [];/* what is left of a step given up fails as the browsers close: that is no one's error */- const unlessCancelled = (/** @type {Promise<any>} */ step) => { step.catch(() => {}); return Promise.race([step, cancelled]); };+ /* a browser that closes while drawing (a crash, or the system ending it for memory) ends the render at once: its tab may never+ answer again, and nothing it was drawing is waited for */+ const gone = new Error('the browser drawing the page closed (it crashed, or the system ended it)');+ /** @type {(e: Error) => void} */+ let crash = () => {};+ const crashed = new Promise((_, fail) => { crash = fail; });+ crashed.catch(() => {});+ /** @type {(() => void)[]} */+ const unwatch = [];+ const watch = (/** @type {import('playwright-core').Browser} */ b) => {+ const closed = () => crash(gone);+ b.on('disconnected', closed);+ unwatch.push(() => b.off('disconnected', closed));+ if (!b.isConnected()) closed();+ };+ const unlessCancelled = (/** @type {Promise<any>} */ step) => { step.catch(() => {}); return Promise.race([step, cancelled, crashed]); };try {/* a browser each: pages sharing one browser share its GPU process, and stop getting faster after three or four */- const more = await unlessCancelled(Promise.all(Array.from({ length: workers - 1 }, () => launch().then((b) => { browsers.push(b); return b; }))));- const pages = await unlessCancelled(Promise.all([browser, ...more].map((b) => FilmPage.open(b, site.url, entry, { scale, transparent: alpha }))));+ const more = pool ? lent.slice(1, workers) : await unlessCancelled(Promise.all(Array.from({ length: workers - 1 }, () => launch().then((b) => { browsers.push(b); return b; }))));+ for (const b of [browser, ...more]) watch(b);+ const opening = [browser, ...more].map((b) => FilmPage.open(b, site.url, entry, { scale, transparent: alpha }).then((p) => { tabs.push(p); return p; }));+ const pages = await unlessCancelled(Promise.all(opening));/* a film paints its stage behind its clips (black, or its own background): with alpha, only what the clips draw is kept */if (alpha && isFilm) await Promise.all(pages.map((p) => p.page.addStyleTag({ content: 'html, body { background: transparent !important; }' })));const per = Math.ceil(total / workers);const parts = pages.map((film, i) => ({ film, a: f0 + i * per, b: Math.min(f1, f0 + (i + 1) * per), file: join(work, `part-${i}${extname(out)}`) })).filter((p) => p.a < p.b);- /** Every part, encoded with `f`: one that fails stops the others before its error is told. @param {typeof format} f */- const encodeAll = async (f) => {+ /** Every part, encoded with `f`: one that fails stops the others before its error is told. @param {typeof format} f @param {boolean} opaque */+ const encodeAll = async (f, opaque) => {const stop = new AbortController();const quit = () => stop.abort();opts.signal?.addEventListener('abort', quit, { once: true });- const runs = parts.map((p) => encode(p, { fps, rate, samples, shutter, duration: end, format: f, onFrame: progress, still: alpha ? 'png' : 'jpeg', signal: stop.signal }));+ const runs = parts.map((p) => encode(p, { fps, rate, samples, shutter, duration: end, first: f0, format: f, onFrame: progress, still: alpha ? 'png' : 'jpeg', opaque, signal: stop.signal }));try {await unlessCancelled(Promise.all(runs));} catch (e) {stop.abort();- await Promise.allSettled(runs);- throw outOfMemory(e, parts.length);+ if (e !== gone) await Promise.allSettled(runs);+ throw e === gone ? e : outOfMemory(e, parts.length);} finally {opts.signal?.removeEventListener('abort', quit);}};+ /** The frames with `f`, watched for a pixel that shows through while the page may be opaque. @param {typeof format} f */+ const drawAll = async (f) => {+ if (!clearFormat || clear) return encodeAll(f, false);+ try {+ await encodeAll(f, true);+ } catch (e) {+ if (/** @type {{ code?: unknown }} */ (e)?.code !== 'CLEAR') throw e;+ /* a pixel shows through: every part stopped between two frames, and the frames are drawn again with alpha */+ clear = true;+ format = clearFormat;+ encoder = encoderOf(format.video);+ done = 0;+ told = 0;+ await encodeAll(format, false);+ }+ };try {- await encodeAll(format);+ await drawAll(format);} catch (e) {/* the chip's encoder gave up partway (a driver, a size it does not take): the frames again without it, and therest of this launch does without it */- if (!chip || opts.signal?.aborted || !/^ffmpeg/.test(e instanceof Error ? e.message : '')) throw e;+ if (!chip || clear || opts.signal?.aborted || !/^ffmpeg/.test(e instanceof Error ? e.message : '')) throw e;broken(chip.encoder);format = own;encoder = encoderOf(own.video);done = 0;told = 0;- await encodeAll(format);+ await drawAll(format);}if (process.stderr.isTTY && !opts.onProgress) process.stderr.write('\n');for (const p of pages) problems.push(...p.problems);- /* names, not paths: concat reads them beside the list, and a path can hold a `'` (C:\\Users\\O'Brien) */- writeFileSync(join(work, 'list.txt'), parts.map((p) => `file '${basename(p.file)}'`).join('\n'));- const video = join(work, `video${extname(out)}`);- await ffmpeg(['-f', 'concat', '-safe', '0', '-i', join(work, 'list.txt'), '-c', 'copy', video]).done;const clips = opts.silent ? [] : await filmSound(probe, from);if (opts.signal?.aborted) throw new Error('cancelled');- if (clips.length) {- const sound = join(work, `sound${extname(out) === '.mp4' ? '.m4a' : extname(out) === '.webm' ? '.webm' : '.wav'}`);- await mix(clips, to - from, sound, format.audio);- await ffmpeg(['-i', video, '-i', sound, '-map', '0:v', '-map', '1:a', '-c', 'copy', ...format.tag, ...(extname(out) === '.mp4' ? ['-movflags', '+faststart'] : []), out]).done;- } else {- await ffmpeg(['-i', video, '-c', 'copy', ...format.tag, ...(extname(out) === '.mp4' ? ['-movflags', '+faststart'] : []), out]).done;+ const sound = clips.length ? join(work, `sound${extname(out) === '.mp4' ? '.m4a' : extname(out) === '.webm' ? '.webm' : '.wav'}`) : null;+ if (sound) await mix(clips, to - from, sound, format.audio);+ const after = [...format.tag, ...(extname(out) === '.mp4' ? ['-movflags', '+faststart'] : [])];+ /* the parts straight into `out`, with the sound: a page's frames are on the disk twice at most while it is written,+ not three times (a long page drawn as PNG for an editing project is gigabytes). A part with nothing to add is+ `out` already, moved into place */+ if (parts.length > 1 || sound || after.length || !moved(parts[0].file, out)) {+ /* names, not paths: concat reads them beside the list, and a path can hold a `'` (C:\\Users\\O'Brien) */+ if (parts.length > 1) writeFileSync(join(work, 'list.txt'), parts.map((p) => `file '${basename(p.file)}'`).join('\n'));+ const video = parts.length > 1 ? ['-f', 'concat', '-safe', '0', '-i', join(work, 'list.txt')] : ['-i', parts[0].file];+ await ffmpeg([...video, ...(sound ? ['-i', sound, '-map', '0:v', '-map', '1:a'] : []), '-c', 'copy', ...after, out]).done;}+ failed = false;} finally {/* Windows lets go of a file a moment after ffmpeg exits, and later still after one that failed: a folder itstill holds is left behind rather than told in place of what went wrong */+ for (const off of unwatch) off();try { rmSync(work, { recursive: true, force: true, maxRetries: 5 }); } catch {}await Promise.allSettled(browsers.map((b) => b.close()));+ if (pool) await Promise.allSettled(tabs.map((p) => p.close()));await probe.close();+ /* a render that failed or was cancelled may have left a browser stuck: those are not lent again */+ await pool?.give(lent, { retire: failed });}const seconds = (Date.now() - started) / 1000;return {out,encoder,+ ...(clearFormat ? { clear } : {}),lines: [`→ ${shown(out)}`,` ${total} frames · ${fmt(to - from)} at ${Number(fps.toFixed(3))} fps · ${Math.round(meta.width * scale)}×${Math.round(meta.height * scale)}${samples > 1 ? ` · ${samples} samples/frame` : ''}${alpha ? ' · alpha' : ''} · ${workers} worker${workers > 1 ? 's' : ''} · ${encoder} · ${seconds.toFixed(1)} s`,@@ -266,37 +342,90 @@ export function defaultWorkers({ cores, available } = machine(), frames = Infinireturn Math.max(1, Math.min(MAX_WORKERS, cores - 1, Math.floor((available - RESERVE) / WORKER_MEMORY), Math.floor(frames / FRAMES_PER_WORKER)));}+/** What a part drawn opaque (render's `clear`) stops with: a pixel that shows through, or alpha that could not be read. @param {string} where */+const showsThrough = (where) => Object.assign(new Error(`a pixel shows through ${where}`), { code: 'CLEAR' });++/**+ * Each frame's least alpha, read as the frames are drawn: an ffmpeg beside the encoder takes the same pictures (a+ * picture with nothing clear comes from the browser as a PNG with no alpha, read as opaque). It starts at the first+ * picture (no probing: the stream is known), decodes one at a time (threads would hold a dozen back) and says each+ * frame's at once (`direct`: not held for a buffer to fill), so a render stops a few frames after a pixel shows+ * through. `clear` turns true at that pixel; `read` counts the frames read; `ended`, its exit code.+ */+function alphaWatch() {+ const proc = spawn('ffmpeg', ['-hide_banner', '-nostdin', '-loglevel', 'error', '-probesize', '32', '-analyzeduration', '0', '-threads', '1', '-f', 'image2pipe', '-c:v', 'png', '-i', '-',+ '-vf', 'format=rgba,alphaextract,format=gray,signalstats,metadata=print:key=lavfi.signalstats.YMIN:file=-:direct=1', '-f', 'null', '-'], { stdio: ['pipe', 'pipe', 'ignore'], windowsHide: true });+ proc.stdin.on('error', () => {});+ const watch = { proc, read: 0, clear: false, ended: new Promise((done) => { proc.on('error', () => done(-1)); proc.on('close', (code) => done(code)); }) };+ let rest = '';+ proc.stdout.on('data', (d) => {+ const lines = (rest + d).split('\n');+ rest = lines.pop() ?? '';+ for (const line of lines) {+ const m = /YMIN=(\d+)/.exec(line);+ if (!m) continue;+ watch.read++;+ if (Number(m[1]) < 255) watch.clear = true;+ }+ });+ return watch;+}+/** Frames [a, b) of one page into one file; sub-frames are averaged by ffmpeg (tmix) and every n-th kept. */-async function encode({ film, a, b, file }, { fps, rate, samples, shutter, duration, format, onFrame, still, signal }) {+async function encode({ film, a, b, file }, { fps, rate, samples, shutter, duration, first = a, format, onFrame, still, opaque, signal }) {const blur = samples > 1 ? `tmix=frames=${samples},select='eq(mod(n\\,${samples})\\,${samples - 1})',setpts=N/((${ffmpegRate(rate)})*TB),` : '';const enc = ffmpeg(['-f', 'image2pipe', '-framerate', ffmpegRate(rate, samples), '-c:v', still === 'png' ? 'png' : 'mjpeg', '-i', '-','-vf', blur + format.pix, '-r', ffmpegRate(rate), ...format.video, file], { input: true });+ /* a page drawn opaque: its alpha read beside the encoder, every frame */+ const watch = opaque ? alphaWatch() : null;/* a cancelled render stops its encoder at once: the frames ffmpeg still holds are thrown away, not encoded (a 4Kencode takes seconds to drain); the parts folder goes with it (see render's finally) */- const stop = () => kill(enc.proc);+ const stop = () => { kill(enc.proc); if (watch) kill(watch.proc); };enc.proc.stdin.on('error', () => {});+ /* a watch that ends before the last frame cannot say the page is opaque */+ const unwatched = watch?.ended.then(() => { throw showsThrough('(its alpha could not be read)'); });+ unwatched?.catch(() => {});/* an ffmpeg that ends before its last frame (not found, an encoder that fails to start) never drains its input:the frame being written fails with ffmpeg's own error instead of waiting for it forever */const ended = enc.done.then(() => { throw new Error('ffmpeg ended before the last frame'); });ended.catch(() => {});signal?.addEventListener('abort', stop, { once: true });try {+ /* a tab's first two pictures come out a little off the rest (a transparent page's soft edges, by a level or two):+ two are taken and thrown away first, of the two frames before this part (of its first frame for the render's+ first part), so a frame is the same however many browsers share the render */+ for (const f of [a - 2, a - 1].map((n) => Math.max(first, n))) {+ await film.seek(Math.min(Math.max(0, f / fps), duration - 1e-4));+ await film.capture(still === 'png' ? { format: 'png' } : { format: 'jpeg', quality: 95 });+ }for (let f = a; f < b; f++) {if (signal?.aborted) {stop();throw new Error('cancelled');}+ if (watch?.clear) {+ stop();+ throw showsThrough(`by ${fmt(f / fps)}`);+ }for (let k = 0; k < samples; k++) {const t = (f + (samples > 1 ? (k / samples - 0.5) * shutter : 0)) / fps;await film.seek(Math.min(Math.max(0, t), duration - 1e-4));const frame = await film.capture(still === 'png' ? { format: 'png' } : { format: 'jpeg', quality: 95 });await Promise.race([write(enc.proc.stdin, frame), ended]);+ if (watch) await Promise.race([write(watch.proc.stdin, frame), unwatched]);}onFrame();}} finally {signal?.removeEventListener('abort', stop);enc.proc.stdin.end();+ watch?.proc.stdin.end();}await enc.done;+ /* opaque only when every frame was read and none let anything through */+ if (watch) {+ const code = await watch.ended;+ if (watch.clear) throw showsThrough('in its last frames');+ if (code !== 0 || watch.read !== (b - a) * samples) throw showsThrough(`(its alpha could not be read: ${watch.read} of ${(b - a) * samples} frames, exit ${code})`);+ }}diff --git a/packages/openfilm/src/render.test.mjs b/packages/openfilm/src/render.test.mjsindex da14ead..68073e0 100644--- a/packages/openfilm/src/render.test.mjs+++ b/packages/openfilm/src/render.test.mjs@@ -1,12 +1,14 @@import { test } from 'node:test';import assert from 'node:assert/strict';import { execFileSync, spawn, spawnSync } from 'node:child_process';-import { chmodSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';+import { chmodSync, existsSync, mkdirSync, mkdtempSync, rmSync, statSync, writeFileSync } from 'node:fs';import { getPriority, tmpdir, totalmem } from 'node:os';import { join } from 'node:path';import { fileURLToPath } from 'node:url';+import { BrowserPool } from './browser-pool.mjs';import { encoders, hardwareEncoder } from './encoders.mjs';import { filmHtml } from './film-doc.mjs';+import { launch } from './host.mjs';import { defaultWorkers, machine, outOfMemory, render } from './render.mjs';const hasFfmpeg = (() => { try { execFileSync('ffmpeg', ['-version'], { stdio: 'ignore' }); return true; } catch { return false; } })();@@ -148,3 +150,90 @@ test('a render runs below the person\'s own apps, with every process it starts',rmSync(dir, { recursive: true, force: true });}});++test('a transparent page comes out the same however many browsers share it', { skip: !hasFfmpeg && 'no ffmpeg', timeout: 180_000 }, async () => {+ const dir = mkdtempSync(join(tmpdir(), 'of-render-split-'));+ try {+ /* soft, see-through shapes turning over nothing: where a tab's first pictures used to come out a level off */+ writeFileSync(join(dir, 'page.html'), `<!doctype html><style>html,body{margin:0;width:960px;height:540px;background:transparent}+.b{position:absolute;width:120px;height:120px;border-radius:24px;background:linear-gradient(135deg,hsla(200,80%,60%,.8),hsla(320,80%,40%,.6));box-shadow:0 16px 48px rgba(0,0,0,.4)}</style>+<body><script>const els=[];for(let k=0;k<24;k++){const d=document.createElement('div');d.className='b';document.body.appendChild(d);els.push(d)}+window.film={duration:2,width:960,height:540,frame(t){els.forEach((d,k)=>{const a=t*0.9+k*0.37;d.style.transform='translate('+(480+Math.cos(a)*(150+k*8)-60)+'px,'+(270+Math.sin(a*1.3)*(100+k*4)-60)+'px) rotate('+(a*60)+'deg) scale('+(0.6+0.4*Math.sin(a+k))+')'})}};</script></body>`);+ writeFileSync(join(dir, 'film.html'), filmHtml({ stage: { w: 960, h: 540 }, tracks: [{ clips: [{ src: 'page.html', id: 'page' }] }] }));+ const format = { video: ['-c:v', 'png'], pix: 'format=rgba' };+ const sums = (file) => execFileSync('ffmpeg', ['-v', 'error', '-i', file, '-vf', 'format=rgba', '-f', 'framemd5', '-']).toString().split('\n').filter((l) => l && !l.startsWith('#')).map((l) => l.split(',').pop());+ const drawn = [];+ for (const workers of [1, 3]) {+ const out = join(dir, `w${workers}.mov`);+ await render([dir], { out, workers, alpha: true, silent: true, onProgress() {}, format });+ drawn.push(sums(out));+ }+ assert.equal(drawn[0].length, 60);+ assert.deepEqual(drawn[1].map((s, i) => (s === drawn[0][i] ? '' : i)).filter(String), [], 'no frame differs');+ } finally {+ rmSync(dir, { recursive: true, force: true });+ }+});++test('a render is written once: its parts straight into the file with the sound, and one part with nothing to add moved there', { skip: !hasFfmpeg && 'no ffmpeg', timeout: 180_000 }, async () => {+ const dir = mkdtempSync(join(tmpdir(), 'of-render-once-'));+ try {+ writeFileSync(join(dir, 'p.html'), '<!doctype html><body style="margin:0"><script>window.film = { duration: 1, width: 320, height: 180, frame(t) { document.body.style.background = `hsl(${t * 360}, 80%, 50%)`; } };</script>');+ execFileSync('ffmpeg', ['-v', 'error', '-f', 'lavfi', '-i', 'sine=frequency=440:duration=1', join(dir, 'tone.wav')]);+ writeFileSync(join(dir, 'film.html'), filmHtml({ stage: { w: 320, h: 180 }, tracks: [{ clips: [{ id: 'p', src: 'p.html', at: 0, time: [0, 1] }] }, { clips: [{ id: 'tone', src: 'tone.wav' }] }] }));+ const probe = (file) => JSON.parse(execFileSync('ffprobe', ['-v', 'error', '-count_frames', '-show_entries', 'stream=codec_type,nb_read_frames,duration', '-of', 'json', file], { encoding: 'utf8' })).streams;+ for (const [ext, workers] of [['.mp4', 1], ['.mp4', 2], ['.mov', 1], ['.mov', 2]]) {+ const streams = probe((await render([dir], { out: join(dir, `w${workers}${ext}`), workers, onProgress() {} })).out);+ assert.equal(Number(streams.find((s) => s.codec_type === 'video')?.nb_read_frames), 30, `${ext} by ${workers}: its frames`);+ assert.ok(Math.abs(Number(streams.find((s) => s.codec_type === 'audio')?.duration) - 1) < 0.05, `${ext} by ${workers}: its sound`);+ }+ /* the part a browser writes is the file, not copied: the same one on the disk */+ const part = join(dir, '.film', 'cache', 'render', `.parts-${process.pid}`, 'part-0.mov');+ let ino = null;+ const look = setInterval(() => { if (existsSync(part)) ino = statSync(part).ino; }, 5);+ const quiet = join(dir, 'quiet.mov');+ try {+ await render([dir], { out: quiet, workers: 1, silent: true, onProgress() {} });+ } finally {+ clearInterval(look);+ }+ assert.ok(ino != null, 'the part was seen while drawn');+ /* Windows may hold the part a moment after ffmpeg ends (an antivirus reading it): then it is copied, as before */+ if (process.platform !== 'win32') assert.equal(statSync(quiet).ino, ino);+ assert.equal(probe(quiet).length, 1, 'no sound asked for');+ } finally {+ rmSync(dir, { recursive: true, force: true });+ }+});++test('a page drawn opaque until a pixel shows through is drawn again with its alpha, in the same tabs', { skip: !hasFfmpeg && 'no ffmpeg', timeout: 180_000 }, async () => {+ const dir = mkdtempSync(join(tmpdir(), 'of-render-opaque-'));+ let started = 0;+ const pool = new BrowserPool({ start: () => { started++; return launch(); } });+ try {+ /* both fill the frame for 4 s; `blink` lets everything through at 0.4 s (frame 10 at 25 fps) */+ const page = (clearAt) => `<!doctype html><body style="margin:0;width:320px;height:180px"><script>window.film={duration:4,width:320,height:180,frame(t){+document.body.style.background=${clearAt === null ? 'false' : `Math.abs(t-${clearAt})<0.01`}?'transparent':'hsl('+Math.round(t*90)+',70%,40%)'}}</script></body>`;+ writeFileSync(join(dir, 'blink.html'), page(0.4));+ writeFileSync(join(dir, 'solid.html'), page(null));+ const formats = { format: { video: ['-c:v', 'libx264', '-crf', '16'], pix: 'format=yuv420p' }, clear: { video: ['-c:v', 'png'], pix: 'format=rgba' } };+ const ask = (name, onProgress = () => {}) => render([join(dir, `${name}.html`)], { alpha: true, silent: true, fps: 25, workers: 1, browsers: pool, ...formats, out: join(dir, `${name}.mov`), onProgress });+ const probe = (file) => execFileSync('ffprobe', ['-v', 'error', '-count_frames', '-show_entries', 'stream=codec_name,pix_fmt,nb_read_frames', '-of', 'csv=p=0', file]).toString().trim();+ const drawn = [];+ const blink = await ask('blink', (done) => drawn.push(done));+ assert.deepEqual([blink.clear, blink.encoder], [true, 'png']);+ /* opaque to a few frames past the clear one, then again from the first */+ const again = drawn.findIndex((d, i) => i && d < drawn[i - 1]);+ assert.ok(again > 0 && drawn[again - 1] >= 10 && drawn[again - 1] < 40, `drawn again after ${drawn[again - 1]} of 100 frames`);+ assert.equal(probe(blink.out), 'png,rgba,100');+ const at10 = execFileSync('ffmpeg', ['-v', 'error', '-i', blink.out, '-vf', 'select=eq(n\\,10),format=rgba,crop=1:1:10:10', '-frames:v', '1', '-f', 'rawvideo', '-']);+ assert.equal(at10[3], 0, 'that frame clear');+ const solid = await ask('solid');+ assert.deepEqual([solid.clear, solid.encoder], [false, 'libx264']);+ assert.equal(probe(solid.out), 'h264,yuv420p,100', 'every frame read and none clear: written as it was drawn');+ assert.equal(started, 1, 'one browser for both pages');+ } finally {+ await pool.close();+ rmSync(dir, { recursive: true, force: true });+ }+});diff --git a/packages/openfilm/src/seekable.mjs b/packages/openfilm/src/seekable.mjsindex a6bb745..322ef19 100644--- a/packages/openfilm/src/seekable.mjs+++ b/packages/openfilm/src/seekable.mjs@@ -5,11 +5,12 @@// frame every quarter second, all but lossless (with its alpha, when it has one), made once and kept in the project's// cache; the render's pages are served the copy, and the sound is still mixed from the original.import { execFile } from 'node:child_process';+import { randomBytes } from 'node:crypto';import { existsSync, readFileSync, renameSync, rmSync, statSync } from 'node:fs';import { extname, join, relative, sep } from 'node:path';import { colorTagsOf, encoders, h264ColorTags, hardwareEncoder, hardwareOptions, onTheChip } from './encoders.mjs';import { kindOf, readFilmFile, srcHref } from './film-doc.mjs';-import { ffmpeg, fileOf, hash, outDir } from './shared.mjs';+import { ffmpeg, fileOf, hash, kill, outDir } from './shared.mjs';/** A key frame further apart than this is slow to seek to frame by frame. */const SLOW_GOP_S = 0.5;@@ -198,32 +199,74 @@ export async function seekableCopies(files, root, { signal, plays, slow = true }if (!need) continue;/* with an alpha to keep: VP9, which every Chromium decodes with its alpha; else H.264 */const out = `${base}${need.alpha ? '.webm' : '.mp4'}`;- const gop = Math.max(1, Math.round(need.fps * COPY_GOP_S));- const part = `${out}.part${extname(out)}`;- /* all but lossless: about 19 Mbps at 1080p and 30 fps; the chip's encoder keeps the source's color tags only- when told them */- const bits = Math.min(120e6, Math.max(4e6, ((need.w * need.h) || 1920 * 1080) * need.fps * 0.3));- /* decoded and written on the graphics chip when it can (encoders.mjs), else on the processor */- const make = async (/** @type {string | null} */ encoder, /** @type {string[]} */ decoding) => {- /* the same frames at the same times (passthrough: no frame is added or dropped), the picture only */- const run = ffmpeg([...decoding, '-i', file, '-map', '0:v:0', '-an', '-sn', '-dn', '-fps_mode', 'passthrough',- ...(need.alpha ? ['-c:v', 'libvpx-vp9', '-pix_fmt', 'yuva420p', '-b:v', '0', '-crf', '18', '-row-mt', '1', '-deadline', 'realtime', '-cpu-used', '8',- '-g', String(gop), '-keyint_min', String(gop), '-auto-alt-ref', '0']- : encoder ? ['-pix_fmt', 'nv12', ...hardwareOptions(encoder, bits, { gop }), ...h264ColorTags(await colorTagsOf(file))]- : ['-c:v', 'libx264', '-preset', 'superfast', '-crf', '16', '-g', String(gop), '-keyint_min', String(gop), '-pix_fmt', 'yuv420p']), part]);- const stop = () => run.proc.kill('SIGKILL');- signal?.addEventListener('abort', stop, { once: true });- try { await run.done; } finally { signal?.removeEventListener('abort', stop); }+ /* one copy of a file at a time in this process (pages drawn at once may share a source): a render asking for one+ being made waits for it, and makes it itself when the render making it was cancelled */+ const shared = () => {+ let job = making.get(out);+ if (!job) {+ job = makeCopy(file, out, need, signal).finally(() => making.delete(out));+ making.set(out, job);+ }+ return job;};- try {- await onTheChip(need.alpha ? null : await hardwareEncoder('h264', await encoders()), make, signal);- renameSync(part, out);- known(out);- } catch {- rmSync(part, { force: true });- }+ let result = await shared();+ if (result === 'cancelled' && !signal?.aborted) result = await shared();+ if (result === 'made') known(out);}};await Promise.all([worker(), worker()]);return copies;}++/** @type {Map<string, Promise<'made' | 'failed' | 'cancelled'>>} the copies being made in this process, by their path */+const making = new Map();++/**+ * `file`'s copy written to `out`, under a name of its own until it is whole: another process making the same copy+ * never writes the same file, and the one renamed last is kept (the same picture). One that cannot replace a copy+ * already there (Windows, while a browser reads it) uses that copy.+ * @param {string} file @param {string} out @param {{ fps: number, w: number, h: number, alpha: boolean }} need @param {AbortSignal} [signal]+ * @returns {Promise<'made' | 'failed' | 'cancelled'>}+ */+async function makeCopy(file, out, need, signal) {+ if (signal?.aborted) return 'cancelled';+ const gop = Math.max(1, Math.round(need.fps * COPY_GOP_S));+ const part = `${out}.part-${process.pid}-${randomBytes(4).toString('hex')}${extname(out)}`;+ /* all but lossless: about 19 Mbps at 1080p and 30 fps; the chip's encoder keeps the source's color tags only+ when told them */+ const bits = Math.min(120e6, Math.max(4e6, ((need.w * need.h) || 1920 * 1080) * need.fps * 0.3));+ /* decoded and written on the graphics chip when it can (encoders.mjs), else on the processor */+ const make = async (/** @type {string | null} */ encoder, /** @type {string[]} */ decoding) => {+ /* the same frames at the same times (passthrough: no frame is added or dropped), the picture only */+ const run = ffmpeg([...decoding, '-i', file, '-map', '0:v:0', '-an', '-sn', '-dn', '-fps_mode', 'passthrough',+ ...(need.alpha ? ['-c:v', 'libvpx-vp9', '-pix_fmt', 'yuva420p', '-b:v', '0', '-crf', '18', '-row-mt', '1', '-deadline', 'realtime', '-cpu-used', '8',+ '-g', String(gop), '-keyint_min', String(gop), '-auto-alt-ref', '0']+ : encoder ? ['-pix_fmt', 'nv12', ...hardwareOptions(encoder, bits, { gop }), ...h264ColorTags(await colorTagsOf(file))]+ : ['-c:v', 'libx264', '-preset', 'superfast', '-crf', '16', '-g', String(gop), '-keyint_min', String(gop), '-pix_fmt', 'yuv420p']), part]);+ /* its whole tree: an ffmpeg that is a shim (Windows' package managers) runs the real one as its child */+ const stop = () => kill(run.proc);+ signal?.addEventListener('abort', stop, { once: true });+ try { await run.done; } finally { signal?.removeEventListener('abort', stop); }+ };+ /* a part that cannot be removed (Windows, a moment after its encoder ends) stays in the cache folder: it never fails+ the render that asked for the copy */+ const drop = () => { try { rmSync(part, { force: true }); } catch { /* left */ } };+ try {+ await onTheChip(need.alpha ? null : await hardwareEncoder('h264', await encoders()), make, signal);+ } catch {+ drop();+ return signal?.aborted ? 'cancelled' : 'failed';+ }+ /* a file a virus scanner or the indexer holds for a moment refuses to move on Windows (EPERM, EACCES, EBUSY): tried+ again for a few seconds before the copy counts as not made */+ for (let i = 1; ; i++) {+ try {+ renameSync(part, out);+ return 'made';+ } catch (e) {+ if (existsSync(out)) { drop(); return 'made'; }+ if (i >= 12 || !['EPERM', 'EACCES', 'EBUSY'].includes(/** @type {NodeJS.ErrnoException} */ (e).code ?? '')) { drop(); return 'failed'; }+ await new Promise((r) => setTimeout(r, i * 100));+ }+ }+}diff --git a/packages/openfilm/src/seekable.test.mjs b/packages/openfilm/src/seekable.test.mjsindex 0de6ede..6461740 100644--- a/packages/openfilm/src/seekable.test.mjs+++ b/packages/openfilm/src/seekable.test.mjs@@ -1,6 +1,6 @@import { test } from 'node:test';import assert from 'node:assert/strict';-import { execFileSync } from 'node:child_process';+import { execFile, execFileSync } from 'node:child_process';import { existsSync, mkdtempSync, readdirSync, rmSync, writeFileSync } from 'node:fs';import { tmpdir } from 'node:os';import { join } from 'node:path';@@ -109,6 +109,55 @@ test('a copy once made is found for each request after it, and none before', ffm}});+test('renders asking for the same copy at once share one, whole; one cancelled leaves it to the other', ffmpeg, async () => {+ const dir = mkdtempSync(join(tmpdir(), 'of-seekable-shared-'));+ try {+ const file = join(dir, 'prores.mov');+ execFileSync('ffmpeg', ['-v', 'error', '-y', '-f', 'lavfi', '-i', 'testsrc2=size=1920x1080:rate=30', '-t', '8', '-c:v', 'prores_ks', '-profile:v', '3', file]);+ const ask = { plays: async () => false, slow: false };+ const seek = join(dir, '.film', 'cache', 'seek');+ /* the first starts the copy; the second asks a moment later and waits on it; the first is cancelled partway: the+ second makes the copy itself */+ const quit = new AbortController();+ const first = seekableCopies([file], dir, { ...ask, signal: quit.signal });+ const later = { plays: () => new Promise((r) => setTimeout(() => r(false), 100)), slow: false };+ const second = seekableCopies([file], dir, later);+ setTimeout(() => quit.abort(), 400);+ const [a, b] = await Promise.all([first, second]);+ assert.equal(a.size, 0, 'the cancelled one has none');+ const copy = /** @type {string} */ (b.get(file));+ assert.ok(copy);+ assert.equal(times(copy).length, 240, 'every frame');+ assert.deepEqual(readdirSync(seek), [copy.split(/[\\/]/).pop()], 'the copy alone: no part left');+ rmSync(seek, { recursive: true, force: true });+ const both = await Promise.all([seekableCopies([file], dir, ask), seekableCopies([file], dir, ask)]);+ assert.equal(both[0].get(file), both[1].get(file));+ assert.equal(times(/** @type {string} */ (both[0].get(file))).length, 240);+ assert.equal(readdirSync(seek).length, 1);+ } finally {+ rmSync(dir, { recursive: true, force: true });+ }+});++test('two processes making the same copy at once never write the same file', ffmpeg, async () => {+ const dir = mkdtempSync(join(tmpdir(), 'of-seekable-two-'));+ try {+ const file = join(dir, 'prores.mov');+ execFileSync('ffmpeg', ['-v', 'error', '-y', '-f', 'lavfi', '-i', 'testsrc2=size=640x360:rate=30', '-t', '3', '-c:v', 'prores_ks', '-profile:v', '3', file]);+ const code = `const { seekableCopies } = await import(${JSON.stringify(new URL('./seekable.mjs', import.meta.url).href)});+const copies = await seekableCopies([${JSON.stringify(file)}], ${JSON.stringify(dir)}, { plays: async () => false, slow: false });+console.log(copies.get(${JSON.stringify(file)}) ?? '');`;+ const run = () => new Promise((done, fail) => execFile(process.execPath, ['--input-type=module', '-e', code], (e, out) => (e ? fail(e) : done(String(out).trim()))));+ const [a, b] = await Promise.all([run(), run()]);+ assert.ok(a);+ assert.equal(a, b);+ assert.equal(times(a).length, 90, 'whole');+ assert.equal(readdirSync(join(dir, '.film', 'cache', 'seek')).length, 1, 'no part left');+ } finally {+ rmSync(dir, { recursive: true, force: true });+ }+});+test('look shows ProRes footage, which no browser decodes, through its copy', ffmpeg, async () => {const dir = mkdtempSync(join(tmpdir(), 'of-seekable-prores-look-'));try {diff --git a/packages/openfilm/src/shared.d.mts b/packages/openfilm/src/shared.d.mtsindex 416d63b..13f1904 100644--- a/packages/openfilm/src/shared.d.mts+++ b/packages/openfilm/src/shared.d.mts@@ -1,6 +1,6 @@/** A sound's fades in the mix: `in` / `out` film seconds of ramp, `length` its film seconds, `skip` seconds cut off its start. */export interface MixFade { in: number; out: number; length: number; skip: number }-export interface MixClip { file: string; at: number; from: number; to?: number; volume: number; speed?: number; fade?: MixFade }+export interface MixClip { file: string; at: number; from: number; to?: number; volume: number; speed?: number; fade?: MixFade; channels?: number }export declare function audioClips(root: string, entry: string, clips: Array<{ src: string; at?: number; from?: number; to?: number; volume?: number; speed?: number; fade?: readonly [number, number] }> | undefined, from?: number): MixClip[];export declare function fadeFilter(fade: MixFade | undefined): string;export declare function mix(clips: MixClip[], duration: number, file: string, codec?: string[]): Promise<void>;@@ -8,6 +8,8 @@ export declare function mix(clips: MixClip[], duration: number, file: string, coexport declare function mixGraph(clips: MixClip[], duration: number): { inputs: string[]; filter: string };/** How long a mix's command may be before its graph goes in a file. */export declare const COMMAND_ROOM: number;+/** The option that reads a filter graph from a file, for this ffmpeg: `-/filter_complex` from 7 on, `-filter_complex_script` before. */+export declare function filterScriptOption(): Promise<string>;/** A film opened in a browser to be drawn: what `look` and `render` start from. */export interface OpenFilm {root: string; entry: string; dir: string; name: string;diff --git a/packages/openfilm/src/shared.mjs b/packages/openfilm/src/shared.mjsindex 19a65e5..9ab1575 100644--- a/packages/openfilm/src/shared.mjs+++ b/packages/openfilm/src/shared.mjs@@ -64,7 +64,8 @@ export function parseTarget(args) {/*** Serve the film's folder, open it in Chromium, wait for window.film and `ready`. `playable`: its picture is wanted,* so footage this browser does not decode (ProRes; HEVC without a chip for it) is served as a copy it plays, made- * before the film opens (seekable.mjs): the film's video clips (`true`), or these files.+ * before the film opens (seekable.mjs): the film's video clips (`true`), or these files. `browser`: one to open it in+ * (a render's, from a BrowserPool), which close() leaves open: only the film's tab is closed.*/export async function open(target, opts = {}) {const loc = locate(target, opts.root);@@ -76,8 +77,9 @@ export async function open(target, opts = {}) {/** @type {Map<string, string>} */const copies = new Map();const site = await serve(loc.root, { swap: (file) => opts.swap?.(file) ?? copies.get(file) ?? null });+ const lent = opts.browser ?? null;let browser;- try { browser = await launch(opts.launch); }+ try { browser = lent ?? await launch(opts.launch); }catch (e) { await site.close(); throw e; }if (opts.playable) {try {@@ -86,10 +88,12 @@ export async function open(target, opts = {}) {for (const [file, copy] of await browserCopies(browser, site.url, loc.root, files, { signal: opts.signal })) copies.set(file, copy);} catch { /* the footage as it is */ }}+ /** @type {FilmPage} */let film;+ const end = async () => { if (lent) await film?.close().catch(() => {}); else await browser.close(); await site.close(); };try { film = await FilmPage.open(browser, site.url, loc.entry, opts); }- catch (e) { await browser.close(); await site.close(); throw e; }- return { ...loc, site, browser, film, async close() { await browser.close(); await site.close(); } };+ catch (e) { await end(); throw e; }+ return { ...loc, site, browser, film, close: end };}/**@@ -216,16 +220,22 @@ export async function filmSound(s, from) {}const audioStreams = new Map();-/** Whether a file has a sound stream (a video clip may be silent). */-export function hasAudio(file) {+/** How many channels a file's first sound stream has: 0 without one (a video clip may be silent). */+export function audioChannels(file) {if (!audioStreams.has(file)) {- const r = spawnSync('ffprobe', ['-v', 'error', '-select_streams', 'a', '-show_entries', 'stream=index', '-of', 'csv=p=0', file], { encoding: 'utf8', windowsHide: true });+ const r = spawnSync('ffprobe', ['-v', 'error', '-select_streams', 'a:0', '-show_entries', 'stream=channels', '-of', 'csv=p=0', file], { encoding: 'utf8', windowsHide: true });if (r.error) throw new Error(`ffprobe: ${/** @type {NodeJS.ErrnoException} */ (r.error).code === 'ENOENT' ? 'not found on the PATH (it comes with ffmpeg)' : r.error.message}`);- audioStreams.set(file, (r.stdout ?? '').trim().length > 0);+ const out = (r.stdout ?? '').trim();+ audioStreams.set(file, out ? Number.parseInt(out, 10) || 2 : 0);}return audioStreams.get(file);}+/** Whether a file has a sound stream (a video clip may be silent). */+export function hasAudio(file) {+ return audioChannels(file) > 0;+}+/** atempo chain for a playback rate (pitch kept); each stage takes 0.5–2. */function tempo(speed) {const out = [];@@ -253,7 +263,7 @@ export function fadeFilter(fade) {/** Mix clips into one track of `duration` seconds. */export async function mix(clips, duration, file, codec = ['-c:a', 'aac', '-b:a', '192k']) {- const { inputs, filter } = mixGraph(clips, duration);+ const { inputs, filter } = mixGraph(clips.map((c) => ({ ...c, channels: c.channels ?? audioChannels(c.file) })), duration);/* a mix takes seconds; one that has not ended in minutes never will */const timeoutMs = (120 + duration * 2) * 1000;/* a long graph goes in a file of its own: a command line has a limit (32,767 characters on Windows), and a film of@@ -305,7 +315,9 @@ export function mixGraph(clips, duration) {const fade = fadeFilter(c.fade);const trim = `asetpts=PTS-STARTPTS${c.speed && c.speed !== 1 ? `,${tempo(c.speed)}` : ''}${fade ? `,${fade}` : ''}`;const delay = Math.round(c.at * 1000);- return `${source[i]}${trim},volume=${c.volume},adelay=${delay}:all=1,aresample=48000,aformat=channel_layouts=stereo[a${i}]`;+ /* a mono sound on both sides at its own level, as a browser plays it (ffmpeg's own upmix puts it 3 dB lower) */+ const mono = c.channels === 1 ? ',pan=stereo|c0=c0|c1=c0' : '';+ return `${source[i]}${trim},volume=${c.volume},adelay=${delay}:all=1${mono},aresample=48000,aformat=channel_layouts=stereo[a${i}]`;});/* padded to the film's length and no further, and cut there: the mix always ends */const sum = `${clips.map((_, i) => `[a${i}]`).join('')}amix=inputs=${clips.length}:normalize=0:dropout_transition=0,apad=whole_dur=${duration},atrim=0:${duration}[out]`;@@ -318,7 +330,7 @@ let scriptOption = null;* The option that reads a filter graph from a file: `-/filter_complex` since ffmpeg 7, `-filter_complex_script` before* (the one older builds know; newer ones drop it).*/-function filterScriptOption() {+export function filterScriptOption() {scriptOption ??= new Promise((done) => {const r = spawnSync('ffmpeg', ['-hide_banner', '-version'], { encoding: 'utf8', windowsHide: true });const major = Number(/^ffmpeg version n?(\d+)\./m.exec(String(r.stdout ?? ''))?.[1]);diff --git a/packages/openfilm/src/shared.test.mjs b/packages/openfilm/src/shared.test.mjsindex 3f7ddb8..abdc66e 100644--- a/packages/openfilm/src/shared.test.mjs+++ b/packages/openfilm/src/shared.test.mjs@@ -74,6 +74,22 @@ test('a sound\'s fades ramp its level in the mix, straight, from where its clip}});+test('a mono sound is mixed on both sides at its own level, as a browser plays it; a stereo one as it is', { timeout: 30_000 }, async () => {+ const dir = mkdtempSync(join(tmpdir(), 'openfilm-mono-'));+ try {+ const mono = join(dir, 'mono.wav'), stereo = join(dir, 'stereo.wav');+ spawnSync('ffmpeg', ['-v', 'error', '-y', '-f', 'lavfi', '-i', 'sine=frequency=1000:duration=2:sample_rate=48000', mono]);+ spawnSync('ffmpeg', ['-v', 'error', '-y', '-i', mono, '-af', 'pan=stereo|c0=c0|c1=0*c0', stereo]);+ const out = join(dir, 'mix.wav');+ for (const file of [mono, stereo]) {+ await mix([{ file, at: 0, from: 0, volume: 1, speed: 1 }], 2, out, ['-c:a', 'pcm_s16le']);+ assert.equal(Math.round((levelAt(out, 1) / levelAt(file, 1)) * 100) / 100, 1, file);+ }+ } finally {+ rmSync(dir, { recursive: true, force: true });+ }+});+test('fadeFilter: the ramps as a per-sample expression, nothing without fades', () => {assert.equal(fadeFilter(undefined), '');assert.equal(fadeFilter({ in: 0, out: 0, length: 3, skip: 0 }), '');diff --git a/packages/openfilm/src/timeline.mjs b/packages/openfilm/src/timeline.mjsindex b4ec469..638f782 100644--- a/packages/openfilm/src/timeline.mjs+++ b/packages/openfilm/src/timeline.mjs@@ -57,7 +57,8 @@ function factsOf(src) {m.preload = 'metadata'; m.muted = true;const done = (facts) => { m.removeAttribute('src'); m.load(); ok(facts); };m.onloadedmetadata = () => done({ duration: m.duration, w: m.videoWidth, h: m.videoHeight });- m.onerror = () => done(null);+ /* a file the browser cannot read (ProRes footage, a codec it has no decoder for): what the host says it is */+ m.onerror = () => { void hostFacts(href).then(done); };const facts = new URL(href);facts.searchParams.set('film-facts', '1');m.src = facts.href;@@ -66,6 +67,24 @@ function factsOf(src) {return factsBySrc.get(href);}+/**+ * What the host says a file is, when the browser cannot read it: ffprobe's length and picture size, asked beside the+ * film (`__film/media-facts`, the file named from the film's folder, see host.mjs); null when it does not say.+ * @param {string} href+ */+function hostFacts(href) {+ const src = new URL(href);+ if (src.origin !== location.origin) return Promise.resolve(null);+ /* the file from the film's folder, `../` and all (an edit kept apart from its project names its files so) */+ const here = new URL('.', location.href).pathname.split('/').slice(0, -1);+ const there = src.pathname.split('/');+ let same = 0;+ while (same < here.length && same < there.length - 1 && here[same] === there[same]) same++;+ const of = [...here.slice(same).map(() => '..'), ...there.slice(same)].join('/');+ return fetch(new URL(`__film/media-facts?of=${encodeURIComponent(of)}`, location.href))+ .then((r) => (r.ok ? r.json() : null)).then((f) => (f?.duration > 0 ? { duration: f.duration, w: f.w ?? 0, h: f.h ?? 0 } : null)).catch(() => null);+}+/*** Put a clip's element where film-doc's pictureRect says its picture lands in `box`. A page is drawn at its own size* (`c.nw` × `c.nh`) and scaled into place; a video or still is laid out at its box's size, so the file's CSS for it@@ -253,7 +272,8 @@ function step(c, want) {/*** A video on screen while a person looks at one moment or scrubs: sought, and waited for until its frame is decoded* (not until the compositor shows it: a seek within the frame on screen shows nothing new, and that wait would only- * run out). The editor asks for the next moment once this one is drawn, so a scrub goes as fast as the decoder.+ * run out). The editor asks for the next moment once this one is drawn, so a scrub goes as fast as the decoder; the+ * frame on screen is then made sure of while the person stays there (onScreen).*/async function glance(c, want) {const v = c.el;@@ -269,14 +289,58 @@ async function glance(c, want) {if (c.lastWant !== want || !c.awake) return;justLoaded = true;}+ follow(c);if (!justLoaded && !v.seeking && Math.abs(v.currentTime - want) < SAME_TIME && v.readyState >= 2) return;v.currentTime = want;- await new Promise((done) => {- if (!v.seeking && v.readyState >= 2) { done(); return; }- v.addEventListener('seeked', done, { once: true });- v.addEventListener('loadeddata', done, { once: true });- setTimeout(done, 3000);- });+ await sought(v);+ void onScreen(c, want);+}++/** A seek done: its frame decoded (`seeked`, or the file's first data), or after a time limit. */+const sought = (v) => new Promise((done) => {+ if (!v.seeking && v.readyState >= 2) { done(undefined); return; }+ v.addEventListener('seeked', done, { once: true });+ v.addEventListener('loadeddata', done, { once: true });+ setTimeout(done, 3000);+});++/** Follow the frames a clip's video puts on screen (its frame callback): `c.presented`, the last one's time. */+function follow(c) {+ const v = c.el;+ if (c.following || typeof v.requestVideoFrameCallback !== 'function') return;+ c.following = true;+ const note = (/** @type {number} */ _now, /** @type {{ mediaTime: number }} */ meta) => {+ c.presented = meta.mediaTime;+ c.onPresented?.();+ v.requestVideoFrameCallback(note);+ };+ v.requestVideoFrameCallback(note);+}++/**+ * A still moment's frame made sure of on screen while the person stays there: a frame decoded may never be presented+ * (Chromium, see showVideoAt), and the picture keeps an older one, frames behind what an export draws at the same t.+ * Not on screen in a moment, it is sought again, half a millisecond on (the same frame), a few times. A scrub that+ * asks for another moment, or the film playing, leaves it; so does a video whose frames' length the host cannot say.+ */+async function onScreen(c, want) {+ const v = c.el;+ const frame = await frameOf(c);+ if (!frame || !c.following) return;+ /* the frame a seek to want lands in, as showVideoAt tells it */+ const right = () => c.presented != null && c.presented <= want + 0.002 && want + 0.001 - c.presented < frame * 1.25;+ const still = () => c.lastWant === want && v.paused && !v.seeking;+ for (let tries = 1; ; tries++) {+ if (right() || !still()) return;+ const shown = await new Promise((done) => {+ const timer = setTimeout(() => done(false), PRESENT_MS);+ c.onPresented = () => { if (right()) { clearTimeout(timer); done(true); } };+ });+ c.onPresented = undefined;+ if (shown || tries >= SEEK_TRIES || !still()) return;+ v.currentTime = want + tries * 0.0005;+ await sought(v);+ }}/*** A video whose clip starts `lead` film seconds from now, the film playing at `rate`: sought ahead (to PREROLL beforediff --git a/packages/openfilm/src/timeline.test.mjs b/packages/openfilm/src/timeline.test.mjsindex 1d74946..2a00709 100644--- a/packages/openfilm/src/timeline.test.mjs+++ b/packages/openfilm/src/timeline.test.mjs@@ -355,6 +355,43 @@ test('a sought frame that is decoded but never presented (a seek just after load}});+test('watched, a still moment whose video frame is decoded but never presented (Chromium) is sought again until it shows', { skip: !hasFfmpeg && 'no ffmpeg', timeout: 60_000 }, async () => {+ const dir = footage('of-unshown-watched-');+ writeFileSync(join(dir, 'film.html'), filmHtml({ stage: { w: 320, h: 180 }, tracks: [{ clips: [{ id: 'v', src: 'a.mp4', at: 1, time: [0, 4] }] }] }));+ const s = await open(dir);+ try {+ const seen = await s.film.page.evaluate(async () => {+ const v = document.querySelector('video');+ /* every frame said presented goes through here; `swallow`: the next one is never said to be (the picture stays+ where it was) */+ const real = v.requestVideoFrameCallback.bind(v);+ let swallow = false;+ v.requestVideoFrameCallback = (callback) => real((now, meta) => {+ if (!swallow) { callback(now, meta); return; }+ swallow = false;+ v.requestVideoFrameCallback(callback);+ });+ await window.__filmEditor.watch(1.5);+ await new Promise((r) => setTimeout(r, 500));+ const seeks = [];+ v.addEventListener('seeking', () => seeks.push(v.currentTime));+ swallow = true;+ const t0 = performance.now();+ await window.__filmEditor.watch(3.25);+ /* the editor is told the moment is drawn at once: the scrub goes on as fast as the decoder */+ const told = performance.now() - t0;+ await new Promise((r) => setTimeout(r, 2500));+ return { seeks, told, swallowed: !swallow };+ });+ assert.ok(seen.swallowed, 'the frame was presented, and not said to be');+ assert.equal(seen.seeks.length, 2, `sought again once: ${JSON.stringify(seen)}`);+ assert.ok(Math.abs(seen.seeks[1] - 2.25) < 0.002, `the same frame: ${seen.seeks[1]}`);+ assert.ok(seen.told < 900, `${Math.round(seen.told)} ms`);+ } finally {+ await s.close();+ }+});+test('a video woken ahead while hidden, then shown: its old frame presented again after the seek is not taken for the sought one', { skip: !hasFfmpeg && 'no ffmpeg', timeout: 60_000 }, async () => {const dir = footage('of-stale-frame-');writeFileSync(join(dir, 'film.html'), filmHtml({ stage: { w: 320, h: 180 }, tracks: [{ clips: [{ id: 'v', src: 'a.mp4', at: 2, time: [0, 4] }] }] }));diff --git a/packages/openfilm/studio/server/exports.mjs b/packages/openfilm/studio/server/exports.mjsindex 7e5e045..676e150 100644--- a/packages/openfilm/studio/server/exports.mjs+++ b/packages/openfilm/studio/server/exports.mjs@@ -32,9 +32,14 @@* { kind: 'slides', format?: 'pptx' (default) | 'pdf', frame?, fill?, shortEdge? (default 1080), at?: number[] }* the scene stills bound as a deck (the scene's name in the speaker notes) or a PDF, one page each* { kind: 'nle', format?: 'xml' (default) | 'fcpxml' | 'jianying' | 'capcut', fps? (snapped to 23.976, 24, 25, 29.97, 30, 50, 59.94 or 60;- * default 30), audio?: 'clips' (default) | 'stems', media?: 'copy' (default) | 'link', subtitles?: boolean (default true) }+ * default 30), audio?: 'clips' (default) | 'stems', media?: 'copy' (default) | 'link', subtitles?: boolean (default true),+ * alpha?: 'auto' | 'png' | 'prores' | 'prores4444' }* a folder an editing program opens (see nle.mjs): `<name>.xml` (xmeml: Premiere Pro, DaVinci Resolve) or `<name>.fcpxml` (Final Cut Pro), each- * web page rendered to a ProRes 4444 with alpha in media/, footage, stills and sounds cloned there (`link`:+ * web page rendered with its alpha to a MOV in media/ (`alpha`, for a draft: `auto`, the default, a page with no+ * clear pixel in any frame as H.264 and the others as lossless PNG, each picture kept once for as long as it+ * shows; `png`, every page so; `prores`, 422 HQ and 4444 the same way. For xml ProRes 4444, the default, or PNG;+ * for fcpxml ProRes 4444. 剪映 shows a VP9 WebM's alpha black, and lossless H.264 off its colors),+ * footage, stills and sounds cloned there (`link`:* pointed at where they are), the sound as clips or as mixed stems, `<name>.srt` when there are subtitles.* FCPXML also includes subtitles as editable Basic Title clips above the picture tracks.* `jianying`, `capcut`: the same timeline as a 剪映 or CapCut draft (see jianying.mjs), made in the app's drafts folder when this@@ -51,6 +56,7 @@* to instead of the exports folder.*/import { execFile, fork, spawn } from 'node:child_process';+import { promisify } from 'node:util';import { randomBytes } from 'node:crypto';import { constants as fsConstants, existsSync, readFileSync, statSync } from 'node:fs';import { copyFile, mkdir, mkdtemp, readdir, readFile, rename, rm, rmdir, stat, writeFile } from 'node:fs/promises';@@ -58,8 +64,9 @@ import { homedir, setPriority, tmpdir } from 'node:os';import { basename, dirname, extname, isAbsolute, join, relative, resolve, sep } from 'node:path';import { fileURLToPath } from 'node:url';import { FilmPage, launch } from '../../src/host.mjs';-import { broken, encoders, hardwareEncoder, hardwareOptions, onTheChip, proresOptions } from '../../src/encoders.mjs';-import { filmSound, kill as killProcess, mix, open } from '../../src/shared.mjs';+import { broken, encoders, FRAMES_709, hardwareEncoder, hardwareOptions, onTheChip, prores709, proresOptions } from '../../src/encoders.mjs';+import { COMMAND_ROOM, filmSound, filterScriptOption, kill as killProcess, mix, open } from '../../src/shared.mjs';+import { defaultWorkers, machine } from '../../src/render.mjs';import { filmVideos } from '../../src/seekable.mjs';import { FILM_FILE, clipFade, filmHtml, fittedFade, kindOf, pictureRect, readFilmFile } from '../../src/film-doc.mjs';import { layerBlocks, rowBadges, trackKind } from './track-layout.mjs';@@ -91,7 +98,7 @@ export { exportsRoot };* @typedef {Framing & { kind: 'poster', format?: 'png' | 'jpg', shortEdge?: number, at?: number }} PosterAsk* @typedef {{ kind: 'subtitles', format?: 'srt' | 'vtt' | 'txt' }} SubtitlesAsk* @typedef {Framing & { kind: 'slides', format?: 'pptx' | 'pdf', shortEdge?: number, at?: number[] }} SlidesAsk- * @typedef {{ kind: 'nle', format?: 'xml' | 'fcpxml' | 'jianying' | 'capcut', fps?: number, audio?: 'clips' | 'stems', media?: 'copy' | 'link', subtitles?: boolean }} NleAsk+ * @typedef {{ kind: 'nle', format?: 'xml' | 'fcpxml' | 'jianying' | 'capcut', fps?: number, audio?: 'clips' | 'stems', media?: 'copy' | 'link', subtitles?: boolean, alpha?: 'auto' | 'png' | 'prores' | 'prores4444' }} NleAsk* @typedef {{ kind: 'project', parts?: ProjectPart[], as?: 'zip' | 'folder' }} ProjectAsk* @typedef {typeof PROJECT_PARTS[number]} ProjectPart* @typedef {VideoAsk | GifAsk | AudioAsk | StillsAsk | PosterAsk | SubtitlesAsk | SlidesAsk | NleAsk | ProjectAsk} ExportSpec@@ -103,9 +110,13 @@ export { exportsRoot };* status: 'waiting' | 'running' | 'done' | 'failed' | 'cancelled', progress: number, phase: ExportPhase | null,* framesDone: number, framesTotal: number, rate: number, etaMs: number | null, encoder: string | null,* out?: string, outputs: string[], bytes: number | null, alpha: { any: boolean } | null, error?: string, cancelling?: boolean,- * at: number, startedAt: number | null, finishedAt: number | null, draftsRefused?: string }} ExportJob+ * at: number, startedAt: number | null, finishedAt: number | null, draftsRefused?: string, diskFull?: boolean }} ExportJob*/+/** Whether an error is the disk being full, in ffmpeg's words or the system's (a Mac, Linux, Windows). @param {unknown} e */+export const diskFull = (e) => /** @type {{ code?: unknown }} */ (e)?.code === 'ENOSPC'+ || /no space left on device|not enough space on the disk|\bENOSPC\b/i.test(e instanceof Error ? e.message : String(e));+export class ExportError extends Error {/** @param {string} message @param {number} status */constructor(message, status = 400) {@@ -204,7 +215,12 @@ export function framing(stage, frame, fill, out, alpha) {/* ── names ──────────────────────────────────────────────────────────────── */-const safeName = (/** @type {string} */ text) => text.replace(/[\\/:*?"<>|\u0000-\u001f]+/g, ' ').replace(/\s+/g, ' ').trim().slice(0, 80) || 'OpenFilm';+const safeName = (/** @type {string} */ text) => {+ /* no dot or space at the end (Windows drops them, and the file is not found under the name it was made with) */+ const name = text.replace(/[\\/:*?"<>|\u0000-\u001f]+/g, ' ').replace(/\s+/g, ' ').trim().slice(0, 80).replace(/[. ]+$/, '') || 'OpenFilm';+ /* names Windows keeps for devices, whatever follows their first dot */+ return /^(con|prn|aux|nul|com[1-9]|lpt[1-9])(\..*)?$/i.test(name) ? name.replace(/^[^.]*/, (m) => `${m} film`) : name;+};/** A name in `dir` that is free: `name.ext`, else `name 2.ext`… */function freeFile(/** @type {string} */ dir, /** @type {string} */ name, /** @type {string} */ ext) {@@ -213,6 +229,66 @@ function freeFile(/** @type {string} */ dir, /** @type {string} */ name, /** @tyreturn candidate;}+/**+ * Where a file is written until it is whole: beside its name, hidden, and renamed to it once made, so a stop or a crash+ * midway never leaves a file under the export's name that looks made. It names the process writing it (`sweep`).+ * @param {string} out @param {string} id+ */+const partialOf = (out, id) => { const ext = extname(out); return join(dirname(out), `.${basename(out, ext)}.partial-${process.pid}-${id}${ext}`); };++/**+ * A rename that rides out Windows: a file that a virus scanner, the search indexer or an editor watching its folder holds+ * for a moment refuses to move (EPERM, EACCES, EBUSY), and is let go a moment later. Tried again for a few seconds, as+ * tools on Windows do; any other error, or a hold that lasts, is the error.+ * @param {string} from @param {string} to+ */+export async function renameSafely(from, to) {+ for (let i = 1; ; i++) {+ try {+ await rename(from, to);+ return;+ } catch (e) {+ if (i >= 12 || !['EPERM', 'EACCES', 'EBUSY'].includes(/** @type {NodeJS.ErrnoException} */ (e).code ?? '')) throw e;+ await new Promise((r) => setTimeout(r, i * 100));+ }+ }+}++/** Scratch removed if it can be: what something still holds is left behind, never failing an export that is made. @param {string} path */+const tidy = (path) => rm(path, { recursive: true, force: true, maxRetries: 5, retryDelay: 200 }).catch(() => {});++/* what an export makes on its way, each named with the process making it: a half-made file or draft beside the made+ ones, a project's export scratch, a render's parts, a seek copy being written (src/seekable.mjs) */+const LEFT = {+ output: /^\..+\.partial-(\d+)-[0-9a-f]+/,+ scratch: /^(?:clip|burn|nle)-(\d+)-[0-9a-f]+$/,+ parts: /^\.parts-(\d+)$/,+ copy: /\.part-(\d+)-[0-9a-f]+\.\w+$/,+};++/** Whether a process is running (one this user may not signal is). @param {number} pid */+function alive(pid) {+ try {+ process.kill(pid, 0);+ return true;+ } catch (e) {+ return /** @type {NodeJS.ErrnoException} */ (e).code === 'EPERM';+ }+}++/**+ * What exports Studio could not finish left in `dir` (it was ended partway: forced to quit, crashed, the computer+ * shut down): each entry `pattern` names whose process (its first group) has ended.+ * @param {string} dir @param {RegExp} pattern+ */+export async function sweep(dir, pattern) {+ const names = await readdir(dir).catch(() => /** @type {string[]} */ ([]));+ await Promise.all(names.map((name) => {+ const pid = Number(pattern.exec(name)?.[1]);+ return pid && pid !== process.pid && !alive(pid) ? tidy(join(dir, name)) : undefined;+ }));+}+const ALPHA_CODECS = new Set(['vp9', 'prores4444', 'hevc_alpha', 'png']);const CODECS = ['h264', 'hevc', 'vp9', 'prores422hq', 'prores4444', 'hevc_alpha', 'png'];const CODEC_EXT = /** @type {Record<VideoCodec, string>} */ ({ h264: '.mp4', hevc: '.mp4', vp9: '.webm', prores422hq: '.mov', prores4444: '.mov', hevc_alpha: '.mov', png: '.zip' });@@ -305,6 +381,11 @@ function checked(/** @type {ExportAsk} */ ask) {if (ask.audio !== undefined && ask.audio !== 'clips' && ask.audio !== 'stems') throw new ExportError('audio: clips or stems');if (ask.media !== undefined && ask.media !== 'copy' && ask.media !== 'link') throw new ExportError('media: copy or link');if (ask.subtitles !== undefined && typeof ask.subtitles !== 'boolean') throw new ExportError('subtitles: true or false');+ if (ask.alpha !== undefined) {+ const draft = ask.format === 'jianying' || ask.format === 'capcut';+ const can = ask.format === 'fcpxml' ? ['prores4444'] : draft ? ['auto', 'png', 'prores'] : ['prores4444', 'png'];+ if (!can.includes(ask.alpha)) throw new ExportError(`alpha for ${ask.format ?? 'xml'}: ${can.join(', ')}`);+ }break;case 'project':if (ask.range !== undefined) throw new ExportError('a project is exported whole: no range');@@ -406,6 +487,9 @@ function run(args, signal, input) {else if (code === 0) done(undefined);else fail(new Error(`ffmpeg: ${err.trim() || `exit ${code}`}`));});+ /* ffmpeg that stops before reading all of it (cancelled, or failing as it starts) closes its end: the write's+ error is not one to throw (Studio would stop with it), its exit says what happened */+ proc.stdin?.on('error', () => {});if (input) proc.stdin?.end(input);});}@@ -469,10 +553,10 @@ async function clipFilm(root, clipId, { change = {}, doc = filmDoc(root) } = {})const track = doc.tracks.find((t) => t.clips.some((c) => c.id === clipId));const clip = track?.clips.find((c) => c.id === clipId);if (!track || !clip) throw new ExportError(`${FILM_FILE} has no clip "${clipId}"`);- const dir = join(root, '.film', 'cache', 'export', `clip-${randomBytes(4).toString('hex')}`);+ const dir = join(root, '.film', 'cache', 'export', `clip-${process.pid}-${randomBytes(4).toString('hex')}`);await mkdir(dir, { recursive: true });await filmElsewhere(root, dir, { stage: doc.stage, tracks: [{ clips: [{ ...clip, ...change, at: 0 }], ...(track.muted ? { muted: true } : {}) }] });- return { dir, stage: doc.stage, remove: () => rm(dir, { recursive: true, force: true }) };+ return { dir, stage: doc.stage, remove: () => tidy(dir) };}/**@@ -486,7 +570,7 @@ async function clipFilm(root, clipId, { change = {}, doc = filmDoc(root) } = {})*/async function burnFilm(root, stage, frame, fill, cues, style, transparent = false) {const box = frameBox(stage, frame, fill);- const dir = join(root, '.film', 'cache', 'export', `burn-${randomBytes(4).toString('hex')}`);+ const dir = join(root, '.film', 'cache', 'export', `burn-${process.pid}-${randomBytes(4).toString('hex')}`);await mkdir(dir, { recursive: true });await copyFile(fileURLToPath(new URL('./film-subtitle.mjs', import.meta.url)), join(dir, 'film-subtitle.mjs'));const entry = existsSync(join(root, FILM_FILE)) ? `/${FILM_FILE}` : '/index.html';@@ -536,7 +620,7 @@ window.__filmCuts = () => inner?.__filmCuts?.() ?? null;window.__filmSounds = () => inner?.__filmSounds?.() ?? [];</script></body></html>`);- return { dir, box, remove: () => rm(dir, { recursive: true, force: true }) };+ return { dir, box, remove: () => tidy(dir) };}const H264_CRF = { standard: 23, high: 18, master: 14 };@@ -546,6 +630,12 @@ const VP9_CRF = { standard: 36, high: 30, master: 20 };const BPP = { standard: 0.07, high: 0.11, master: 0.2 };const BT709 = ['-colorspace', 'bt709', '-color_primaries', 'bt709', '-color_trc', 'bt709', '-color_range', 'tv'];const TO_709 = 'scale=out_color_matrix=bt709:out_range=tv';+/** The browser's frames as YUV in `format` for an encoder: BT.709 limited range (swscale's own default is BT.601), said on the frames. */+const YUV_709 = (/** @type {string} */ format) => `${TO_709},format=${format},${FRAMES_709}`;+/* BT.709 written into the stream once it is encoded, whatever encoder wrote it (never into HEVC with alpha: its alpha+ layer does not survive it) */+const H264_709 = ['-bsf:v', 'h264_metadata=colour_primaries=1:transfer_characteristics=1:matrix_coefficients=1:video_full_range_flag=0'];+const HEVC_709 = ['-bsf:v', 'hevc_metadata=colour_primaries=1:transfer_characteristics=1:matrix_coefficients=1:video_full_range_flag=0'];/*** The graphics chip's encoder to try for a codec, or null: H.264 and HEVC below master, as editors do (several times@@ -572,22 +662,22 @@ function encoding(codec, quality, size, fps, have, hw = null) {if (hw) return { encoder: hw, format: { video: [...hardwareOptions(hw, bits(1.5), { gop }), ...BT709] } };return { encoder: 'libx264', format: { video: ['-c:v', 'libx264', '-preset', quality === 'master' ? 'slow' : 'medium', '-crf', String(H264_CRF[quality]), ...BT709] } };case 'hevc':- if (hw) return { encoder: hw, format: { video: [...hardwareOptions(hw, bits(0.8), { gop }), ...BT709], tag: ['-tag:v', 'hvc1'] } };+ if (hw) return { encoder: hw, format: { video: [...hardwareOptions(hw, bits(0.8), { gop }), ...BT709], tag: ['-tag:v', 'hvc1', ...HEVC_709] } };if (have.has('libx265')) {- return { encoder: 'libx265', format: { video: ['-c:v', 'libx265', '-preset', 'medium', '-crf', String(X265_CRF[quality]), '-x265-params', 'log-level=error', '-tag:v', 'hvc1', ...BT709], tag: ['-tag:v', 'hvc1'] } };+ return { encoder: 'libx265', format: { video: ['-c:v', 'libx265', '-preset', 'medium', '-crf', String(X265_CRF[quality]), '-x265-params', 'log-level=error', '-tag:v', 'hvc1', ...BT709], tag: ['-tag:v', 'hvc1', ...HEVC_709] } };}- if (have.has('hevc_videotoolbox')) return { encoder: 'hevc_videotoolbox', format: { video: ['-c:v', 'hevc_videotoolbox', '-b:v', bitrate(0.8), '-g', String(gop), '-tag:v', 'hvc1', ...BT709], tag: ['-tag:v', 'hvc1'] } };+ if (have.has('hevc_videotoolbox')) return { encoder: 'hevc_videotoolbox', format: { video: ['-c:v', 'hevc_videotoolbox', '-b:v', bitrate(0.8), '-g', String(gop), '-tag:v', 'hvc1', ...BT709], tag: ['-tag:v', 'hvc1', ...HEVC_709] } };throw new ExportError('this computer\'s ffmpeg has no HEVC encoder (libx265 or hevc_videotoolbox)');case 'vp9':- return { encoder: 'libvpx-vp9', format: { video: ['-c:v', 'libvpx-vp9', '-b:v', '0', '-crf', String(VP9_CRF[quality]), '-row-mt', '1'] } };+ return { encoder: 'libvpx-vp9', format: { video: ['-c:v', 'libvpx-vp9', '-b:v', '0', '-crf', String(VP9_CRF[quality]), '-row-mt', '1', ...BT709] } };case 'prores422hq':case 'prores4444': {const encoder = hw ?? 'prores_ks';- return { encoder, format: proresOptions(encoder, codec === 'prores4444') };+ return { encoder, format: prores709(encoder, codec === 'prores4444') };}case 'hevc_alpha':if (!have.has('hevc_videotoolbox')) throw new ExportError('HEVC with alpha needs macOS\'s hevc_videotoolbox encoder');- return { encoder: 'hevc_videotoolbox', format: { video: ['-c:v', 'hevc_videotoolbox', '-alpha_quality', '0.75', '-b:v', bitrate(0.8), '-tag:v', 'hvc1'], pix: 'format=bgra', tag: ['-tag:v', 'hvc1'] } };+ return { encoder: 'hevc_videotoolbox', format: { video: ['-c:v', 'hevc_videotoolbox', '-alpha_quality', '0.75', '-b:v', bitrate(0.8), '-tag:v', 'hvc1'], pix: `format=bgra,${FRAMES_709}`, tag: ['-tag:v', 'hvc1'] } };case 'png':return { encoder: 'png', format: { video: ['-c:v', 'png'], pix: 'format=rgba' } };}@@ -635,11 +725,13 @@ function tempo(speed) {}/**- * A source an editor can't read, made into one it can: video → ProRes (4444 with its alpha, else 422 HQ), sound →- * 48 kHz WAV (`segment`: only those source seconds, at that speed, exactly `seconds` long).+ * A source an editor can't read, made into one it can: video → ProRes 422 HQ, or with its alpha the export's alpha+ * codec (PNG, ProRes 4444 or HEVC with alpha); sound → 48 kHz WAV (`segment`: only those source seconds, at that+ * speed, exactly `seconds` long). `video`: the picture's size and rate, which HEVC's bitrate follows.* @param {Extract<import('./nle.mjs').NleMediaJob, { kind: 'transcode' }>} make @param {string} out @param {AbortSignal} signal+ * @param {{ w: number, h: number, fps?: number }} [video]*/-async function transcode(make, out, signal) {+async function transcode(make, out, signal, video) {if (make.to === 'wav') {const seg = make.segment;/* the part is cut from the source before its speed changes, as the film's mix cuts it (after the -i, -ss and -t@@ -650,12 +742,15 @@ async function transcode(make, out, signal) {'-c:a', 'pcm_s16le', '-ar', '48000', out], signal);return;}- const alpha = make.to === 'prores4444';- /* decoded on the graphics chip and written by the Mac's ProRes engine when it can, else by ffmpeg's own encoder */- const chip = await hardwareEncoder('prores', await encoders());+ const alpha = make.to !== 'prores422hq';+ const prores = make.to === 'prores422hq' || make.to === 'prores4444';+ const have = await encoders();+ /* decoded on the graphics chip and, ProRes, written by the Mac's ProRes engine when it can, else by ffmpeg's own encoder */+ const chip = prores ? await hardwareEncoder('prores', have) : null;await onTheChip(chip, async (encoder, decoding) => {- const { video, pix } = proresOptions(encoder ?? 'prores_ks', alpha);- const tail = ['-map', '0:v:0', '-map', '0:a?', '-vf', pix, ...video, '-c:a', 'pcm_s16le', '-ar', '48000', out];+ const own = prores ? null : encoding('png', 'master', video ?? { w: 1920, h: 1080 }, video?.fps ?? 30, have).format;+ const { video: codec, pix } = own ? { video: own.video, pix: /** @type {{ pix: string }} */ (own).pix } : proresOptions(encoder ?? 'prores_ks', alpha);+ const tail = ['-map', '0:v:0', '-map', '0:a?', '-vf', pix, ...codec, '-c:a', 'pcm_s16le', '-ar', '48000', out];/* VP9's alpha is a side channel ffmpeg's own decoder drops: libvpx reads it */if (alpha) {try { await run(['-c:v', 'libvpx-vp9', '-i', make.from, ...tail], signal); return; } catch (e) { if (signal.aborted) throw e; }@@ -664,6 +759,54 @@ async function transcode(make, out, signal) {}, signal);}+const execFileP = promisify(execFile);++/**+ * How a draft's page with alpha is drawn before its pictures are thinned (keepChanges writes them at ffmpeg's own PNG+ * settings): PNG at zlib's fastest with each pixel kept as its difference from the one on its left. The same pixels for+ * an eighth of the processor: ffmpeg's own PNG settings take about 0.4 s of processor time per 1080p frame, more than+ * drawing it.+ */+const QUICK_PNG = { video: ['-c:v', 'png', '-compression_level', '1', '-pred', 'sub'], pix: 'format=rgba' };++/**+ * A PNG video with each picture kept once: a frame that repeats the one before is dropped and the one before lasts+ * until the next (the timestamps say so), so a page that holds still is one picture, not a second's worth. A repeat is+ * found by its bytes: one encoder at one setting writes the same picture the same way, so a frame a pixel apart is+ * never taken for one; the frames kept are written again as PNG at ffmpeg's own settings, the same pixels. The last+ * frame stays, so the length does too; a video that changes in most of its frames keeps every frame, written again the+ * same way (it may have been drawn as a quick PNG).+ * @param {string} file @param {AbortSignal} signal+ */+export async function keepChanges(file, signal) {+ const { stdout } = await execFileP('ffmpeg', ['-hide_banner', '-nostdin', '-loglevel', 'error', '-i', file, '-map', '0:v:0', '-c', 'copy', '-f', 'framemd5', '-'],+ { maxBuffer: 256 << 20, signal, windowsHide: true });+ const sums = String(stdout).split('\n').filter((l) => l && !l.startsWith('#')).map((l) => l.split(',').pop()?.trim());+ const kept = sums.flatMap((sum, i) => (i === 0 || i === sums.length - 1 || sum !== sums[i - 1] ? [i] : []));+ /* frames kept one after another are one term; the terms are added as a balanced tree, since ffmpeg parses no more+ than 100 in a row */+ /** @type {[number, number][]} */+ const runs = [];+ for (const i of kept) { const last = runs.at(-1); if (last && last[1] === i - 1) last[1] = i; else runs.push([i, i]); }+ /** @type {(part: [number, number][]) => string} */+ const sum = (part) => (part.length === 1 ? (part[0][0] === part[0][1] ? `eq(n,${part[0][0]})` : `between(n,${part[0][0]},${part[0][1]})`)+ : `(${sum(part.slice(0, part.length >> 1))}+${sum(part.slice(part.length >> 1))})`);+ const part = `${file}.part.mov`;+ const all = kept.length > sums.length / 2;+ const graph = all ? '' : `[0:v:0]select='${sum(runs)}'[v]`;+ /* a long one goes in a file: Windows takes a command of no more than 32,767 characters, and a long page that steps+ every few frames keeps thousands of runs */+ const script = graph.length > COMMAND_ROOM ? `${part}.filter` : null;+ if (script) await writeFile(script, graph, 'utf8');+ try {+ const select = all ? ['-map', '0:v:0'] : [...(script ? [await filterScriptOption(), script] : ['-filter_complex', graph]), '-map', '[v]', '-fps_mode', 'vfr'];+ await run(['-i', file, ...select, '-c:v', 'png', part], signal);+ } finally {+ if (script) await tidy(script);+ }+ await renameSafely(part, file);+}+const IMAGE_TYPES = /** @type {Record<string, string>} */ ({ svg: 'image/svg+xml', webp: 'image/webp', avif: 'image/avif', gif: 'image/gif', png: 'image/png', jpg: 'image/jpeg', jpeg: 'image/jpeg', bmp: 'image/bmp' });/**@@ -701,6 +844,9 @@ export async function writableFolder(dir) {}}+/** How long Studio's close waits for a stopped export to clean up after itself (the app waits ten seconds for Studio). */+const STOP_WAIT_MS = 8000;+/*** The export queue. `onChange(job)` is told every change of a job (its progress included), for the pages watching.* `captions(root)`: a film's subtitle cues and the person's style for them (captions.mjs by default).@@ -714,6 +860,10 @@ export function createExports({ onChange = () => {}, captions = subtitlesForExpo/** @type {Map<string, { root: string, folder: string, abort: AbortController, twin?: string }>} */const work = new Map();let running = false;+ /* Studio is closing: nothing new starts (stopAll) */+ let closing = false;+ /** settles when no export is running */+ let idle = Promise.resolve();const update = (/** @type {ExportJob} */ job, /** @type {Partial<ExportJob>} */ change) => { Object.assign(job, change); onChange({ ...job, outputs: [...job.outputs] }); };@@ -729,8 +879,10 @@ export function createExports({ onChange = () => {}, captions = subtitlesForExpo*/const burned = async (root, ask, transparent = false) => {if (!ask.burnSubtitles) return null;- const { cues, style } = await subtitlesOf(root);- if (!cues.length) return null;+ if (!(await subtitlesOf(root)).cues.length) return null;+ /* the cues as stored: the page picks the shown language itself (filmSubtitleFrameHtml), and picking it twice+ puts a bilingual line's translation under it twice */+ const { cues, style } = await captions(root);return burnFilm(root, await stageOf(root), ask.frame ?? 'native', ask.fill ?? 'contain', cues, style, transparent);};@@ -770,7 +922,7 @@ export function createExports({ onChange = () => {}, captions = subtitlesForExpoconst alpha = ALPHA_CODECS.has(codec);const clip = ask.clip ? await clipFilm(root, ask.clip) : null;const burn = clip ? null : await burned(root, ask, alpha);- const scratch = codec === 'png' ? await mkdtemp(join(tmpdir(), 'openfilm-export-')) : null;+ const scratch = codec === 'png' || codec === 'hevc_alpha' ? await mkdtemp(join(tmpdir(), 'openfilm-export-')) : null;try {const stage = clip?.stage ?? await stageOf(root);let size, place;@@ -796,7 +948,7 @@ export function createExports({ onChange = () => {}, captions = subtitlesForExpo/** @param {string | null} chip */const how = (chip) => {const { encoder, format } = encoding(codec, quality, size, fps ?? 30, have, chip);- const container = /** @type {{ pix?: string }} */ (format).pix ?? (codec === 'h264' || codec === 'hevc' ? `${TO_709},format=yuv420p` : codec === 'vp9' ? 'format=yuva420p' : 'format=yuva444p10le');+ const container = /** @type {{ pix?: string }} */ (format).pix ?? (codec === 'h264' || codec === 'hevc' ? YUV_709('yuv420p') : codec === 'vp9' ? YUV_709('yuva420p') : 'format=yuva444p10le');return { encoder, opts: { fps, scale: place.scale, alpha, silent: ask.audio === false || codec === 'png', format: { ...format, pix: `${place.vf},${container}` }, ...(clip || burn ? { root } : {}) } };};let { encoder, opts } = how(hw);@@ -804,18 +956,30 @@ export function createExports({ onChange = () => {}, captions = subtitlesForExpoconst target = clip ? clip.dir : burn ? burn.dir : root;const out = freeFile(dir, baseName(job.name, ask), CODEC_EXT[codec]);if (codec !== 'png') {- update(job, { out, outputs: [out] });+ const part = partialOf(out, job.id);+ update(job, { out: part, outputs: [part] });try {- await renderTo(job, target, out, opts, signal);+ if (codec === 'hevc_alpha') {+ /* HEVC with alpha is encoded once, straight into its file: ffmpeg copying it (as render does, joining its+ parts and adding the sound) drops its alpha layer's parameter sets, and no Apple app opens it then. Drawn+ first as a quick PNG with its sound (the same frames, as many browsers as any render), then encoded */+ const frames = join(/** @type {string} */ (scratch), 'frames.mov');+ await renderTo(job, target, frames, { ...opts, format: { ...QUICK_PNG, pix: `${place.vf},format=rgba` } }, signal, 0.85);+ update(job, { phase: 'encoding', progress: 0.86, rate: 0, etaMs: null });+ const { format } = encoding(codec, quality, size, fps ?? 30, have);+ await run(['-i', frames, '-map', '0:v:0', '-map', '0:a?', '-vf', /** @type {{ pix: string }} */ (format).pix, ...format.video, '-c:a', 'copy', part], signal);+ } else await renderTo(job, target, part, opts, signal);} catch (e) {- /* the graphics chip's encoder gave up partway (a driver, a size it does not take): the rest of this launch- encodes without it, starting with this export again */- if (!hw || signal.aborted || !/^ffmpeg: /.test(e instanceof Error ? e.message : '')) throw e;+ /* the graphics chip's encoder gave up partway (a driver, a size it does not take, sessions or memory it ran out+ of): the rest of this launch encodes without it, starting with this export again */+ if (!hw || signal.aborted || !/^ffmpeg[: ]/.test(e instanceof Error ? e.message : '')) throw e;broken(hw);({ encoder, opts } = how(null));update(job, { encoder, framesDone: 0, progress: 0, etaMs: null });- await renderTo(job, target, out, opts, signal);+ await renderTo(job, target, part, opts, signal);}+ await renameSafely(part, out);+ update(job, { out, outputs: [out] });if (alpha) update(job, { alpha: await alphaOf(out, codec === 'vp9') });/* the subtitles as a file beside it, timed to the part exported */if (ask.sidecar) {@@ -834,17 +998,20 @@ export function createExports({ onChange = () => {}, captions = subtitlesForExpoconst scratchDir = /** @type {string} */ (scratch);const movie = join(scratchDir, 'frames.mov');await renderTo(job, target, movie, opts, signal, 0.8);- update(job, { phase: 'encoding', progress: 0.82, out, outputs: [out] });+ const part = partialOf(out, job.id);+ update(job, { phase: 'encoding', progress: 0.82, out: part, outputs: [part] });const stem = baseName(job.name, ask);await mkdir(join(scratchDir, 'png'));await run(['-i', movie, '-c:v', 'copy', '-f', 'image2', '-start_number', '0', join(scratchDir, 'png', '%06d.png')], signal);const names = (await readdir(join(scratchDir, 'png'))).sort();update(job, { phase: 'finishing', progress: 0.9 });- await zipFiles(out, names.map((n) => ({ name: `${stem}_${n}`, file: join(scratchDir, 'png', n) })), signal);+ await zipFiles(part, names.map((n) => ({ name: `${stem}_${n}`, file: join(scratchDir, 'png', n) })), signal);+ await renameSafely(part, out);+ update(job, { out, outputs: [out] });} finally {await clip?.remove();await burn?.remove();- if (scratch) await rm(scratch, { recursive: true, force: true, maxRetries: 5 });+ if (scratch) await tidy(scratch);}}@@ -866,11 +1033,14 @@ export function createExports({ onChange = () => {}, captions = subtitlesForExpoawait renderTo(job, burn ? burn.dir : root, movie, { fps, scale: place.scale, silent: true, ...(burn ? { root } : {}),format: { video: ['-c:v', 'libx264', '-preset', 'veryfast', '-crf', '8', ...BT709], pix: `${place.vf},${TO_709},format=yuv420p` } }, signal, 0.8);const out = freeFile(dir, baseName(job.name, ask), '.gif');- update(job, { phase: 'encoding', progress: 0.85, out, outputs: [out] });- await run(['-i', movie, '-filter_complex', '[0:v]split[a][b];[a]palettegen=stats_mode=diff[p];[b][p]paletteuse=dither=sierra2_4a:diff_mode=rectangle', '-loop', '0', out], signal);+ const part = partialOf(out, job.id);+ update(job, { phase: 'encoding', progress: 0.85, out: part, outputs: [part] });+ await run(['-i', movie, '-filter_complex', '[0:v]split[a][b];[a]palettegen=stats_mode=diff[p];[b][p]paletteuse=dither=sierra2_4a:diff_mode=rectangle', '-loop', '0', part], signal);+ await renameSafely(part, out);+ update(job, { out, outputs: [out] });} finally {await burn?.remove();- await rm(scratch, { recursive: true, force: true, maxRetries: 5 });+ await tidy(scratch);}}@@ -905,9 +1075,13 @@ export function createExports({ onChange = () => {}, captions = subtitlesForExpofor (const [i, step] of plan.entries()) {if (signal.aborted) throw new Error('cancelled');const out = freeFile(dir, `${base}${step.suffix}`, `.${format}`);- outputs.push(out);- update(job, { out: outputs[0], outputs, progress: Math.floor((i / plan.length) * 100) / 100 });- await mix(step.clips, to - from, out, codec);+ const part = partialOf(out, job.id);+ outputs.push(part);+ update(job, { out: outputs[0], outputs: [...outputs], progress: Math.floor((i / plan.length) * 100) / 100 });+ await mix(step.clips, to - from, part, codec);+ await renameSafely(part, out);+ outputs[i] = out;+ update(job, { out: outputs[0], outputs: [...outputs] });}} finally {await probe.close();@@ -995,11 +1169,14 @@ export function createExports({ onChange = () => {}, captions = subtitlesForExpoupdate(job, { out, outputs: [out] });} else {const out = freeFile(dir, base, '.zip');+ const part = partialOf(out, job.id);+ update(job, { out: part, outputs: [part] });+ await zipFiles(part, files, signal);+ await renameSafely(part, out);update(job, { out, outputs: [out] });- await zipFiles(out, files, signal);}} finally {- await rm(scratch, { recursive: true, force: true, maxRetries: 5 });+ await tidy(scratch);}}@@ -1015,11 +1192,14 @@ export function createExports({ onChange = () => {}, captions = subtitlesForExpoupdate(job, { phase: 'encoding', progress: 0.92 });const pages = await Promise.all(files.map(async (f) => ({ jpeg: await readFile(f.file), w: size.w, h: size.h, label: f.label })));const out = freeFile(dir, baseName(job.name, ask), pdf ? '.pdf' : '.pptx');+ const part = partialOf(out, job.id);+ update(job, { out: part, outputs: [part] });+ if (pdf) await writeFile(part, await slidesPdf(pages));+ else await writePptx(part, pages, job.name, signal);+ await renameSafely(part, out);update(job, { out, outputs: [out] });- if (pdf) await writeFile(out, await slidesPdf(pages));- else await writePptx(out, pages, job.name, signal);} finally {- await rm(scratch, { recursive: true, force: true, maxRetries: 5 });+ await tidy(scratch);}}@@ -1052,22 +1232,32 @@ export function createExports({ onChange = () => {}, captions = subtitlesForExpoconst draftsDir = app ? drafts(app) : null;const dir = draftsDir && await writableFolder(draftsDir) ? draftsDir : out;if (draftsDir && dir !== draftsDir) update(job, { draftsRefused: draftsDir });+ if (dir !== out) await sweep(dir, LEFT.output);const fps = nleFps(ask.fps ?? 30);const mode = ask.audio ?? 'clips';const title = safeName(job.name);- update(job, { encoder: (await hardwareEncoder('prores', await encoders())) ?? 'prores_ks' });+ /* how the pages are written: for a draft, `auto` (each page's alpha read in every frame as it is drawn, as Studio+ shows it: an opaque page as H.264, one with any clear pixel as lossless PNG), all `png`, or `prores` (422 HQ and+ 4444 the same way); ProRes 4444 or PNG for the other editors */+ const alpha = ask.alpha ?? (app ? 'auto' : 'prores4444');+ const prores = alpha === 'prores' || alpha === 'prores4444';+ const have = await encoders();+ /** how a page with alpha is drawn: PNG (a draft's a quick one, its pictures thinned after; PNG's alpha is exact,+ ProRes's comes back a little off full), or (null) ProRes 4444, whose encoder render picks */+ const pngFormat = app ? QUICK_PNG : alpha === 'png' ? encoding('png', 'master', { w: 0, h: 0 }, fps, have).format : null;/* the edit with every track shown: a hidden track comes over disabled, one click from back on, so it is drawn too */const doc = existsSync(join(root, FILM_FILE)) ? filmDoc(root): { stage: await stageOf(root), tracks: [{ clips: [{ src: 'index.html', id: title }] }] };+ update(job, { encoder: prores && !app ? (await hardwareEncoder('prores', have)) ?? 'prores_ks' : 'png' });/* a source out of the project fails the export before anything is drawn */for (const track of doc.tracks) for (const row of track.clips) clipPath(root, row.src);const { cues } = ask.subtitles === false ? { cues: [] } : await subtitlesOf(root).catch((error) => {if (fcp) throw error;return { cues: [] };});- const scratch = join(root, '.film', 'cache', 'export', `nle-${randomBytes(4).toString('hex')}`);+ const scratch = join(root, '.film', 'cache', 'export', `nle-${process.pid}-${randomBytes(4).toString('hex')}`);const final = freeFile(dir, baseName(job.name, ask), '');- const staging = join(dir, `.${basename(final)}.partial-${job.id}`);+ const staging = join(dir, `.${basename(final)}.partial-${process.pid}-${job.id}`);try {await mkdir(scratch, { recursive: true });await filmElsewhere(root, scratch, { stage: doc.stage, tracks: doc.tracks.map((t) => ({ clips: t.clips })) });@@ -1160,43 +1350,126 @@ export function createExports({ onChange = () => {}, captions = subtitlesForExpoconst timeline = planNleTimeline({ stage: doc.stage, duration: duration || 0.001, pictures, sounds },{ title, fps, audio: mode, media: ask.media ?? 'copy', mediaDir: join(final, 'media'), stems: [...stems.keys()],- audioTracks: fcp ? 'source' : 'roles' });+ audioTracks: fcp ? 'source' : 'roles', alpha: prores ? 'prores4444' : 'png' });const staged = (/** @type {import('./nle.mjs').NleFile} */ f) => join(staging, 'media', basename(f.path));- /* each web page (and each picture Basic Motion can't place) drawn alone, full frame, with alpha */- const renders = timeline.files.filter((f) => f.job.kind === 'render');- const totalFrames = renders.reduce((n, f) => n + (f.job.kind === 'render' ? f.job.frames : 0), 0);+ /* each web page (and each picture Basic Motion can't place) drawn alone, full frame, with alpha. Several are drawn at+ once, the longest first, with as many browsers in all as one render of every frame would get (render.mjs+ defaultWorkers), shared by their frames (shareOf), kept from page to page in one process (drawer); a PNG page's+ pictures are thinned while the next ones draw. One that fails stops the others */+ const renders = timeline.files.filter((f) => f.job.kind === 'render').sort((a, b) => framesOf(b) - framesOf(a));+ const totalFrames = renders.reduce((n, f) => n + framesOf(f), 0);const tick = frames(job, 0.88);- let framesBefore = 0;+ /** @type {Map<string, number>} */+ const drawnFrames = new Map();+ const budget = machine();+ let free = defaultWorkers(budget, totalFrames);/* the chip's ProRes until one of the pages has to do without it: the rest do without it too */let hardware = true;- for (const file of renders) {- if (file.job.kind !== 'render') continue;- const { clip: id, frames } = file.job;+ const stop = new AbortController();+ const quit = () => stop.abort();+ signal.addEventListener('abort', quit, { once: true });+ /* the pages' browsers, started once for all of them */+ const pool = drawer();+ /** @type {Set<Promise<void>>} */+ const drawing = new Set();+ /** @type {Promise<void>[]} */+ const pages = [];+ /** @type {unknown} */+ let failed = null;+ /**+ * A draft's page written as it is drawn, as editors play it best: opaque in every frame, H.264 (the graphics+ * chip's when it has one, else x264 at master quality, BT.709 like a video export) or ProRes 422 HQ; drawn again+ * at the first pixel that shows through as quick PNG (its pictures thinned after) or ProRes 4444. The result+ * says which (`clear`).+ * @param {(how: Record<string, unknown>) => Promise<{ clear?: boolean }>} draw @param {'h264' | 'prores'} codec+ */+ const asEditorsPlay = async (draw, codec) => {+ /** @param {string | null} encoder */+ const formats = (encoder) => (codec === 'h264'+ ? { format: { video: [...encoding('h264', 'master', doc.stage, timeline.rate.fps, have, encoder).format.video, ...H264_709], pix: YUV_709('yuv420p') }, clear: QUICK_PNG }+ : { format: prores709(encoder ?? 'prores_ks', false), clear: prores709(encoder ?? 'prores_ks', true) });+ const chip = await hardwareEncoder(codec, have);+ try {+ return await draw(formats(chip));+ } catch (e) {+ /* the chip's encoder gave up partway: the page again without it, and the rest of this launch does without it (a+ full disk is no encoder's doing) */+ if (!chip || stop.signal.aborted || diskFull(e) || !/^ffmpeg/.test(e instanceof Error ? e.message : '')) throw e;+ broken(chip);+ return draw(formats(null));+ }+ };+ /** @param {import('./nle.mjs').NleFile} file @param {number} workers @param {() => void} drawn */+ const page = async (file, workers, drawn) => {+ const { clip: id, frames } = /** @type {Extract<import('./nle.mjs').NleMediaJob, { kind: 'render' }>} */ (file.job);const p = /** @type {import('./nle.mjs').NlePicture} */ (pictures.find((x) => x.id === id));const native = byId.get(id)?.native ?? Infinity;/* as long as its frames on the sequence: a page holds its last frame, a still lasts as long as it is told */const time = p.kind === 'video' ? [p.from, Math.min(native, p.from + (frames / timeline.rate.fps) * p.speed)] : [p.from, p.from + frames / timeline.rate.fps];- const one = await clipFilm(root, id, { change: { time }, doc });- const offset = framesBefore;+ /* a draft's page with no clear pixel in any frame needs no alpha (each frame's alpha is read as it is drawn, so+ it is what Studio shows): written opaque as it is drawn, nothing written twice. At the first pixel that shows+ through it is drawn again with its alpha: as lossless PNG, or as ProRes 4444 (`prores`) */+ let clear = true;try {- const drawn = await render([one.dir], {- root, alpha: true, silent: true, fps: timeline.rate.fps, out: staged(file), signal, hardware,- onProgress: (/** @type {number} */ done) => tick(offset + done, totalFrames),- });- if (drawn.encoder !== job.encoder) update(job, { encoder: drawn.encoder });- if (drawn.encoder === 'prores_ks') hardware = false;+ const one = await clipFilm(root, id, { change: { time }, doc });+ try {+ /** @param {Record<string, unknown>} how */+ const draw = async (how) => {+ const result = await pool.render([one.dir], {+ root, alpha: true, silent: true, fps: timeline.rate.fps, out: staged(file), signal: stop.signal, workers, ...how,+ /* a page drawn again counts from where it was: it holds until the second drawing passes it */+ onProgress: (/** @type {number} */ done) => {+ drawnFrames.set(file.id, Math.max(drawnFrames.get(file.id) ?? 0, done));+ tick([...drawnFrames.values()].reduce((n, d) => n + d, 0), totalFrames);+ },+ });+ if (result.encoder !== job.encoder) update(job, { encoder: result.encoder });+ return result;+ };+ if (app && alpha !== 'png') clear = (await asEditorsPlay(draw, alpha === 'prores' ? 'prores' : 'h264')).clear === true;+ else if ((await draw(pngFormat ? { format: pngFormat } : { hardware })).encoder === 'prores_ks') hardware = false;+ } finally {+ await one.remove();+ }} finally {- await one.remove();+ drawn();}- framesBefore += frames;/* a source shorter than its place by a frame of rounding: the clip ends where its picture does */const made = await countFrames(staged(file));if (made > 0 && made !== frames) {file.durationFrames = made;for (const t of timeline.video) for (const c of t.clips) if (c.fileId === file.id) { c.out = made; c.end = c.start + made; }}+ /* a draft's PNG page keeps each picture once, for as long as it shows (剪映 plays a picture's own length): a+ fraction of the frames, the same pixels */+ if (app && clear && alpha !== 'prores') await keepChanges(staged(file), stop.signal);+ };+ try {+ let left = totalFrames;+ for (const file of renders) {+ while (!free && !failed) await Promise.race(drawing);+ if (failed) break;+ const workers = shareOf(free, framesOf(file), left, defaultWorkers(budget, framesOf(file)));+ left -= framesOf(file);+ free -= workers;+ /* its browsers back for the next page once it is drawn, while its pictures are thinned (a PNG page) */+ /** @type {() => void} */+ let drawn = () => {};+ const slot = new Promise((r) => { drawn = () => r(undefined); }).then(() => { drawing.delete(slot); free += workers; });+ drawing.add(slot);+ pages.push(page(file, workers, drawn).catch((e) => {+ failed ??= e;+ stop.abort();+ }));+ }+ await Promise.all(pages);+ } finally {+ signal.removeEventListener('abort', quit);+ await pool.close();}+ if (signal.aborted) throw new Error('cancelled');+ if (failed) throw failed;update(job, { phase: 'encoding', progress: 0.9, rate: 0, etaMs: null });const others = timeline.files.filter((f) => f.job.kind !== 'render' && f.job.kind !== 'link');@@ -1213,7 +1486,7 @@ export function createExports({ onChange = () => {}, captions = subtitlesForExpoelse if (make.kind === 'raster') {browser ??= await launch();await rasterize(browser, make.from, make.w, make.h, to);- } else if (make.kind === 'transcode') await transcode(make, to, signal);+ } else if (make.kind === 'transcode') await transcode(make, to, signal, file.video);update(job, { progress: Math.floor((0.9 + 0.08 * ((i + 1) / others.length)) * 100) / 100 });}} finally {@@ -1236,7 +1509,7 @@ export function createExports({ onChange = () => {}, captions = subtitlesForExpo}if (signal.aborted) throw new Error('cancelled');if (!(await readdir(join(staging, 'media'))).length) await rmdir(join(staging, 'media'));- await rename(staging, final);+ await renameSafely(staging, final);const media = existsSync(join(final, 'media')) ? (await readdir(join(final, 'media'))).sort().map((n) => join(final, 'media', n)) : [];const first = app ? 'draft_info.json' : fcp ? '.fcpxml' : '.xml';const top = (await readdir(final)).filter((n) => n !== 'media').sort((a, b) => Number(!a.endsWith(first)) - Number(!b.endsWith(first)) || a.localeCompare(b));@@ -1244,8 +1517,8 @@ export function createExports({ onChange = () => {}, captions = subtitlesForExpoconst outputs = [...top.map((n) => join(final, n)), ...media];update(job, { out: outputs[0], outputs });} finally {- await rm(scratch, { recursive: true, force: true, maxRetries: 5 });- await rm(staging, { recursive: true, force: true });+ await tidy(scratch);+ await tidy(staging);}}@@ -1267,12 +1540,15 @@ export function createExports({ onChange = () => {}, captions = subtitlesForExpo};if ((ask.as ?? 'zip') === 'zip') {const out = freeFile(dir, title, '.zip');+ const part = partialOf(out, job.id);+ update(job, { out: part, outputs: [part] });+ await zipFiles(part, files.map((f) => ({ name: `${title}/${f.name}`, file: f.file })), signal, copied);+ await renameSafely(part, out);update(job, { out, outputs: [out] });- await zipFiles(out, files.map((f) => ({ name: `${title}/${f.name}`, file: f.file })), signal, copied);return;}const final = freeFile(dir, title, '');- const staging = join(dir, `.${basename(final)}.partial-${job.id}`);+ const staging = join(dir, `.${basename(final)}.partial-${process.pid}-${job.id}`);try {let bytes = 0;for (const f of files) {@@ -1283,10 +1559,10 @@ export function createExports({ onChange = () => {}, captions = subtitlesForExpocopied(bytes += f.size);}await mkdir(staging, { recursive: true });- await rename(staging, final);+ await renameSafely(staging, final);update(job, { out: final, outputs: [final] });} finally {- await rm(staging, { recursive: true, force: true });+ await tidy(staging);}}@@ -1312,14 +1588,20 @@ export function createExports({ onChange = () => {}, captions = subtitlesForExpo}async function next() {- if (running) return;+ if (running || closing) return;/* the oldest waiting: the list is newest first, and exports run in the order asked */const job = jobs.findLast((j) => j.status === 'waiting');if (!job) return;running = true;+ /** @type {() => void} */+ let finished = () => {};+ idle = new Promise((r) => { finished = () => r(undefined); });const { root, folder, abort, twin } = /** @type {{ root: string, folder: string, abort: AbortController, twin?: string }} */ (work.get(job.id));try {await mkdir(folder, { recursive: true });+ /* what an export Studio could not finish left behind, gone before this one starts */+ await Promise.all([sweep(folder, LEFT.output), sweep(join(root, '.film', 'cache', 'export'), LEFT.scratch),+ sweep(join(root, '.film', 'cache', 'render'), LEFT.parts), sweep(join(root, '.film', 'cache', 'seek'), LEFT.copy)]);update(job, { status: 'running', phase: 'preparing', startedAt: Date.now() });const kind = job.ask.kind;/* the same export as one made just before it (asked together), but for its name: that one's files, cloned */@@ -1340,12 +1622,16 @@ export function createExports({ onChange = () => {}, captions = subtitlesForExpo/* what a cancelled or failed export left half written is not an export */for (const f of job.outputs) await rm(f, { force: true }).catch(() => {});const ended = { phase: null, etaMs: null, outputs: [], out: undefined, finishedAt: Date.now() };- const error = e instanceof Error ? e.message : String(e);- update(job, abort.signal.aborted ? { ...ended, status: 'cancelled' } : { ...ended, status: 'failed', error });- if (!abort.signal.aborted) recordProblem({ area: 'export', step: job.ask.kind, what: error, project: root });+ const said = e instanceof Error ? e.message : String(e);+ /* a full disk is the person's to fix, said plainly (in ffmpeg's words it is lines of its own) */+ const full = diskFull(e);+ const error = full ? 'the disk is full: free up some space, then export again' : said;+ update(job, abort.signal.aborted ? { ...ended, status: 'cancelled' } : { ...ended, status: 'failed', error, ...(full ? { diskFull: true } : {}) });+ if (!abort.signal.aborted) recordProblem({ area: 'export', step: job.ask.kind, what: said, project: root });} finally {work.delete(job.id);running = false;+ finished();next();}}@@ -1413,8 +1699,16 @@ export function createExports({ onChange = () => {}, captions = subtitlesForExpo},/** Whether an export is waiting or running. */busy: () => work.size > 0 || jobs.some((j) => j.status === 'waiting'),- /** Stop everything (Studio is closing). */- stopAll() { for (const { abort } of work.values()) abort.abort(); },+ /**+ * Stop everything (Studio is closing): what waits is cancelled, what runs stops. Resolves once the running export has+ * cleaned up after itself (its half-made files gone), or after STOP_WAIT_MS.+ */+ stopAll() {+ closing = true;+ for (const job of jobs) if (job.status === 'waiting') { work.delete(job.id); update(job, { status: 'cancelled', finishedAt: Date.now() }); }+ for (const { abort } of work.values()) abort.abort();+ return Promise.race([idle, new Promise((r) => { setTimeout(r, STOP_WAIT_MS).unref(); })]);+ },};}@@ -1438,7 +1732,8 @@ function render(args, opts) {const settle = (end) => { if (settled) return; settled = true; signal?.removeEventListener('abort', cancel); end(); };function cancel() {if (child.connected) child.send({ type: 'cancel' });- setTimeout(() => { if (child.exitCode == null && child.signalCode == null) child.kill('SIGKILL'); }, 5000).unref();+ /* its browsers and encoders with it: on Windows ending a process leaves its children running */+ setTimeout(() => killProcess(child), 5000).unref();}signal?.addEventListener('abort', cancel, { once: true });child.on('message', (/** @type {any} */ m) => {@@ -1455,6 +1750,81 @@ function render(args, opts) {});}+/**+ * Renders in one process of their own (render-worker.mjs `--pool`) that share its browsers: an editing project's+ * pages, drawn several at once and page after page, start a browser once rather than for each page, and one whose+ * browser crashed or stopped answering is drawn again in new ones. `render` takes render's arguments and gives its+ * result, as the one-shot render does; `close` ends the process and every browser (after the last page, or on cancel).+ * A process that dies fails every render still in it.+ */+function drawer() {+ const child = fork(fileURLToPath(new URL('./render-worker.mjs', import.meta.url)), ['--pool'], { stdio: ['ignore', 'inherit', 'pipe', 'ipc'] });+ below(child);+ let said = '';+ child.stderr?.on('data', (d) => { said = (said + d).slice(-4000); process.stderr.write(d); });+ /** @type {Map<string, { onProgress?: (done: number, total: number) => void, done: (r: any) => void, fail: (e: Error) => void, signal?: AbortSignal }>} */+ const jobs = new Map();+ /** @type {Error | null} */+ let gone = null;+ let next = 0;+ child.on('message', (/** @type {any} */ m) => {+ const job = jobs.get(m?.id);+ if (!job) return;+ if (m.type === 'progress') job.onProgress?.(m.done, m.total);+ else if (m.type === 'done' || m.type === 'failed') {+ jobs.delete(m.id);+ if (m.type === 'done') job.done(m.result);+ else job.fail(new Error(job.signal?.aborted ? 'cancelled' : m.error));+ }+ });+ const end = (/** @type {Error} */ e) => {+ gone ??= e;+ for (const [id, job] of jobs) { jobs.delete(id); job.fail(job.signal?.aborted ? new Error('cancelled') : e); }+ };+ child.on('error', end);+ child.on('exit', (code, sig) => {+ const last = said.trim().split('\n').filter((l) => l.trim() && !/^\s+at /.test(l) && !/drawn again in new browsers$/.test(l)).pop();+ end(new Error(`the renderer stopped (${sig ?? `exit ${code}`})${last ? `: ${last.slice(0, 300)}` : ''}`));+ });+ return {+ /** @param {string[]} args @param {Record<string, any>} opts @returns {Promise<{ out: string, encoder: string, clear?: boolean, lines: string[] }>} */+ render(args, opts) {+ const { signal, onProgress, ...rest } = opts;+ return new Promise((done, fail) => {+ if (signal?.aborted) return fail(new Error('cancelled'));+ if (gone) return fail(gone);+ const id = String(++next);+ const cancel = () => { if (child.connected) child.send({ type: 'cancel', id }); };+ signal?.addEventListener('abort', cancel, { once: true });+ const off = () => signal?.removeEventListener('abort', cancel);+ jobs.set(id, { onProgress, signal, done: (r) => { off(); done(r); }, fail: (e) => { off(); fail(e); } });+ child.send({ type: 'render', id, args, opts: rest });+ });+ },+ /** Ends the process: what still draws stops, the browsers close; ended outright if it has not after a few seconds. */+ async close() {+ if (child.exitCode != null || child.signalCode != null) return;+ const exited = new Promise((r) => child.once('exit', r));+ if (child.connected) child.send({ type: 'close' });+ /* its browsers and encoders with it: on Windows ending a process leaves its children running */+ const kill = setTimeout(() => killProcess(child), 6000);+ await exited;+ clearTimeout(kill);+ },+ };+}++/** How many frames a file of an editing project is drawn with: a page's or a still's, 0 for anything else. @param {import('./nle.mjs').NleFile} file */+const framesOf = (file) => (file.job.kind === 'render' ? file.job.frames : 0);++/**+ * How many of the `free` browsers a page of `frames` draws with, when `left` frames are still to be drawn (its own+ * included): its share by its frames, at least one, and no more than it would get alone (`alone`). Pages of a film each+ * draw with one, several at once, so no browser is started for a few frames; a long page gets more.+ * @param {number} free @param {number} frames @param {number} left @param {number} alone+ */+export const shareOf = (free, frames, left, alone) => Math.max(1, Math.min(free, alone, Math.ceil((free * frames) / Math.max(1, left))));+/** A number as render's range takes it: plain digits, no exponent. @param {number} n */const num = (n) => String(Number(n.toFixed(3)));diff --git a/packages/openfilm/studio/server/exports.test.mjs b/packages/openfilm/studio/server/exports.test.mjsindex 069d768..c731b84 100644--- a/packages/openfilm/studio/server/exports.test.mjs+++ b/packages/openfilm/studio/server/exports.test.mjs@@ -1,11 +1,11 @@import { test, before } from 'node:test';import assert from 'node:assert/strict';-import { execFileSync, spawnSync } from 'node:child_process';+import { execFileSync, spawn, spawnSync } from 'node:child_process';import { chmodSync, existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, symlinkSync, writeFileSync } from 'node:fs';import { homedir, tmpdir } from 'node:os';-import { basename, join } from 'node:path';+import { basename, dirname, join } from 'node:path';import { fileURLToPath } from 'node:url';-import { createExports, exportCapabilities, exportsRoot, ExportError, frameBox, dimsForShortEdge, projectFiles, projectSizes, writableFolder } from './exports.mjs';+import { createExports, diskFull, exportCapabilities, exportsRoot, ExportError, frameBox, dimsForShortEdge, keepChanges, projectFiles, projectSizes, shareOf, writableFolder } from './exports.mjs';import { encoders, hardwareEncoder } from '../../src/encoders.mjs';import { allocateLanes, containMotion, fadeKeyframes, frameSpan, nleRate, planNleTimeline, xmemlDocument } from './nle.mjs';import { fcpxmlDocument } from './fcpxml.mjs';@@ -180,6 +180,29 @@ test('a name is text, at most 120 characters', () => {}});+test('names Windows can hold: no dot or space at the end, none of its device names', async () => {+ const exports = createExports({ captions: subtitles });+ for (const [name, file] of [['Intro.', 'Intro.srt'], ['Outro . . ', 'Outro.srt'], ['CON', 'CON film.srt'], ['aux.final', 'aux film.final.srt'], ['Console', 'Console.srt']]) {+ const done = await ended(exports, exports.start(project(), { kind: 'subtitles', name }));+ assert.equal(done.status, 'done', done.error);+ assert.equal(basename(done.out), file, JSON.stringify(name));+ }+});++test('a full disk is said plainly, whichever words ffmpeg or the system use for it', async () => {+ for (const words of ['ffmpeg: [out#0/mov @ 0x1] Error writing trailer: No space left on device', 'ENOSPC: no space left on device, write', 'There is not enough space on the disk.']) {+ assert.ok(diskFull(new Error(words)), words);+ }+ assert.ok(diskFull(Object.assign(new Error('write'), { code: 'ENOSPC' })));+ for (const other of [new Error('ffmpeg: Invalid data found when processing input'), new Error('cancelled'), 'cancelled', null]) assert.ok(!diskFull(other), String(other));+ /* the export that meets it fails with that, and the tray says it in the person's language */+ const full = createExports({ captions: async () => { throw Object.assign(new Error('ENOSPC: no space left on device, write'), { code: 'ENOSPC' }); } });+ const done = await ended(full, full.start(project(), { kind: 'subtitles' }));+ assert.equal(done.status, 'failed');+ assert.equal(done.diskFull, true);+ assert.equal(done.error, 'the disk is full: free up some space, then export again');+});+test('a renderer that dies fails its export with the reason, and exports go on', { ...ffmpeg, skip: ffmpeg.skip || (process.platform === 'win32' && 'no pgrep') }, async () => {const exports = createExports();const job = exports.start(project('Crash'), { kind: 'video' });@@ -211,13 +234,52 @@ test('a cancelled export stops and leaves no file, and a bad ask is refused befoconst stopped = await ended(exports, job);assert.equal(stopped.status, 'cancelled');assert.ok(!existsSync(running.out), 'the half-written file is gone');+ assert.deepEqual(readdirSync(exportsRoot()).filter((n) => n.startsWith('Cancel') || n.includes('.partial-')), [], 'nothing under its name, nothing half made');for (const bad of [{ kind: 'pdf' }, { kind: 'video', range: [3, 1] }, { kind: 'video', codec: 'av1' }, { kind: 'video', frame: '9:1' }, { kind: 'video', frame: 'wide' },{ kind: 'audio', mix: false }, { kind: 'stills', range: [0, 1] }, { kind: 'gif', width: 5000 }, { kind: 'subtitles', format: 'ass' },- { kind: 'slides', format: 'key' }, { kind: 'slides', range: [0, 1] }, { kind: 'nle', fps: 500 }, { kind: 'nle', audio: 'mix' }, { kind: 'nle', range: [0, 1] }]) {+ { kind: 'slides', format: 'key' }, { kind: 'slides', range: [0, 1] }, { kind: 'nle', fps: 500 }, { kind: 'nle', audio: 'mix' }, { kind: 'nle', range: [0, 1] },+ { kind: 'nle', format: 'fcpxml', alpha: 'png' }, { kind: 'nle', alpha: 'auto' }, { kind: 'nle', format: 'jianying', alpha: 'hevc_alpha' }, { kind: 'nle', format: 'jianying', alpha: 'prores4444' }]) {assert.throws(() => exports.start(project(), bad), ExportError, JSON.stringify(bad));}});+test('what an export Studio could not finish left behind goes before the next one; what a running Studio is making stays', ffmpeg, async () => {+ /* a process that has ended, one still running, and this one */+ const gone = spawn(process.execPath, ['-e', '']);+ await new Promise((r) => gone.on('exit', r));+ const live = spawn(process.execPath, ['-e', 'setTimeout(() => {}, 60000)']);+ try {+ const out = exportsRoot();+ const cache = (sub) => join(root, '.film', 'cache', sub);+ const left = (pid) => [join(out, `.Old.partial-${pid}-0123456789ab.mp4`), join(out, `.Old draft.partial-${pid}-0123456789ab`, 'media', 'p.mov'),+ join(cache('export'), `nle-${pid}-0123abcd`, 'film.html'), join(cache('render'), `.parts-${pid}`, 'part-0.mov'),+ join(cache('seek'), `0123abcd.mp4.part-${pid}-0123abcd.mp4`)];+ for (const pid of [gone.pid, live.pid, process.pid]) for (const f of left(pid)) { mkdirSync(dirname(f), { recursive: true }); writeFileSync(f, 'x'); }+ const theirs = [join(out, '.Notes.partial'), join(out, 'Old.partial-1-0123456789ab.mp4'), join(cache('export'), 'nle-mine')];+ for (const f of theirs) writeFileSync(f, 'x');+ const exports = createExports({ captions: subtitles });+ assert.equal((await ended(exports, exports.start(project(), { kind: 'subtitles' }))).status, 'done');+ for (const f of left(gone.pid)) assert.ok(!existsSync(dirname(f).endsWith('media') ? dirname(dirname(f)) : f), `swept: ${f}`);+ for (const f of [...left(live.pid), ...left(process.pid), ...theirs]) assert.ok(existsSync(f), `kept: ${f}`);+ for (const f of [...left(live.pid), ...left(process.pid), ...theirs]) rmSync(dirname(f).endsWith('media') ? dirname(dirname(f)) : /(nle|\.parts)-\d+/.test(dirname(f)) ? dirname(f) : f, { recursive: true, force: true });+ } finally {+ live.kill();+ }+});++test('Studio closing stops its exports and waits for their half-made files to go', ffmpeg, async () => {+ const exports = createExports();+ const first = exports.start(project('Closing'), { kind: 'video' });+ const second = exports.start(project('Closing next'), { kind: 'video' });+ await until(exports, first.id, (j) => j.status === 'running' && j.framesDone > 0);+ await exports.stopAll();+ const [a, b] = [first, second].map((j) => exports.list('p').find((x) => x.id === j.id));+ assert.deepEqual([a.status, b.status], ['cancelled', 'cancelled']);+ assert.deepEqual(readdirSync(exportsRoot()).filter((n) => n.startsWith('Closing') || n.includes('.partial-')), [], 'nothing under their names, nothing half made');+ const parts = join(root, '.film', 'cache', 'render');+ assert.deepEqual(existsSync(parts) ? readdirSync(parts).filter((n) => n.startsWith('.parts-')) : [], [], 'no render parts');+});+test('the delivery frame: the same box as the editor, the short side asked, never past 4096', () => {assert.deepEqual(frameBox({ w: 1920, h: 1080 }, '9:16', 'contain'), { w: 1920, h: 3414, ratio: 9 / 16, scale: 1 });assert.deepEqual(dimsForShortEdge(frameBox({ w: 1920, h: 1080 }, '9:16', 'contain'), 1080), { w: 1080, h: 1920 });@@ -259,6 +321,45 @@ test('each codec this machine encodes, at the size asked, transparent where theif (hevc) assert.equal(probe(hevc, 'stream=codec_tag_string'), 'hvc1', 'HEVC that Apple players open');});+test('every codec carries the colors the page draws, tagged BT.709, through the graphics chip and without it', { ...ffmpeg, timeout: 300_000 }, async () => {+ const dir = join(scratch, 'Colors');+ mkdirSync(dir, { recursive: true });+ /* saturated red and green, where BT.601 and BT.709 differ most */+ writeFileSync(join(dir, 'colors.html'), `<!doctype html><body style="margin:0;width:320px;height:180px;background:#00ff00"><div style="position:absolute;left:0;top:0;width:160px;height:180px;background:#ff0000"></div>+<script>window.film={duration:0.4,width:320,height:180,frame(t){}}</script></body>`);+ writeFileSync(join(dir, 'film.html'), filmHtml({ stage: { w: 320, h: 180 }, tracks: [{ clips: [{ src: 'colors.html', id: 'colors' }] }] }));+ const can = await exportCapabilities();+ const codecs = ['h264', 'prores422hq', 'prores4444', 'vp9', ...(can.hevc ? ['hevc'] : []), ...(can.hevcAlpha ? ['hevc_alpha'] : [])];+ for (const codec of codecs) {+ for (const hardware of [true, false]) {+ const done = await exported({ kind: 'video', codec, fps: 5, hardware }, 'Colors', dir);+ const label = `${codec}${hardware ? '' : ' without the chip'}`;+ assert.equal(probe(done.out, 'stream=color_space,color_primaries,color_transfer'), 'bt709,bt709,bt709', label);+ const frame = firstFrame(done.out);+ assert.ok(near(frame.at(80, 90), [255, 0, 0], 10), `${label}: red ${frame.at(80, 90)}`);+ assert.ok(near(frame.at(240, 90), [0, 255, 0], 10), `${label}: green ${frame.at(240, 90)}`);+ /* its alpha layer whole: ffmpeg copying such a file drops the layer's parameter sets, and Apple's apps open it no more */+ if (codec === 'hevc_alpha') assert.equal(spawnSync('ffmpeg', ['-v', 'error', '-i', done.out, '-f', 'null', '-'], { encoding: 'utf8' }).stderr, '', `${label}: decodes without an error`);+ }+ }+});++test('footage no browser reads, placed whole (no trim): its length is ffprobe\'s, in a video and in a draft', { ...ffmpeg, timeout: 240_000 }, async () => {+ const dir = join(scratch, 'Whole ProRes');+ mkdirSync(dir, { recursive: true });+ execFileSync('ffmpeg', ['-v', 'error', '-f', 'lavfi', '-i', 'testsrc2=size=320x180:rate=25:duration=1.2', '-c:v', 'prores_ks', '-profile:v', '3', join(dir, 'pro.mov')]);+ writeFileSync(join(dir, 'film.html'), filmHtml({ stage: { w: 320, h: 180 }, tracks: [{ clips: [{ id: 'pro', src: 'pro.mov' }] }] }));+ const video = await exported({ kind: 'video', fps: 25 }, 'Whole ProRes', dir);+ const length = Number(execFileSync('ffprobe', ['-v', 'error', '-show_entries', 'format=duration', '-of', 'csv=p=0', video.out]).toString());+ assert.ok(Math.abs(length - 1.2) < 0.05, `${length} s`);+ const drafts = join(scratch, 'Whole ProRes drafts');+ mkdirSync(drafts, { recursive: true });+ const exports = createExports({ captions: subtitles, drafts: () => drafts });+ const draft = await ended(exports, exports.start(project('Whole ProRes', dir), { kind: 'nle', format: 'jianying', fps: 25 }));+ assert.equal(draft.status, 'done', draft.error);+ assert.equal(JSON.parse(readFileSync(draft.out, 'utf8')).duration, 1_200_000);+});+test('a PNG sequence comes zipped, one PNG with alpha per frame', ffmpeg, async () => {const done = await exported({ kind: 'video', codec: 'png', fps: 5, range: [0, 0.4] });assert.equal(done.out, join(exportsRoot(), 'Tiny (alpha) 0-0.4s.zip'));@@ -727,6 +828,7 @@ test('editing software: an xmeml folder; pages rendered with alpha, footage placassert.deepEqual(rel, ['Cut.xml', 'Cut.srt', 'media/badge.png', 'media/bed x2.wav', 'media/shot one x0.5.wav', 'media/shot one.mp4', 'media/slow.mov','media/title-2.mov', 'media/title.mov', 'media/voice.wav']);assert.equal(done.out, join(folder, 'Cut.xml'));+ assert.equal(probe(join(folder, 'media', 'title.mov'), 'stream=codec_name,pix_fmt').split(',')[0], 'prores', 'ProRes 4444 for the other editors');assert.equal(readFileSync(join(folder, 'Cut.srt'), 'utf8'), filmSubtitleSrt(CUES));for (const page of ['title', 'title-2']) {const mov = join(folder, 'media', `${page}.mov`);@@ -801,6 +903,14 @@ test('剪映: the same timeline as a draft in 剪映\'s drafts folder, its mediaassert.deepEqual(readdirSync(folder).sort(), ['draft_content.json', 'draft_info.json', 'draft_meta_info.json', 'media']);assert.equal(readFileSync(join(folder, 'draft_content.json'), 'utf8'), readFileSync(join(folder, 'draft_info.json'), 'utf8'));assert.deepEqual(readdirSync(drafts).filter((n) => n.startsWith('.')), [], 'no half-made draft');+ /* a page lossless, with its alpha (剪映 shows PNG's on both systems), and clear where the page draws nothing */+ const page = join(folder, 'media', 'title.mov');+ assert.equal(probe(page, 'stream=codec_name,pix_fmt'), 'png,rgba');+ const corner = execFileSync('ffmpeg', ['-v', 'error', '-i', page, '-frames:v', '1', '-vf', 'crop=1:1:200:120,format=rgba', '-f', 'rawvideo', '-']);+ assert.equal(corner[3], 0, 'transparent away from the page\'s box');+ /* the page holds still: its first and last pictures, for its whole second */+ assert.equal(frameCount(page), 2);+ assert.equal(Number(execFileSync('ffprobe', ['-v', 'error', '-show_entries', 'format=duration', '-of', 'csv=p=0', page]).toString()), 1);const info = JSON.parse(readFileSync(done.out, 'utf8'));const meta = JSON.parse(readFileSync(join(folder, 'draft_meta_info.json'), 'utf8'));assert.deepEqual([meta.draft_name, meta.draft_fold_path, meta.draft_root_path, meta.draft_id], ['Cut', folder, drafts, info.id]);@@ -835,6 +945,195 @@ test('剪映: the same timeline as a draft in 剪映\'s drafts folder, its media}});+test('剪映: a page with no clear pixel in any frame comes as H.264, one clear for a single frame stays PNG', { ...ffmpeg, timeout: 240_000 }, async () => {+ const dir = join(scratch, 'Opaque');+ mkdirSync(dir, { recursive: true });+ /* both fill the frame; `blink` lets the stage through for one frame (0.4 s, the eleventh at 25 fps) */+ const page = (clearAt) => `<!doctype html><body style="margin:0;width:320px;height:180px"><script>window.film={duration:1,width:320,height:180,frame(t){+document.body.style.background=${clearAt === null ? 'false' : `Math.abs(t-${clearAt})<0.01`}?'transparent':'hsl('+Math.round(t*300)+',70%,40%)'}}</script></body>`;+ writeFileSync(join(dir, 'solid.html'), page(null));+ writeFileSync(join(dir, 'blink.html'), page(0.4));+ writeFileSync(join(dir, 'film.html'), filmHtml({ stage: { w: 320, h: 180 }, tracks: [{ clips: [{ src: 'solid.html', id: 'solid' }, { src: 'blink.html', id: 'blink', at: 1 }] }] }));+ const drafts = join(scratch, 'Opaque drafts');+ mkdirSync(drafts, { recursive: true });+ const seen = [];+ const exports = createExports({ captions: subtitles, drafts: (app) => (app === 'jianying' ? drafts : null), onChange: (job) => seen.push(job) });+ const media = async (alpha) => {+ const done = await ended(exports, exports.start(project('Opaque', dir), { kind: 'nle', format: 'jianying', fps: 25, subtitles: false, ...(alpha ? { alpha } : {}) }));+ assert.equal(done.status, 'done', done.error);+ /* `blink` is drawn opaque up to its clear frame, then again with its alpha: what was drawn stays counted */+ const progress = seen.filter((j) => j.id === done.id && j.status === 'running').map((j) => j.progress);+ assert.deepEqual(progress.filter((v, i) => i && v < progress[i - 1]), [], 'progress never goes back');+ return (name) => join(dirname(done.out), 'media', name);+ };+ const auto = await media();+ assert.equal(probe(auto('solid.mov'), 'stream=codec_name,pix_fmt'), 'h264,yuv420p', 'opaque in every frame: H.264');+ /* the first frame is hsl(0, 70%, 40%): rgb(173, 31, 31), in the colors Studio shows, tagged so editors read them so */+ const red = (file) => firstFrame(file).at(160, 90);+ assert.equal(probe(auto('solid.mov'), 'stream=color_space,color_primaries,color_transfer,color_range'), 'tv,bt709,bt709,bt709', 'range, matrix, transfer, primaries');+ assert.ok(near(red(auto('solid.mov')), [173, 31, 31], 8), `H.264: ${red(auto('solid.mov'))}`);+ assert.equal(frameCount(auto('solid.mov')), 25);+ assert.equal(probe(auto('blink.mov'), 'stream=codec_name,pix_fmt'), 'png,rgba', 'one clear frame: lossless, with its alpha');+ const clear = execFileSync('ffmpeg', ['-v', 'error', '-ss', '0.4', '-i', auto('blink.mov'), '-frames:v', '1', '-vf', 'crop=1:1:10:10,format=rgba', '-f', 'rawvideo', '-']);+ assert.equal(clear[3], 0, 'that frame clear, as Studio shows it');+ const prores = await media('prores');+ assert.equal(probe(prores('solid.mov'), 'stream=codec_name,profile'), 'prores,HQ');+ /* ProRes carries no range flag (it is video range): ffprobe before FFmpeg 7 says unknown */+ assert.match(probe(prores('solid.mov'), 'stream=color_space,color_primaries,color_transfer,color_range'), /^(tv|unknown),bt709,bt709,bt709$/, 'range, matrix, transfer, primaries');+ assert.ok(near(red(prores('solid.mov')), [173, 31, 31], 6), `ProRes: ${red(prores('solid.mov'))}`);+ assert.match(probe(prores('blink.mov'), 'stream=codec_name,profile'), /^prores,4444/);+ const png = await media('png');+ assert.equal(probe(png('solid.mov'), 'stream=codec_name'), 'png', 'all PNG when asked');+});++test('剪映: a page that fails stops the export, its error said, nothing left behind', { ...ffmpeg, timeout: 240_000 }, async () => {+ const dir = join(scratch, 'Broken');+ mkdirSync(dir, { recursive: true });+ /* `broken` throws partway, while the pages around it may be drawing */+ const page = (breaks) => `<!doctype html><body style="margin:0;width:160px;height:90px"><script>window.film={duration:2,width:160,height:90,frame(t){+${breaks ? "if(t>1)throw new Error('the page broke');" : ''}document.body.style.background='hsl('+Math.round(t*100)+',70%,40%)'}}</script></body>`;+ writeFileSync(join(dir, 'a.html'), page(false));+ writeFileSync(join(dir, 'broken.html'), page(true));+ writeFileSync(join(dir, 'c.html'), page(false));+ writeFileSync(join(dir, 'film.html'), filmHtml({ stage: { w: 160, h: 90 }, tracks: [{ clips: [{ src: 'a.html', id: 'a' }, { src: 'broken.html', id: 'broken', at: 2 }, { src: 'c.html', id: 'c', at: 4 }] }] }));+ const drafts = join(scratch, 'Broken drafts');+ mkdirSync(drafts, { recursive: true });+ const exports = createExports({ captions: subtitles, drafts: (app) => (app === 'jianying' ? drafts : null) });+ const done = await ended(exports, exports.start(project('Broken', dir), { kind: 'nle', format: 'jianying', fps: 30, subtitles: false }));+ assert.equal(done.status, 'failed');+ assert.match(done.error, /the page broke/);+ assert.deepEqual(readdirSync(drafts), [], 'no draft, and no part of one');+});++/** A film of three pages long enough to be ended partway, for a draft. */+function threePages(name) {+ const dir = join(scratch, name);+ mkdirSync(dir, { recursive: true });+ const page = (hue) => `<!doctype html><body style="margin:0;width:320px;height:180px"><script>window.film={duration:4,width:320,height:180,frame(t){+document.body.style.background='hsl('+Math.round(${hue}+t*60)+',70%,40%)'}}</script></body>`;+ for (const [i, id] of ['a', 'b', 'c'].entries()) writeFileSync(join(dir, `${id}.html`), page(i * 100));+ writeFileSync(join(dir, 'film.html'), filmHtml({ stage: { w: 320, h: 180 }, tracks: [{ clips: [{ src: 'a.html', id: 'a' }, { src: 'b.html', id: 'b', at: 4 }, { src: 'c.html', id: 'c', at: 8 }] }] }));+ const drafts = join(scratch, `${name} drafts`);+ mkdirSync(drafts, { recursive: true });+ return { dir, drafts, exports: createExports({ captions: subtitles, drafts: (app) => (app === 'jianying' ? drafts : null) }) };+}+/** The processes started by `parent` whose command matches `pattern`. */+const children = (parent, pattern) => spawnSync('pgrep', ['-P', String(parent), '-f', pattern], { encoding: 'utf8' }).stdout.trim().split('\n').filter(Boolean).map(Number);++test('剪映: the pages\' browsers, ended partway as a crash would end them, are started again and the draft is whole', { ...ffmpeg, timeout: 240_000, skip: ffmpeg.skip || (process.platform === 'win32' && 'no pgrep') }, async () => {+ const { dir, exports } = threePages('Crashing browsers');+ const job = exports.start(project('Crashing browsers', dir), { kind: 'nle', format: 'jianying', fps: 30, subtitles: false });+ await until(exports, job.id, (j) => ['done', 'failed', 'cancelled'].includes(j.status) || (j.framesDone ?? 0) > 20);+ /* every browser the pages are drawing in, ended at once */+ const [worker] = children(process.pid, 'render-worker');+ assert.ok(worker, 'the pages draw in a process of their own');+ const browsers = children(worker, 'chrom');+ assert.ok(browsers.length, 'with browsers of its own');+ for (const pid of browsers) process.kill(pid, 'SIGKILL');+ const done = await ended(exports, job);+ assert.equal(done.status, 'done', done.error);+ for (const id of ['a', 'b', 'c']) assert.equal(frameCount(join(dirname(done.out), 'media', `${id}.mov`)), 120, `${id}: every frame`);+});++test('剪映: the process drawing the pages, ended partway, fails the draft with the reason and leaves nothing', { ...ffmpeg, timeout: 240_000, skip: ffmpeg.skip || (process.platform === 'win32' && 'no pgrep') }, async () => {+ const { dir, drafts, exports } = threePages('Crashing drawer');+ const job = exports.start(project('Crashing drawer', dir), { kind: 'nle', format: 'jianying', fps: 30, subtitles: false });+ await until(exports, job.id, (j) => ['done', 'failed', 'cancelled'].includes(j.status) || (j.framesDone ?? 0) > 20);+ const [worker] = children(process.pid, 'render-worker');+ assert.ok(worker);+ process.kill(worker, 'SIGKILL');+ const failed = await ended(exports, job);+ assert.equal(failed.status, 'failed');+ assert.match(failed.error, /renderer stopped/);+ assert.deepEqual(readdirSync(drafts), [], 'no draft, and no part of one');+});++test('剪映: a draft cancelled partway stops drawing at once, its browsers closed, nothing left', { ...ffmpeg, timeout: 240_000, skip: ffmpeg.skip || (process.platform === 'win32' && 'no pgrep') }, async () => {+ const { dir, drafts, exports } = threePages('Cancelled draft');+ const job = exports.start(project('Cancelled draft', dir), { kind: 'nle', format: 'jianying', fps: 30, subtitles: false });+ await until(exports, job.id, (j) => ['done', 'failed', 'cancelled'].includes(j.status) || (j.framesDone ?? 0) > 20);+ const [worker] = children(process.pid, 'render-worker');+ assert.ok(worker);+ const asked = Date.now();+ exports.cancel(job.id);+ const done = await ended(exports, job);+ assert.equal(done.status, 'cancelled');+ assert.ok(Date.now() - asked < 8000, `stopped in ${Date.now() - asked} ms`);+ assert.deepEqual(readdirSync(drafts), [], 'no draft, and no part of one');+ for (let i = 0; i < 100 && children(process.pid, 'render-worker').length; i++) await new Promise((r) => setTimeout(r, 50));+ assert.deepEqual(children(process.pid, 'render-worker'), [], 'its process and browsers gone');+});++test('an editing project\'s pages share the browsers by their frames', () => {+ /* ten pages of 150 frames, three browsers: one each, three pages at once */+ assert.equal(shareOf(3, 150, 1500, 2), 1);+ /* the last page alone: as many as it would get on its own */+ assert.equal(shareOf(3, 150, 150, 2), 2);+ /* a 900-frame page among four of 150: two of three, the short ones share the third */+ assert.equal(shareOf(3, 900, 1500, 3), 2);+ assert.equal(shareOf(1, 150, 600, 2), 1);+ /* never more than are free */+ assert.equal(shareOf(1, 900, 900, 3), 1);+});++test('an export is written under a name of its own until it is whole, then renamed: none is left behind', ffmpeg, async () => {+ const exports = createExports({ captions: subtitles });+ for (const ask of [{ kind: 'video', name: 'Whole' }, { kind: 'gif', name: 'Whole' }, { kind: 'audio', name: 'Whole', stems: true }, { kind: 'stills', name: 'Whole' },+ { kind: 'slides', name: 'Whole' }, { kind: 'project', name: 'Whole' }]) {+ const done = await ended(exports, exports.start(project(), ask));+ assert.equal(done.status, 'done', `${ask.kind}: ${done.error}`);+ assert.ok(done.outputs.length && done.outputs.every((f) => existsSync(f) && basename(f).startsWith('Whole')), `${ask.kind}: ${done.outputs.join(', ')}`);+ }+ assert.deepEqual(readdirSync(exportsRoot()).filter((n) => n.includes('.partial-')), []);+});++test('a PNG video keeps each picture once, for as long as it shows: a pixel apart is not a repeat', ffmpeg, async () => {+ const dir = mkdtempSync(join(tmpdir(), 'of-keep-'));+ try {+ const file = join(dir, 'page.mov');+ /* 30 clear frames; in frames 10 to 19 one pixel is a step off clear (red and alpha 1 of 255) */+ execFileSync('ffmpeg', ['-v', 'error', '-f', 'lavfi', '-i', 'color=c=black@0:s=16x16:r=30:d=1,format=rgba,drawbox=x=3:y=3:w=1:h=1:color=0x01000001:t=fill:replace=1:enable=between(n\\,10\\,19)', '-c:v', 'png', file]);+ const decoded = (f) => execFileSync('ffmpeg', ['-v', 'error', '-i', f, '-vf', 'format=rgba', '-fps_mode', 'cfr', '-r', '30', '-f', 'framemd5', '-']).toString().split('\n').filter((l) => l && !l.startsWith('#')).map((l) => l.split(',').pop());+ const before = decoded(file);+ await keepChanges(file, new AbortController().signal);+ assert.equal(frameCount(file), 4, 'the first, the step, the step back, the last');+ assert.deepEqual(decoded(file), before, 'every frame the same pixels at the same time');+ /* a picture that changes every third frame: far more pictures kept than ffmpeg takes in one sum */+ const busy = join(dir, 'busy.mov');+ execFileSync('ffmpeg', ['-v', 'error', '-f', 'lavfi', '-i', 'testsrc2=s=32x32:r=10:d=40,fps=30,format=rgba', '-c:v', 'png', busy]);+ const busyBefore = decoded(busy);+ await keepChanges(busy, new AbortController().signal);+ const pictures = busyBefore.filter((sum, i) => i === 0 || i === busyBefore.length - 1 || sum !== busyBefore[i - 1]).length;+ assert.ok(pictures > 300, `${pictures} pictures`);+ assert.equal(frameCount(busy), pictures, 'each picture once, and the last frame');+ assert.deepEqual(decoded(busy), busyBefore);+ /* a long page that steps every few frames: more runs than a command line holds on Windows, the sum read from a file */+ const steps = join(dir, 'steps.mov');+ execFileSync('ffmpeg', ['-v', 'error', '-f', 'lavfi', '-i', 'testsrc2=s=32x32:r=10:d=240,fps=30,format=rgba', '-c:v', 'png', steps]);+ const stepsBefore = decoded(steps);+ const stepPictures = stepsBefore.filter((sum, i) => i === 0 || i === stepsBefore.length - 1 || sum !== stepsBefore[i - 1]).length;+ assert.ok(stepPictures > 2000, `${stepPictures} pictures`);+ await keepChanges(steps, new AbortController().signal);+ assert.equal(frameCount(steps), stepPictures);+ assert.deepEqual(decoded(steps), stepsBefore);+ assert.deepEqual(readdirSync(dir).filter((n) => n.endsWith('.filter')), [], 'its sum file gone');+ /* drawn as a quick PNG and changing in every frame: every frame kept, written again at ffmpeg's own settings */+ const quick = join(dir, 'quick.mov');+ const own = join(dir, 'own.mov');+ const moving = ['-v', 'error', '-f', 'lavfi', '-i', 'testsrc2=s=64x36:r=30:d=1,format=rgba'];+ execFileSync('ffmpeg', [...moving, '-c:v', 'png', '-compression_level', '1', '-pred', 'sub', quick]);+ execFileSync('ffmpeg', [...moving, '-c:v', 'png', own]);+ const quickBefore = decoded(quick);+ await keepChanges(quick, new AbortController().signal);+ assert.equal(frameCount(quick), 30);+ assert.deepEqual(decoded(quick), quickBefore, 'the same pixels at the same time');+ const packets = (f) => execFileSync('ffmpeg', ['-v', 'error', '-i', f, '-map', '0:v:0', '-c', 'copy', '-f', 'framemd5', '-']).toString().split('\n').filter((l) => l && !l.startsWith('#')).map((l) => l.split(',').pop());+ assert.deepEqual(packets(quick), packets(own), 'written as ffmpeg writes a PNG');+ } finally {+ rmSync(dir, { recursive: true, force: true });+ }+});+test('burned-in subtitles: drawn over the frame, bars included; a subtitle file beside the video', ffmpeg, async () => {/* the pixels of the bottom tenth of a picture that are near white (the title's own text is at the top) */const bright = (file) => {@@ -860,6 +1159,31 @@ test('burned-in subtitles: drawn over the frame, bars included; a subtitle fileassert.deepEqual(readdirSync(join(root, '.film', 'cache', 'export')), [], 'the page drawing them is gone');});+test('burned-in bilingual subtitles: the original and its translation, each once', ffmpeg, async () => {+ /* how much of the bottom half of a picture (the title's own text is at the top) is near-white: the subtitles' text */+ const lit = (file) => {+ const [w, h] = probe(file, 'stream=width,height').split(',').map(Number);+ const rgb = execFileSync('ffmpeg', ['-v', 'error', '-i', file, '-frames:v', '1', '-f', 'rawvideo', '-pix_fmt', 'rgb24', '-']);+ let n = 0;+ for (let i = Math.floor(h / 2) * w; i < w * h; i++) if (Math.min(rgb[i * 3], rgb[i * 3 + 1], rgb[i * 3 + 2]) > 200) n++;+ return n;+ };+ const cues = [{ startMs: 0, durMs: 900, text: 'Hello there', alt: { fr: 'Bonjour' } }];+ /* the original alone, the translation alone, then both, each in the same type: the two lines together are as much+ text as the two alone, however a font draws them; the translation twice would be a whole translation more */+ const burned = async (style, name) => {+ const exports = createExports({ captions: async () => ({ cues, style: { ...parseFilmSubtitleStyle(null), ...style } }) });+ const done = await ended(exports, exports.start(project(name), { kind: 'poster', at: 0.25, shortEdge: 360, burnSubtitles: true }));+ assert.equal(done.status, 'done', done.error);+ return lit(done.out);+ };+ const original = await burned({}, 'Original');+ const translation = await burned({ language: 'fr', bilingual: false }, 'Translation');+ const both = await burned({ language: 'fr', bilingual: true }, 'Bilingual');+ assert.ok(original > 0 && translation > 0);+ assert.ok(Math.abs(both - (original + translation)) < translation / 2, `both ${both}, original ${original}, translation ${translation}`);+});+test('a zip of many files, readable', async () => {const dir = mkdtempSync(join(tmpdir(), 'of-zip-'));const files = Array.from({ length: 3 }, (_, i) => { const file = join(dir, `${i}.txt`); writeFileSync(file, `file ${i} ünïcode`); return { name: `frames/${i}.txt`, file }; });diff --git a/packages/openfilm/studio/server/jianying.mjs b/packages/openfilm/studio/server/jianying.mjsindex 324dfff..20f9e58 100644--- a/packages/openfilm/studio/server/jianying.mjs+++ b/packages/openfilm/studio/server/jianying.mjs@@ -25,6 +25,7 @@* lists none of them and finds a video by nothing but its path.*/import { randomUUID } from 'node:crypto';+import { readFileSync } from 'node:fs';import { join } from 'node:path';/**@@ -40,15 +41,22 @@ import { join } from 'node:path';const APP_FOLDER = /** @type {Record<DraftApp, string>} */ ({ jianying: 'JianyingPro', capcut: 'CapCut' });/**- * Where 剪映 or CapCut keeps its drafts by default (its Settings › Drafts location can move them): the folder when it- * is there, else null.+ * Where 剪映 or CapCut keeps its drafts: the folder its Settings › Drafts location names (`currentCustomDraftPath` in+ * its `User Data/Config/globalSetting`, the same in both apps), else its default one, when that folder is there; else+ * null. A draft made anywhere else is not on the app's home screen.* @param {DraftApp} app- * @param {{ platform?: NodeJS.Platform, home: string, localAppData?: string, exists: (path: string) => boolean }} where+ * @param {{ platform?: NodeJS.Platform, home: string, localAppData?: string, exists: (path: string) => boolean, read?: (path: string) => string }} where*/-export function draftsRoot(app, { platform = process.platform, home, localAppData, exists }) {+export function draftsRoot(app, { platform = process.platform, home, localAppData, exists, read = (path) => readFileSync(path, 'utf8') }) {const base = platform === 'darwin' ? join(home, 'Movies') : platform === 'win32' ? localAppData ?? join(home, 'AppData', 'Local') : null;- const dir = base && join(base, APP_FOLDER[app], 'User Data', 'Projects', 'com.lveditor.draft');- return dir && exists(dir) ? dir : null;+ if (!base) return null;+ const user = join(base, APP_FOLDER[app], 'User Data');+ let chosen = null;+ try {+ chosen = /^currentCustomDraftPath=(.+)$/m.exec(read(join(user, 'Config', 'globalSetting')))?.[1].trim() || null;+ } catch { /* the app has saved no settings yet */ }+ for (const dir of [chosen, join(user, 'Projects', 'com.lveditor.draft')]) if (dir && exists(dir)) return dir;+ return null;}/** 剪映 ids: 32 hex digits for materials and clips, an upper-case UUID for the draft. */diff --git a/packages/openfilm/studio/server/jianying.test.mjs b/packages/openfilm/studio/server/jianying.test.mjsindex c8270f3..4b53595 100644--- a/packages/openfilm/studio/server/jianying.test.mjs+++ b/packages/openfilm/studio/server/jianying.test.mjs@@ -96,6 +96,12 @@ test('the drafts folders: 剪映\'s and CapCut\'s, on a Mac and on Windows, whenconst win = join('D:\\local-app-data', 'JianyingPro', 'User Data', 'Projects', 'com.lveditor.draft');assert.equal(draftsRoot('jianying', { platform: 'win32', home: 'D:\\win-home', localAppData: 'D:\\local-app-data', exists: (p) => p === win }), win);assert.equal(draftsRoot('jianying', { platform: 'linux', home: '/linux-home', exists: () => true }), null);+ /* moved in the app's Settings › Drafts location: that folder, the default one no longer looked in */+ const settings = join('/mac-home', 'Movies', 'JianyingPro', 'User Data', 'Config', 'globalSetting');+ const read = (p) => { if (p !== settings) throw new Error('ENOENT'); return 'currentCachePath=/x\r\ncurrentCustomDraftPath=/Volumes/Work/剪映草稿\r\nother=1\r\n'; };+ assert.equal(draftsRoot('jianying', { platform: 'darwin', home: '/mac-home', read, exists: (p) => p === '/Volumes/Work/剪映草稿' || p === mac('JianyingPro') }), '/Volumes/Work/剪映草稿');+ /* a folder it names that is gone (a drive not plugged in): the default one, if it is there */+ assert.equal(draftsRoot('jianying', { platform: 'darwin', home: '/mac-home', read, exists: (p) => p === mac('JianyingPro') }), mac('JianyingPro'));});test('a draft says which app wrote it: 剪映 or CapCut', () => {@@ -116,7 +122,7 @@ test('each file is in the draft\'s media library, once, and each material namesassert.ok(entry, `${m.path} is in the library`);assert.equal(entry.file_Path, m.path);}- const kinds = Object.fromEntries(library.map((e) => [e.file_Path.split('/').pop(), e.metetype]));+ const kinds = Object.fromEntries(library.map((e) => [e.file_Path.split(/[\\/]/).pop(), e.metetype]));assert.equal(kinds['bed fade.wav'], 'music', 'the bed, its fade its own part');assert.equal(kinds['b.png'], 'photo');assert.equal(Object.values(kinds).filter((k) => k === 'video').length, 2, 'the page rendered and the footage');diff --git a/packages/openfilm/studio/server/nle.mjs b/packages/openfilm/studio/server/nle.mjsindex 91589e1..860946d 100644--- a/packages/openfilm/studio/server/nle.mjs+++ b/packages/openfilm/studio/server/nle.mjs@@ -10,7 +10,8 @@* number for the cut, so there is never a one-frame gap.* · Tracks reversed: film.html's first track is on top; xmeml's V1 is at the bottom. Clips overlapping on one track* are spread over several (an editor's track shows one clip a frame).- * · A web page (a motion graphic) is always rendered: a full-frame ProRes 4444 with alpha, its placement in the+ * · A web page (a motion graphic) is always rendered: a full-frame MOV with alpha (`alpha`: ProRes 4444 or PNG;+ * footage with alpha that has to be converted is written the same way), its placement in the* picture. Footage and stills come as they are, placed with Basic Motion (uniform scale and a move); what Basic* Motion can't say (a rotation, a speed change, an unknown size) is rendered too: rather less to adjust than a* picture in the wrong place.@@ -22,6 +23,7 @@* from the file's start, which is the clip's, so editors that count keyframes from either agree).*/import { spawn } from 'node:child_process';+import { join } from 'node:path';import { pathToFileURL } from 'node:url';import { clipKind, pictureRect, soundRole } from '../../src/film-doc.mjs';import { exactRate } from '../../src/rate.mjs';@@ -43,13 +45,14 @@ import { exactRate } from '../../src/rate.mjs';* the sound of; `fade`: its fades in and out, film seconds (fitted to its length)* @typedef {{ stage: { w: number, h: number }, duration: number, pictures: NlePicture[], sounds: NleSound[] }} NleFilm* @typedef {{ title: string, fps: number, audio: 'clips' | 'stems', media: 'copy' | 'link', mediaDir: string,- * stems?: SoundRole[], audioTracks?: 'roles' | 'source' }} NlePlanOptions+ * stems?: SoundRole[], audioTracks?: 'roles' | 'source', alpha?: NleAlpha }} NlePlanOptions+ * @typedef {'png' | 'prores4444'} NleAlpha How a picture with alpha is written, in a MOV (default ProRes 4444)* @typedef {{ timebase: number, ntsc: boolean, fps: number }} NleRate* @typedef {{ kind: 'render', clip: string, frames: number }* | { kind: 'copy', from: string }* | { kind: 'link', from: string }* | { kind: 'raster', from: string, src: string, w: number, h: number }- * | { kind: 'transcode', from: string, to: 'prores422hq' | 'prores4444' | 'wav', segment?: { from: number, to: number, speed: number, seconds: number } }+ * | { kind: 'transcode', from: string, to: 'prores422hq' | NleAlpha | 'wav', segment?: { from: number, to: number, speed: number, seconds: number } }* | { kind: 'stem', role: SoundRole }} NleMediaJob* How a file is made: a clip rendered, a source cloned or pointed at, a still drawn to PNG, a source converted* (`segment`: only those source seconds, at that speed, `seconds` long), a mixed stem@@ -183,7 +186,8 @@ export function planNleTimeline(film, opts) {let name = `${safeStem(stem)}.${ext}`;for (let n = 2; names.has(name.toLowerCase()); n++) name = `${safeStem(stem)} (${n}).${ext}`;names.add(name.toLowerCase());- return { name, path: `${opts.mediaDir.replace(/\/$/, '')}/${name}` };+ /* the system's own separators: a draft on Windows names its files with backslashes, as 剪映 does */+ return { name, path: join(opts.mediaDir, name) };};const addFile = (/** @type {Omit<NleFile, 'id'>} */ f) => { const file = { id: `file-${++fileSeq}`, ...f }; files.push(file); return file; };const clipId = () => `clipitem-${++clipSeq}`;@@ -217,7 +221,7 @@ export function planNleTimeline(film, opts) {const video = want === 'video' ? facts.hasVideo !== false : facts.hasVideo === true;const audio = facts.hasAudio ? { channels: facts.channels ?? 2, sampleRate: facts.sampleRate ?? 48_000 } : undefined;let made;- if (video && VIDEO_CONVERT.has(ext)) made = { ...mediaPath(stem, 'mov'), job: /** @type {NleMediaJob} */ ({ kind: 'transcode', from: s.path, to: facts.alpha ? 'prores4444' : 'prores422hq' }) };+ if (video && VIDEO_CONVERT.has(ext)) made = { ...mediaPath(stem, 'mov'), job: /** @type {NleMediaJob} */ ({ kind: 'transcode', from: s.path, to: facts.alpha ? opts.alpha ?? 'prores4444' : 'prores422hq' }) };else if (!video && !AUDIO_COPY.has(ext)) made = { ...mediaPath(stem, 'wav'), job: /** @type {NleMediaJob} */ ({ kind: 'transcode', from: s.path, to: 'wav' }) };else made = original();const converted = made.job.kind === 'transcode';diff --git a/packages/openfilm/studio/server/render-worker.mjs b/packages/openfilm/studio/server/render-worker.mjsindex a949288..196b0e1 100644--- a/packages/openfilm/studio/server/render-worker.mjs+++ b/packages/openfilm/studio/server/render-worker.mjs@@ -1,29 +1,79 @@// @ts-check/**- * A film rendered in a process of its own (exports.mjs `renderApart`): its browsers and encoders are this process's,- * so whatever breaks in them (a browser's pipe, a crash, memory running out) ends this render and says why, and Studio- * keeps serving. Told `{ args, opts }` once; says `progress`, then `done` (render's result) or `failed`; `cancel`- * stops it.+ * Films rendered in a process of their own (exports.mjs `render` and `drawer`): its browsers and encoders are this+ * process's, so whatever breaks in them (a browser's pipe, a crash, memory running out) ends its renders and says why,+ * and Studio keeps serving.+ *+ * Told `{ args, opts }` once: one render, then it exits; says `progress`, then `done` (render's result) or `failed`;+ * `cancel` stops it.+ *+ * Started with `--pool` (an editing project's pages): told `{ type: 'render', id, args, opts }` for each render, at+ * any time, `{ type: 'cancel', id }` to stop one and `{ type: 'close' }` when no more come; says `progress`, `done`+ * and `failed` with the render's id. Its renders share one BrowserPool, so a browser is started once for many pages;+ * a render whose browser crashed or stopped answering is drawn once more in new browsers.*/import { render } from '../../src/render.mjs';+import { BrowserPool, browserFailed } from '../../src/browser-pool.mjs';/* Studio gone (it stopped, or was ended): nothing will take this render, and its browsers and encoders end with it */process.on('disconnect', () => process.exit(1));-process.once('message', async (/** @type {{ args: string[], opts: Record<string, unknown> }} */ { args, opts }) => {- const abort = new AbortController();- process.on('message', (/** @type {{ type?: string }} */ m) => { if (m?.type === 'cancel') abort.abort(); });- /** @param {unknown} message */- const say = (message) => new Promise((done) => { process.send?.(message, () => done(undefined)); });- try {- const result = await render(args, {- ...opts, signal: abort.signal,- onProgress: (/** @type {number} */ done, /** @type {number} */ total) => { process.send?.({ type: 'progress', done, total }); },- });- await say({ type: 'done', result });- process.exit(0);- } catch (e) {- await say({ type: 'failed', error: e instanceof Error ? e.message : String(e) });- process.exit(1);- }-});+/** @param {unknown} message */+const say = (message) => new Promise((done) => { process.send?.(message, () => done(undefined)); });++if (process.argv.includes('--pool')) {+ const pool = new BrowserPool();+ /** @type {Map<string, { abort: AbortController, drawn: Promise<void> }>} */+ const running = new Map();+ /** @param {string} id @param {string[]} args @param {Record<string, unknown>} opts @param {AbortSignal} signal */+ const draw = async (id, args, opts, signal) => {+ try {+ for (let tries = 1; ; tries++) {+ try {+ const result = await render(args, {+ ...opts, signal, browsers: pool,+ onProgress: (/** @type {number} */ done, /** @type {number} */ total) => { process.send?.({ type: 'progress', id, done, total }); },+ });+ await say({ type: 'done', id, result });+ return;+ } catch (e) {+ if (tries > 1 || signal.aborted || !browserFailed(e)) throw e;+ process.stderr.write(`openfilm: ${e instanceof Error ? e.message.split('\n')[0] : e}: drawn again in new browsers\n`);+ }+ }+ } catch (e) {+ await say({ type: 'failed', id, error: e instanceof Error ? e.message : String(e) });+ }+ };+ process.on('message', async (/** @type {{ type?: string, id: string, args: string[], opts: Record<string, unknown> }} */ m) => {+ if (m?.type === 'render') {+ const job = { abort: new AbortController(), drawn: Promise.resolve() };+ running.set(m.id, job);+ job.drawn = draw(m.id, m.args, m.opts, job.abort.signal).finally(() => running.delete(m.id));+ } else if (m?.type === 'cancel') {+ running.get(m.id)?.abort.abort();+ } else if (m?.type === 'close') {+ /* what is still drawing stops (its encoders and parts go with it), then every browser closes */+ for (const job of running.values()) job.abort.abort();+ await Promise.race([Promise.allSettled([...running.values()].map((j) => j.drawn)), new Promise((r) => setTimeout(r, 4000))]);+ await pool.close();+ process.exit(0);+ }+ });+} else {+ process.once('message', async (/** @type {{ args: string[], opts: Record<string, unknown> }} */ { args, opts }) => {+ const abort = new AbortController();+ process.on('message', (/** @type {{ type?: string }} */ m) => { if (m?.type === 'cancel') abort.abort(); });+ try {+ const result = await render(args, {+ ...opts, signal: abort.signal,+ onProgress: (/** @type {number} */ done, /** @type {number} */ total) => { process.send?.({ type: 'progress', done, total }); },+ });+ await say({ type: 'done', result });+ process.exit(0);+ } catch (e) {+ await say({ type: 'failed', error: e instanceof Error ? e.message : String(e) });+ process.exit(1);+ }+ });+}diff --git a/packages/openfilm/studio/server/server.mjs b/packages/openfilm/studio/server/server.mjsindex c4a693f..5485fa6 100644--- a/packages/openfilm/studio/server/server.mjs+++ b/packages/openfilm/studio/server/server.mjs@@ -1037,7 +1037,8 @@ export async function startStudio({ port = 4747, key, openBrowser = systemBrowseasync function shutDown() {/* the file is ours unless a later Studio has written its own (with the same key: it is the machine's) */if (advertise && advertised()?.pid === process.pid) rmSync(runFile(), { force: true });- exports.stopAll();+ /* a running export stops, and cleans up after itself while the rest closes: its half-made files go before Studio */+ const exporting = exports.stopAll();fetches.stopAll();await closePageFrames();watchers.closeAll();@@ -1045,6 +1046,7 @@ export async function startStudio({ port = 4747, key, openBrowser = systemBrowseeditor.closeAllConnections();for (const socket of streamSockets) socket.destroy();await new Promise((done) => editor.close(() => done(undefined)));+ await exporting;}return studio;}diff --git a/packages/openfilm/studio/server/stage.js b/packages/openfilm/studio/server/stage.jsindex 916be1e..5ede781 100644--- a/packages/openfilm/studio/server/stage.js+++ b/packages/openfilm/studio/server/stage.js@@ -529,8 +529,21 @@const value = styleValue(el, key, cs);if (value != null && value !== '') style[key] = value;}+ /* words drawn in a fill of their page's own (-webkit-text-fill-color: gold or gradient words), not in their color */+ if (kind === 'text' && !isSvgText(el) && ownTextFill(el)) style.WebkitTextFillColor = styleValue(el, 'WebkitTextFillColor', cs);return style;};+ /**+ * Whether `el`'s words are drawn in a fill of their own rather than their color. A fill that follows the color+ * reads as that color, so the color is changed for a moment (nothing is drawn in between) to see if it follows.+ */+ const ownTextFill = (el) => {+ const was = el.getAttribute('style');+ el.style.setProperty('color', 'rgb(1, 2, 3)', 'important');+ const fill = css(el).webkitTextFillColor;+ if (was == null) el.removeAttribute('style'); else el.setAttribute('style', was);+ return fill !== 'rgb(1, 2, 3)';+ };const labelOf = (el, kind) => {if (kind === 'text') {@@ -747,7 +760,7 @@if (blocks) holdBlocks(c, blocks, g0, paintedOf(el));};/* what a block keeps when the line it is written in changes: its look (the type it takes from the line) and its place */- const INHERITED = ['color', 'fontFamily', 'fontSize', 'fontWeight', 'fontStyle', 'letterSpacing', 'lineHeight', 'wordSpacing', 'textTransform', 'textAlign', 'textShadow', 'fontVariant'];+ const INHERITED = ['color', 'WebkitTextFillColor', 'fontFamily', 'fontSize', 'fontWeight', 'fontStyle', 'letterSpacing', 'lineHeight', 'wordSpacing', 'textTransform', 'textAlign', 'textShadow', 'fontVariant'];const blockNow = (b) => {const cs = css(b);const turn = totalTurn(b);diff --git a/packages/openfilm/studio/server/stage.test.mjs b/packages/openfilm/studio/server/stage.test.mjsindex 8ca3cb3..57417c6 100644--- a/packages/openfilm/studio/server/stage.test.mjs+++ b/packages/openfilm/studio/server/stage.test.mjs@@ -238,6 +238,37 @@ test('a title with its subtitle inside it: its box hugs its own words, and moved}});+test('words drawn in a fill of their page\'s own (gold words) say so; recolored with it taken over, they are drawn in the new color and the block inside keeps its own', { timeout: 60_000 }, async () => {+ const dir = mkdtempSync(join(tmpdir(), 'of-stage-text-fill-'));+ writeFileSync(join(dir, 'title.html'), `<!doctype html><head><style>.gold{color:#b39458!important;-webkit-text-fill-color:#b39458}</style></head><body style="margin:0">+<h1 id="head" class="gold" style="position:absolute;left:20px;top:20px;margin:0;font:700 40px sans-serif">Only you<div id="sub" style="font-size:14px">for yourself</div></h1>+<p id="plain" style="position:absolute;left:20px;top:120px;margin:0;font:20px sans-serif;color:#fff">Plain</p>+<script>window.film={duration:2,frame(){}}</script></body>`);+ writeFileSync(join(dir, 'film.html'), filmHtml({ stage: { w: 320, h: 180 }, tracks: [{ clips: [{ id: 'title', src: 'title.html' }] }] }));+ const stage = await stageOf(dir);+ try {+ const page = stage.page.frames().find((f) => f.url().endsWith('title.html'));+ const drawn = (id) => page.evaluate((id) => getComputedStyle(document.getElementById(id)).webkitTextFillColor, id);+ const head = await stage.ask('hit', { x: 40, y: 40, detail: true });+ assert.equal(head.style.color, '#b39458');+ assert.equal(head.style.WebkitTextFillColor, '#b39458', 'its fill is told, though it is the color it has now');+ const plain = await stage.ask('hit', { x: 30, y: 130, detail: true });+ assert.equal(plain.style.WebkitTextFillColor, undefined, 'words drawn in their color have no fill of their own');+ assert.equal(await page.evaluate(() => document.getElementById('plain').getAttribute('style')), 'position:absolute;left:20px;top:120px;margin:0;font:20px sans-serif;color:#fff', 'looked at, its style is as it was');++ const subWas = await drawn('sub');+ const patch = { style: { color: '#ff0000', WebkitTextFillColor: 'currentcolor' } };+ const riders = await stage.ask('riders', { clip: 'title', at: '#head', patch });+ assert.deepEqual(riders.map((r) => r.at), ['#sub']);+ assert.equal(riders[0].patch.style?.WebkitTextFillColor, subWas, 'the block inside keeps the fill it was drawn in');+ assert.equal(await stage.ask('tweak', { clip: 'title', at: '#head', patch }), true);+ assert.equal(await drawn('head'), 'rgb(255, 0, 0)', 'drawn in the new color');+ assert.equal((await stage.ask('hit', { x: 40, y: 40, detail: true })).style.WebkitTextFillColor, undefined, 'taken over, its fill follows its color');+ } finally {+ await stage.close();+ }+});+test('a layer restyled leaves the others where they are, and takes its new size: the words beside a number stay put', { timeout: 60_000 }, async () => {const dir = mkdtempSync(join(tmpdir(), 'of-stage-hold-'));writeFileSync(join(dir, 'row.html'), `<!doctype html><body style="margin:0">diff --git a/packages/openfilm/studio/ui/src/components/DesktopDialog.tsx b/packages/openfilm/studio/ui/src/components/DesktopDialog.tsxindex a04aaf4..267da4a 100644--- a/packages/openfilm/studio/ui/src/components/DesktopDialog.tsx+++ b/packages/openfilm/studio/ui/src/components/DesktopDialog.tsx@@ -37,6 +37,7 @@ const AGENTS: { id: string; name: string }[] = [{ id: 'qwen', name: 'Qwen Code' },{ id: 'kimi', name: 'Kimi' },{ id: 'minimax', name: 'MiniMax Code' },+ { id: 'deepseek', name: 'DeepSeek Harness' },];const SYSTEM_NAMES = { mac: 'macOS', win: 'Windows' } as const;diff --git a/packages/openfilm/studio/ui/src/components/FilmInspector.tsx b/packages/openfilm/studio/ui/src/components/FilmInspector.tsxindex 9a1b7a4..0f24b6a 100644--- a/packages/openfilm/studio/ui/src/components/FilmInspector.tsx+++ b/packages/openfilm/studio/ui/src/components/FilmInspector.tsx@@ -37,6 +37,7 @@ import { plainClipRefusal, type ClipEdit } from '@/lib/film-clip-edits';import { clipFieldPart, clipFields, compactBox, transformOf } from '@/lib/stage-clip';import type { TimelineBlock } from '@/lib/timeline-layout';import { editsForMoves } from '@/lib/timeline-drag';+import { dbToVolume, VOLUME_DB_MAX, VOLUME_DB_MIN, volumeToDb } from '@/lib/loudness';import { useTimelinePrefs } from '@/lib/timeline-prefs';import { formatTimecode, FRAME_RATES, timecodeFps, useTimecodeFps } from '@/lib/timecode';import { MenuSelect } from './MenuSelect';@@ -54,8 +55,9 @@ import {import {AdjustSection, AdvancedSection, AppearanceSection, CropSection, EffectsSection, FillSection, MaskSection, NEUTRAL, StrokeSection,TypographySection, camelOf,- isNeutral, kebabOf, makeLook, rawDeclarations, type Look, type LookRead, type LookSet,+ isNeutral, kebabOf, rawDeclarations,} from './inspector-look';+import { makeLook, type Look, type LookRead, type LookSet } from '@/lib/look';import type { MaskSession } from '@/lib/stage-mask';import type { MaskShape } from '@/lib/clip-mask';import type { FadeMs } from '@/lib/clip-fade';@@ -1414,7 +1416,7 @@ function linearVolumeOf(b: TimelineBlock): number {function volumeEdits(blocks: readonly TimelineBlock[], apply: FieldApply): ClipEdit[] {return blocks.flatMap((b) => {if (!b.loc || !blockHasVolume(b)) return [];- const v = round3(Math.max(0, Math.min(2, apply(linearVolumeOf(b) * 100) / 100)));+ const v = dbToVolume(apply(volumeToDb(linearVolumeOf(b))));if (v === round3(linearVolumeOf(b))) return [];if (parseFilmDocLoc(b.loc) != null) return [{ loc: b.loc, prop: 'volume', value: v }];return [{ loc: b.loc, prop: 'gainDb', value: filmVolumeToGainDb(v) ?? null }];@@ -1919,16 +1921,18 @@ function SourceBar({ t, block, timecode, onSlip, onCommit, maxDurSec }: {);}-/** Volume: a percentage (100% = as is) on a slider — adjusted by ear, so dragging beats typing. */+/** Volume: dB (0 = as is) on a slider, as the ear hears it; adjusted by ear, so dragging beats typing. */function VolumeField({ value, onCommit, t }: { value: number | Mixed; onCommit: (apply: FieldApply) => void; t: T }) {return (<SliderFieldlabel={t('inspector.gainDb')}- value={value === MIXED ? MIXED : Math.round(value * 100)}- min={0}- max={200}- neutral={100}- unit="%"+ value={value === MIXED ? MIXED : volumeToDb(value)}+ min={VOLUME_DB_MIN}+ max={VOLUME_DB_MAX}+ neutral={0}+ step={0.1}+ unit="dB"+ fieldWidth={72}onCommit={(_, apply) => onCommit(apply)}/>);diff --git a/packages/openfilm/studio/ui/src/components/PlaybackMenu.tsx b/packages/openfilm/studio/ui/src/components/PlaybackMenu.tsxindex 97e5756..2ceb6c1 100644--- a/packages/openfilm/studio/ui/src/components/PlaybackMenu.tsx+++ b/packages/openfilm/studio/ui/src/components/PlaybackMenu.tsx@@ -1,6 +1,6 @@import React from 'react';import { createPortal } from 'react-dom';-import { Captions, Gauge, Repeat } from 'lucide-react';+import { Gauge, Repeat } from 'lucide-react';import { useT } from '@/i18n';import { useKeyHint } from '@/lib/keymap-store';import { Tooltip } from './Tooltip';@@ -15,12 +15,10 @@ export type PlaybackSettings = {onLoop: () => void;speed: number;onSpeed: (speed: number) => void;- /** Subtitles on the picture, when the film has any. */- captions?: { on: boolean; onToggle: () => void } | null;};/**- * The viewer's playback settings: loop, subtitles on the picture, and the speed. What is not the default shows on the+ * The viewer's playback settings: loop and the speed (subtitles are the timeline's CC button). What is not the default shows on the* button itself (the loop icon, "1.5×"), so it is seen without opening the menu. Nothing here takes focus on click:* the next Space still plays.*/@@ -58,7 +56,7 @@ export function PlaybackMenu({ className, disabled, settings }: {};}, [anchor]);- const { loop, ranged, onLoop, speed, onSpeed, captions } = settings;+ const { loop, ranged, onLoop, speed, onSpeed } = settings;const loopLabel = t(ranged ? 'viewer.loopRange' : 'viewer.loopFilm');const speedText = (s: number) => `${s}×`;const keep = (e: React.MouseEvent) => e.preventDefault();@@ -95,7 +93,6 @@ export function PlaybackMenu({ className, disabled, settings }: {style={anchor} className="fixed z-[110] w-56 rounded-[14px] border border-[var(--border)] bg-[var(--surface)] p-1.5 shadow-[0_18px_48px_-12px_rgba(0,0,0,0.45),0_2px_8px_rgba(0,0,0,0.12)]"><div className="px-2 pb-1 pt-1 text-[11px] font-medium text-[var(--text-faint)]">{t('viewer.playback')}</div>{toggle('loop', loopLabel, loop, onLoop, Repeat, keyHint('loop'))}- {captions ? toggle('captions', t('viewer.subtitles'), captions.on, captions.onToggle, Captions) : null}<div className="mx-1 my-1 h-px bg-[var(--border)]" /><div className="px-2 pb-1.5 pt-1 text-[11px] font-medium text-[var(--text-faint)]">{t('viewer.speed')}</div><div role="radiogroup" aria-label={t('viewer.speed')} className="grid grid-cols-4 gap-1 px-1 pb-1">diff --git a/packages/openfilm/studio/ui/src/components/ProjectView.tsx b/packages/openfilm/studio/ui/src/components/ProjectView.tsxindex e91ddab..9886457 100644--- a/packages/openfilm/studio/ui/src/components/ProjectView.tsx+++ b/packages/openfilm/studio/ui/src/components/ProjectView.tsx@@ -63,7 +63,7 @@ import { SubtitleOverlay } from './SubtitleOverlay';import { SubtitleInspector, subtitleLineKey, type SubtitleLineAt, type SubtitleLineRefer } from './SubtitleInspector';import { useFilmSubtitles } from '@/lib/use-film-subtitles';import { speakerAt, useSubtitleEdits } from '@/lib/use-subtitle-edits';-import { cueAt, filmSubtitleStyleFor, filmSubtitleWords, sameFilmSubtitleLanguage, type SubtitleCue } from '@/lib/subtitles';+import { cueAt, filmSubtitleStyleFor, filmSubtitleWords, sameFilmSubtitleLanguage } from '@/lib/subtitles';import { DEFAULT_INSPECTOR, DOCK_GAP, DockPaneDivider, MIN_DOCK_H, MIN_INSPECTOR, MIN_STAGE_H, MIN_VIEWER, PaneDivider, usePaneSize } from './PaneDivider';import { PANE_BAR, PANE_BTN_ON, PANE_TOGGLE, PANE_TOGGLE_ICON } from './dock-pane-bar';import { Tooltip } from './Tooltip';@@ -190,9 +190,6 @@ function clipIdAt(film: Film | null, loc: string): string | null {return at ? film?.tracks[at.track]?.clips[at.clip]?.id ?? null : null;}-/** No subtitles shown (fullscreen's CC off): the same list each time, so nothing renders again for it. */-const NO_CUES: readonly SubtitleCue[] = [];-export function ProjectView({ projectId, onProject, onLeave }: {projectId: string;/** go to another project */@@ -736,15 +733,12 @@ export function ProjectView({ projectId, onProject, onLeave }: {const [pickedSaved, setPickedSaved] = React.useState(false);const [inspectorContent, setInspectorContent] = React.useState<'selection' | 'subtitles' | WorkspaceResource>('selection');const subs = useFilmSubtitles(projectId);- /* fullscreen's CC button (and c): only what is shown, not the film's own setting */- const [captionsOn, setCaptionsOn] = React.useState(true);/* the subtitle over the picture renders again while playing only when another line, or another of its words, is said */- const shownCues = captionsOn ? subs.cues : NO_CUES;const subtitleStep = React.useCallback((ms: number) => {- const cue = subs.style.on ? cueAt(shownCues, ms) : null;+ const cue = subs.style.on ? cueAt(subs.cues, ms) : null;if (!cue) return '';- return `${shownCues.indexOf(cue)}:${subs.style.karaoke ? filmSubtitleWords(cue, ms).map((w) => (w.active ? 1 : 0)).join('') : ''}`;- }, [shownCues, subs.style.on, subs.style.karaoke]);+ return `${subs.cues.indexOf(cue)}:${subs.style.karaoke ? filmSubtitleWords(cue, ms).map((w) => (w.active ? 1 : 0)).join('') : ''}`;+ }, [subs.cues, subs.style.on, subs.style.karaoke]);const configureSubtitles = React.useCallback(() => {setInspectorContent('subtitles');setInspectorOpen(!(inspectorOpen && inspectorContent === 'subtitles'));@@ -2935,7 +2929,7 @@ export function ProjectView({ projectId, onProject, onLeave }: {frame puts them on its own bottom (the bar), not pushed up with the picture */}<LiveTime clock={playing ? clock : null} timeMs={timeMs} step={subtitleStep}>{(liveMs) => (<SubtitleOverlay- cues={shownCues}+ cues={subs.cues}timeMs={liveMs}style={filmSubtitleStyleFor(subs.style, frameBox)}onMove={fullscreen ? undefined : subs.move}@@ -2959,8 +2953,8 @@ export function ProjectView({ projectId, onProject, onLeave }: {onToggleMute={() => { player.current.setMuted(!player.current.muted); setMutedState(player.current.muted); }}onRate={setSpeed}onExit={toggleFullscreen}- captionsOn={subs.count ? captionsOn : undefined}- onToggleCaptions={() => setCaptionsOn((v) => !v)}+ captionsOn={subs.count ? subs.style.on : undefined}+ onToggleCaptions={subs.toggle}/>) : null}</div>@@ -2986,7 +2980,6 @@ export function ProjectView({ projectId, onProject, onLeave }: {onLoop: () => setLoop((on) => !on),speed: rate,onSpeed: setSpeed,- captions: subs.count ? { on: captionsOn, onToggle: () => setCaptionsOn((v) => !v) } : null,}}onFullscreen={toggleFullscreen}/>diff --git a/packages/openfilm/studio/ui/src/components/ViewerBar.tsx b/packages/openfilm/studio/ui/src/components/ViewerBar.tsxindex e4a1f8c..cbe8eff 100644--- a/packages/openfilm/studio/ui/src/components/ViewerBar.tsx+++ b/packages/openfilm/studio/ui/src/components/ViewerBar.tsx@@ -33,7 +33,7 @@ export function ViewerBar({onPlayPause: () => void;/** Full screen; the button is disabled while the film has no length. */onFullscreen?: () => void;- /** Loop, subtitles and speed (see PlaybackMenu). Without it there is no settings button. */+ /** Loop and speed (see PlaybackMenu). Without it there is no settings button. */playback?: PlaybackSettings | null;/** Shown before the buttons on the right (the source viewer: the part marked). */extra?: React.ReactNode;diff --git a/packages/openfilm/studio/ui/src/components/export/ExportCenter.tsx b/packages/openfilm/studio/ui/src/components/export/ExportCenter.tsxindex 03daba0..5fff6d7 100644--- a/packages/openfilm/studio/ui/src/components/export/ExportCenter.tsx+++ b/packages/openfilm/studio/ui/src/components/export/ExportCenter.tsx@@ -11,7 +11,7 @@* What was chosen last time for each destination is kept in this browser (localStorage) and comes back next time.*/import {- AlertTriangle, AudioLines, Captions, Check, Clapperboard, Download, Film, FolderArchive, FolderOpen, Images, Layers, Loader2, MonitorPlay, Presentation,+ AlertTriangle, AudioLines, Captions, Check, ChevronDown, Clapperboard, Download, Film, FolderArchive, FolderOpen, Images, Layers, Loader2, MonitorPlay, Presentation,Sparkles, X,} from 'lucide-react';import React from 'react';@@ -50,6 +50,7 @@ import {type ExportCapabilities,type ExportJob,type ExportRequest,+ type NleAlpha,type ProjectPart,type ProjectSizes,} from '@/lib/export-jobs';@@ -112,7 +113,9 @@ interface Settings {/** For an editing program: the sound as clips or as mixed stems. *//** the app it is for: `draft` 剪映 (in Chinese) or CapCut (elsewhere), the same draft; `auto`: the draft when that appis on this computer, else Premiere Pro */- nle: { app: 'auto' | 'draft' | keyof typeof NLE_APPS; fps: number | 'source'; audio: 'clips' | 'stems'; subtitles: boolean };+ nle: { app: 'auto' | 'draft' | keyof typeof NLE_APPS; fps: number | 'source'; audio: 'clips' | 'stems'; subtitles: boolean;+ /** How the pages are written: for a draft, and for Premiere Pro or DaVinci Resolve (Final Cut Pro takes ProRes). */+ alphaDraft: NleAlpha; alphaXml: NleAlpha };subtitles: { format: 'srt' | 'vtt' | 'txt' };images: { format: 'png' | 'jpg'; shortEdge: number; frame: FrameChoice; fill: FilmFrameFill; burn: boolean };slides: { format: 'pptx' | 'pdf'; shortEdge: number; frame: FrameChoice; fill: FilmFrameFill; burn: boolean };@@ -133,7 +136,7 @@ const DEFAULTS: Settings = {gif: { width: 640, fps: 15, frame: 'current', fill: 'contain', burn: true },alpha: { codec: 'prores4444', source: WHOLE_FILM, shortEdge: 1080, scale: 1, fps: 30, audio: true },audio: { format: 'wav', mix: true, stems: false },- nle: { app: 'auto', fps: 'source', audio: 'clips', subtitles: true },+ nle: { app: 'auto', fps: 'source', audio: 'clips', subtitles: true, alphaDraft: 'auto', alphaXml: 'prores4444' },subtitles: { format: 'srt' },images: { format: 'png', shortEdge: 1080, frame: 'current', fill: 'contain', burn: false },slides: { format: 'pptx', shortEdge: 1080, frame: 'current', fill: 'contain', burn: false },@@ -187,6 +190,9 @@ const ALPHA_SCALES = [0.5, 1, 2];const PRESET_GROUPS: ExportPresetGroup[] = ['landscape', 'vertical', 'feed'];/** The rates editors take; a source at another NTSC rate (23.976, 59.94) comes as the "source" choice. */const NLE_FPS = [23.976, 24, 25, 29.97, 30, 50, 59.94, 60];+/** How a draft and an XML write their pages (exports.mjs), the default first. */+const NLE_ALPHA: Record<'draft' | 'xml', readonly NleAlpha[]> = { draft: ['auto', 'png', 'prores'], xml: ['prores4444', 'png'] };+const NLE_ALPHA_NAME: Record<Exclude<NleAlpha, 'auto'>, string> = { png: 'PNG', prores: 'ProRes', prores4444: 'ProRes 4444' };const VIDEO_SRC = /\.(mp4|m4v|mov|webm|mkv|avi|ogv)$/i;/** Whether this machine's ffmpeg makes a codec (unknown until the server answers: assumed yes). */@@ -379,6 +385,11 @@ export function ExportCenter(props: ExportCenterProps) {const nleApp = s.nle.app === 'auto' ? (draftHere ? 'draft' : 'premiere') : s.nle.app;/* a draft goes into its app's drafts folder, whatever folder is picked */const toDrafts = dest === 'nle' && nleApp === 'draft' && draftHere;+ /* Final Cut Pro takes ProRes only: no choice */+ const nleAlphaKind = nleApp === 'draft' ? 'draft' : nleApp === 'finalcut' ? null : 'xml';+ const nleAlphaPicked = nleAlphaKind === 'draft' ? s.nle.alphaDraft : s.nle.alphaXml;+ const nleAlpha: NleAlpha = !nleAlphaKind ? 'prores4444'+ : NLE_ALPHA[nleAlphaKind].includes(nleAlphaPicked) && (!nleAlphaPicked.startsWith('prores') || codecAvailable('prores4444', can)) ? nleAlphaPicked : NLE_ALPHA[nleAlphaKind][0];const codecName = (c: ExportVideoCodec) => CODEC_LABEL[c] + (c === 'png' ? ` ${t('exportCenter.sequence')}` : '');/* the parts of the project asked that it has (a part it has none of is left out) */@@ -421,7 +432,7 @@ export function ExportCenter(props: ExportCenterProps) {}case 'nle':return [{- kind: 'nle', format: nleApp === 'draft' ? draftApp : NLE_APPS[nleApp].format, fps: s.nle.fps === 'source' ? (nleSourceFps ?? projectFps) : s.nle.fps, audio: s.nle.audio, media: 'copy',+ kind: 'nle', format: nleApp === 'draft' ? draftApp : NLE_APPS[nleApp].format, fps: s.nle.fps === 'source' ? (nleSourceFps ?? projectFps) : s.nle.fps, audio: s.nle.audio, media: 'copy', alpha: nleAlpha,subtitles: s.nle.subtitles, label: nleApp === 'draft' ? tApp('exportCenter.nleDraftTitle') : `${NLE_APPS[nleApp].name} XML`,}];case 'subtitles':@@ -443,7 +454,7 @@ export function ExportCenter(props: ExportCenterProps) {}];}// eslint-disable-next-line react-hooks/exhaustive-deps- }, [dest, s, can, presets, nleApp, draftApp, rangeSec?.[0], rangeSec?.[1], props.frame, blocks, nleSourceFps, captions, projectParts]);+ }, [dest, s, can, presets, nleApp, nleAlpha, draftApp, rangeSec?.[0], rangeSec?.[1], props.frame, blocks, nleSourceFps, captions, projectParts]);/* a range, not a size: what is in the picture decides a video's or a GIF's size (export-spec.ts); sound is exact */const estBytes = React.useMemo(() => requests.reduce<[number, number]>(([low, high], r) => {@@ -864,6 +875,25 @@ export function ExportCenter(props: ExportCenterProps) {onChange={(app) => patch('nle', { app })}/></Field>+ {nleAlphaKind && (+ <Field label={t('exportCenter.nleAlpha')}>+ <div className="grid grid-cols-2 gap-2">+ {NLE_ALPHA[nleAlphaKind].map((c) => {+ const unavailable = c.startsWith('prores') && !codecAvailable('prores4444', can);+ return (+ <OptionCard+ key={c}+ on={nleAlpha === c}+ disabled={unavailable}+ title={c === 'auto' ? t('exportCenter.nleAlphaAuto') : NLE_ALPHA_NAME[c]}+ desc={unavailable ? t('exportCenter.notOnThisComputer') : t(`exportCenter.nleAlphaHint.${c}`)}+ onClick={() => patch('nle', nleAlphaKind === 'draft' ? { alphaDraft: c } : { alphaXml: c })}+ />+ );+ })}+ </div>+ </Field>+ )}<Field label={t('exportCenter.fps')}><FpsSelectlabel={t('exportCenter.fps')}@@ -1132,72 +1162,61 @@ export function ExportCenter(props: ExportCenterProps) {</div></div>- {/* bottom: where it goes (the folder, a click changes it), what comes out, and the primary button */}- <div className="flex shrink-0 flex-wrap items-center gap-x-4 gap-y-3 border-t border-[var(--border-soft)] bg-[color-mix(in_srgb,var(--surface-2)_45%,transparent)] px-5 py-3.5">- {toDrafts ? (- <div className="-ml-2 flex min-w-0 max-w-[340px] flex-1 items-center gap-2.5 px-2 py-1.5">- <span className="flex h-8 w-8 shrink-0 items-center justify-center rounded-lg bg-[var(--surface-2)] text-[var(--text-muted)] ring-1 ring-[var(--border)]">- <Clapperboard size={15} />- </span>- <span className="flex min-w-0 items-baseline gap-1.5">- <span className="shrink-0 text-[11px] text-[var(--text-faint)]">{t('exportCenter.saveTo')}</span>- <span className="truncate text-[13px] font-medium text-[var(--text)]">{tApp('exportCenter.nleDraftTitle')}</span>- </span>- </div>- ) : folderDraft != null ? (- <input- autoFocus- value={folderDraft}- spellCheck={false}- aria-label={t('exportCenter.saveTo')}- onChange={(e) => setFolderDraft(e.target.value)}- onFocus={(e) => e.currentTarget.select()}- onBlur={commitFolder}- onKeyDown={(e) => {- if (e.key === 'Enter') { e.preventDefault(); commitFolder(); }- if (e.key === 'Escape') { e.preventDefault(); e.stopPropagation(); setFolderDraft(null); }- }}- className="h-10 min-w-[200px] flex-1 rounded-lg border border-[var(--border)] bg-[var(--bg)] px-3 font-mono text-[12px] text-[var(--text)] outline-none focus:border-[var(--text-faint)]"- />- ) : (- <button- type="button"- onClick={() => void changeFolder()}- disabled={picking}- title={folder}- aria-label={`${t('exportCenter.saveTo')}: ${folder || '—'}. ${t('exportCenter.change')}`}- className="group -ml-2 flex min-w-0 max-w-[340px] flex-1 items-center gap-2.5 rounded-lg px-2 py-1.5 text-left transition hover:bg-[var(--bg-hover)] disabled:opacity-60"- >- <span className="flex h-8 w-8 shrink-0 items-center justify-center rounded-lg bg-[var(--surface-2)] text-[var(--text-muted)] ring-1 ring-[var(--border)] transition group-hover:text-[var(--text)]">- {picking ? <Loader2 size={15} className="animate-spin" /> : <FolderOpen size={15} />}+ {/* bottom: where it goes (a button like the dialog's other pickers: a click picks another folder), what it is+ called, what comes out, and the primary button */}+ <div className="flex shrink-0 flex-wrap items-center gap-x-5 gap-y-3 border-t border-[var(--border-soft)] bg-[color-mix(in_srgb,var(--surface-2)_45%,transparent)] px-5 py-3.5">+ <div className="flex min-w-[280px] max-w-[560px] flex-1 items-center gap-2">+ {toDrafts ? (+ <span className="flex h-10 min-w-0 items-center gap-2 rounded-lg border border-[var(--border)] px-3 text-[13px] text-[var(--text-muted)]">+ <Clapperboard size={15} className="shrink-0" />+ <span className="truncate">{tApp('exportCenter.nleDraftTitle')}</span></span>- <span className="flex min-w-0 flex-col">- <span className="flex min-w-0 items-baseline gap-1.5">- <span className="shrink-0 text-[11px] text-[var(--text-faint)]">{t('exportCenter.saveTo')}</span>- <span className="truncate text-[13px] font-medium text-[var(--text)]">{folderName(folder) || '—'}</span>- </span>- <span className="truncate text-[11px] text-[var(--text-faint)] group-hover:text-[var(--text-muted)]">{t('exportCenter.change')}</span>- </span>- </button>- )}- {/* what the files are called: the project's own name unless typed over (a draft is named by its app) */}- {!toDrafts && (- <label className="flex min-w-0 items-center gap-2">- <span className="shrink-0 text-[11px] text-[var(--text-faint)]">{t('exportCenter.fileName')}</span>+ ) : folderDraft != null ? (+ <input+ autoFocus+ value={folderDraft}+ spellCheck={false}+ aria-label={t('exportCenter.saveTo')}+ onChange={(e) => setFolderDraft(e.target.value)}+ onFocus={(e) => e.currentTarget.select()}+ onBlur={commitFolder}+ onKeyDown={(e) => {+ if (e.key === 'Enter') { e.preventDefault(); commitFolder(); }+ if (e.key === 'Escape') { e.preventDefault(); e.stopPropagation(); setFolderDraft(null); }+ }}+ className="h-10 min-w-0 flex-1 rounded-lg border border-[var(--border)] bg-[var(--bg)] px-3 font-mono text-[12px] text-[var(--text)] outline-none focus:border-[var(--text-faint)]"+ />+ ) : (+ <button+ type="button"+ onClick={() => void changeFolder()}+ disabled={picking}+ title={folder}+ aria-label={`${t('exportCenter.saveTo')}: ${folder || '—'}. ${t('exportCenter.change')}`}+ className="flex h-10 min-w-0 max-w-[45%] shrink-0 items-center gap-2 rounded-lg border border-[var(--border)] bg-[var(--fill-tsp)] pl-3 pr-2.5 text-[13px] text-[var(--text)] outline-none transition hover:border-[var(--text-faint)] hover:bg-[var(--bg-hover)] focus-visible:border-[var(--accent)] disabled:opacity-60"+ >+ {picking ? <Loader2 size={15} className="shrink-0 animate-spin text-[var(--text-muted)]" /> : <FolderOpen size={15} className="shrink-0 text-[var(--text-muted)]" />}+ <span className="min-w-0 truncate">{folderName(folder) || '—'}</span>+ <ChevronDown size={12} aria-hidden className="shrink-0 text-[var(--text-faint)]" />+ </button>+ )}+ {/* what the files are called: the project's own name unless typed over (a draft is named by its app) */}+ {!toDrafts && folderDraft == null && (<inputvalue={exportName}spellCheck={false}placeholder={props.title}aria-label={t('exportCenter.fileName')}+ title={t('exportCenter.fileName')}onChange={(e) => { setExportName(e.target.value); setNameTyped(true); }}onFocus={(e) => e.currentTarget.select()}onKeyDown={(e) => {if (e.key === 'Enter' && !e.nativeEvent.isComposing && !disabled) { e.preventDefault(); void queue(requests); }}}- className="h-10 w-[220px] min-w-[140px] rounded-lg border border-[var(--border)] bg-[var(--bg)] px-3 text-[13px] text-[var(--text)] outline-none focus:border-[var(--text-faint)]"+ className="h-10 min-w-0 flex-1 rounded-lg border border-[var(--border)] bg-[var(--bg)] px-3 text-[13px] text-[var(--text)] outline-none transition focus:border-[var(--text-faint)]"/>- </label>- )}+ )}+ </div><div className="ml-auto flex min-w-0 items-center gap-4"><div className="min-w-0 text-right text-[12px] leading-snug">{result ? (diff --git a/packages/openfilm/studio/ui/src/components/export/ExportTray.tsx b/packages/openfilm/studio/ui/src/components/export/ExportTray.tsxindex 82c257c..154e14c 100644--- a/packages/openfilm/studio/ui/src/components/export/ExportTray.tsx+++ b/packages/openfilm/studio/ui/src/components/export/ExportTray.tsx@@ -3,7 +3,8 @@** Once exports are queued the dialog closes and the person keeps editing; this says how far each one has got (which* frame, frames per second, how long is left, which encoder), opens or shows what it made, and shows a failure in- * its own words. It is there only while there are exports to show; the project's event stream tells it every change.+ * its own words. It is there while there are exports to show, until the person closes it; the project's event stream+ * tells it every change.*/import { Ban, Check, ChevronDown, ChevronUp, CircleAlert, FolderOpen, Loader2, Play, X } from 'lucide-react';import React from 'react';@@ -24,20 +25,28 @@ import {type ExportJob,} from '@/lib/export-jobs';-/** A finished export's card stays this long after the last one ends. */-const AUTO_HIDE_MS = 12_000;-/** Reopening the project shows exports that ended this recently (a failure a little longer). */-const DONE_KEPT_MS = 2 * 60_000;-const FAILED_KEPT_MS = 10 * 60_000;+/* A card stays until the person closes it: a finished export is found where it was left, its file one click away.+ Closed ones stay closed while Studio runs, the project or the page opened again or not (its exports are this+ launch's): kept in the tab's session storage, which may refuse (a private window), then for as long as the page. */+const closedByProject = new Map<string, Set<string>>();+const CLOSED_KEY = 'openfilm.exportTray.closed';+function closedOf(projectId: string): Set<string> {+ const known = closedByProject.get(projectId);+ if (known) return known;+ let ids: string[] = [];+ try { ids = JSON.parse(sessionStorage.getItem(`${CLOSED_KEY}.${projectId}`) ?? '[]'); } catch { /* nothing kept */ }+ return new Set(Array.isArray(ids) ? ids.filter((id) => typeof id === 'string') : []);+}+function keepClosed(projectId: string, ids: Set<string>) {+ closedByProject.set(projectId, ids);+ try { sessionStorage.setItem(`${CLOSED_KEY}.${projectId}`, JSON.stringify([...ids])); } catch { /* kept for this page only */ }+}export interface ExportTrayHandle {/** Exports were just queued: show them now. */push: (jobs: ExportJob[]) => void;}-/** Only what is running or just ended: exports finished long ago are in the export folder, not a card. */-const recent = (job: ExportJob, now: number) => jobIsActive(job)- || (job.finishedAt != null && now - job.finishedAt < (job.status === 'failed' ? FAILED_KEPT_MS : DONE_KEPT_MS));/*** `onError`: a finished export that could not be opened or shown (its file moved or deleted since), in a sentence.@@ -48,7 +57,7 @@ export const ExportTray = React.forwardRef<ExportTrayHandle, { projectId: stringconst fileAction = (run: Promise<void>) => { void run.catch(() => onError?.(t('exportTray.fileGone'))); };const [jobs, setJobs] = React.useState<ExportJob[]>([]);const [collapsed, setCollapsed] = React.useState(false);- const [dismissed, setDismissed] = React.useState<Set<string>>(() => new Set());+ const [dismissed, setDismissed] = React.useState<Set<string>>(() => closedOf(projectId));/* Cancel was pressed: said at once, before the server's word that it is stopping arrives */const [cancelAsked, setCancelAsked] = React.useState<Set<string>>(() => new Set());const cancelling = (job: ExportJob) => jobIsActive(job) && (job.cancelling || cancelAsked.has(job.id));@@ -83,15 +92,14 @@ export const ExportTray = React.forwardRef<ExportTrayHandle, { projectId: string/* when the project opens: exports still running from before are still shown; then every change as it happens */React.useEffect(() => {setJobs([]);- setDismissed(new Set());+ setDismissed(closedOf(projectId));setCancelAsked(new Set());let live = true;void listExports(projectId).then((r) => {if (!live) return;- const now = Date.now();setJobs((prev) => {const known = new Set(prev.map((j) => j.id));- return [...prev, ...r.exports.filter((j) => !known.has(j.id) && recent(j, now))];+ return [...prev, ...r.exports.filter((j) => !known.has(j.id))];});}).catch(() => {});const stop = projectEvents(projectId, (event) => {@@ -124,18 +132,6 @@ export const ExportTray = React.forwardRef<ExportTrayHandle, { projectId: string}, [projectId, t]);const visible = jobs.filter((j) => !dismissed.has(j.id));- /* All done and none failed: the card goes by itself after a moment (not while the pointer is on it). A failure- stays until it is closed: that one needs reading. */- const [hovered, setHovered] = React.useState(false);- const settled = visible.length > 0 && !visible.some(jobIsActive) && !visible.some((j) => j.status === 'failed');- /* the cards shown, as a string: `visible` is a new array on every render (many a second while the film plays),- which would start the timer over each time */- const shownIds = visible.map((j) => j.id).join('\n');- React.useEffect(() => {- if (!settled || hovered) return undefined;- const timer = window.setTimeout(() => setDismissed((d) => new Set([...d, ...shownIds.split('\n')])), AUTO_HIDE_MS);- return () => window.clearTimeout(timer);- }, [settled, hovered, shownIds]);if (!visible.length) return null;const active = visible.filter(jobIsActive);@@ -144,8 +140,6 @@ export const ExportTray = React.forwardRef<ExportTrayHandle, { projectId: stringreturn (<div- onMouseEnter={() => setHovered(true)}- onMouseLeave={() => setHovered(false)}style={{ right: 16 + inset }}className="pointer-events-auto fixed bottom-4 z-[120] w-[340px] overflow-hidden rounded-2xl border border-[var(--border)] bg-[var(--surface)] shadow-[var(--shadow-lg)]">@@ -170,7 +164,7 @@ export const ExportTray = React.forwardRef<ExportTrayHandle, { projectId: string{!active.length && (<buttontype="button"- onClick={() => setDismissed(new Set(jobs.map((j) => j.id)))}+ onClick={() => { const closed = new Set(jobs.map((j) => j.id)); keepClosed(projectId, closed); setDismissed(closed); }}aria-label={t('exportTray.close')}className="rounded-md p-1 text-[var(--text-faint)] transition hover:bg-[var(--bg-hover)] hover:text-[var(--text)]">@@ -273,7 +267,7 @@ function JobLine({ job, cancelling, t }: { job: ExportJob; cancelling: boolean;return <p className="mt-0.5 text-[11.5px] text-[var(--text-faint)]">{t('exportTray.cancelled')}</p>;}/* a long failure is cut to three lines: a click (or the pointer resting on it) shows all of it */- const error = job.error || t('exportTray.failed');+ const error = job.diskFull ? t('exportTray.diskFull') : job.error || t('exportTray.failed');return (<prole="button"diff --git a/packages/openfilm/studio/ui/src/components/inspector-fields.tsx b/packages/openfilm/studio/ui/src/components/inspector-fields.tsxindex 02aa77e..7b51a65 100644--- a/packages/openfilm/studio/ui/src/components/inspector-fields.tsx+++ b/packages/openfilm/studio/ui/src/components/inspector-fields.tsx@@ -16,6 +16,7 @@ import { ChevronRight } from 'lucide-react';import { useT } from '@/i18n';import { applyFieldInput, parseFieldInput, type FieldContext } from '@/lib/field-input';import { scrubFromDrag } from '@/lib/inspect-scrub';+import { MIXED, type Mixed } from '@/lib/mixed';import { metricCss, parseMetric, showMetric, stepMetric, type Metric, type MetricKind } from '@/lib/type-metrics';import { Tooltip } from './Tooltip';import { Popover } from './Popover';@@ -32,9 +33,7 @@ const PREFIX = 'flex h-[26px] w-[22px] shrink-0 select-none items-center justifyexport const HEAD_BTN = 'flex h-6 w-6 shrink-0 items-center justify-center rounded-[5px] text-[var(--text-muted)] transition hover:bg-[var(--bg-hover)] hover:text-[var(--text)] disabled:pointer-events-none disabled:opacity-40';export const ICON = 13;-/** Several things selected that differ in a value. */-export const MIXED: unique symbol = Symbol('mixed');-export type Mixed = typeof MIXED;+export { MIXED, type Mixed };/** Turns a thing's own value into its new one (one value typed for all, or a change of each). */export type FieldApply = (base: number) => number;@@ -509,7 +508,7 @@ export function MetricField({* double-click puts it back to neutral. The number beside it takes everything a number field takes.*/export function SliderField({- label, value, min, max, neutral = min, step = 1, unit, fieldMin, fieldMax, onPreview, onCommit, onRevert, disabled, placeholder,+ label, value, min, max, neutral = min, step = 1, unit, fieldMin, fieldMax, fieldWidth = 56, onPreview, onCommit, onRevert, disabled, placeholder,}: {label: string;value: number | undefined | Mixed;@@ -522,6 +521,8 @@ export function SliderField({unit?: string;fieldMin?: number;fieldMax?: number;+ /** The number's width in px, for values longer than a percentage (`-14.4 dB`). */+ fieldWidth?: number;onPreview?: (n: number, apply: FieldApply) => void;onCommit: (n: number, apply: FieldApply) => void;onRevert?: () => void;@@ -545,7 +546,7 @@ export function SliderField({const to = Math.max(pct(neutral), pct(shown));const centred = neutral > min && neutral < max;return (- <div className={`grid grid-cols-[64px_minmax(0,1fr)_56px] items-center gap-2 ${disabled ? 'opacity-50' : ''}`}>+ <div className={`grid items-center gap-2 ${disabled ? 'opacity-50' : ''}`} style={{ gridTemplateColumns: `64px minmax(0,1fr) ${fieldWidth}px` }}><span className="truncate text-[11px] text-[var(--text-muted)]" title={label}>{label}</span><div className="relative flex h-[26px] items-center"><div className="pointer-events-none absolute inset-x-0 top-1/2 h-[3px] -translate-y-1/2 rounded-full bg-[var(--border-strong)]">diff --git a/packages/openfilm/studio/ui/src/components/inspector-look.tsx b/packages/openfilm/studio/ui/src/components/inspector-look.tsxindex a9895bf..08916e2 100644--- a/packages/openfilm/studio/ui/src/components/inspector-look.tsx+++ b/packages/openfilm/studio/ui/src/components/inspector-look.tsx@@ -17,13 +17,14 @@ import type { useT } from '@/i18n';import {ADJUST_KEYS, BLEND_MODES, FADE_DEFAULT, INNER_SHADOW_DEFAULT, PLACED_STROKE_DEFAULT, SHADOW_DEFAULT, adjustOf, backdropBlurCss,backdropBlurOf, cropCss, cropForRatio, cropOf, cropRatio, filterParts, framingCss, framingOf, innerShadowCss, innerShadowOf,- ownedDeclaration, placedStrokeCss, placedStrokeOf, pxOf, radiusCorners, shadowOf, strokeOf, withAdjust, withShadow, type AdjustKey, type Crop, type Fade,+ ownedDeclaration, placedStrokeCss, placedStrokeOf, pxOf, radiusCorners, shadowOf, strokeOf, textColorCss, textColorOf, withAdjust, withShadow, type AdjustKey, type Crop, type Fade,type FadeSide, type InnerShadow, type PlacedStroke, type Shadow, type StrokePosition, type StrokeStyle,} from '@/lib/clip-look';import {MASK_FIELDS, MASK_SHAPES, fadeIn, maskDefault, maskOf, maskOwned, withFade, withMask, type Mask, type MaskBox, type MaskShape,} from '@/lib/clip-mask';import type { MaskSession } from '@/lib/stage-mask';+import { previewSettled, type Look, type LookEdit, type LookRead, type LookSet } from '@/lib/look';import { knownAvailable, stylesOf } from '@/lib/font-menu';import {NAMED_WEIGHTS, isItalic, joinFontFamily, parseStyleValue, snapStyle, splitFontFamily, styleOptions, styleValue, weightKey, weightNumber,@@ -33,92 +34,13 @@ import { FONT_SIZES, metricOf } from '@/lib/type-metrics';import { familyRenders, useInstalledFonts, useProjectFonts } from '@/lib/use-fonts';import { declarations } from '../../../../src/film-doc.mjs';import {- ColorField, EdgeGlyph, FontSizeGlyph, ICON, LetterSpacingGlyph, LineHeightGlyph, MIXED, MetricField, NumField, RadiusGlyph, RotateGlyph, Row, Section, SectionAction, Segmented, SelectField, SliderField, SubLabel, type FieldApply, type Mixed,+ ColorField, EdgeGlyph, FontSizeGlyph, ICON, LetterSpacingGlyph, LineHeightGlyph, MIXED, MetricField, NumField, RadiusGlyph, RotateGlyph, Row, Section, SectionAction, Segmented, SelectField, SliderField, SubLabel, type FieldApply,} from './inspector-fields';import { FontFamilyPicker } from './FontFamilyPicker';import { Popover } from './Popover';type T = ReturnType<typeof useT>;-/* ───────────────────────────── the model ───────────────────────────── */--/** One thing's CSS property (kebab-case), as it is now. */-export type LookRead = (prop: string) => string | undefined;-/** Properties to write: a value, or null — "none of mine" (the panel decides what that leaves). */-export type LookSet = Record<string, string | null>;-/** What a control does to one thing, from that thing's own values. */-export type LookEdit = (read: LookRead) => LookSet;--export interface Look {- /** How many things it is. */- n: number;- /** What `fn` makes of every thing's values, when they agree; MIXED when they do not. */- common<V>(fn: (read: LookRead) => V): V | Mixed;- /** Shows an edit on the picture, nothing written. */- preview(edit: LookEdit): void;- /** Writes an edit, as one change. */- commit(edit: LookEdit): void;- /** Takes a preview back off the picture. */- revert(): void;-}--/**- * A Look over `targets`: read each through `readOf`, and hand each one's properties to `write` (the panel stores- * them). `write` gets only the things an edit changes.- */-export function makeLook<Tg>(- targets: readonly Tg[],- readOf: (t: Tg) => LookRead,- write: {- commit: (sets: [Tg, LookSet][]) => void;- preview?: (sets: [Tg, LookSet][]) => void;- revert?: () => void;- },-): Look {- const reads = targets.map(readOf);- const sets = (edit: LookEdit, kept?: ReadonlyMap<string, string | undefined>[] | null): [Tg, LookSet][] => targets.flatMap((tg, i) => {- const read = reads[i]!;- const set = edit(read);- /* unchanged properties are not written: the agent reads the diff, and a no-op would be an undo step. Unchanged- from what was there before a preview: the layer is measured again while previewed, so by now it reads as the- preview, and a kept preview would read as no change */- const was = (k: string) => (kept?.[i]?.has(k) ? kept[i]!.get(k) : read(k));- const changed = Object.entries(set).filter(([k, v]) => (v ?? undefined) !== was(k));- return changed.length ? [[tg, Object.fromEntries(changed)] as [Tg, LookSet]] : [];- });- return {- n: targets.length,- common(fn) {- if (!reads.length) return MIXED;- const first = fn(reads[0]!);- const key = JSON.stringify(first);- return reads.every((r) => JSON.stringify(fn(r)) === key) ? first : MIXED;- },- /* shown as said, unchanged or not: a layer reads as its kept change while a preview paints over it, so painting- back what was there would read as no change */- preview(edit) {- if (!previewedBefore || previewedBefore.length !== targets.length) previewedBefore = targets.map(() => new Map());- const list = targets.flatMap((tg, i) => {- const set = edit(reads[i]!);- for (const k of Object.keys(set)) if (!previewedBefore![i]!.has(k)) previewedBefore![i]!.set(k, reads[i]!(k));- return Object.keys(set).length ? [[tg, set] as [Tg, LookSet]] : [];- });- write.preview?.(list);- },- commit(edit) {- const list = sets(edit, previewedBefore);- previewedBefore = null;- if (list.length) write.commit(list);- },- revert() { previewedBefore = null; write.revert?.(); },- };-}--/** What each previewed property of the things in hand was before its first preview (see makeLook's `sets`). */-let previewedBefore: Map<string, string | undefined>[] | null = null;-/** A preview was painted back by hand (as a preview of what was there): nothing previewed stands any more. */-const previewSettled = () => { previewedBefore = null; };-/** What a property is when nothing sets it: writing "none of mine" over a look the film's CSS gives, this is written. */export const NEUTRAL: Record<string, string> = {opacity: '1', filter: 'none', 'clip-path': 'none', 'border-radius': '0', 'mix-blend-mode': 'normal', 'object-fit': 'contain',@@ -678,22 +600,27 @@ export function FillSection({ look, t, fills, add }: {<SectionAction label={t('inspector.fillAdd')} onClick={() => look.commit(() => ({ [add.prop]: add.value }))}><Plus size={12} /></SectionAction>) : null}>- {fills.map((f) => (- <div key={f.prop} className="flex items-center gap-1.5">- <div className="min-w-0 flex-1">- <ColorField- label={f.label}- value={look.common((read) => read(f.prop) ?? '')}- onPreview={(c) => look.preview(() => ({ [f.prop]: c }))}- onCommit={(c) => look.commit(() => ({ [f.prop]: c }))}- />+ {fills.map((f) => {+ /* words: the color they are drawn in, which a fill of their page's own paints over `color` */+ const of = (read: LookRead) => (f.prop === 'color' ? textColorOf(read) : read(f.prop));+ const to = (c: string): LookEdit => (read) => (f.prop === 'color' ? textColorCss(read, c) : { [f.prop]: c });+ return (+ <div key={f.prop} className="flex items-center gap-1.5">+ <div className="min-w-0 flex-1">+ <ColorField+ label={f.label}+ value={look.common((read) => of(read) ?? '')}+ onPreview={(c) => look.preview(to(c))}+ onCommit={(c) => look.commit(to(c))}+ />+ </div>+ {f.removable ? (+ /* see-through, not "none of mine": that would bring back the page's own fill */+ <SectionAction label={t('inspector.fillRemove')} onClick={() => look.commit(() => ({ [f.prop]: 'transparent' }))}><Minus size={12} /></SectionAction>+ ) : null}</div>- {f.removable ? (- /* see-through, not "none of mine": that would bring back the page's own fill */- <SectionAction label={t('inspector.fillRemove')} onClick={() => look.commit(() => ({ [f.prop]: 'transparent' }))}><Minus size={12} /></SectionAction>- ) : null}- </div>- ))}+ );+ })}</Section>);}diff --git a/packages/openfilm/studio/ui/src/components/settings/AgentsSection.tsx b/packages/openfilm/studio/ui/src/components/settings/AgentsSection.tsxindex 4950fbb..bfb1677 100644--- a/packages/openfilm/studio/ui/src/components/settings/AgentsSection.tsx+++ b/packages/openfilm/studio/ui/src/components/settings/AgentsSection.tsx@@ -50,7 +50,7 @@ function useHostAgents(): HostAgent[] | null {}/** The person's own agents (the app's AGENTS), in the roster's order. */-const OWN: readonly HostAgentId[] = ['codex', 'claude', 'copilot', 'cursor', 'opencode', 'codebuddy', 'qwen', 'kimi', 'minimax'];+const OWN: readonly HostAgentId[] = ['codex', 'claude', 'copilot', 'cursor', 'opencode', 'codebuddy', 'qwen', 'kimi', 'minimax', 'deepseek'];/** whose maker's mark is its company's */function AgentMark({ id }: { id: HostAgentId }) {return id === 'byok' ? <KeyRound size={16} /> : <ProviderMark id={agentMark(id)} size={17} />;diff --git a/packages/openfilm/studio/ui/src/i18n/en.ts b/packages/openfilm/studio/ui/src/i18n/en.tsindex 7a2dcf4..226d376 100644--- a/packages/openfilm/studio/ui/src/i18n/en.ts+++ b/packages/openfilm/studio/ui/src/i18n/en.ts@@ -478,6 +478,14 @@ export const en = {nleAudio: 'Sound on the timeline',nleAudioClips: 'Separate clips',nleAudioStems: 'Mixed stems',+ nleAlpha: 'Pages',+ nleAlphaAuto: 'Automatic',+ nleAlphaHint: {+ auto: 'Each page checked frame by frame: an opaque one as H.264, small and quick to play; one with any transparency as lossless PNG',+ png: 'Lossless, pixel for pixel. A moderate size; works on Mac and Windows',+ prores: 'An editing master: ProRes 422 HQ for an opaque page, 4444 for one with transparency. Very large',+ prores4444: 'The editors\' master format. Very large: about 2.8 GB a minute at 1080p',+ },projectZip: 'Zip',projectZipHint: 'One file, easy to send or upload.',projectFolder: 'Folder',@@ -531,6 +539,7 @@ export const en = {cancelling: 'Cancelling…',failed: 'Export failed',draftsRefused: "The drafts folder refused the write (security software often does), so the draft is in the export folder: move it into {folder}.",+ diskFull: 'The disk is full. Free up some space, then export again.',notTransparent: 'No transparent pixels: this element paints its own background.',},player: {@@ -915,7 +924,6 @@ export const en = {fullscreen: 'Full screen',playback: 'Playback',speed: 'Speed',- subtitles: 'Subtitles',},inspector: {fades: 'Fades',@@ -1401,7 +1409,7 @@ export const en = {normalizing: "Measuring loudness…",normalized: "{n} clips normalized to {target} LUFS",normalizedOne: "{clip}: {from} LUFS → {target} LUFS (volume {pct}%)",- normalizeShort: "{clip} can't reach {target} LUFS: at 200% it is {lufs} LUFS",+ normalizeShort: "{clip} can't reach {target} LUFS: at 500% it is {lufs} LUFS",normalizeSilent: "{clip} has no sound to measure",normalizeFail: "Couldn't measure {clip}: {reason}",freeze: "Freeze frame",diff --git a/packages/openfilm/studio/ui/src/i18n/es.ts b/packages/openfilm/studio/ui/src/i18n/es.tsindex 6da3a59..79ee408 100644--- a/packages/openfilm/studio/ui/src/i18n/es.ts+++ b/packages/openfilm/studio/ui/src/i18n/es.ts@@ -477,6 +477,14 @@ export const es: Words = {nleAudio: 'Sonido en la línea de tiempo',nleAudioClips: 'Clips separados',nleAudioStems: 'Stems mezclados',+ nleAlpha: 'Codificación de las páginas',+ nleAlphaAuto: 'Automático',+ nleAlphaHint: {+ auto: 'Cada página revisada fotograma a fotograma: una opaca en H.264, ligera y fluida; una con cualquier transparencia en PNG sin pérdida',+ png: 'Sin pérdida, idéntico píxel a píxel. Tamaño moderado; funciona en Mac y Windows',+ prores: 'Un máster de edición: ProRes 422 HQ para una página opaca, 4444 para una con transparencia. Muy pesado',+ prores4444: 'El formato máster de los editores. Muy pesado: unos 2,8 GB por minuto a 1080p',+ },projectZip: 'Zip',projectZipHint: 'Un solo archivo, fácil de enviar o subir.',projectFolder: 'Carpeta',@@ -530,6 +538,7 @@ export const es: Words = {cancelling: 'Cancelando…',failed: 'La exportación falló',draftsRefused: "La carpeta de borradores rechazó la escritura (suele hacerlo el software de seguridad), así que el borrador está en la carpeta de exportación: muévelo a {folder}.",+ diskFull: 'El disco está lleno. Libera espacio y vuelve a exportar.',notTransparent: 'No hay píxeles transparentes: este elemento pinta su propio fondo.',},player: {@@ -914,7 +923,6 @@ export const es: Words = {fullscreen: 'Pantalla completa',playback: 'Reproducción',speed: 'Velocidad',- subtitles: 'Subtítulos',},inspector: {fades: 'Fundidos',@@ -1400,7 +1408,7 @@ export const es: Words = {normalizing: "Midiendo la sonoridad…",normalized: "{n} clips normalizados a {target} LUFS",normalizedOne: "{clip}: {from} LUFS → {target} LUFS (volumen {pct} %)",- normalizeShort: "{clip} no llega a {target} LUFS: al 200 % queda en {lufs} LUFS",+ normalizeShort: "{clip} no llega a {target} LUFS: al 500 % queda en {lufs} LUFS",normalizeSilent: "{clip} no tiene sonido que medir",normalizeFail: "No se pudo medir {clip}: {reason}",freeze: "Congelar fotograma",diff --git a/packages/openfilm/studio/ui/src/i18n/ja.ts b/packages/openfilm/studio/ui/src/i18n/ja.tsindex b7baa43..82f36a9 100644--- a/packages/openfilm/studio/ui/src/i18n/ja.ts+++ b/packages/openfilm/studio/ui/src/i18n/ja.ts@@ -477,6 +477,14 @@ export const ja: Words = {nleAudio: 'タイムライン上のサウンド',nleAudioClips: '個別のクリップ',nleAudioStems: 'ミックス済みステム',+ nleAlpha: 'ページのエンコード',+ nleAlphaAuto: '自動',+ nleAlphaHint: {+ auto: 'ページごとに全フレームを確認:不透明なら H.264(小さく軽快に再生)、少しでも透過があればロスレス PNG',+ png: 'ロスレスで、ピクセル単位で同じ。サイズは中程度で、Mac と Windows で使えます',+ prores: '編集用マスター:不透明なページは ProRes 422 HQ、透過のあるページは 4444。サイズが非常に大きい',+ prores4444: '編集ソフト共通のマスター形式。サイズが非常に大きく、1080p で 1 分あたり約 2.8 GB',+ },projectZip: 'Zip',projectZipHint: '1 つのファイルで、送信やアップロードが簡単です。',projectFolder: 'フォルダー',@@ -530,6 +538,7 @@ export const ja: Words = {cancelling: 'キャンセル中…',failed: '書き出しに失敗しました',draftsRefused: "下書きフォルダへの書き込みが拒否されたため(セキュリティソフトによることが多いです)、下書きは書き出しフォルダにあります。{folder} に移してください。",+ diskFull: 'ディスクの空き容量が足りません。空き容量を増やしてから、もう一度書き出してください。',notTransparent: '透明なピクセルがありません。この要素は自身の背景を描画しています。',},player: {@@ -914,7 +923,6 @@ export const ja: Words = {fullscreen: '全画面表示',playback: '再生設定',speed: '再生速度',- subtitles: '字幕を表示',},inspector: {fades: 'フェード',@@ -1400,7 +1408,7 @@ export const ja: Words = {normalizing: "ラウドネスを測定中…",normalized: "{n} 個のクリップを {target} LUFS にノーマライズしました",normalizedOne: "{clip}:{from} LUFS → {target} LUFS(音量 {pct}%)",- normalizeShort: "{clip} は {target} LUFS に届きません:200% で {lufs} LUFS です",+ normalizeShort: "{clip} は {target} LUFS に届きません:500% で {lufs} LUFS です",normalizeSilent: "{clip} には測定できる音がありません",normalizeFail: "{clip} を測定できませんでした:{reason}",freeze: "フリーズフレーム",diff --git a/packages/openfilm/studio/ui/src/i18n/ko.ts b/packages/openfilm/studio/ui/src/i18n/ko.tsindex 3c83deb..dd37f04 100644--- a/packages/openfilm/studio/ui/src/i18n/ko.ts+++ b/packages/openfilm/studio/ui/src/i18n/ko.ts@@ -477,6 +477,14 @@ export const ko: Words = {nleAudio: '타임라인의 사운드',nleAudioClips: '개별 클립',nleAudioStems: '믹스된 스템',+ nleAlpha: '페이지 인코딩',+ nleAlphaAuto: '자동',+ nleAlphaHint: {+ auto: '페이지마다 모든 프레임을 확인합니다. 불투명하면 H.264(작고 재생이 매끄러움), 조금이라도 투명하면 무손실 PNG',+ png: '무손실이라 픽셀 하나까지 같습니다. 용량은 보통이고 Mac과 Windows 모두에서 쓸 수 있습니다',+ prores: '편집용 마스터: 불투명한 페이지는 ProRes 422 HQ, 투명한 페이지는 4444. 용량이 매우 큽니다',+ prores4444: '편집 프로그램 공통의 마스터 형식. 용량이 매우 큽니다: 1080p 기준 1분에 약 2.8GB',+ },projectZip: 'Zip',projectZipHint: '파일 하나로, 보내거나 업로드하기 쉽습니다.',projectFolder: '폴더',@@ -530,6 +538,7 @@ export const ko: Words = {cancelling: '취소하는 중…',failed: '내보내기 실패',draftsRefused: "초안 폴더에 쓰기가 거부되어(보안 소프트웨어 때문인 경우가 많습니다) 초안은 내보내기 폴더에 있습니다. {folder}(으)로 옮기세요.",+ diskFull: '디스크 공간이 부족합니다. 공간을 확보한 뒤 다시 내보내세요.',notTransparent: '투명한 픽셀이 없습니다. 이 요소가 자체 배경을 칠하고 있습니다.',},player: {@@ -914,7 +923,6 @@ export const ko: Words = {fullscreen: '전체 화면',playback: '재생 설정',speed: '재생 속도',- subtitles: '자막 표시',},inspector: {fades: '페이드',@@ -1400,7 +1408,7 @@ export const ko: Words = {normalizing: "라우드니스 측정 중…",normalized: "클립 {n}개를 {target} LUFS로 정규화했습니다",normalizedOne: "{clip}: {from} LUFS → {target} LUFS (볼륨 {pct}%)",- normalizeShort: "{clip}은(는) {target} LUFS에 도달할 수 없습니다: 200%에서 {lufs} LUFS",+ normalizeShort: "{clip}은(는) {target} LUFS에 도달할 수 없습니다: 500%에서 {lufs} LUFS",normalizeSilent: "{clip}에는 측정할 소리가 없습니다",normalizeFail: "{clip}을(를) 측정할 수 없습니다: {reason}",freeze: "프레임 고정",diff --git a/packages/openfilm/studio/ui/src/i18n/zh-CN.ts b/packages/openfilm/studio/ui/src/i18n/zh-CN.tsindex 5c203db..5fe7b8e 100644--- a/packages/openfilm/studio/ui/src/i18n/zh-CN.ts+++ b/packages/openfilm/studio/ui/src/i18n/zh-CN.ts@@ -478,6 +478,14 @@ export const zhCN: Words = {nleAudio: '时间轴上的声音',nleAudioClips: '独立片段',nleAudioStems: '混合分轨',+ nleAlpha: '页面编码',+ nleAlphaAuto: '自动',+ nleAlphaHint: {+ auto: '逐帧检查每个页面:不透明的用 H.264,体积小、播放流畅;有任何透明的用无损 PNG',+ png: '无损,与画面逐像素一致。体积适中,Mac 和 Windows 都能用',+ prores: '剪辑母版:不透明的页面用 ProRes 422 HQ,有透明的用 4444。体积很大',+ prores4444: '剪辑软件通用的母版格式。体积很大:1080p 每分钟约 2.8 GB',+ },projectZip: 'Zip',projectZipHint: '一个文件,方便发送或上传。',projectFolder: '文件夹',@@ -531,6 +539,7 @@ export const zhCN: Words = {cancelling: '正在取消…',failed: '导出失败',draftsRefused: "草稿文件夹拒绝写入(常见于安全软件拦截),草稿已放到导出文件夹:把它移到 {folder} 即可。",+ diskFull: '磁盘空间不足。清理出一些空间后,再导出一次。',notTransparent: '没有透明像素:此元素绘制了自己的背景。',},player: {@@ -915,7 +924,6 @@ export const zhCN: Words = {fullscreen: '全屏',playback: '播放设置',speed: '播放速度',- subtitles: '显示字幕',},inspector: {fades: '淡入淡出',@@ -1401,7 +1409,7 @@ export const zhCN: Words = {normalizing: "正在测量响度…",normalized: "已将 {n} 个片段标准化到 {target} LUFS",normalizedOne: "{clip}:{from} LUFS → {target} LUFS(音量 {pct}%)",- normalizeShort: "{clip} 达不到 {target} LUFS:音量 200% 时为 {lufs} LUFS",+ normalizeShort: "{clip} 达不到 {target} LUFS:音量 500% 时为 {lufs} LUFS",normalizeSilent: "{clip} 没有可测量的声音",normalizeFail: "无法测量 {clip}:{reason}",freeze: "定格帧",diff --git a/packages/openfilm/studio/ui/src/lib/clip-look.test.ts b/packages/openfilm/studio/ui/src/lib/clip-look.test.tsindex 52c3bb1..0e7320f 100644--- a/packages/openfilm/studio/ui/src/lib/clip-look.test.ts+++ b/packages/openfilm/studio/ui/src/lib/clip-look.test.ts@@ -2,7 +2,7 @@ import { test } from 'node:test';import assert from 'node:assert/strict';import {adjustOf, cropCss, cropForRatio, cropOf, cropRatio, declared, fadeCss, fadeOf, framingCss, framingOf, ownedDeclaration, shadowOf,- backdropBlurCss, backdropBlurOf, innerShadowCss, innerShadowOf, placedStrokeCss, placedStrokeOf, radiusCorners, strokeCss, strokeOf, withAdjust, withDeclarations, withShadow,+ backdropBlurCss, backdropBlurOf, innerShadowCss, innerShadowOf, placedStrokeCss, placedStrokeOf, radiusCorners, strokeCss, strokeOf, textColorCss, textColorOf, withAdjust, withDeclarations, withShadow,} from './clip-look.ts';test('a clip\'s own CSS: each control writes its properties, the rest stays as it was', () => {@@ -130,3 +130,19 @@ test('corners from the border-radius shorthand: one to four lengths, filled in aassert.deepEqual(radiusCorners('4px 20px 20px 20px'), [4, 20, 20, 20]);assert.deepEqual(radiusCorners('10px 5px / 2px'), [10, 5, 10, 5]);});++test('words: the color they are drawn in, and a new one written over a fill of their page\'s own', () => {+ const readOf = (style: Record<string, string>) => (prop: string) => style[prop];+ const gold = readOf({ color: '#b39458', '-webkit-text-fill-color': '#c9a86a' });+ assert.equal(textColorOf(gold), '#c9a86a', 'the fill is what shows');+ assert.deepEqual(textColorCss(gold, '#a08b63'), { color: '#a08b63', '-webkit-text-fill-color': 'currentcolor' });+ const plain = readOf({ color: '#ffffff' });+ assert.equal(textColorOf(plain), '#ffffff');+ assert.deepEqual(textColorCss(plain, '#a08b63'), { color: '#a08b63' }, 'no fill of its own: the color alone');+ const taken = readOf({ color: '#a08b63', '-webkit-text-fill-color': 'currentcolor' });+ assert.equal(textColorOf(taken), '#a08b63');+ assert.deepEqual(textColorCss(taken, '#ff0000'), { color: '#ff0000' }, 'taken over once, only the color changes after');+ const gradient = readOf({ color: '#ffffff', '-webkit-text-fill-color': '#00000000' });+ assert.equal(textColorOf(gradient), '#ffffff', 'see-through words (a gradient behind them) show their color');+ assert.deepEqual(textColorCss(gradient, '#a08b63'), { color: '#a08b63', '-webkit-text-fill-color': 'currentcolor' }, 'a color picked fills them');+});diff --git a/packages/openfilm/studio/ui/src/lib/clip-look.ts b/packages/openfilm/studio/ui/src/lib/clip-look.tsindex 57d77de..b565065 100644--- a/packages/openfilm/studio/ui/src/lib/clip-look.ts+++ b/packages/openfilm/studio/ui/src/lib/clip-look.ts@@ -355,6 +355,29 @@ export function placedStrokeCss(s: PlacedStroke | null, was?: PlacedStroke | nul};}+/* ───────────── text color: `color`, or the page's own text fill over it ───────────── */++/**+ * What words are drawn in when their page gives them a fill of its own: `-webkit-text-fill-color` paints the glyphs+ * over `color` (gold or gradient words often have one).+ */+export const TEXT_FILL = '-webkit-text-fill-color';++const ownFill = (fill: string | undefined) => fill != null && fill !== '' && !/^currentcolor$/i.test(fill.trim());+/** A fill no one sees: words showing what is behind them (a gradient clipped to the text). */+const seeThrough = (fill: string) => /^transparent$/i.test(fill.trim()) || /^#[0-9a-f]{6}00$/i.test(fill.trim());++/** The color words are drawn in: their own fill when it shows, else `color`. */+export function textColorOf(read: (prop: string) => string | undefined): string | undefined {+ const fill = read(TEXT_FILL);+ return ownFill(fill) && !seeThrough(fill!) ? fill : read('color');+}++/** Words drawn in `color`: with the page's own fill taken over by it (`currentcolor`), or the new color would not show. */+export function textColorCss(read: (prop: string) => string | undefined, color: string): Record<string, string> {+ return ownFill(read(TEXT_FILL)) ? { color, [TEXT_FILL]: 'currentcolor' } : { color };+}+/* ───────────── inner shadow: an inset box-shadow ───────────── *//** A shadow cast inside the box, over its fill (an inner shadow). */diff --git a/packages/openfilm/studio/ui/src/lib/export-jobs.ts b/packages/openfilm/studio/ui/src/lib/export-jobs.tsindex bb92912..fd7fb9b 100644--- a/packages/openfilm/studio/ui/src/lib/export-jobs.ts+++ b/packages/openfilm/studio/ui/src/lib/export-jobs.ts@@ -10,6 +10,12 @@ type Framing = { frame?: FilmFrameId; fill?: FilmFrameFill; burnSubtitles?: bool/** One export, as the server takes it. `range` is [from, to] in seconds; `name` is what its files are called (the* project's name when there is none). */+/**+ * How an editing-program export writes its pages. A draft: `auto` (a page with no clear pixel in any frame as H.264, the+ * others as lossless PNG), `png`, or `prores` (422 HQ and 4444 the same way). An XML: ProRes 4444 or PNG.+ */+export type NleAlpha = 'auto' | 'png' | 'prores' | 'prores4444';+export type ExportRequest = { range?: [number, number]; label?: string; name?: string } & (| (Framing & { kind: 'video'; codec: ExportVideoCodec; shortEdge?: number; fps: number; quality: ExportQuality; audio: boolean;clip?: string; scale?: number; preset?: string; sidecar?: 'srt' | 'vtt' })@@ -20,7 +26,9 @@ export type ExportRequest = { range?: [number, number]; label?: string; name?: s| { kind: 'subtitles'; format: 'srt' | 'vtt' | 'txt' }| (Framing & { kind: 'slides'; format: 'pptx' | 'pdf'; shortEdge: number; at?: number[] })/* for an editing program: an XML and media folder; `media: 'link'` points at the sources where they are instead of cloning them */- | { kind: 'nle'; format?: 'xml' | 'fcpxml' | 'jianying' | 'capcut'; fps: number; audio: 'clips' | 'stems'; media?: 'copy' | 'link'; subtitles: boolean }+ | { kind: 'nle'; format?: 'xml' | 'fcpxml' | 'jianying' | 'capcut'; fps: number; audio: 'clips' | 'stems'; media?: 'copy' | 'link'; subtitles: boolean;+ /* how the pages, rendered with their alpha, are written */+ alpha?: NleAlpha }/* the project itself: its work, and the parts of its .film asked; a zip of its folder, or the folder */| { kind: 'project'; parts: ProjectPart[]; as: 'zip' | 'folder' });@@ -59,6 +67,8 @@ export interface ExportJob {alpha: { any: boolean } | null;/** A draft's app's drafts folder that refused the write: the draft is in the exports folder instead. */draftsRefused?: string;+ /** The export stopped because the disk is full. */+ diskFull?: boolean;error?: string;/** Cancel was asked and the export is stopping (still `running` until it has). */cancelling?: boolean;diff --git a/packages/openfilm/studio/ui/src/lib/host.ts b/packages/openfilm/studio/ui/src/lib/host.tsindex 4fff05a..746ba5b 100644--- a/packages/openfilm/studio/ui/src/lib/host.ts+++ b/packages/openfilm/studio/ui/src/lib/host.ts@@ -211,7 +211,7 @@ export interface HostUpdateState {/** An update of the app, ready to install or installing; `waiting`: why the last Restart did not go ahead. */export type HostUpdate = { status: 'ready' | 'installing'; version: string; waiting?: 'agent-busy' | 'exporting' | 'install-failed' | null };-export type HostAgentId = 'codex' | 'claude' | 'copilot' | 'cursor' | 'opencode' | 'codebuddy' | 'qwen' | 'kimi' | 'minimax' | 'byok';+export type HostAgentId = 'codex' | 'claude' | 'copilot' | 'cursor' | 'opencode' | 'codebuddy' | 'qwen' | 'kimi' | 'minimax' | 'deepseek' | 'byok';/*** What was searched for an agent (`agents.diagnose`): the report to copy, the program's name, where it was found, and* when it was not, why: chosen-missing (the program chosen is gone) · wsl (only inside WSL, which the app cannot use) ·diff --git a/packages/openfilm/studio/ui/src/lib/look.test.ts b/packages/openfilm/studio/ui/src/lib/look.test.tsnew file mode 100644index 0000000..154dbb7--- /dev/null+++ b/packages/openfilm/studio/ui/src/lib/look.test.ts@@ -0,0 +1,83 @@+import { test } from 'node:test';+import assert from 'node:assert/strict';+import { makeLook, previewSettled, type LookRead, type LookSet } from './look.ts';+import { MIXED } from './mixed.ts';+import { TEXT_FILL, textColorCss } from './clip-look.ts';++type Thing = { id: string; css: Record<string, string> };+const readOf = (t: Thing): LookRead => (prop) => t.css[prop];++/** A look over `things` that records what it is asked to write and to show. */+function watched(things: Thing[]) {+ const commits: [string, LookSet][][] = [];+ const previews: [string, LookSet][][] = [];+ let reverts = 0;+ const look = makeLook(things, readOf, {+ commit: (sets) => commits.push(sets.map(([t, s]) => [t.id, s])),+ preview: (sets) => previews.push(sets.map(([t, s]) => [t.id, s])),+ revert: () => { reverts++; },+ });+ return { look, commits, previews, reverts: () => reverts };+}++test('a look tells a value its things agree on, and MIXED when they do not', () => {+ const a = { id: 'a', css: { opacity: '0.5', color: '#ffffff' } };+ const b = { id: 'b', css: { opacity: '0.5', color: '#000000' } };+ const { look } = watched([a, b]);+ assert.equal(look.n, 2);+ assert.equal(look.common((read) => read('opacity')), '0.5');+ assert.equal(look.common((read) => read('color')), MIXED);+ assert.equal(watched([]).look.common((read) => read('opacity')), MIXED, 'nothing in hand agrees on nothing');+});++test('a commit writes only what it changes, and only for the things it changes', () => {+ const a = { id: 'a', css: { opacity: '1' } };+ const b = { id: 'b', css: { opacity: '0.5' } };+ const { look, commits } = watched([a, b]);+ look.commit(() => ({ opacity: '0.5', filter: 'none' }));+ assert.deepEqual(commits, [[['a', { opacity: '0.5', filter: 'none' }], ['b', { filter: 'none' }]]]);+ look.commit((read) => ({ opacity: read('opacity') ?? null }));+ assert.equal(commits.length, 1, 'a no-op is not an undo step');+});++test('a preview kept by a commit is still written, though the things read as the preview by then', () => {+ const thing = { id: 'a', css: { opacity: '1' } };+ const { look, previews } = watched([thing]);+ look.preview(() => ({ opacity: '0.4' }));+ assert.deepEqual(previews, [[['a', { opacity: '0.4' }]]]);+ /* measured again while previewed: the panel makes a new look over what the picture shows */+ const now = watched([{ id: 'a', css: { opacity: '0.4' } }]);+ now.look.commit(() => ({ opacity: '0.4' }));+ assert.deepEqual(now.commits, [[['a', { opacity: '0.4' }]]]);+});++test('a commit is worked out from before its preview: a text color dragged in the picker keeps taking over the words\' own fill', () => {+ /* gold words: their page paints them with a fill of their own over their color */+ const gold = { id: 'head', css: { color: '#b39458', [TEXT_FILL]: '#b39458' } };+ const { look, previews } = watched([gold]);+ look.preview((read) => textColorCss(read, '#6b5020'));+ assert.deepEqual(previews, [[['head', { color: '#6b5020', [TEXT_FILL]: 'currentcolor' }]]]);+ /* previewed, the words' fill follows their color: measured again, they have no fill of their own */+ const now = watched([{ id: 'head', css: { color: '#6b5020' } }]);+ now.look.preview((read) => textColorCss(read, '#402d07'));+ now.look.commit((read) => textColorCss(read, '#402d07'));+ assert.deepEqual(now.commits, [[['head', { color: '#402d07', [TEXT_FILL]: 'currentcolor' }]]]);+});++test('a preview taken back, or painted back by hand, leaves nothing for the next commit to work from', () => {+ const gold = { id: 'head', css: { color: '#b39458', [TEXT_FILL]: '#b39458' } };+ const first = watched([gold]);+ first.look.preview((read) => textColorCss(read, '#6b5020'));+ first.look.revert();+ assert.equal(first.reverts(), 1);+ const plain = watched([{ id: 'head', css: { color: '#ffffff' } }]);+ plain.look.commit((read) => textColorCss(read, '#ff0000'));+ assert.deepEqual(plain.commits, [[['head', { color: '#ff0000' }]]], 'reverted: the fill from before is not used');++ const second = watched([gold]);+ second.look.preview((read) => textColorCss(read, '#6b5020'));+ previewSettled();+ const again = watched([{ id: 'head', css: { color: '#ffffff' } }]);+ again.look.commit((read) => textColorCss(read, '#ff0000'));+ assert.deepEqual(again.commits, [[['head', { color: '#ff0000' }]]], 'settled: the fill from before is not used');+});diff --git a/packages/openfilm/studio/ui/src/lib/look.ts b/packages/openfilm/studio/ui/src/lib/look.tsnew file mode 100644index 0000000..c7155fc--- /dev/null+++ b/packages/openfilm/studio/ui/src/lib/look.ts@@ -0,0 +1,83 @@+/**+ * The inspector's look controls over one or several things (layers inside a page, or clips): what each reads of their+ * CSS, and how an edit is shown (previewed) and written (committed) for each of them.+ */+import { MIXED, type Mixed } from './mixed.ts';++/** One thing's CSS property (kebab-case), as it is now. */+export type LookRead = (prop: string) => string | undefined;+/** Properties to write: a value, or null — "none of mine" (the panel decides what that leaves). */+export type LookSet = Record<string, string | null>;+/** What a control does to one thing, from that thing's own values. */+export type LookEdit = (read: LookRead) => LookSet;++export interface Look {+ /** How many things it is. */+ n: number;+ /** What `fn` makes of every thing's values, when they agree; MIXED when they do not. */+ common<V>(fn: (read: LookRead) => V): V | Mixed;+ /** Shows an edit on the picture, nothing written. */+ preview(edit: LookEdit): void;+ /** Writes an edit, as one change. */+ commit(edit: LookEdit): void;+ /** Takes a preview back off the picture. */+ revert(): void;+}++/**+ * A Look over `targets`: read each through `readOf`, and hand each one's properties to `write` (the panel stores+ * them). `write` gets only the things an edit changes.+ */+export function makeLook<Tg>(+ targets: readonly Tg[],+ readOf: (t: Tg) => LookRead,+ write: {+ commit: (sets: [Tg, LookSet][]) => void;+ preview?: (sets: [Tg, LookSet][]) => void;+ revert?: () => void;+ },+): Look {+ const reads = targets.map(readOf);+ const sets = (edit: LookEdit, kept?: ReadonlyMap<string, string | undefined>[] | null): [Tg, LookSet][] => targets.flatMap((tg, i) => {+ const read = reads[i]!;+ /* what was there before a preview: the layer is measured again while previewed, so by now it reads as the+ preview. The edit is worked out from it (a preview may have changed what the edit decides by, as text color+ taking over the words' own fill), and unchanged properties are not written: the agent reads the diff, a no-op+ would be an undo step, and a kept preview would read as no change */+ const was = (k: string) => (kept?.[i]?.has(k) ? kept[i]!.get(k) : read(k));+ const set = edit(was);+ const changed = Object.entries(set).filter(([k, v]) => (v ?? undefined) !== was(k));+ return changed.length ? [[tg, Object.fromEntries(changed)] as [Tg, LookSet]] : [];+ });+ return {+ n: targets.length,+ common(fn) {+ if (!reads.length) return MIXED;+ const first = fn(reads[0]!);+ const key = JSON.stringify(first);+ return reads.every((r) => JSON.stringify(fn(r)) === key) ? first : MIXED;+ },+ /* shown as said, unchanged or not: a layer reads as its kept change while a preview paints over it, so painting+ back what was there would read as no change */+ preview(edit) {+ if (!previewedBefore || previewedBefore.length !== targets.length) previewedBefore = targets.map(() => new Map());+ const list = targets.flatMap((tg, i) => {+ const set = edit(reads[i]!);+ for (const k of Object.keys(set)) if (!previewedBefore![i]!.has(k)) previewedBefore![i]!.set(k, reads[i]!(k));+ return Object.keys(set).length ? [[tg, set] as [Tg, LookSet]] : [];+ });+ write.preview?.(list);+ },+ commit(edit) {+ const list = sets(edit, previewedBefore);+ previewedBefore = null;+ if (list.length) write.commit(list);+ },+ revert() { previewedBefore = null; write.revert?.(); },+ };+}++/** What each previewed property of the things in hand was before its first preview (see makeLook's `sets`). */+let previewedBefore: Map<string, string | undefined>[] | null = null;+/** A preview was painted back by hand (as a preview of what was there): nothing previewed stands any more. */+export const previewSettled = () => { previewedBefore = null; };diff --git a/packages/openfilm/studio/ui/src/lib/loudness.test.ts b/packages/openfilm/studio/ui/src/lib/loudness.test.tsindex 5af6bf3..31ec0aa 100644--- a/packages/openfilm/studio/ui/src/lib/loudness.test.ts+++ b/packages/openfilm/studio/ui/src/lib/loudness.test.ts@@ -1,6 +1,6 @@import { test } from 'node:test';import assert from 'node:assert/strict';-import { measuredSpan, normalizeVolume } from './loudness.ts';+import { dbToVolume, measuredSpan, normalizeVolume, VOLUME_DB_MAX, VOLUME_DB_MIN, volumeToDb } from './loudness.ts';test('a clip 6 dB too quiet gets twice the gain; one too loud less', () => {assert.deepEqual(normalizeVolume(-20, -14), { volume: 1.995, reached: true });@@ -8,11 +8,31 @@ test('a clip 6 dB too quiet gets twice the gain; one too loud less', () => {assert.deepEqual(normalizeVolume(-16, -16), { volume: 1, reached: true });});-test('past 200% the clip gets the most it can, and says the loudness it reaches', () => {+test('a clip 13 dB too quiet, as a voice recorded on a phone often is, reaches the target', () => {+ assert.deepEqual(normalizeVolume(-27, -14), { volume: 4.467, reached: true });+});++test('past 500% the clip gets the most it can, and says the loudness it reaches', () => {const plan = normalizeVolume(-30, -14);- assert.equal(plan.volume, 2);+ assert.equal(plan.volume, 5);assert.equal('reached' in plan && plan.reached, false);- assert.ok('lufs' in plan && Math.abs(plan.lufs - -24) < 0.01, JSON.stringify(plan));+ assert.ok('lufs' in plan && Math.abs(plan.lufs - -16) < 0.01, JSON.stringify(plan));+});++test('the volume slider in dB: 0 dB as is, +6 dB twice, the bottom silent, the top 500%', () => {+ assert.equal(VOLUME_DB_MAX, 14);+ assert.equal(volumeToDb(1), 0);+ assert.equal(volumeToDb(2), 6);+ assert.equal(volumeToDb(0.5), -6);+ assert.equal(volumeToDb(5), 14);+ assert.equal(volumeToDb(0), VOLUME_DB_MIN);+ assert.equal(dbToVolume(0), 1);+ assert.equal(dbToVolume(6), 1.995);+ assert.equal(dbToVolume(-6), 0.501);+ assert.equal(dbToVolume(14), 5);+ assert.equal(dbToVolume(VOLUME_DB_MIN), 0);+ /* what the slider writes it shows again */+ for (const db of [-40, -12.5, -0.1, 0.1, 3, 9.9]) assert.equal(volumeToDb(dbToVolume(db)), db);});test('silence has no loudness to bring anywhere', () => {diff --git a/packages/openfilm/studio/ui/src/lib/loudness.ts b/packages/openfilm/studio/ui/src/lib/loudness.tsindex dbc552b..8533e7e 100644--- a/packages/openfilm/studio/ui/src/lib/loudness.ts+++ b/packages/openfilm/studio/ui/src/lib/loudness.ts@@ -1,7 +1,7 @@/*** Loudness normalization: a clip's `volume` set so the part of its file it plays measures a target loudness. Studio* measures the integrated loudness of that part (EBU R128, LUFS; studio/server/derived.mjs `loudness`); the gain is- * the difference, within the volume a clip can have (0–200%, the inspector's range).+ * the difference, within the volume a clip can have (0–500%, the inspector's range).*//** The targets offered: web video, podcasts, broadcast (EBU R128). */@@ -11,8 +11,25 @@ export const LOUDNESS_TARGETS = [{ id: 'broadcast', lufs: -23 },] as const;-/** The loudest a clip's volume goes (200%), as the inspector's slider. */-export const VOLUME_MAX = 2;+/** The loudest a clip's volume goes (500%), as the inspector's slider. */+export const VOLUME_MAX = 5;++/** The volume slider's range in dB: its bottom is silence, its top the loudest a clip goes (500%, +14 dB). */+export const VOLUME_DB_MIN = -60;+export const VOLUME_DB_MAX = Math.round(20 * Math.log10(VOLUME_MAX) * 10) / 10;++/** A clip's volume as the slider shows it: dB to the tenth, silence at the bottom. */+export function volumeToDb(volume: number): number {+ if (!(volume > 0)) return VOLUME_DB_MIN;+ const db = Math.round(20 * Math.log10(volume) * 10) / 10;+ return Math.max(VOLUME_DB_MIN, Math.min(VOLUME_DB_MAX, db));+}++/** The volume a dB on the slider writes, to the thousandth: silent at its bottom, at most 500%. */+export function dbToVolume(db: number): number {+ if (!(db > VOLUME_DB_MIN)) return 0;+ return Math.round(Math.min(VOLUME_MAX, 10 ** (db / 20)) * 1000) / 1000;+}/** Quieter than this is silence: EBU R128 gates out anything under −70 LUFS. */const SILENT_LUFS = -70;@@ -23,7 +40,7 @@ export type NormalizePlan =| { volume: number; reached: false; wantedVolume: number; lufs: number }| { silent: true };-/** The volume that brings `measured` LUFS to `target` LUFS, kept to 0–200%, to the thousandth. */+/** The volume that brings `measured` LUFS to `target` LUFS, kept to 0–500%, to the thousandth. */export function normalizeVolume(measured: number | null, target: number): NormalizePlan {if (measured == null || !Number.isFinite(measured) || measured <= SILENT_LUFS) return { silent: true };const wanted = 10 ** ((target - measured) / 20);diff --git a/packages/openfilm/studio/ui/src/lib/mixed.ts b/packages/openfilm/studio/ui/src/lib/mixed.tsnew file mode 100644index 0000000..0e85d3c--- /dev/null+++ b/packages/openfilm/studio/ui/src/lib/mixed.ts@@ -0,0 +1,3 @@+/** Several things selected that differ in a value. */+export const MIXED: unique symbol = Symbol('mixed');+export type Mixed = typeof MIXED;diff --git a/packages/openfilm/studio/ui/src/lib/provider-marks.ts b/packages/openfilm/studio/ui/src/lib/provider-marks.tsindex c118afb..0498bdf 100644--- a/packages/openfilm/studio/ui/src/lib/provider-marks.ts+++ b/packages/openfilm/studio/ui/src/lib/provider-marks.ts@@ -17,6 +17,7 @@ export const MARKS: Record<string, { viewBox: string; d: string; parts?: { d: stcodebuddy: { viewBox: '0 0 24 24', d: 'M18.777 1.647c.28-.02.536.114.972.51 1.018.926 2.437 2.828 3.318 4.452l.34.631.482.24.11.06v3.638a5.206 5.206 0 00-5.32-1.23c-.491.166-1.021.471-2.08 1.082l-6.09 3.516c-1.057.61-1.586.916-1.975 1.259a5.208 5.208 0 00-1.493 5.572c.165.49.471 1.02 1.082 2.08l.315.543h-3.26c-.685 0-1.34-.135-1.939-.377-.169-.956-.009-1.789.469-2.335.158-.18.164-.189.13-.493a11.846 11.846 0 01-.057-1.711l.02-.444-.667-1.18C2.1 15.622 1.445 14.078 1.192 12.9c-.133-.647-.125-.934.04-1.146.1-.128.427-.261.822-.334.994-.175 3.162-.017 5.575.41l.25.043.551-.487c.915-.81 1.522-1.264 2.641-1.962 1.167-.73 2.484-1.331 3.967-1.807l.476-.152.261-.688c.937-2.471 1.896-4.293 2.58-4.9.235-.21.25-.22.422-.23zM12.139 18.2a1.203 1.203 0 011.642.44l1.296 2.243a1.204 1.204 0 01-2.083 1.203l-1.296-2.243a1.203 1.203 0 01.44-1.644zM18.629 14.452a1.203 1.203 0 011.642.44l1.295 2.244a1.203 1.203 0 11-2.083 1.203l-1.295-2.243a1.203 1.203 0 01.44-1.644z' },qwen: { viewBox: '0 0 24 24', d: 'M12.604 1.34c.393.69.784 1.382 1.174 2.075a.18.18 0 00.157.091h5.552c.174 0 .322.11.446.327l1.454 2.57c.19.337.24.478.024.837-.26.43-.513.864-.76 1.3l-.367.658c-.106.196-.223.28-.04.512l2.652 4.637c.172.301.111.494-.043.77-.437.785-.882 1.564-1.335 2.34-.159.272-.352.375-.68.37-.777-.016-1.552-.01-2.327.016a.099.099 0 00-.081.05 575.097 575.097 0 01-2.705 4.74c-.169.293-.38.363-.725.364-.997.003-2.002.004-3.017.002a.537.537 0 01-.465-.271l-1.335-2.323a.09.09 0 00-.083-.049H4.982c-.285.03-.553-.001-.805-.092l-1.603-2.77a.543.543 0 01-.002-.54l1.207-2.12a.198.198 0 000-.197 550.951 550.951 0 01-1.875-3.272l-.79-1.395c-.16-.31-.173-.496.095-.965.465-.813.927-1.625 1.387-2.436.132-.234.304-.334.584-.335a338.3 338.3 0 012.589-.001.124.124 0 00.107-.063l2.806-4.895a.488.488 0 01.422-.246c.524-.001 1.053 0 1.583-.006L11.704 1c.341-.003.724.032.9.34zm-3.432.403a.06.06 0 00-.052.03L6.254 6.788a.157.157 0 01-.135.078H3.253c-.056 0-.07.025-.041.074l5.81 10.156c.025.042.013.062-.034.063l-2.795.015a.218.218 0 00-.2.116l-1.32 2.31c-.044.078-.021.118.068.118l5.716.008c.046 0 .08.02.104.061l1.403 2.454c.046.081.092.082.139 0l5.006-8.76.783-1.382a.055.055 0 01.096 0l1.424 2.53a.122.122 0 00.107.062l2.763-.02a.04.04 0 00.035-.02.041.041 0 000-.04l-2.9-5.086a.108.108 0 010-.113l.293-.507 1.12-1.977c.024-.041.012-.062-.035-.062H9.2c-.059 0-.073-.026-.043-.077l1.434-2.505a.107.107 0 000-.114L9.225 1.774a.06.06 0 00-.053-.031zm6.29 8.02c.046 0 .058.02.034.06l-.832 1.465-2.613 4.585a.056.056 0 01-.05.029.058.058 0 01-.05-.029L8.498 9.841c-.02-.034-.01-.052.028-.054l.216-.012 6.722-.012z' },kimi: { viewBox: '0 0 24 24', d: 'M21.846 0a1.923 1.923 0 110 3.846H20.15a.226.226 0 01-.227-.226V1.923C19.923.861 20.784 0 21.846 0zM11.065 11.199l7.257-7.2c.137-.136.06-.41-.116-.41H14.3a.164.164 0 00-.117.051l-7.82 7.756c-.122.12-.302.013-.302-.179V3.82c0-.127-.083-.23-.185-.23H3.186c-.103 0-.186.103-.186.23V19.77c0 .128.083.23.186.23h2.69c.103 0 .186-.102.186-.23v-3.25c0-.069.025-.135.069-.178l2.424-2.406a.158.158 0 01.205-.023l6.484 4.772a7.677 7.677 0 003.453 1.283c.108.012.2-.095.2-.23v-3.06c0-.117-.07-.212-.164-.227a5.028 5.028 0 01-2.027-.807l-5.613-4.064c-.117-.078-.132-.279-.028-.381z' },+ deepseek: { viewBox: '0 0 24 24', d: 'M23.748 4.482c-.254-.124-.364.113-.512.234-.051.039-.094.09-.137.136-.372.397-.806.657-1.373.626-.829-.046-1.537.214-2.163.848-.133-.782-.575-1.248-1.247-1.548-.352-.156-.708-.311-.955-.65-.172-.241-.219-.51-.305-.774-.055-.16-.11-.323-.293-.35-.2-.031-.278.136-.356.276-.313.572-.434 1.202-.422 1.84.027 1.436.633 2.58 1.838 3.393.137.093.172.187.129.323-.082.28-.18.552-.266.833-.055.179-.137.217-.329.14a5.526 5.526 0 01-1.736-1.18c-.857-.828-1.631-1.742-2.597-2.458a11.365 11.365 0 00-.689-.471c-.985-.957.13-1.743.388-1.836.27-.098.093-.432-.779-.428-.872.004-1.67.295-2.687.684a3.055 3.055 0 01-.465.137 9.597 9.597 0 00-2.883-.102c-1.885.21-3.39 1.102-4.497 2.623C.082 8.606-.231 10.684.152 12.85c.403 2.284 1.569 4.175 3.36 5.653 1.858 1.533 3.997 2.284 6.438 2.14 1.482-.085 3.133-.284 4.994-1.86.47.234.962.327 1.78.397.63.059 1.236-.03 1.705-.128.735-.156.684-.837.419-.961-2.155-1.004-1.682-.595-2.113-.926 1.096-1.296 2.746-2.642 3.392-7.003.05-.347.007-.565 0-.845-.004-.17.035-.237.23-.256a4.173 4.173 0 001.545-.475c1.396-.763 1.96-2.015 2.093-3.517.02-.23-.004-.467-.247-.588zM11.581 18c-2.089-1.642-3.102-2.183-3.52-2.16-.392.024-.321.471-.235.763.09.288.207.486.371.739.114.167.192.416-.113.603-.673.416-1.842-.14-1.897-.167-1.361-.802-2.5-1.86-3.301-3.307-.774-1.393-1.224-2.887-1.298-4.482-.02-.386.093-.522.477-.592a4.696 4.696 0 011.529-.039c2.132.312 3.946 1.265 5.468 2.774.868.86 1.525 1.887 2.202 2.891.72 1.066 1.494 2.082 2.48 2.914.348.292.625.514.891.677-.802.09-2.14.11-3.054-.614zm1-6.44a.306.306 0 01.415-.287.302.302 0 01.2.288.306.306 0 01-.31.307.303.303 0 01-.304-.308zm3.11 1.596c-.2.081-.399.151-.59.16a1.245 1.245 0 01-.798-.254c-.274-.23-.47-.358-.552-.758a1.73 1.73 0 01.016-.588c.07-.327-.008-.537-.239-.727-.187-.156-.426-.199-.688-.199a.559.559 0 01-.254-.078c-.11-.054-.2-.19-.114-.358.028-.054.16-.186.192-.21.356-.202.767-.136 1.146.016.352.144.618.408 1.001.782.391.451.462.576.685.914.176.265.336.537.445.848.067.195-.019.354-.25.452z' },minimax: { viewBox: '0 0 24 24', d: 'M11.43 3.92a.86.86 0 1 0-1.718 0v14.236a1.999 1.999 0 0 1-3.997 0V9.022a.86.86 0 1 0-1.718 0v3.87a1.999 1.999 0 0 1-3.997 0V11.49a.57.57 0 0 1 1.139 0v1.404a.86.86 0 0 0 1.719 0V9.022a1.999 1.999 0 0 1 3.997 0v9.134a.86.86 0 0 0 1.719 0V3.92a1.998 1.998 0 1 1 3.996 0v11.788a.57.57 0 1 1-1.139 0zm10.572 3.105a2 2 0 0 0-1.999 1.997v7.63a.86.86 0 0 1-1.718 0V3.923a1.999 1.999 0 0 0-3.997 0v16.16a.86.86 0 0 1-1.719 0V18.08a.57.57 0 1 0-1.138 0v2a1.998 1.998 0 0 0 3.996 0V3.92a.86.86 0 0 1 1.719 0v12.73a1.999 1.999 0 0 0 3.996 0V9.023a.86.86 0 1 1 1.72 0v6.686a.57.57 0 0 0 1.138 0V9.022a2 2 0 0 0-1.998-1.997' },fal: { viewBox: '0 0 24 24', d: 'M15.477 0c.415 0 .749.338.788.752a7.775 7.775 0 006.985 6.984c.413.04.752.373.752.788v6.952c0 .415-.338.748-.752.788a7.775 7.775 0 00-6.985 6.984c-.04.414-.373.752-.788.752H8.525c-.416 0-.749-.338-.789-.752a7.775 7.775 0 00-6.984-6.984c-.414-.04-.752-.373-.752-.788V8.524c0-.415.338-.748.752-.788A7.775 7.775 0 007.736.752C7.776.338 8.11 0 8.526 0h6.95zM4.819 11.98a7.226 7.226 0 007.223 7.23 7.226 7.226 0 007.223-7.23c0-3.994-3.234-7.23-7.223-7.23a7.227 7.227 0 00-7.223 7.23z' },brave: { viewBox: '0 0 24 24', d: 'M15.68 0l2.096 2.38s1.84-.512 2.709.358c.868.87 1.584 1.638 1.584 1.638l-.562 1.381.715 2.047s-2.104 7.98-2.35 8.955c-.486 1.919-.818 2.66-2.198 3.633-1.38.972-3.884 2.66-4.293 2.916-.409.256-.92.692-1.38.692-.46 0-.97-.436-1.38-.692a185.796 185.796 0 01-4.293-2.916c-1.38-.973-1.712-1.714-2.197-3.633-.247-.975-2.351-8.955-2.351-8.955l.715-2.047-.562-1.381s.716-.768 1.585-1.638c.868-.87 2.708-.358 2.708-.358L8.321 0h7.36zm-3.679 14.936c-.14 0-1.038.317-1.758.69-.72.373-1.242.637-1.409.742-.167.104-.065.301.087.409.152.107 2.194 1.69 2.393 1.866.198.175.489.464.687.464.198 0 .49-.29.688-.464.198-.175 2.24-1.759 2.392-1.866.152-.108.254-.305.087-.41-.167-.104-.689-.368-1.41-.741-.72-.373-1.617-.69-1.757-.69zm0-11.278s-.409.001-1.022.206-1.278.46-1.584.46c-.307 0-2.581-.434-2.581-.434S4.119 7.152 4.119 7.849c0 .697.339.881.68 1.243l2.02 2.149c.192.203.59.511.356 1.066-.235.555-.58 1.26-.196 1.977.384.716 1.042 1.194 1.464 1.115.421-.08 1.412-.598 1.776-.834.364-.237 1.518-1.19 1.518-1.554 0-.365-1.193-1.02-1.413-1.168-.22-.15-1.226-.725-1.247-.95-.02-.227-.012-.293.284-.851.297-.559.831-1.304.742-1.8-.089-.495-.95-.753-1.565-.986-.615-.232-1.799-.671-1.947-.74-.148-.068-.11-.133.339-.175.448-.043 1.719-.212 2.292-.052.573.16 1.552.403 1.632.532.079.13.149.134.067.579-.081.445-.5 2.581-.541 2.96-.04.38-.12.63.288.724.409.094 1.097.256 1.333.256s.924-.162 1.333-.256c.408-.093.329-.344.288-.723-.04-.38-.46-2.516-.541-2.961-.082-.445-.012-.45.067-.579.08-.129 1.059-.372 1.632-.532.573-.16 1.845.009 2.292.052.449.042.487.107.339.175-.148.069-1.332.508-1.947.74-.615.233-1.476.49-1.565.986-.09.496.445 1.241.742 1.8.297.558.304.624.284.85-.02.226-1.026.802-1.247.95-.22.15-1.413.804-1.413 1.169 0 .364 1.154 1.317 1.518 1.554.364.236 1.355.755 1.776.834.422.079 1.08-.4 1.464-1.115.384-.716.039-1.422-.195-1.977-.235-.555.163-.863.355-1.066l2.02-2.149c.341-.362.68-.546.68-1.243 0-.697-2.695-3.96-2.695-3.96s-2.274.436-2.58.436c-.307 0-.972-.256-1.585-.461-.613-.205-1.022-.206-1.022-.206z' },