@sixb/core 0.2.7 → 0.2.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/actions/index.js +52 -49
- package/dist/actions/index.js.map +1 -1
- package/dist/actions/request.d.ts.map +1 -1
- package/dist/actions/run/context.d.ts.map +1 -1
- package/dist/actions/run/execute.d.ts +3 -0
- package/dist/actions/run/execute.d.ts.map +1 -1
- package/dist/actions/run/executor.d.ts +3 -0
- package/dist/actions/run/executor.d.ts.map +1 -1
- package/dist/actions/run/normalize.d.ts.map +1 -1
- package/dist/actions/run/types.d.ts +6 -0
- package/dist/actions/run/types.d.ts.map +1 -1
- package/dist/actions/types/context.d.ts +7 -0
- package/dist/actions/types/context.d.ts.map +1 -1
- package/dist/agents/context/entries.d.ts +17 -0
- package/dist/agents/context/entries.d.ts.map +1 -1
- package/dist/agents/context/index.js +3 -2
- package/dist/agents/context/index.js.map +1 -1
- package/dist/agents/context-model.d.ts.map +1 -1
- package/dist/agents/context-resolution.d.ts.map +1 -1
- package/dist/agents/execution.d.ts +7 -0
- package/dist/agents/execution.d.ts.map +1 -1
- package/dist/agents/index.js +57 -54
- package/dist/agents/index.js.map +1 -1
- package/dist/agents/model-call-retry.d.ts +28 -0
- package/dist/agents/model-call-retry.d.ts.map +1 -0
- package/dist/agents/model-loop.d.ts +4 -2
- package/dist/agents/model-loop.d.ts.map +1 -1
- package/dist/agents/run-artifacts.d.ts +2 -0
- package/dist/agents/run-artifacts.d.ts.map +1 -1
- package/dist/agents/run-plan.d.ts +1 -1
- package/dist/agents/run-plan.d.ts.map +1 -1
- package/dist/agents/run-session-http.d.ts.map +1 -1
- package/dist/agents/run-session-serve.d.ts.map +1 -1
- package/dist/agents/run-session-wire.d.ts +6 -0
- package/dist/agents/run-session-wire.d.ts.map +1 -1
- package/dist/agents/thread-access.d.ts +13 -0
- package/dist/agents/thread-access.d.ts.map +1 -1
- package/dist/agents/tool-files.d.ts.map +1 -1
- package/dist/artifacts/caller.d.ts +10 -0
- package/dist/artifacts/caller.d.ts.map +1 -0
- package/dist/artifacts/comment-place.d.ts +26 -0
- package/dist/artifacts/comment-place.d.ts.map +1 -0
- package/dist/artifacts/comment-text.d.ts +12 -4
- package/dist/artifacts/comment-text.d.ts.map +1 -1
- package/dist/artifacts/comments.d.ts +8 -2
- package/dist/artifacts/comments.d.ts.map +1 -1
- package/dist/artifacts/errors.d.ts +1 -1
- package/dist/artifacts/errors.d.ts.map +1 -1
- package/dist/artifacts/execution.d.ts +73 -5
- package/dist/artifacts/execution.d.ts.map +1 -1
- package/dist/artifacts/frames/assets.d.ts +27 -7
- package/dist/artifacts/frames/assets.d.ts.map +1 -1
- package/dist/artifacts/frames/entries.d.ts +5 -4
- package/dist/artifacts/frames/entries.d.ts.map +1 -1
- package/dist/artifacts/frames/hyperframes.d.ts +12 -0
- package/dist/artifacts/frames/hyperframes.d.ts.map +1 -0
- package/dist/artifacts/frames/index.d.ts +3 -1
- package/dist/artifacts/frames/index.d.ts.map +1 -1
- package/dist/artifacts/frames/index.js +20 -6
- package/dist/artifacts/frames/index.js.map +3 -3
- package/dist/artifacts/frames/kit.d.ts.map +1 -1
- package/dist/artifacts/frames/protocol.d.ts +33 -0
- package/dist/artifacts/frames/protocol.d.ts.map +1 -1
- package/dist/artifacts/frames/video-runtime.d.ts +21 -0
- package/dist/artifacts/frames/video-runtime.d.ts.map +1 -0
- package/dist/artifacts/html.d.ts +23 -0
- package/dist/artifacts/html.d.ts.map +1 -1
- package/dist/artifacts/index.d.ts +5 -3
- package/dist/artifacts/index.d.ts.map +1 -1
- package/dist/artifacts/index.js +71 -10
- package/dist/artifacts/index.js.map +4 -4
- package/dist/artifacts/kinds.d.ts +1 -1
- package/dist/artifacts/kinds.d.ts.map +1 -1
- package/dist/artifacts/limits.d.ts +37 -0
- package/dist/artifacts/limits.d.ts.map +1 -1
- package/dist/artifacts/live/document.d.ts.map +1 -1
- package/dist/artifacts/live/index.d.ts +6 -2
- package/dist/artifacts/live/index.d.ts.map +1 -1
- package/dist/artifacts/live/index.js +3432 -13
- package/dist/artifacts/live/index.js.map +6 -3
- package/dist/artifacts/live/presence.d.ts +7 -5
- package/dist/artifacts/live/presence.d.ts.map +1 -1
- package/dist/artifacts/live/protocol.d.ts +4 -2
- package/dist/artifacts/live/protocol.d.ts.map +1 -1
- package/dist/artifacts/live/server.d.ts +1 -1
- package/dist/artifacts/live/server.d.ts.map +1 -1
- package/dist/artifacts/live/server.js +36 -31
- package/dist/artifacts/live/server.js.map +1 -1
- package/dist/artifacts/live/state.d.ts +2 -2
- package/dist/artifacts/media/media.d.ts +96 -0
- package/dist/artifacts/media/media.d.ts.map +1 -0
- package/dist/artifacts/media/transcriber.d.ts +29 -0
- package/dist/artifacts/media/transcriber.d.ts.map +1 -0
- package/dist/artifacts/outline.d.ts +26 -1
- package/dist/artifacts/outline.d.ts.map +1 -1
- package/dist/artifacts/publish/files.d.ts +5 -2
- package/dist/artifacts/publish/files.d.ts.map +1 -1
- package/dist/artifacts/publish/index.d.ts +11 -2
- package/dist/artifacts/publish/index.d.ts.map +1 -1
- package/dist/artifacts/publish/index.js +110 -33
- package/dist/artifacts/publish/index.js.map +1 -1
- package/dist/artifacts/publish/pdf.d.ts.map +1 -1
- package/dist/artifacts/publish/previews.d.ts +7 -2
- package/dist/artifacts/publish/previews.d.ts.map +1 -1
- package/dist/artifacts/publish/publish.d.ts.map +1 -1
- package/dist/artifacts/publish/validate/html.d.ts +5 -4
- package/dist/artifacts/publish/validate/html.d.ts.map +1 -1
- package/dist/artifacts/publish/validate/video.d.ts +36 -0
- package/dist/artifacts/publish/validate/video.d.ts.map +1 -0
- package/dist/artifacts/publish/video.d.ts +79 -0
- package/dist/artifacts/publish/video.d.ts.map +1 -0
- package/dist/artifacts/resolve.d.ts +9 -0
- package/dist/artifacts/resolve.d.ts.map +1 -1
- package/dist/artifacts/video/composition.d.ts +183 -0
- package/dist/artifacts/video/composition.d.ts.map +1 -0
- package/dist/artifacts/video/edit.d.ts +298 -0
- package/dist/artifacts/video/edit.d.ts.map +1 -0
- package/dist/artifacts/video/export.d.ts +142 -0
- package/dist/artifacts/video/export.d.ts.map +1 -0
- package/dist/artifacts/video/looks.d.ts +133 -0
- package/dist/artifacts/video/looks.d.ts.map +1 -0
- package/dist/artifacts/video/project.d.ts +74 -0
- package/dist/artifacts/video/project.d.ts.map +1 -0
- package/dist/artifacts/video/stills.d.ts +65 -0
- package/dist/artifacts/video/stills.d.ts.map +1 -0
- package/dist/artifacts/video/timeline.d.ts +153 -0
- package/dist/artifacts/video/timeline.d.ts.map +1 -0
- package/dist/auth/index.js +9 -8
- package/dist/auth/index.js.map +1 -1
- package/dist/authorization/index.js +5 -4
- package/dist/authorization/index.js.map +1 -1
- package/dist/blob-storage/browser.d.ts +1 -1
- package/dist/blob-storage/browser.d.ts.map +1 -1
- package/dist/blob-storage/browser.js +8 -4
- package/dist/blob-storage/browser.js.map +1 -1
- package/dist/blob-storage/constants.d.ts +8 -3
- package/dist/blob-storage/constants.d.ts.map +1 -1
- package/dist/blob-storage/download-urls.d.ts +2 -2
- package/dist/blob-storage/download-urls.d.ts.map +1 -1
- package/dist/blob-storage/index.d.ts +1 -1
- package/dist/blob-storage/index.d.ts.map +1 -1
- package/dist/blob-storage/index.js +9 -5
- package/dist/blob-storage/index.js.map +1 -1
- package/dist/blob-storage/internal.d.ts +2 -2
- package/dist/blob-storage/internal.d.ts.map +1 -1
- package/dist/blob-storage/internal.js +10 -4
- package/dist/blob-storage/internal.js.map +1 -1
- package/dist/blob-storage/media-type.d.ts +12 -3
- package/dist/blob-storage/media-type.d.ts.map +1 -1
- package/dist/bootstrap/index.js +61 -58
- package/dist/bootstrap/index.js.map +2 -2
- package/dist/{chunk-az95tsh3.js → chunk-0fjryajn.js} +3 -2
- package/dist/chunk-0fjryajn.js.map +11 -0
- package/dist/{chunk-9tpmfh3n.js → chunk-0mk8qqp9.js} +3 -3
- package/dist/{chunk-9tpmfh3n.js.map → chunk-0mk8qqp9.js.map} +3 -3
- package/dist/{chunk-88xeh6zx.js → chunk-1g4me5f2.js} +182 -7
- package/dist/{chunk-88xeh6zx.js.map → chunk-1g4me5f2.js.map} +3 -3
- package/dist/{chunk-7mhbs9yy.js → chunk-1z8ccj53.js} +1 -1
- package/dist/{chunk-wkv3h3q7.js → chunk-2138b0ng.js} +115 -8
- package/dist/chunk-2138b0ng.js.map +12 -0
- package/dist/chunk-23eaayw4.js +3757 -0
- package/dist/chunk-23eaayw4.js.map +22 -0
- package/dist/{chunk-m1cwncee.js → chunk-2mxq1yd9.js} +45 -40
- package/dist/{chunk-m1cwncee.js.map → chunk-2mxq1yd9.js.map} +3 -3
- package/dist/{chunk-dmnd9ea6.js → chunk-30a0ftnm.js} +2 -2
- package/dist/chunk-389ybvw9.js.map +2 -2
- package/dist/{chunk-qbd282aa.js → chunk-39q9ybav.js} +2 -2
- package/dist/{chunk-06kpaqg4.js → chunk-3d9w8t4n.js} +110 -22
- package/dist/{chunk-06kpaqg4.js.map → chunk-3d9w8t4n.js.map} +5 -5
- package/dist/chunk-3dvcdn9w.js +8 -0
- package/dist/chunk-3dvcdn9w.js.map +10 -0
- package/dist/{chunk-fdk0ks9p.js → chunk-3x902cja.js} +3 -3
- package/dist/{chunk-c4nrycdy.js → chunk-4q4a5a96.js} +1 -1
- package/dist/{chunk-9cyq87qa.js → chunk-4tdm3fsj.js} +95 -32
- package/dist/{chunk-9cyq87qa.js.map → chunk-4tdm3fsj.js.map} +9 -9
- package/dist/{chunk-cpy282b8.js → chunk-4w8qzy9w.js} +6 -6
- package/dist/{chunk-c29729me.js → chunk-4z9xtxke.js} +8 -7
- package/dist/chunk-4z9xtxke.js.map +12 -0
- package/dist/chunk-5278qwax.js +270 -0
- package/dist/chunk-5278qwax.js.map +11 -0
- package/dist/{chunk-k1g14kk3.js → chunk-6a4hp405.js} +4 -5
- package/dist/{chunk-k1g14kk3.js.map → chunk-6a4hp405.js.map} +3 -3
- package/dist/{chunk-xavet0qs.js → chunk-6whr25gw.js} +2 -2
- package/dist/{chunk-mbwsgjn7.js → chunk-7zygtcqq.js} +14 -2
- package/dist/chunk-7zygtcqq.js.map +11 -0
- package/dist/chunk-88s9z5pf.js +3 -0
- package/dist/{chunk-de5sfbc3.js.map → chunk-88s9z5pf.js.map} +1 -1
- package/dist/{chunk-ygvay56w.js → chunk-896te58g.js} +2 -2
- package/dist/{chunk-pfdq1ga6.js → chunk-8eqqqc1h.js} +3 -3
- package/dist/{chunk-yyj009tv.js → chunk-8tefbhdx.js} +14 -14
- package/dist/{chunk-yyj009tv.js.map → chunk-8tefbhdx.js.map} +1 -1
- package/dist/{chunk-jka26v8c.js → chunk-8x1bsp68.js} +1 -1
- package/dist/{chunk-wzcjhv54.js → chunk-93e4w7te.js} +113 -1
- package/dist/chunk-93e4w7te.js.map +11 -0
- package/dist/{chunk-phs5fbff.js → chunk-990a5bkc.js} +424 -76
- package/dist/chunk-990a5bkc.js.map +21 -0
- package/dist/{chunk-6md802cc.js → chunk-9az7kjv4.js} +6 -6
- package/dist/{chunk-95ps0k5q.js → chunk-9e5t53z0.js} +1 -1
- package/dist/{chunk-js9e6487.js → chunk-9p7kyeqz.js} +4 -4
- package/dist/chunk-a68zz4r0.js +537 -0
- package/dist/chunk-a68zz4r0.js.map +11 -0
- package/dist/{chunk-d7vyc7k6.js → chunk-a700gjc8.js} +10 -2
- package/dist/chunk-a700gjc8.js.map +10 -0
- package/dist/{chunk-gfdh9kqa.js → chunk-a9qzjaz2.js} +2 -5
- package/dist/chunk-a9qzjaz2.js.map +10 -0
- package/dist/{chunk-8qmp9fpa.js → chunk-as94vcty.js} +619 -482
- package/dist/{chunk-8qmp9fpa.js.map → chunk-as94vcty.js.map} +19 -19
- package/dist/{chunk-brybqw5k.js → chunk-bkxm2dzx.js} +1 -1
- package/dist/{chunk-ppxf7gw2.js → chunk-d7t9jnym.js} +3 -3
- package/dist/{chunk-tapdssb5.js → chunk-e3b1r4d2.js} +8 -7
- package/dist/{chunk-tapdssb5.js.map → chunk-e3b1r4d2.js.map} +4 -4
- package/dist/{chunk-cg7cygfj.js → chunk-fdmvaz7x.js} +1 -1
- package/dist/{chunk-191jz8a6.js → chunk-ff0qdk7z.js} +1 -1
- package/dist/{chunk-9vsc919v.js → chunk-fgxvrxx4.js} +22 -2
- package/dist/chunk-fgxvrxx4.js.map +10 -0
- package/dist/{chunk-wx7smkn2.js → chunk-fq8drkp0.js} +1 -1
- package/dist/{chunk-60662fyv.js → chunk-fqms1a8c.js} +132 -4
- package/dist/{chunk-60662fyv.js.map → chunk-fqms1a8c.js.map} +8 -6
- package/dist/{chunk-v8a1qjzp.js → chunk-hpswfrjj.js} +1 -1
- package/dist/{chunk-qttqe1ah.js → chunk-jbg905hn.js} +80 -13
- package/dist/chunk-jbg905hn.js.map +25 -0
- package/dist/{chunk-bz5bnjjt.js → chunk-jjte0h4p.js} +1 -1
- package/dist/{chunk-hvq8xs66.js → chunk-jwjwvzj1.js} +797 -51
- package/dist/chunk-jwjwvzj1.js.map +38 -0
- package/dist/{chunk-k29rdc3h.js → chunk-k3vd2vyz.js} +6 -2
- package/dist/chunk-k3vd2vyz.js.map +10 -0
- package/dist/chunk-kcqpk9w1.js.map +2 -2
- package/dist/chunk-mvz89age.js.map +2 -2
- package/dist/{chunk-xrmbhap8.js → chunk-mwg0nwkb.js} +1 -1
- package/dist/{chunk-a3faa34j.js → chunk-nfjt8k8j.js} +1 -1
- package/dist/{chunk-394zjzjs.js → chunk-nhgqz3g3.js} +3 -3
- package/dist/{chunk-bqr703h4.js → chunk-p0c5kkhn.js} +23 -6
- package/dist/{chunk-bqr703h4.js.map → chunk-p0c5kkhn.js.map} +3 -3
- package/dist/{chunk-92wrka15.js → chunk-q150fnj9.js} +1 -1
- package/dist/{chunk-w3t94hm6.js → chunk-qk3zq25v.js} +1 -1
- package/dist/chunk-rtkqczbr.js.map +2 -2
- package/dist/{chunk-3qtk0070.js → chunk-ssqbrts1.js} +86 -9
- package/dist/chunk-ssqbrts1.js.map +14 -0
- package/dist/{chunk-5qqmndhm.js → chunk-szzhrn3e.js} +1 -1
- package/dist/{chunk-n1hbwc6y.js → chunk-t6pfch4e.js} +1 -1
- package/dist/{chunk-xtztwpkh.js → chunk-v6h4tc2v.js} +86 -5
- package/dist/chunk-v6h4tc2v.js.map +12 -0
- package/dist/{chunk-brhp9d97.js → chunk-vmqjndb3.js} +2 -2
- package/dist/chunk-vtjzwymr.js.map +2 -2
- package/dist/{chunk-fycac8e9.js → chunk-wpba6hhd.js} +10 -10
- package/dist/{chunk-fycac8e9.js.map → chunk-wpba6hhd.js.map} +1 -1
- package/dist/{chunk-t58sktr5.js → chunk-wrb1x1ss.js} +2 -2
- package/dist/{chunk-5sswe5ej.js → chunk-wsv23wvr.js} +2 -2
- package/dist/{chunk-04r2fdsd.js → chunk-x0hqc54n.js} +4 -4
- package/dist/{chunk-sgx82nnt.js → chunk-xg31er4p.js} +43 -4
- package/dist/chunk-xg31er4p.js.map +10 -0
- package/dist/{chunk-3neam1y5.js → chunk-y3qb965a.js} +2 -2
- package/dist/{chunk-5pz3htbe.js → chunk-y57eawng.js} +3 -3
- package/dist/{chunk-8ncg20rd.js → chunk-yah25st6.js} +5 -5
- package/dist/chunk-yah25st6.js.map +10 -0
- package/dist/{chunk-mrs7jy17.js → chunk-ywvxcmqj.js} +106 -13
- package/dist/{chunk-mrs7jy17.js.map → chunk-ywvxcmqj.js.map} +8 -7
- package/dist/{chunk-44hrfmbk.js → chunk-zmq9xc9s.js} +70 -31
- package/dist/{chunk-44hrfmbk.js.map → chunk-zmq9xc9s.js.map} +4 -3
- package/dist/datasets/write.js +8 -7
- package/dist/datasets/write.js.map +1 -1
- package/dist/errors/catalog.d.ts.map +1 -1
- package/dist/execution/agent.js +52 -49
- package/dist/execution/agent.js.map +1 -1
- package/dist/execution/kernel-operation.d.ts +1 -0
- package/dist/execution/kernel-operation.d.ts.map +1 -1
- package/dist/execution/primitive.js +52 -49
- package/dist/execution/primitive.js.map +1 -1
- package/dist/execution/request.js +53 -50
- package/dist/execution/request.js.map +2 -2
- package/dist/execution/types.d.ts +5 -0
- package/dist/execution/types.d.ts.map +1 -1
- package/dist/http/api-routes.d.ts.map +1 -1
- package/dist/http/index.js +1 -1
- package/dist/index.d.ts +5 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +69 -65
- package/dist/index.js.map +1 -1
- package/dist/lake-storage/index.js +9 -8
- package/dist/lake-storage/index.js.map +1 -1
- package/dist/maintenance/index.js +9 -8
- package/dist/maintenance/index.js.map +2 -2
- package/dist/materializer/index.js +26 -24
- package/dist/materializer/index.js.map +1 -1
- package/dist/models/audio/runtime.d.ts +3 -1
- package/dist/models/audio/runtime.d.ts.map +1 -1
- package/dist/models/audio/speech.d.ts +2 -2
- package/dist/models/audio/speech.d.ts.map +1 -1
- package/dist/models/audio/types.d.ts +23 -3
- package/dist/models/audio/types.d.ts.map +1 -1
- package/dist/models/calls.d.ts +7 -0
- package/dist/models/calls.d.ts.map +1 -1
- package/dist/models/catalog.d.ts +5 -0
- package/dist/models/catalog.d.ts.map +1 -1
- package/dist/models/definitions.d.ts +3 -2
- package/dist/models/definitions.d.ts.map +1 -1
- package/dist/models/errors.d.ts +5 -3
- package/dist/models/errors.d.ts.map +1 -1
- package/dist/models/events.d.ts +2 -0
- package/dist/models/events.d.ts.map +1 -1
- package/dist/models/execution/index.d.ts +1 -0
- package/dist/models/execution/index.d.ts.map +1 -1
- package/dist/models/execution/index.js +33 -28
- package/dist/models/execution/index.js.map +1 -1
- package/dist/models/execution/model-call-limits.d.ts.map +1 -1
- package/dist/models/execution/model-call-recorder.d.ts +8 -0
- package/dist/models/execution/model-call-recorder.d.ts.map +1 -1
- package/dist/models/execution/session.d.ts +12 -0
- package/dist/models/execution/session.d.ts.map +1 -1
- package/dist/models/execution/usage.d.ts.map +1 -1
- package/dist/models/execution.d.ts +1 -1
- package/dist/models/execution.d.ts.map +1 -1
- package/dist/models/generation-types.d.ts +2 -0
- package/dist/models/generation-types.d.ts.map +1 -1
- package/dist/models/index.d.ts +4 -2
- package/dist/models/index.d.ts.map +1 -1
- package/dist/models/index.js +2 -2
- package/dist/models/input-file.d.ts.map +1 -1
- package/dist/models/pricing.d.ts +2 -1
- package/dist/models/pricing.d.ts.map +1 -1
- package/dist/models/rate-card.d.ts +6 -0
- package/dist/models/rate-card.d.ts.map +1 -1
- package/dist/models/tools.d.ts +5 -0
- package/dist/models/tools.d.ts.map +1 -1
- package/dist/models/video/catalog.d.ts +13 -0
- package/dist/models/video/catalog.d.ts.map +1 -0
- package/dist/models/video/check.d.ts +76 -0
- package/dist/models/video/check.d.ts.map +1 -0
- package/dist/models/video/runtime.d.ts +21 -0
- package/dist/models/video/runtime.d.ts.map +1 -0
- package/dist/models/video/types.d.ts +188 -0
- package/dist/models/video/types.d.ts.map +1 -0
- package/dist/objects/execution.d.ts.map +1 -1
- package/dist/objects/index.js +52 -49
- package/dist/objects/index.js.map +1 -1
- package/dist/objects/query/index.js +25 -23
- package/dist/objects/query/index.js.map +1 -1
- package/dist/objects/sdk/object-set.d.ts.map +1 -1
- package/dist/ontology/index.js +4 -4
- package/dist/ontology/internal.js +4 -4
- package/dist/pipelines/index.js +28 -26
- package/dist/pipelines/index.js.map +1 -1
- package/dist/projections/internal.js +26 -24
- package/dist/projections/internal.js.map +1 -1
- package/dist/queues/index.d.ts +1 -1
- package/dist/queues/index.d.ts.map +1 -1
- package/dist/queues/types.d.ts +103 -6
- package/dist/queues/types.d.ts.map +1 -1
- package/dist/runtime/host.d.ts.map +1 -1
- package/dist/runtime/internal.d.ts +3 -1
- package/dist/runtime/internal.d.ts.map +1 -1
- package/dist/runtime/internal.js +49 -43
- package/dist/runtime/internal.js.map +2 -2
- package/dist/runtime/sixb.d.ts.map +1 -1
- package/dist/sandboxes/index.js +6 -6
- package/dist/security/public.d.ts +6 -4
- package/dist/security/public.d.ts.map +1 -1
- package/dist/security/public.js +5 -3
- package/dist/security/public.js.map +1 -1
- package/dist/shares/internal.js +25 -23
- package/dist/shares/internal.js.map +2 -2
- package/dist/storage/agents/in-memory.d.ts +2 -1
- package/dist/storage/agents/in-memory.d.ts.map +1 -1
- package/dist/storage/agents/index.d.ts +1 -1
- package/dist/storage/agents/index.d.ts.map +1 -1
- package/dist/storage/agents/provider.js +4 -4
- package/dist/storage/agents/types.d.ts +13 -0
- package/dist/storage/agents/types.d.ts.map +1 -1
- package/dist/storage/ai-cost/analytics.d.ts +1 -0
- package/dist/storage/ai-cost/analytics.d.ts.map +1 -1
- package/dist/storage/ai-cost/groups.d.ts +2 -0
- package/dist/storage/ai-cost/groups.d.ts.map +1 -1
- package/dist/storage/ai-cost/provider.js +5 -5
- package/dist/storage/ai-cost/types.d.ts +2 -1
- package/dist/storage/ai-cost/types.d.ts.map +1 -1
- package/dist/storage/ai-cost/validation.d.ts.map +1 -1
- package/dist/storage/ai-limits/enforcement.js +3 -3
- package/dist/storage/ai-limits/provider.js +2 -2
- package/dist/storage/ai-usage/in-memory.d.ts +8 -1
- package/dist/storage/ai-usage/in-memory.d.ts.map +1 -1
- package/dist/storage/ai-usage/index.d.ts +2 -2
- package/dist/storage/ai-usage/index.d.ts.map +1 -1
- package/dist/storage/ai-usage/record.d.ts +11 -1
- package/dist/storage/ai-usage/record.d.ts.map +1 -1
- package/dist/storage/ai-usage/types.d.ts +80 -1
- package/dist/storage/ai-usage/types.d.ts.map +1 -1
- package/dist/storage/ai-usage/usage.d.ts +1 -1
- package/dist/storage/ai-usage/usage.d.ts.map +1 -1
- package/dist/storage/artifacts/in-memory.d.ts +25 -2
- package/dist/storage/artifacts/in-memory.d.ts.map +1 -1
- package/dist/storage/artifacts/index.d.ts +1 -1
- package/dist/storage/artifacts/index.d.ts.map +1 -1
- package/dist/storage/artifacts/record.d.ts +12 -1
- package/dist/storage/artifacts/record.d.ts.map +1 -1
- package/dist/storage/artifacts/types.d.ts +198 -6
- package/dist/storage/artifacts/types.d.ts.map +1 -1
- package/dist/storage/executions/provider.js +3 -2
- package/dist/storage/executions/provider.js.map +1 -1
- package/dist/storage/file-upload-sessions/provider.js +10 -9
- package/dist/storage/file-upload-sessions/provider.js.map +1 -1
- package/dist/storage/in-memory/index.d.ts.map +1 -1
- package/dist/storage/index.d.ts +4 -4
- package/dist/storage/index.d.ts.map +1 -1
- package/dist/storage/index.js +24 -22
- package/dist/storage/index.js.map +1 -1
- package/dist/storage/internal.d.ts +2 -2
- package/dist/storage/internal.d.ts.map +1 -1
- package/dist/storage/internal.js +34 -14
- package/dist/storage/internal.js.map +2 -2
- package/dist/storage/workflow-runs/provider.js +10 -9
- package/dist/storage/workflow-runs/provider.js.map +1 -1
- package/dist/syncs/index.js +29 -27
- package/dist/syncs/index.js.map +1 -1
- package/dist/testing/agent-storage-contract.d.ts.map +1 -1
- package/dist/testing/ai-cost-storage-contract.d.ts.map +1 -1
- package/dist/testing/ai-usage-storage-contract.d.ts.map +1 -1
- package/dist/testing/artifact-storage-contract.d.ts +3 -0
- package/dist/testing/artifact-storage-contract.d.ts.map +1 -1
- package/dist/testing/execution-storage-contract.d.ts.map +1 -1
- package/dist/testing/index.js +619 -72
- package/dist/testing/index.js.map +9 -9
- package/dist/testing/lake-storage-contract.d.ts.map +1 -1
- package/dist/testing/queues-contract.d.ts.map +1 -1
- package/dist/webhooks/internal.js +26 -24
- package/dist/webhooks/internal.js.map +2 -2
- package/dist/workers/index.js +5 -5
- package/dist/workers/index.js.map +1 -1
- package/dist/workflows/index.js +58 -55
- package/dist/workflows/index.js.map +1 -1
- package/package.json +3 -1
- package/src/actions/request.ts +2 -0
- package/src/actions/run/context.ts +1 -0
- package/src/actions/run/execute.ts +5 -1
- package/src/actions/run/executor.ts +4 -0
- package/src/actions/run/normalize.ts +4 -0
- package/src/actions/run/types.ts +6 -0
- package/src/actions/types/context.ts +7 -0
- package/src/agents/README.md +3 -1
- package/src/agents/context/entries.ts +63 -9
- package/src/agents/context-model.ts +9 -0
- package/src/agents/context-resolution.ts +7 -0
- package/src/agents/execution.ts +39 -0
- package/src/agents/model-call-retry.ts +69 -0
- package/src/agents/model-loop.ts +86 -12
- package/src/agents/run-artifacts.ts +10 -5
- package/src/agents/run-plan.ts +2 -1
- package/src/agents/run-session-http.ts +1 -0
- package/src/agents/run-session-serve.ts +23 -2
- package/src/agents/run-session-wire.ts +45 -0
- package/src/agents/thread-access.ts +34 -1
- package/src/agents/tool-files.ts +23 -7
- package/src/artifacts/caller.ts +42 -0
- package/src/artifacts/comment-place.ts +51 -0
- package/src/artifacts/comment-text.ts +50 -7
- package/src/artifacts/comments.ts +132 -34
- package/src/artifacts/errors.ts +1 -0
- package/src/artifacts/execution.ts +249 -5
- package/src/artifacts/frames/assets.ts +93 -15
- package/src/artifacts/frames/entries.ts +7 -4
- package/src/artifacts/frames/generated/hyperframes-notices.txt +290 -0
- package/src/artifacts/frames/generated/hyperframes.txt +1145 -0
- package/src/artifacts/frames/generated.d.ts +5 -0
- package/src/artifacts/frames/hyperframes.ts +14 -0
- package/src/artifacts/frames/index.ts +5 -0
- package/src/artifacts/frames/kit.ts +7 -3
- package/src/artifacts/frames/protocol.ts +56 -0
- package/src/artifacts/frames/video-runtime.ts +165 -0
- package/src/artifacts/html.ts +13 -0
- package/src/artifacts/index.ts +7 -0
- package/src/artifacts/kinds.ts +9 -1
- package/src/artifacts/limits.ts +43 -0
- package/src/artifacts/live/document.ts +27 -3
- package/src/artifacts/live/index.ts +58 -2
- package/src/artifacts/live/presence.ts +17 -5
- package/src/artifacts/live/protocol.ts +4 -2
- package/src/artifacts/live/server.ts +1 -1
- package/src/artifacts/live/state.ts +3 -3
- package/src/artifacts/media/README.md +75 -0
- package/src/artifacts/media/media.ts +316 -0
- package/src/artifacts/media/transcriber.ts +84 -0
- package/src/artifacts/outline.ts +28 -0
- package/src/artifacts/publish/files.ts +19 -6
- package/src/artifacts/publish/index.ts +49 -2
- package/src/artifacts/publish/pdf.ts +3 -1
- package/src/artifacts/publish/previews.ts +22 -8
- package/src/artifacts/publish/publish.ts +4 -1
- package/src/artifacts/publish/validate/html.ts +23 -18
- package/src/artifacts/publish/validate/video.ts +309 -0
- package/src/artifacts/publish/video.ts +235 -0
- package/src/artifacts/resolve.ts +45 -0
- package/src/artifacts/video/README.md +402 -0
- package/src/artifacts/video/composition.ts +898 -0
- package/src/artifacts/video/edit.ts +3967 -0
- package/src/artifacts/video/export.ts +352 -0
- package/src/artifacts/video/looks.ts +694 -0
- package/src/artifacts/video/project.ts +198 -0
- package/src/artifacts/video/stills.ts +168 -0
- package/src/artifacts/video/timeline.ts +658 -0
- package/src/blob-storage/browser.ts +1 -1
- package/src/blob-storage/constants.ts +9 -3
- package/src/blob-storage/download-urls.ts +5 -3
- package/src/blob-storage/index.ts +1 -0
- package/src/blob-storage/internal.ts +13 -2
- package/src/blob-storage/media-type.ts +149 -8
- package/src/errors/catalog.ts +2 -1
- package/src/execution/authorization.ts +2 -2
- package/src/execution/kernel-operation.ts +14 -0
- package/src/execution/types.ts +2 -0
- package/src/http/api-routes.ts +117 -0
- package/src/index.ts +18 -1
- package/src/lake-storage/definition-updates.ts +3 -9
- package/src/models/audio/runtime.ts +48 -3
- package/src/models/audio/speech.ts +2 -2
- package/src/models/audio/types.ts +24 -3
- package/src/models/calls.ts +35 -18
- package/src/models/catalog.ts +9 -2
- package/src/models/definitions.ts +4 -2
- package/src/models/errors.ts +5 -3
- package/src/models/events.ts +2 -0
- package/src/models/execution/README.md +68 -2
- package/src/models/execution/index.ts +1 -0
- package/src/models/execution/model-call-limits.ts +11 -5
- package/src/models/execution/model-call-recorder.ts +14 -0
- package/src/models/execution/session.ts +52 -23
- package/src/models/execution/usage.ts +1 -0
- package/src/models/execution.ts +13 -2
- package/src/models/generation-types.ts +2 -0
- package/src/models/index.ts +20 -1
- package/src/models/input-file.ts +7 -2
- package/src/models/pricing.ts +19 -1
- package/src/models/rate-card.ts +15 -3
- package/src/models/tools.ts +5 -0
- package/src/models/video/catalog.ts +101 -0
- package/src/models/video/check.ts +441 -0
- package/src/models/video/runtime.ts +550 -0
- package/src/models/video/types.ts +194 -0
- package/src/objects/execution.ts +2 -0
- package/src/objects/sdk/object-set.ts +2 -0
- package/src/queues/index.ts +5 -0
- package/src/queues/types.ts +126 -5
- package/src/runtime/host.ts +3 -0
- package/src/runtime/internal.ts +3 -1
- package/src/runtime/sixb.ts +12 -4
- package/src/sandboxes/README.md +3 -2
- package/src/security/public.ts +6 -4
- package/src/storage/agents/in-memory.ts +39 -0
- package/src/storage/agents/index.ts +1 -0
- package/src/storage/agents/types.ts +14 -0
- package/src/storage/ai-cost/analytics.ts +2 -0
- package/src/storage/ai-cost/codec.ts +1 -0
- package/src/storage/ai-cost/groups.ts +7 -0
- package/src/storage/ai-cost/types.ts +2 -0
- package/src/storage/ai-cost/validation.ts +2 -0
- package/src/storage/ai-usage/in-memory.ts +93 -0
- package/src/storage/ai-usage/index.ts +10 -0
- package/src/storage/ai-usage/record.ts +106 -0
- package/src/storage/ai-usage/types.ts +102 -1
- package/src/storage/ai-usage/usage.ts +2 -0
- package/src/storage/artifacts/in-memory.ts +108 -11
- package/src/storage/artifacts/index.ts +14 -0
- package/src/storage/artifacts/record.ts +203 -1
- package/src/storage/artifacts/types.ts +199 -6
- package/src/storage/executions/validation.ts +2 -2
- package/src/storage/in-memory/index.ts +1 -1
- package/src/storage/index.ts +22 -1
- package/src/storage/internal.ts +9 -0
- package/src/testing/agent-storage-contract.ts +108 -0
- package/src/testing/ai-cost-storage-contract.ts +1 -0
- package/src/testing/ai-usage-storage-contract.ts +145 -1
- package/src/testing/artifact-storage-contract.ts +320 -0
- package/src/testing/execution-storage-contract.ts +10 -2
- package/src/testing/lake-storage-contract.ts +27 -0
- package/src/testing/queues-contract.ts +71 -0
- package/dist/chunk-3723kgba.js +0 -29
- package/dist/chunk-3723kgba.js.map +0 -10
- package/dist/chunk-3qtk0070.js.map +0 -14
- package/dist/chunk-8ncg20rd.js.map +0 -10
- package/dist/chunk-9vsc919v.js.map +0 -10
- package/dist/chunk-az95tsh3.js.map +0 -11
- package/dist/chunk-c29729me.js.map +0 -12
- package/dist/chunk-d7vyc7k6.js.map +0 -10
- package/dist/chunk-de5sfbc3.js +0 -3
- package/dist/chunk-gfdh9kqa.js.map +0 -11
- package/dist/chunk-hvq8xs66.js.map +0 -34
- package/dist/chunk-k29rdc3h.js.map +0 -10
- package/dist/chunk-mbwsgjn7.js.map +0 -11
- package/dist/chunk-phs5fbff.js.map +0 -19
- package/dist/chunk-qttqe1ah.js.map +0 -24
- package/dist/chunk-sgx82nnt.js.map +0 -10
- package/dist/chunk-vs2s3p8n.js +0 -2274
- package/dist/chunk-vs2s3p8n.js.map +0 -20
- package/dist/chunk-wkv3h3q7.js.map +0 -12
- package/dist/chunk-wzcjhv54.js.map +0 -11
- package/dist/chunk-xtztwpkh.js.map +0 -12
- /package/dist/{chunk-7mhbs9yy.js.map → chunk-1z8ccj53.js.map} +0 -0
- /package/dist/{chunk-dmnd9ea6.js.map → chunk-30a0ftnm.js.map} +0 -0
- /package/dist/{chunk-qbd282aa.js.map → chunk-39q9ybav.js.map} +0 -0
- /package/dist/{chunk-fdk0ks9p.js.map → chunk-3x902cja.js.map} +0 -0
- /package/dist/{chunk-c4nrycdy.js.map → chunk-4q4a5a96.js.map} +0 -0
- /package/dist/{chunk-cpy282b8.js.map → chunk-4w8qzy9w.js.map} +0 -0
- /package/dist/{chunk-xavet0qs.js.map → chunk-6whr25gw.js.map} +0 -0
- /package/dist/{chunk-ygvay56w.js.map → chunk-896te58g.js.map} +0 -0
- /package/dist/{chunk-pfdq1ga6.js.map → chunk-8eqqqc1h.js.map} +0 -0
- /package/dist/{chunk-jka26v8c.js.map → chunk-8x1bsp68.js.map} +0 -0
- /package/dist/{chunk-6md802cc.js.map → chunk-9az7kjv4.js.map} +0 -0
- /package/dist/{chunk-95ps0k5q.js.map → chunk-9e5t53z0.js.map} +0 -0
- /package/dist/{chunk-js9e6487.js.map → chunk-9p7kyeqz.js.map} +0 -0
- /package/dist/{chunk-brybqw5k.js.map → chunk-bkxm2dzx.js.map} +0 -0
- /package/dist/{chunk-ppxf7gw2.js.map → chunk-d7t9jnym.js.map} +0 -0
- /package/dist/{chunk-cg7cygfj.js.map → chunk-fdmvaz7x.js.map} +0 -0
- /package/dist/{chunk-191jz8a6.js.map → chunk-ff0qdk7z.js.map} +0 -0
- /package/dist/{chunk-wx7smkn2.js.map → chunk-fq8drkp0.js.map} +0 -0
- /package/dist/{chunk-v8a1qjzp.js.map → chunk-hpswfrjj.js.map} +0 -0
- /package/dist/{chunk-bz5bnjjt.js.map → chunk-jjte0h4p.js.map} +0 -0
- /package/dist/{chunk-xrmbhap8.js.map → chunk-mwg0nwkb.js.map} +0 -0
- /package/dist/{chunk-a3faa34j.js.map → chunk-nfjt8k8j.js.map} +0 -0
- /package/dist/{chunk-394zjzjs.js.map → chunk-nhgqz3g3.js.map} +0 -0
- /package/dist/{chunk-92wrka15.js.map → chunk-q150fnj9.js.map} +0 -0
- /package/dist/{chunk-w3t94hm6.js.map → chunk-qk3zq25v.js.map} +0 -0
- /package/dist/{chunk-5qqmndhm.js.map → chunk-szzhrn3e.js.map} +0 -0
- /package/dist/{chunk-n1hbwc6y.js.map → chunk-t6pfch4e.js.map} +0 -0
- /package/dist/{chunk-brhp9d97.js.map → chunk-vmqjndb3.js.map} +0 -0
- /package/dist/{chunk-t58sktr5.js.map → chunk-wrb1x1ss.js.map} +0 -0
- /package/dist/{chunk-5sswe5ej.js.map → chunk-wsv23wvr.js.map} +0 -0
- /package/dist/{chunk-04r2fdsd.js.map → chunk-x0hqc54n.js.map} +0 -0
- /package/dist/{chunk-3neam1y5.js.map → chunk-y3qb965a.js.map} +0 -0
- /package/dist/{chunk-5pz3htbe.js.map → chunk-y57eawng.js.map} +0 -0
|
@@ -0,0 +1,3967 @@
|
|
|
1
|
+
import { ARTIFACT_LIMITS } from "../limits"
|
|
2
|
+
import { diffSequences, diffText, textLines } from "../live/diff"
|
|
3
|
+
import {
|
|
4
|
+
parseVideoRoot,
|
|
5
|
+
readVideoComposition,
|
|
6
|
+
VIDEO_ENTRY,
|
|
7
|
+
type VideoRoot,
|
|
8
|
+
type VideoSourceSpan,
|
|
9
|
+
type VideoStart,
|
|
10
|
+
videoFilePath,
|
|
11
|
+
} from "./composition"
|
|
12
|
+
import {
|
|
13
|
+
type CssBlock,
|
|
14
|
+
cssBlocks,
|
|
15
|
+
keyframesOf,
|
|
16
|
+
LOOK_ID,
|
|
17
|
+
type Look,
|
|
18
|
+
type LookContext,
|
|
19
|
+
type LookElement,
|
|
20
|
+
type LookSheet,
|
|
21
|
+
lookDiffers,
|
|
22
|
+
lookKey,
|
|
23
|
+
lookOf,
|
|
24
|
+
type SceneKeyframes,
|
|
25
|
+
scriptNames,
|
|
26
|
+
scriptReaches,
|
|
27
|
+
scriptReachesIn,
|
|
28
|
+
writtenLook,
|
|
29
|
+
} from "./looks"
|
|
30
|
+
import {
|
|
31
|
+
goesOnFrom,
|
|
32
|
+
readRootLayers,
|
|
33
|
+
readRootTimeline,
|
|
34
|
+
readSceneTimeline,
|
|
35
|
+
sceneAfter,
|
|
36
|
+
type VideoChain,
|
|
37
|
+
type VideoChainPart,
|
|
38
|
+
type VideoClip,
|
|
39
|
+
type VideoElementSource,
|
|
40
|
+
type VideoFileTimeline,
|
|
41
|
+
type VideoText,
|
|
42
|
+
type VideoTrack,
|
|
43
|
+
videoChains,
|
|
44
|
+
videoChapters,
|
|
45
|
+
} from "./timeline"
|
|
46
|
+
|
|
47
|
+
// What an editor does to a video, as the smallest changes to the text of its files: a scene made
|
|
48
|
+
// longer is one `data-duration` written again, a clip split is one attribute and one element
|
|
49
|
+
// added. Each operation changes a working copy of the files step by step, reading them again
|
|
50
|
+
// between steps, checks that the result is still a video or refuses it, and returns what changed
|
|
51
|
+
// as the live layer's diff finds it: each changed run of lines narrowed to its attributes and
|
|
52
|
+
// words. So two people editing at once change different text, which the live document merges, and
|
|
53
|
+
// the Agent's next diff reads as an edit, not a rewrite. Browser-safe, as `composition.ts`. See
|
|
54
|
+
// README.md.
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* A change to a file's text: `remove` characters at `index`, then `insert` there, as the live
|
|
58
|
+
* layer's `BoardChange`. A file's changes are sorted and apart, each at its place in the text
|
|
59
|
+
* before the edit, so `applyBoardEditToText` applies them.
|
|
60
|
+
*/
|
|
61
|
+
export interface VideoTextChange {
|
|
62
|
+
readonly index: number
|
|
63
|
+
readonly remove: number
|
|
64
|
+
readonly insert: string
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** What an edit does to one file: changes its text, creates it, or removes it. */
|
|
68
|
+
export type VideoFileEdit =
|
|
69
|
+
| { readonly path: string; readonly changes: readonly VideoTextChange[] }
|
|
70
|
+
| { readonly path: string; readonly create: string }
|
|
71
|
+
| { readonly path: string; readonly remove: true }
|
|
72
|
+
|
|
73
|
+
export type VideoEdit = readonly VideoFileEdit[]
|
|
74
|
+
|
|
75
|
+
/** A video's files as an editor holds them: each HTML file's text, null for a media file. */
|
|
76
|
+
export type VideoSources = ReadonlyMap<string, string | null>
|
|
77
|
+
|
|
78
|
+
/** An edit, or why it is refused: the video it would leave is not one. */
|
|
79
|
+
export type VideoEditResult =
|
|
80
|
+
| { readonly edit: VideoEdit }
|
|
81
|
+
| {
|
|
82
|
+
readonly error: string
|
|
83
|
+
/** Why, for an editor to say in its reader's words: a file grown past what publishing reads. */
|
|
84
|
+
readonly refusal?: "too_large"
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** A stretch of time, in milliseconds, its end not included. */
|
|
88
|
+
export interface VideoRange {
|
|
89
|
+
readonly startMs: number
|
|
90
|
+
readonly endMs: number
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* A stretch to cut, and the `room` around it the cut may take as it grows to whole frames without
|
|
95
|
+
* touching anything kept: for a transcript's words, the pauses up to the words either side.
|
|
96
|
+
* Without one, the cut grows as much on each side.
|
|
97
|
+
*/
|
|
98
|
+
export interface VideoCut extends VideoRange {
|
|
99
|
+
readonly room?: VideoRange
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** The loudest a clip's sound is made, `data-volume`, as many times as loud as recorded. */
|
|
103
|
+
export const MAX_VOLUME = 4
|
|
104
|
+
|
|
105
|
+
/** The shortest a scene or a clip may become: a few frames. */
|
|
106
|
+
export const MIN_CLIP_MS = 100
|
|
107
|
+
|
|
108
|
+
/** Within this, two instants are one: under a frame at 60 fps. */
|
|
109
|
+
const SAME_MS = 15
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* How an edit names an element of a file: its ref, or its ref with what the editor read there.
|
|
113
|
+
* An element without an id is named by its place, which someone else's edit may shift between
|
|
114
|
+
* the reader's choice and the edit: with what it held, the edit still finds it, or refuses.
|
|
115
|
+
* {@link videoTarget} makes one.
|
|
116
|
+
*/
|
|
117
|
+
export type VideoTarget =
|
|
118
|
+
| string
|
|
119
|
+
| {
|
|
120
|
+
readonly ref: string
|
|
121
|
+
readonly tag: string
|
|
122
|
+
/** Its words, as a text or a caption holds them. */
|
|
123
|
+
readonly words?: string
|
|
124
|
+
/** The file it plays or shows. */
|
|
125
|
+
readonly src?: string
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** The target that names a clip or a text as the editor reads it now. */
|
|
129
|
+
export function videoTarget(element: VideoClip | VideoText): VideoTarget {
|
|
130
|
+
if ("id" in element && element.id) return element.ref
|
|
131
|
+
return {
|
|
132
|
+
ref: element.ref,
|
|
133
|
+
tag: element.tag,
|
|
134
|
+
...(element.text === undefined ? {} : { words: element.text }),
|
|
135
|
+
...("src" in element && element.src ? { src: element.src } : {}),
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** The clips an editor can import. */
|
|
140
|
+
export type VideoImportKind = "video" | "audio" | "image"
|
|
141
|
+
|
|
142
|
+
// Scenes ------------------------------------------------------------------------------------------
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Makes a scene about `ms` long, as long as {@link videoSceneLength} says. The scenes after it
|
|
146
|
+
* move with its end: those chained to it as HyperFrames moves them, and those at a number of
|
|
147
|
+
* seconds by as much. The video's own length, and a soundtrack that ran to its end, follow.
|
|
148
|
+
*/
|
|
149
|
+
export function setSceneLength(sources: VideoSources, scene: string, ms: number): VideoEditResult {
|
|
150
|
+
return run(sources, (work) => {
|
|
151
|
+
if (!(ms >= MIN_CLIP_MS)) throw new EditError(`A scene lasts at least ${MIN_CLIP_MS} ms.`)
|
|
152
|
+
lengthenScene(work, scene, videoSceneLength(ms, rootOf(work).fps))
|
|
153
|
+
})
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* The length {@link setSceneLength} gives a scene asked to last `ms` in a video at `fps`: the
|
|
158
|
+
* nearest whole number of frame steps, 100 ms at 30 fps, so the scenes after move by whole frames.
|
|
159
|
+
* A field shows it as soon as it is typed.
|
|
160
|
+
*/
|
|
161
|
+
export function videoSceneLength(ms: number, fps: number): number {
|
|
162
|
+
const step = frameStepMs(fps)
|
|
163
|
+
return Math.max(Math.ceil(MIN_CLIP_MS / step), Math.round(ms / step)) * step
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** Names a scene, as its chapter shows it; an empty label removes it. */
|
|
167
|
+
export function setSceneLabel(
|
|
168
|
+
sources: VideoSources,
|
|
169
|
+
scene: string,
|
|
170
|
+
label: string
|
|
171
|
+
): VideoEditResult {
|
|
172
|
+
return run(sources, (work) => {
|
|
173
|
+
const named = label.replace(/\s+/g, " ").trim().slice(0, ARTIFACT_LIMITS.maxTitleLength)
|
|
174
|
+
const host = hostOf(work, scene)
|
|
175
|
+
setAttributes(work, VIDEO_ENTRY, host.host, { "data-label": named === "" ? null : named })
|
|
176
|
+
})
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Moves a scene to `to` in play order: each scene that follows another than before starts when
|
|
181
|
+
* that one ends, keeping its own overlap or gap. Hosts keep their lines in `index.html`, so a move
|
|
182
|
+
* is attribute values written again, which merge with someone else's edit of the same host. Scenes
|
|
183
|
+
* on screen together then stack in play order, the later on top (`restack`).
|
|
184
|
+
*/
|
|
185
|
+
export function moveScene(sources: VideoSources, scene: string, to: number): VideoEditResult {
|
|
186
|
+
return run(sources, (work) => {
|
|
187
|
+
const before = openRoot(work)
|
|
188
|
+
// The chapters play one after the other; a scene laid over them keeps its start.
|
|
189
|
+
const chapters = videoChapters(before.scenes)
|
|
190
|
+
const moving = before.scenes.find((each) => each.id === scene)
|
|
191
|
+
if (!moving) throw new EditError(noScene(scene))
|
|
192
|
+
if (!chapters.includes(moving)) {
|
|
193
|
+
throw new EditError(
|
|
194
|
+
`'${scene}' is laid over the other scenes: it plays in no order of theirs.`
|
|
195
|
+
)
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const order = [...before.scenes]
|
|
199
|
+
const from = order.indexOf(moving)
|
|
200
|
+
const target = Math.min(Math.max(Math.round(to), 0), order.length - 1)
|
|
201
|
+
if (target === from) return
|
|
202
|
+
order.splice(from, 1)
|
|
203
|
+
order.splice(target, 0, moving)
|
|
204
|
+
|
|
205
|
+
// A scene laid over them that starts after one of them starts at its time from now on: it
|
|
206
|
+
// stays where it plays, wherever that chapter goes.
|
|
207
|
+
for (const scene of before.scenes) {
|
|
208
|
+
if (chapters.includes(scene) || scene.start.type !== "after") continue
|
|
209
|
+
setAttributes(work, VIDEO_ENTRY, hostOf(work, scene.id).host, {
|
|
210
|
+
"data-start": seconds(scene.startMs),
|
|
211
|
+
})
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// Each chapter whose predecessor changed starts after its new one, and so, from there on,
|
|
215
|
+
// does each that starts at a number of seconds: its predecessor's place changed.
|
|
216
|
+
const played = order.filter((each) => chapters.includes(each))
|
|
217
|
+
const was = new Map(chapters.map((each, index) => [each.id, chapters[index - 1]?.id]))
|
|
218
|
+
const changed = played.findIndex((each, index) => was.get(each.id) !== played[index - 1]?.id)
|
|
219
|
+
if (changed < 0) return
|
|
220
|
+
played.forEach((each, index) => {
|
|
221
|
+
const after = played[index - 1]
|
|
222
|
+
const kept = was.get(each.id) === after?.id
|
|
223
|
+
if (kept && (index < changed || each.start.type === "after")) return
|
|
224
|
+
|
|
225
|
+
const start = after ? chainedStart(after.id, gapBefore(chapters, each)) : "0"
|
|
226
|
+
setAttributes(work, VIDEO_ENTRY, hostOf(work, each.id).host, { "data-start": start })
|
|
227
|
+
})
|
|
228
|
+
|
|
229
|
+
restack(work)
|
|
230
|
+
})
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Copies a scene: its file under a new id, its host after its own, and the scenes that followed it
|
|
235
|
+
* now following the copy. Every element of the copy gets an id of its own, as HyperFrames plays
|
|
236
|
+
* each recording by its id and a clip is one clip by its id, and none of it goes on from a clip of
|
|
237
|
+
* the original's. Returns the copy's id with the edit.
|
|
238
|
+
*/
|
|
239
|
+
export function duplicateScene(
|
|
240
|
+
sources: VideoSources,
|
|
241
|
+
scene: string
|
|
242
|
+
): VideoEditResult & { readonly scene?: string } {
|
|
243
|
+
let copy: string | undefined
|
|
244
|
+
const result = run(sources, (work) => {
|
|
245
|
+
const chains = chainsOf(work)
|
|
246
|
+
const before = openRoot(work)
|
|
247
|
+
const original = hostOf(work, scene)
|
|
248
|
+
const taken = takenNames(work)
|
|
249
|
+
const id = uniqueName(scene, taken)
|
|
250
|
+
copy = id
|
|
251
|
+
taken.add(id)
|
|
252
|
+
|
|
253
|
+
const folder = original.src.includes("/")
|
|
254
|
+
? original.src.slice(0, original.src.lastIndexOf("/") + 1)
|
|
255
|
+
: ""
|
|
256
|
+
const src = uniquePath(work, `${folder}${id}`, ".html")
|
|
257
|
+
work.create(src, copySceneFile(work.text(original.src), scene, id, taken))
|
|
258
|
+
|
|
259
|
+
// The copy's host is its original's, renamed, starting when the original ends.
|
|
260
|
+
const index = work.text(VIDEO_ENTRY)
|
|
261
|
+
const block = lineBlock(index, original.host.element)
|
|
262
|
+
const host = withAttributes(
|
|
263
|
+
index.slice(original.host.element.start, original.host.element.end),
|
|
264
|
+
{
|
|
265
|
+
"data-composition-id": id,
|
|
266
|
+
"data-composition-src": src,
|
|
267
|
+
"data-start": scene,
|
|
268
|
+
...(original.host.attributes.id ? { id } : {}),
|
|
269
|
+
}
|
|
270
|
+
)
|
|
271
|
+
const indent = index.slice(block.start, original.host.element.start)
|
|
272
|
+
work.replace(VIDEO_ENTRY, block.end, block.end, `${indent}${host}${lineEnd(index, block)}`)
|
|
273
|
+
|
|
274
|
+
// What followed the original follows the copy; a scene after it at a number of seconds
|
|
275
|
+
// starts as much later as the copy lasts.
|
|
276
|
+
const place = before.scenes.findIndex((each) => each.id === scene)
|
|
277
|
+
const length = before.scenes[place]?.durationMs ?? 0
|
|
278
|
+
before.scenes.forEach((each, index) => {
|
|
279
|
+
if (index <= place) return
|
|
280
|
+
|
|
281
|
+
if (startsAfter(each.start, original)) {
|
|
282
|
+
const offset = each.start.type === "after" ? each.start.offset : 0
|
|
283
|
+
setAttributes(work, VIDEO_ENTRY, hostOf(work, each.id).host, {
|
|
284
|
+
"data-start": chainedStart(id, offset),
|
|
285
|
+
})
|
|
286
|
+
} else if (each.start.type === "at") {
|
|
287
|
+
setAttributes(work, VIDEO_ENTRY, hostOf(work, each.id).host, {
|
|
288
|
+
"data-start": seconds(each.startMs + length),
|
|
289
|
+
})
|
|
290
|
+
}
|
|
291
|
+
})
|
|
292
|
+
strandedParts(work, chains)
|
|
293
|
+
})
|
|
294
|
+
return "edit" in result && copy ? { ...result, scene: copy } : result
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Removes a scene: its host, and its file unless another scene plays it. The scenes that followed
|
|
299
|
+
* it start where it started, keeping their own overlap or gap, and the video is shorter by as
|
|
300
|
+
* much.
|
|
301
|
+
*/
|
|
302
|
+
export function removeScene(sources: VideoSources, scene: string): VideoEditResult {
|
|
303
|
+
return run(sources, (work) => {
|
|
304
|
+
const chains = chainsOf(work)
|
|
305
|
+
openRoot(work)
|
|
306
|
+
dropScene(work, scene)
|
|
307
|
+
strandedParts(work, chains)
|
|
308
|
+
})
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* Lets go of the parts a scene copied or removed leaves out of the clip they went on from, as a
|
|
313
|
+
* copy coming between two of its scenes does: each loses `data-continues`, so no later
|
|
314
|
+
* placement links it again across what came between, and it is a clip of its own.
|
|
315
|
+
*/
|
|
316
|
+
function strandedParts(work: Working, before: ReadonlyMap<string, VideoChain>): void {
|
|
317
|
+
const after = new Set([...chainsOf(work).keys()])
|
|
318
|
+
for (const [key, chain] of before) {
|
|
319
|
+
const [file] = key.split("\n")
|
|
320
|
+
const part = chain.parts.find((each) => `${each.file}\n${each.clip.ref}` === key)
|
|
321
|
+
if (!file || !part || part === chain.parts[0] || after.has(key)) continue
|
|
322
|
+
if (work.textOf(file) === undefined || !part.clip.id) continue
|
|
323
|
+
|
|
324
|
+
const now = readTimeline(work, file)?.clips.find((clip) => clip.id === part.clip.id)
|
|
325
|
+
if (now?.continues) setAttributes(work, file, now.source, { "data-continues": null })
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// Clips -------------------------------------------------------------------------------------------
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Sets a clip's timing, in milliseconds from its composition's start and into its file, as an
|
|
333
|
+
* inspector's fields do: nothing around it moves.
|
|
334
|
+
*/
|
|
335
|
+
export function setClipTiming(
|
|
336
|
+
sources: VideoSources,
|
|
337
|
+
file: string,
|
|
338
|
+
clip: VideoTarget,
|
|
339
|
+
timing: {
|
|
340
|
+
readonly startMs?: number
|
|
341
|
+
readonly durationMs?: number
|
|
342
|
+
readonly mediaStartMs?: number
|
|
343
|
+
}
|
|
344
|
+
): VideoEditResult {
|
|
345
|
+
return run(sources, (work) => {
|
|
346
|
+
const found = clipOf(work, file, clip)
|
|
347
|
+
const { startMs, durationMs, mediaStartMs } = timing
|
|
348
|
+
if (startMs !== undefined && !(startMs >= 0))
|
|
349
|
+
throw new EditError("A clip starts at 0 or later.")
|
|
350
|
+
if (durationMs !== undefined && !(durationMs >= MIN_CLIP_MS)) {
|
|
351
|
+
throw new EditError(`A clip lasts at least ${MIN_CLIP_MS} ms.`)
|
|
352
|
+
}
|
|
353
|
+
if (mediaStartMs !== undefined && !(mediaStartMs >= 0)) {
|
|
354
|
+
throw new EditError("A clip plays its file from 0 or later.")
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
// A clip that goes on in later scenes plays its file on from as far further in each.
|
|
358
|
+
const chain = chainsOf(work).get(`${file}\n${found.ref}`)
|
|
359
|
+
if (mediaStartMs !== undefined && playsFile(found) && chain) {
|
|
360
|
+
const by = mediaStartMs - found.mediaStartMs
|
|
361
|
+
if (chain.parts.some((part) => part.clip.mediaStartMs + by < 0)) {
|
|
362
|
+
throw new EditError("A clip plays its file from 0 or later.")
|
|
363
|
+
}
|
|
364
|
+
for (const part of chain.parts) {
|
|
365
|
+
if (part.file === file) continue
|
|
366
|
+
setAttributes(work, part.file, clipOf(work, part.file, part.clip.ref).source, {
|
|
367
|
+
"data-media-start": seconds(part.clip.mediaStartMs + by),
|
|
368
|
+
})
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
setAttributes(work, file, clipOf(work, file, found.ref).source, {
|
|
373
|
+
...(startMs === undefined ? {} : { "data-start": seconds(startMs) }),
|
|
374
|
+
...(durationMs === undefined ? {} : { "data-duration": seconds(durationMs) }),
|
|
375
|
+
...(mediaStartMs === undefined || !playsFile(found)
|
|
376
|
+
? {}
|
|
377
|
+
: { "data-media-start": seconds(mediaStartMs) }),
|
|
378
|
+
})
|
|
379
|
+
})
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* Moves a clip's edge to `toMs`, from its composition's start. Its end makes it shorter or longer;
|
|
384
|
+
* its start, into its file as much, as dragging a clip's edge does. An end pulled past its scene's
|
|
385
|
+
* end goes on in the next scene, as {@link moveClips} places a clip, or the last scene grows to
|
|
386
|
+
* hold it. A start never goes before its scene's. With `ripple`, what comes after the edge moves
|
|
387
|
+
* with it, the scene's end included, so the clip stays whole in its scene, and a start keeps its
|
|
388
|
+
* place: the clip pulls the rest of the scene in.
|
|
389
|
+
*/
|
|
390
|
+
export function trimClip(
|
|
391
|
+
sources: VideoSources,
|
|
392
|
+
file: string,
|
|
393
|
+
clip: VideoTarget,
|
|
394
|
+
edge: "start" | "end",
|
|
395
|
+
toMs: number,
|
|
396
|
+
options: { readonly ripple?: boolean; readonly endMs?: number } = {}
|
|
397
|
+
): VideoPlaceResult {
|
|
398
|
+
return placing(sources, (work) => {
|
|
399
|
+
const found = clipOf(work, file, clip)
|
|
400
|
+
const end = clipEnd(found, options.endMs)
|
|
401
|
+
// Where the file's composition starts in the video: `toMs` is from there.
|
|
402
|
+
const offsetMs = videoStartOf(work, file, found) - found.startMs
|
|
403
|
+
const chains = file === VIDEO_ENTRY ? new Map<string, VideoChain>() : chainsOf(work)
|
|
404
|
+
const logical = logicalClip(work, chains, { file, clip: found })
|
|
405
|
+
|
|
406
|
+
if (edge === "end") {
|
|
407
|
+
if (options.ripple || file === VIDEO_ENTRY) {
|
|
408
|
+
const length = Math.round(toMs) - found.startMs
|
|
409
|
+
if (length < MIN_CLIP_MS) throw new EditError(`A clip lasts at least ${MIN_CLIP_MS} ms.`)
|
|
410
|
+
if (length === found.durationMs) return undefined
|
|
411
|
+
|
|
412
|
+
setAttributes(work, file, found.source, { "data-duration": seconds(length) })
|
|
413
|
+
if (options.ripple)
|
|
414
|
+
ripple(work, file, end, found.startMs + length - end, new Set([found.ref]))
|
|
415
|
+
return undefined
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
// The clip as one, whichever part's end is pulled: it ends there in the video.
|
|
419
|
+
const length = offsetMs + Math.round(toMs) - logical.startMs
|
|
420
|
+
if (length < MIN_CLIP_MS) throw new EditError(`A clip lasts at least ${MIN_CLIP_MS} ms.`)
|
|
421
|
+
// As long as its scenes show it already: nothing changes.
|
|
422
|
+
if (length === logical.durationMs) return undefined
|
|
423
|
+
return placeFound(work, [{ ...logical, durationMs: length }])
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
// A clip that goes on in later scenes starts later or sooner as one, and is placed again:
|
|
427
|
+
// never before its file's start, nor its first scene's.
|
|
428
|
+
if ((logical.others?.length ?? 0) > 0 && !options.ripple) {
|
|
429
|
+
const head = sceneOf(work, logical.file)
|
|
430
|
+
const into = logical.clip.mediaStartMs
|
|
431
|
+
const floor = Math.max(
|
|
432
|
+
head?.startMs ?? 0,
|
|
433
|
+
playsFile(logical.clip) ? logical.startMs - into : (head?.startMs ?? 0)
|
|
434
|
+
)
|
|
435
|
+
const startMs = Math.max(offsetMs + Math.round(toMs), floor)
|
|
436
|
+
const endMs = logical.startMs + (logical.durationMs ?? 0)
|
|
437
|
+
if (endMs - startMs < MIN_CLIP_MS) {
|
|
438
|
+
throw new EditError(`A clip lasts at least ${MIN_CLIP_MS} ms.`)
|
|
439
|
+
}
|
|
440
|
+
if (startMs === logical.startMs) return undefined
|
|
441
|
+
return placeFound(work, [
|
|
442
|
+
{
|
|
443
|
+
...logical,
|
|
444
|
+
startMs,
|
|
445
|
+
durationMs: endMs - startMs,
|
|
446
|
+
...jumpsFrom(into, logical.jumps, startMs - logical.startMs),
|
|
447
|
+
},
|
|
448
|
+
])
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
// How far its start moves into its file, or back out of it: never before its file's start,
|
|
452
|
+
// nor, unless it ripples and keeps its place, before its composition's.
|
|
453
|
+
const floors = [playsFile(found) ? -found.mediaStartMs : -found.startMs]
|
|
454
|
+
if (!options.ripple) floors.push(-found.startMs)
|
|
455
|
+
const moved = Math.max(Math.round(toMs) - found.startMs, ...floors)
|
|
456
|
+
if (end - (found.startMs + moved) < MIN_CLIP_MS) {
|
|
457
|
+
throw new EditError(`A clip lasts at least ${MIN_CLIP_MS} ms.`)
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
setAttributes(work, file, found.source, {
|
|
461
|
+
...(options.ripple ? {} : { "data-start": seconds(found.startMs + moved) }),
|
|
462
|
+
"data-duration": seconds(end - found.startMs - moved),
|
|
463
|
+
...(playsFile(found) ? { "data-media-start": seconds(found.mediaStartMs + moved) } : {}),
|
|
464
|
+
})
|
|
465
|
+
if (options.ripple) ripple(work, file, end, -moved, new Set([found.ref]))
|
|
466
|
+
return undefined
|
|
467
|
+
})
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
/**
|
|
471
|
+
* Moves a clip to start at `startMs` from its composition's start, its length kept, never before
|
|
472
|
+
* the video's: before its scene's start, it goes into the scene it then plays in, as
|
|
473
|
+
* {@link moveClips} places several.
|
|
474
|
+
*/
|
|
475
|
+
export function moveClip(
|
|
476
|
+
sources: VideoSources,
|
|
477
|
+
file: string,
|
|
478
|
+
clip: VideoTarget,
|
|
479
|
+
startMs: number
|
|
480
|
+
): VideoPlaceResult {
|
|
481
|
+
return placing(sources, (work) => {
|
|
482
|
+
const found = clipOf(work, file, clip)
|
|
483
|
+
return moveFound(work, [{ file, clip: found }], Math.round(startMs) - found.startMs)
|
|
484
|
+
})
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
/** Sets how loud a clip plays, from 0 to 1, and how long it fades in and out, in milliseconds. */
|
|
488
|
+
export function setClipSound(
|
|
489
|
+
sources: VideoSources,
|
|
490
|
+
file: string,
|
|
491
|
+
clip: VideoTarget,
|
|
492
|
+
sound: {
|
|
493
|
+
readonly volume?: number
|
|
494
|
+
readonly fadeInMs?: number
|
|
495
|
+
readonly fadeOutMs?: number
|
|
496
|
+
/** Footage whose sound is not played, or played again. */
|
|
497
|
+
readonly muted?: boolean
|
|
498
|
+
}
|
|
499
|
+
): VideoEditResult {
|
|
500
|
+
return run(sources, (work) => {
|
|
501
|
+
const found = clipOf(work, file, clip)
|
|
502
|
+
if (found.kind !== "video" && found.kind !== "audio") {
|
|
503
|
+
throw new EditError(`'${found.ref}' plays no sound.`)
|
|
504
|
+
}
|
|
505
|
+
if (sound.muted !== undefined && found.kind !== "video") {
|
|
506
|
+
throw new EditError(`'${found.ref}' is a sound: turn its volume down instead.`)
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
// As loud as 4 times as recorded, or as loud as it already is.
|
|
510
|
+
const { volume, fadeInMs, fadeOutMs } = sound
|
|
511
|
+
const loudest = Math.max(MAX_VOLUME, found.volume ?? 1)
|
|
512
|
+
if (volume !== undefined && !(volume >= 0 && volume <= loudest)) {
|
|
513
|
+
throw new EditError(`A clip's volume is from 0 to ${loudest}.`)
|
|
514
|
+
}
|
|
515
|
+
for (const fade of [fadeInMs, fadeOutMs]) {
|
|
516
|
+
if (fade !== undefined && !(fade >= 0)) throw new EditError("A fade lasts 0 s or more.")
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
// A clip that goes on in later scenes sounds as one: as loud in each part, fading in on the
|
|
520
|
+
// first and out on the last.
|
|
521
|
+
const parts = chainsOf(work).get(`${file}\n${found.ref}`)?.parts ?? [
|
|
522
|
+
{ file, clip: found, startMs: 0, endMs: 0 },
|
|
523
|
+
]
|
|
524
|
+
parts.forEach((part, index) => {
|
|
525
|
+
setAttributes(work, part.file, clipOf(work, part.file, part.clip.ref).source, {
|
|
526
|
+
...(volume === undefined ? {} : { "data-volume": String(Math.round(volume * 100) / 100) }),
|
|
527
|
+
...(fadeInMs === undefined || index > 0
|
|
528
|
+
? {}
|
|
529
|
+
: { "data-fade-in": fadeInMs === 0 ? null : seconds(fadeInMs) }),
|
|
530
|
+
...(fadeOutMs === undefined || index < parts.length - 1
|
|
531
|
+
? {}
|
|
532
|
+
: { "data-fade-out": fadeOutMs === 0 ? null : seconds(fadeOutMs) }),
|
|
533
|
+
// Muted, as HyperFrames mixes no sound of it; heard, its sound mixed in again.
|
|
534
|
+
...(sound.muted === undefined
|
|
535
|
+
? {}
|
|
536
|
+
: sound.muted
|
|
537
|
+
? { muted: "", "data-has-audio": null }
|
|
538
|
+
: { muted: null, "data-has-audio": "true" }),
|
|
539
|
+
})
|
|
540
|
+
})
|
|
541
|
+
})
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
/**
|
|
545
|
+
* Types over the words of an element that holds text alone, by its ref among the file's texts.
|
|
546
|
+
* A line break in `text` is a `<br>`.
|
|
547
|
+
*/
|
|
548
|
+
export function setVideoText(
|
|
549
|
+
sources: VideoSources,
|
|
550
|
+
file: string,
|
|
551
|
+
ref: VideoTarget,
|
|
552
|
+
text: string
|
|
553
|
+
): VideoEditResult {
|
|
554
|
+
return run(sources, (work) => {
|
|
555
|
+
const found = resolveTarget(timelineOf(work, file).texts, ref, file, "text")
|
|
556
|
+
if (!found.source.content) throw new EditError(`${file} shows no text '${found.ref}'.`)
|
|
557
|
+
|
|
558
|
+
const words = text.trim()
|
|
559
|
+
if (words === "") throw new EditError("A text says something: remove it instead.")
|
|
560
|
+
|
|
561
|
+
const html = words
|
|
562
|
+
.split(/\r?\n/)
|
|
563
|
+
.map((line) => escapeText(line.trim()))
|
|
564
|
+
.join("<br>")
|
|
565
|
+
|
|
566
|
+
// A caption that goes on in later scenes says the same in each.
|
|
567
|
+
const clip = timelineOf(work, file).clips.find(
|
|
568
|
+
(each) => each.source.element.start === found.source.element.start
|
|
569
|
+
)
|
|
570
|
+
const chain = clip ? chainsOf(work).get(`${file}\n${clip.ref}`) : undefined
|
|
571
|
+
for (const part of chain?.parts ?? []) {
|
|
572
|
+
if (part.file === file) continue
|
|
573
|
+
const text = timelineOf(work, part.file).texts.find(
|
|
574
|
+
(each) => each.source.element.start === part.clip.source.element.start
|
|
575
|
+
)
|
|
576
|
+
if (text?.source.content) {
|
|
577
|
+
work.replace(part.file, text.source.content.start, text.source.content.end, html)
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
work.replace(file, found.source.content.start, found.source.content.end, html)
|
|
581
|
+
})
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
/**
|
|
585
|
+
* Splits a clip at `atMs` from its composition's start into two, the second a copy that starts
|
|
586
|
+
* there and plays on from where the first stops. `endMs` is where a clip that writes no length
|
|
587
|
+
* ends, as its file or its scene ends it.
|
|
588
|
+
*/
|
|
589
|
+
export function splitClip(
|
|
590
|
+
sources: VideoSources,
|
|
591
|
+
file: string,
|
|
592
|
+
clip: VideoTarget,
|
|
593
|
+
atMs: number,
|
|
594
|
+
options: { readonly endMs?: number } = {}
|
|
595
|
+
): VideoEditResult & { readonly clip?: string } {
|
|
596
|
+
let second: string | undefined
|
|
597
|
+
const result = run(sources, (work) => {
|
|
598
|
+
const chains = chainsOf(work)
|
|
599
|
+
second = splitChained(work, chains, file, clipOf(work, file, clip), atMs, options.endMs)
|
|
600
|
+
})
|
|
601
|
+
return "edit" in result && second ? { ...result, clip: second } : result
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
/**
|
|
605
|
+
* Removes a clip. With `ripple`, what comes after it moves back by its length, the scene's end
|
|
606
|
+
* included, as a magnetic timeline closes the gap.
|
|
607
|
+
*/
|
|
608
|
+
export function removeClip(
|
|
609
|
+
sources: VideoSources,
|
|
610
|
+
file: string,
|
|
611
|
+
clip: VideoTarget,
|
|
612
|
+
options: { readonly ripple?: boolean; readonly endMs?: number } = {}
|
|
613
|
+
): VideoEditResult {
|
|
614
|
+
return run(sources, (work) => {
|
|
615
|
+
const found = clipOf(work, file, clip)
|
|
616
|
+
|
|
617
|
+
// A clip that goes on in later scenes goes whole.
|
|
618
|
+
const chain = file === VIDEO_ENTRY ? undefined : chainsOf(work).get(`${file}\n${found.ref}`)
|
|
619
|
+
if (chain) {
|
|
620
|
+
removeFound(work, chain.parts, options.ripple === true)
|
|
621
|
+
return
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
const end = clipEnd(found, options.endMs)
|
|
625
|
+
// What followed it keeps its time: its name will name nothing.
|
|
626
|
+
if (found.id) freezeFollowers(work, file, (each) => each.id === found.id)
|
|
627
|
+
|
|
628
|
+
const removed = clipOf(work, file, clip)
|
|
629
|
+
const block = lineBlock(work.text(file), removed.source.element)
|
|
630
|
+
work.replace(file, block.start, block.end, "")
|
|
631
|
+
pruneLooks(work, file)
|
|
632
|
+
if (options.ripple) rippleRemove(work, file, [{ startMs: found.startMs, endMs: end }])
|
|
633
|
+
})
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
// Several clips ----------------------------------------------------------------------------------
|
|
637
|
+
|
|
638
|
+
/** A clip of one of the video's files, as an operation on several clips names it. */
|
|
639
|
+
export interface VideoClipTarget {
|
|
640
|
+
/** Its file: a scene's, or `index.html` for the video's own. */
|
|
641
|
+
readonly file: string
|
|
642
|
+
readonly clip: VideoTarget
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
/**
|
|
646
|
+
* A clip that left a scene whose script animates it: its look came with it, the motion the
|
|
647
|
+
* script writes could not.
|
|
648
|
+
*/
|
|
649
|
+
export interface VideoAnimationLeft {
|
|
650
|
+
/** Where the clip is now. */
|
|
651
|
+
readonly clip: VideoClipTarget
|
|
652
|
+
/** The scene it left, whose script still names it. */
|
|
653
|
+
readonly scene: string
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
/**
|
|
657
|
+
* A clip that landed in a scene whose script animates what it is, a class of it or its tag: that
|
|
658
|
+
* motion now plays on it too.
|
|
659
|
+
*/
|
|
660
|
+
export interface VideoAnimationJoined {
|
|
661
|
+
/** Where the clip is now. */
|
|
662
|
+
readonly clip: VideoClipTarget
|
|
663
|
+
/** The scene it landed in, whose script reaches it. */
|
|
664
|
+
readonly scene: string
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
/**
|
|
668
|
+
* An edit that places clips in the video, as a move, an end trim or an import does: a clip of a
|
|
669
|
+
* scene lives in the scene that plays where it starts, its own while that holds it, else the one
|
|
670
|
+
* on top there, and goes on past that scene's end as a copy in the next, playing on from where it
|
|
671
|
+
* stopped; the last scene grows to hold it. `clips` names each clip placed where it now starts, in
|
|
672
|
+
* the order given, `animationsLeft` each that left a scene whose script animates it, and
|
|
673
|
+
* `animationsJoined` each that landed in one whose script animates what it is, and
|
|
674
|
+
* `looksMayDiffer` each whose look could not all be taken along.
|
|
675
|
+
*/
|
|
676
|
+
export type VideoPlaceResult = VideoEditResult & {
|
|
677
|
+
readonly clips?: readonly VideoClipTarget[]
|
|
678
|
+
readonly animationsLeft?: readonly VideoAnimationLeft[]
|
|
679
|
+
readonly animationsJoined?: readonly VideoAnimationJoined[]
|
|
680
|
+
/**
|
|
681
|
+
* Each clip that landed in another scene where a rule may style it that its look could not take
|
|
682
|
+
* along, one through a wrapper or by its place among its siblings: it may look a little different.
|
|
683
|
+
*/
|
|
684
|
+
readonly looksMayDiffer?: readonly VideoClipTarget[]
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
/**
|
|
688
|
+
* Moves clips together by `deltaMs`, as one edit: they keep their places one to another, none
|
|
689
|
+
* before the video's start, so the whole group moves less. Each clip of a scene lands in the scene
|
|
690
|
+
* that plays where it now starts, and goes on in the next scene past that one's end; the last
|
|
691
|
+
* scene grows to hold it (see {@link VideoPlaceResult}). The video's own clips stay its own.
|
|
692
|
+
*/
|
|
693
|
+
export function moveClips(
|
|
694
|
+
sources: VideoSources,
|
|
695
|
+
clips: readonly VideoClipTarget[],
|
|
696
|
+
deltaMs: number
|
|
697
|
+
): VideoPlaceResult {
|
|
698
|
+
return placing(sources, (work) => moveFound(work, clipsOf(work, clips), deltaMs))
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
/**
|
|
702
|
+
* Removes clips at once, each from its own file, as {@link removeClip} removes one. With `ripple`,
|
|
703
|
+
* each file closes the gaps its clips leave, as one cut of all their stretches. `endMs` is where
|
|
704
|
+
* a clip that writes no length ends.
|
|
705
|
+
*/
|
|
706
|
+
export function removeClips(
|
|
707
|
+
sources: VideoSources,
|
|
708
|
+
clips: readonly (VideoClipTarget & { readonly endMs?: number })[],
|
|
709
|
+
options: { readonly ripple?: boolean } = {}
|
|
710
|
+
): VideoEditResult {
|
|
711
|
+
return run(sources, (work) => {
|
|
712
|
+
// A clip that goes on in later scenes goes whole, each of its parts once.
|
|
713
|
+
const chains = chainsOf(work)
|
|
714
|
+
const found = new Map<string, { file: string; clip: VideoClip; endMs?: number }>()
|
|
715
|
+
for (const each of clipsOf(work, clips)) {
|
|
716
|
+
const endMs = clips[each.index]?.endMs
|
|
717
|
+
const parts = chains.get(`${each.file}\n${each.clip.ref}`)?.parts ?? [each]
|
|
718
|
+
for (const part of parts) {
|
|
719
|
+
found.set(`${part.file}\n${part.clip.ref}`, {
|
|
720
|
+
file: part.file,
|
|
721
|
+
clip: part.clip,
|
|
722
|
+
...(endMs === undefined ? {} : { endMs }),
|
|
723
|
+
})
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
removeFound(work, [...found.values()], options.ripple === true)
|
|
727
|
+
})
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
/**
|
|
731
|
+
* Removes clips found in their files, and the looks they carried. With `ripple`, each file closes
|
|
732
|
+
* the gaps its clips leave, as one cut of all their stretches.
|
|
733
|
+
*/
|
|
734
|
+
function removeFound(
|
|
735
|
+
work: Working,
|
|
736
|
+
found: readonly { readonly file: string; readonly clip: VideoClip; readonly endMs?: number }[],
|
|
737
|
+
ripple: boolean
|
|
738
|
+
): void {
|
|
739
|
+
for (const [file, each] of byFile(found)) {
|
|
740
|
+
const stretches = each.map(({ clip, endMs }) => ({
|
|
741
|
+
startMs: clip.startMs,
|
|
742
|
+
endMs: clipEnd(clip, endMs),
|
|
743
|
+
}))
|
|
744
|
+
|
|
745
|
+
// What followed them keeps its time: their names will name nothing.
|
|
746
|
+
const ids = new Set(each.flatMap(({ clip }) => (clip.id ? [clip.id] : [])))
|
|
747
|
+
freezeFollowers(work, file, (clip) => clip.id !== undefined && ids.has(clip.id))
|
|
748
|
+
|
|
749
|
+
rewriteClips(work, file, new Map(each.map(({ clip }) => [clip.ref, "remove" as const])))
|
|
750
|
+
pruneLooks(work, file)
|
|
751
|
+
if (ripple) rippleRemove(work, file, stretches)
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
/**
|
|
756
|
+
* Splits clips at once, each at its own `atMs` from its composition's start, as {@link splitClip}
|
|
757
|
+
* splits one. Returns the second part of each, in the order they are named.
|
|
758
|
+
*/
|
|
759
|
+
export function splitClips(
|
|
760
|
+
sources: VideoSources,
|
|
761
|
+
splits: readonly (VideoClipTarget & { readonly atMs: number; readonly endMs?: number })[]
|
|
762
|
+
): VideoEditResult & { readonly clips?: readonly VideoClipTarget[] } {
|
|
763
|
+
const parts: (VideoClipTarget | undefined)[] = []
|
|
764
|
+
const result = run(sources, (work) => {
|
|
765
|
+
const found = clipsOf(work, splits)
|
|
766
|
+
const chains = chainsOf(work)
|
|
767
|
+
|
|
768
|
+
for (const [file, each] of byFile(found)) {
|
|
769
|
+
// From the last in the file to the first: a part added shifts only the places after it.
|
|
770
|
+
const lastFirst = [...each].sort(
|
|
771
|
+
(a, b) => b.clip.source.element.start - a.clip.source.element.start
|
|
772
|
+
)
|
|
773
|
+
for (const { clip, index } of lastFirst) {
|
|
774
|
+
const split = splits[index]
|
|
775
|
+
if (!split) continue
|
|
776
|
+
|
|
777
|
+
const now = clipOf(work, file, clip.ref)
|
|
778
|
+
const second = splitChained(work, chains, file, now, split.atMs, split.endMs)
|
|
779
|
+
if (second) parts[index] = { file, clip: second }
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
})
|
|
783
|
+
return "edit" in result
|
|
784
|
+
? { ...result, clips: parts.filter((part) => part !== undefined) }
|
|
785
|
+
: result
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
/**
|
|
789
|
+
* Keeps only these stretches of a clip's file, in milliseconds into it: the clip becomes one clip
|
|
790
|
+
* per stretch, each starting where the one before ends, and what played after the clip moves
|
|
791
|
+
* back by what was cut, the scene's end included. What is cut out of the timeline goes with it,
|
|
792
|
+
* such as the captions of words no longer said. A clip that goes on keeps them in every part,
|
|
793
|
+
* each part cutting what it plays of the rest.
|
|
794
|
+
*/
|
|
795
|
+
export function keepClipRanges(
|
|
796
|
+
sources: VideoSources,
|
|
797
|
+
file: string,
|
|
798
|
+
clip: VideoTarget,
|
|
799
|
+
ranges: readonly VideoRange[],
|
|
800
|
+
options: { readonly endMs?: number } = {}
|
|
801
|
+
): VideoEditResult {
|
|
802
|
+
return run(sources, (work) => {
|
|
803
|
+
const found = clipOf(work, file, clip)
|
|
804
|
+
if (!playsFile(found)) throw new EditError(`'${found.ref}' plays no recording.`)
|
|
805
|
+
|
|
806
|
+
const cuts = new Map<string, VideoRange[]>()
|
|
807
|
+
let left = false
|
|
808
|
+
for (const part of chainParts(work, file, [found])) {
|
|
809
|
+
const from = part.clip.mediaStartMs
|
|
810
|
+
const end = clipEnd(part.clip, part.file === file ? options.endMs : undefined)
|
|
811
|
+
const to = from + end - part.clip.startMs
|
|
812
|
+
const kept = mergeRanges(
|
|
813
|
+
ranges.map((range) => ({
|
|
814
|
+
startMs: Math.max(from, Math.round(range.startMs)),
|
|
815
|
+
endMs: Math.min(to, Math.round(range.endMs)),
|
|
816
|
+
}))
|
|
817
|
+
).filter((range) => range.endMs - range.startMs >= MIN_CLIP_MS)
|
|
818
|
+
if (kept.length > 0) left = true
|
|
819
|
+
|
|
820
|
+
// What is cut, as stretches of its composition's timeline.
|
|
821
|
+
const cut = cuts.get(part.file) ?? []
|
|
822
|
+
cuts.set(part.file, cut)
|
|
823
|
+
let cursor = from
|
|
824
|
+
for (const range of [...kept, { startMs: to, endMs: to }]) {
|
|
825
|
+
if (range.startMs - cursor > 0) {
|
|
826
|
+
cut.push({
|
|
827
|
+
startMs: part.clip.startMs + cursor - from,
|
|
828
|
+
endMs: part.clip.startMs + range.startMs - from,
|
|
829
|
+
})
|
|
830
|
+
}
|
|
831
|
+
cursor = range.endMs
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
if (!left) throw new EditError("Nothing of the clip would be left: remove it instead.")
|
|
835
|
+
|
|
836
|
+
for (const [each, cut] of cuts) if (cut.length > 0) rippleRemove(work, each, cut)
|
|
837
|
+
})
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
/**
|
|
841
|
+
* Cuts stretches of a recording, in milliseconds into its file, wherever the composition plays
|
|
842
|
+
* them: each clip of `src` loses what it played of them, as {@link keepClipRanges} cuts one
|
|
843
|
+
* clip, and what followed moves back. A transcript's words cut out are such stretches. A clip
|
|
844
|
+
* that goes on loses them in every part, so words across a scene's edge go whole.
|
|
845
|
+
*/
|
|
846
|
+
export function removeMediaRanges(
|
|
847
|
+
sources: VideoSources,
|
|
848
|
+
file: string,
|
|
849
|
+
src: string,
|
|
850
|
+
ranges: readonly VideoCut[],
|
|
851
|
+
options: { readonly endMs?: number } = {}
|
|
852
|
+
): VideoEditResult {
|
|
853
|
+
return run(sources, (work) => {
|
|
854
|
+
const removed = mergeCuts(ranges)
|
|
855
|
+
// The file, however the clips name it: `media/a.mp4` and `../media/a.mp4` are one.
|
|
856
|
+
const exists = (path: string) => work.has(path)
|
|
857
|
+
const recording = videoFilePath(file, src, exists) ?? src
|
|
858
|
+
const played = timelineOf(work, file).clips.filter(
|
|
859
|
+
(clip) =>
|
|
860
|
+
clip.src !== undefined &&
|
|
861
|
+
(videoFilePath(file, clip.src, exists) ?? clip.src) === recording &&
|
|
862
|
+
playsFile(clip)
|
|
863
|
+
)
|
|
864
|
+
const parts = chainParts(work, file, played)
|
|
865
|
+
const endOf = (part: { readonly file: string; readonly clip: VideoClip }) =>
|
|
866
|
+
clipEnd(part.clip, part.file === file ? options.endMs : undefined)
|
|
867
|
+
|
|
868
|
+
const cuts = new Map<string, VideoCut[]>()
|
|
869
|
+
for (const part of parts) {
|
|
870
|
+
const { clip } = part
|
|
871
|
+
// From the file's time to the composition's, within what the clip plays.
|
|
872
|
+
const from = clip.mediaStartMs
|
|
873
|
+
const to = from + endOf(part) - clip.startMs
|
|
874
|
+
const at = (ms: number) => clip.startMs + Math.min(to, Math.max(from, ms)) - from
|
|
875
|
+
|
|
876
|
+
const cut = cuts.get(part.file) ?? []
|
|
877
|
+
cuts.set(part.file, cut)
|
|
878
|
+
for (const range of removed) {
|
|
879
|
+
const startMs = at(range.startMs)
|
|
880
|
+
const endMs = at(range.endMs)
|
|
881
|
+
if (endMs <= startMs) continue
|
|
882
|
+
|
|
883
|
+
const room = range.room ?? range
|
|
884
|
+
cut.push({ startMs, endMs, room: { startMs: at(room.startMs), endMs: at(room.endMs) } })
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
if ([...cuts.values()].every((cut) => cut.length === 0)) return
|
|
888
|
+
|
|
889
|
+
const left = parts.some((part) =>
|
|
890
|
+
subtractRanges(
|
|
891
|
+
{ startMs: part.clip.startMs, endMs: endOf(part) },
|
|
892
|
+
mergeRanges(cuts.get(part.file) ?? [])
|
|
893
|
+
).some((piece) => piece.endMs - piece.startMs >= MIN_CLIP_MS)
|
|
894
|
+
)
|
|
895
|
+
if (!left) throw new EditError("Nothing of the recording would be left: remove it instead.")
|
|
896
|
+
for (const [each, cut] of cuts) if (cut.length > 0) rippleRemove(work, each, cut)
|
|
897
|
+
})
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
/**
|
|
901
|
+
* The clips of a file, each with every part it goes on in, wherever that is, each once: what a
|
|
902
|
+
* cut by the words of a recording cuts.
|
|
903
|
+
*/
|
|
904
|
+
function chainParts(
|
|
905
|
+
work: Working,
|
|
906
|
+
file: string,
|
|
907
|
+
clips: readonly VideoClip[]
|
|
908
|
+
): { readonly file: string; readonly clip: VideoClip }[] {
|
|
909
|
+
const chains = clips.length > 0 && file !== VIDEO_ENTRY ? chainsOf(work) : new Map()
|
|
910
|
+
const parts = new Map<string, { readonly file: string; readonly clip: VideoClip }>()
|
|
911
|
+
for (const clip of clips) {
|
|
912
|
+
const chain = chains.get(`${file}\n${clip.ref}`)
|
|
913
|
+
for (const part of chain?.parts ?? [{ file, clip }]) {
|
|
914
|
+
parts.set(`${part.file}\n${part.clip.ref}`, { file: part.file, clip: part.clip })
|
|
915
|
+
}
|
|
916
|
+
}
|
|
917
|
+
return [...parts.values()]
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
/**
|
|
921
|
+
* Takes a stretch out of the video, in milliseconds from its start, as a magnetic timeline deletes
|
|
922
|
+
* a range: each scene loses what it shows of it, from its start up to where the next scene starts,
|
|
923
|
+
* so a crossfade is cut once. Every clip of a scene loses what played then, a recording cut
|
|
924
|
+
* through is split around it, and what followed moves back; a scene the range covers whole goes.
|
|
925
|
+
*/
|
|
926
|
+
export function cutRange(sources: VideoSources, range: VideoRange): VideoEditResult {
|
|
927
|
+
return run(sources, (work) => {
|
|
928
|
+
const startMs = Math.max(0, Math.round(range.startMs))
|
|
929
|
+
const endMs = Math.round(range.endMs)
|
|
930
|
+
if (endMs - startMs < MIN_CLIP_MS) {
|
|
931
|
+
throw new EditError(`A cut lasts at least ${MIN_CLIP_MS} ms.`)
|
|
932
|
+
}
|
|
933
|
+
|
|
934
|
+
const scenes = [...openRoot(work).scenes].sort((a, b) => a.startMs - b.startMs)
|
|
935
|
+
const shares = scenes.map((scene, index) => {
|
|
936
|
+
const until = Math.min(
|
|
937
|
+
scene.startMs + scene.durationMs,
|
|
938
|
+
scenes[index + 1]?.startMs ?? Number.POSITIVE_INFINITY
|
|
939
|
+
)
|
|
940
|
+
return {
|
|
941
|
+
scene,
|
|
942
|
+
from: Math.max(startMs, scene.startMs),
|
|
943
|
+
to: Math.min(endMs, until),
|
|
944
|
+
whole: startMs <= scene.startMs + SAME_MS && endMs >= until - SAME_MS,
|
|
945
|
+
}
|
|
946
|
+
})
|
|
947
|
+
|
|
948
|
+
// From the last: a cut moves only what comes after it, so each scene before is where it was.
|
|
949
|
+
for (const { scene, from, to, whole } of shares.reverse()) {
|
|
950
|
+
if (to - from < SAME_MS) continue
|
|
951
|
+
|
|
952
|
+
if (whole) dropScene(work, scene.id)
|
|
953
|
+
else {
|
|
954
|
+
rippleRemove(work, scene.src, [
|
|
955
|
+
{ startMs: from - scene.startMs, endMs: to - scene.startMs },
|
|
956
|
+
])
|
|
957
|
+
}
|
|
958
|
+
}
|
|
959
|
+
})
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
/**
|
|
963
|
+
* Adds a clip that plays or shows a file of the video, at `startMs` in its composition, for
|
|
964
|
+
* `durationMs`: footage fills the frame under what the scene shows, a sound plays under it. It is
|
|
965
|
+
* placed as {@link moveClips} places a clip: past its scene's end, it goes on in the next scene,
|
|
966
|
+
* or the last scene grows to hold it. Footage added where a scene's clip is heard is added muted,
|
|
967
|
+
* as B-roll over a voice is, which `muted` says; the video's own music is no reason. Returns the clip's id with the edit.
|
|
968
|
+
*/
|
|
969
|
+
export function insertClip(
|
|
970
|
+
sources: VideoSources,
|
|
971
|
+
file: string,
|
|
972
|
+
clip: {
|
|
973
|
+
readonly kind: VideoImportKind
|
|
974
|
+
readonly src: string
|
|
975
|
+
readonly startMs: number
|
|
976
|
+
readonly durationMs: number
|
|
977
|
+
readonly mediaStartMs?: number
|
|
978
|
+
}
|
|
979
|
+
): VideoEditResult & { readonly clip?: string; readonly muted?: true } {
|
|
980
|
+
let added: string | undefined
|
|
981
|
+
let muted = false
|
|
982
|
+
const result = run(sources, (work) => {
|
|
983
|
+
if (!sources.has(clip.src))
|
|
984
|
+
throw new EditError(`'${clip.src}' is not one of the video's files.`)
|
|
985
|
+
if (!(clip.durationMs >= MIN_CLIP_MS)) {
|
|
986
|
+
throw new EditError(`A clip lasts at least ${MIN_CLIP_MS} ms.`)
|
|
987
|
+
}
|
|
988
|
+
|
|
989
|
+
const timeline = timelineOf(work, file)
|
|
990
|
+
const stem = clip.src.replace(/^.*\//, "").replace(/\.[^.]*$/, "")
|
|
991
|
+
const id = uniqueName(slug(stem) || clip.kind, takenNames(work))
|
|
992
|
+
added = id
|
|
993
|
+
|
|
994
|
+
const startMs = (sceneOf(work, file)?.startMs ?? 0) + Math.max(0, clip.startMs)
|
|
995
|
+
muted = clip.kind === "video" && heardDuring(work, startMs, startMs + clip.durationMs)
|
|
996
|
+
const html = clipHtml(clip.kind, {
|
|
997
|
+
id,
|
|
998
|
+
// A scene reads a path from the project's root, unless one beside it is the version's.
|
|
999
|
+
src: clip.src,
|
|
1000
|
+
start: seconds(Math.max(0, clip.startMs)),
|
|
1001
|
+
duration: seconds(clip.durationMs),
|
|
1002
|
+
mediaStart: seconds(clip.mediaStartMs ?? 0),
|
|
1003
|
+
muted,
|
|
1004
|
+
})
|
|
1005
|
+
|
|
1006
|
+
// A picture goes over the pictures before it and under the words; a sound beside the sounds.
|
|
1007
|
+
const track = clip.kind === "audio" ? "sound" : "picture"
|
|
1008
|
+
insertInComposition(work, file, timeline, [html], track)
|
|
1009
|
+
|
|
1010
|
+
const found = clipOf(work, file, id)
|
|
1011
|
+
placeFound(work, [
|
|
1012
|
+
{ file, clip: found, startMs: videoStartOf(work, file, found), durationMs: clip.durationMs },
|
|
1013
|
+
])
|
|
1014
|
+
})
|
|
1015
|
+
if (!("edit" in result) || !added) return result
|
|
1016
|
+
return { ...result, clip: added, ...(muted ? { muted: true as const } : {}) }
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
/**
|
|
1020
|
+
* Whether something is said between `fromMs` and `toMs`: a scene's sound, or its footage, or a line
|
|
1021
|
+
* of the video's own voice-over that belongs to a chapter, heard at a volume above none. The video's
|
|
1022
|
+
* own sounds under it all, music or ambience, are not.
|
|
1023
|
+
*/
|
|
1024
|
+
function heardDuring(work: Working, fromMs: number, toMs: number): boolean {
|
|
1025
|
+
const heard = (clip: VideoClip) =>
|
|
1026
|
+
(clip.kind === "audio" || clip.kind === "video") && !clip.silent && clip.volume !== 0
|
|
1027
|
+
const during = (from: number, to: number) => from < toMs && fromMs < to
|
|
1028
|
+
|
|
1029
|
+
const root = rootOf(work)
|
|
1030
|
+
const chapters = { ...root, scenes: videoChapters(root.scenes) }
|
|
1031
|
+
const voiceOver = (readTimeline(work, VIDEO_ENTRY)?.clips ?? []).some(
|
|
1032
|
+
(clip) =>
|
|
1033
|
+
heard(clip) &&
|
|
1034
|
+
clip.durationMs !== undefined &&
|
|
1035
|
+
numericStart(work, VIDEO_ENTRY, clip) &&
|
|
1036
|
+
sceneOfClip(chapters, clip) !== undefined &&
|
|
1037
|
+
during(clip.startMs, clip.startMs + clip.durationMs)
|
|
1038
|
+
)
|
|
1039
|
+
if (voiceOver) return true
|
|
1040
|
+
|
|
1041
|
+
return root.scenes.some((scene) => {
|
|
1042
|
+
if (work.textOf(scene.src) === undefined) return false
|
|
1043
|
+
const endMs = scene.startMs + scene.durationMs
|
|
1044
|
+
return (readTimeline(work, scene.src)?.clips ?? []).some((clip) => {
|
|
1045
|
+
if (!heard(clip)) return false
|
|
1046
|
+
const from = scene.startMs + clip.startMs
|
|
1047
|
+
return during(
|
|
1048
|
+
from,
|
|
1049
|
+
Math.min(endMs, clip.durationMs === undefined ? endMs : from + clip.durationMs)
|
|
1050
|
+
)
|
|
1051
|
+
})
|
|
1052
|
+
})
|
|
1053
|
+
}
|
|
1054
|
+
|
|
1055
|
+
/**
|
|
1056
|
+
* Plays another file of the video in a clip's place, one of the same kind: every part of the clip,
|
|
1057
|
+
* from the new file's start on, its timing, sound and look as they were. One edit: the file it
|
|
1058
|
+
* played, once no clip plays it, leaves the version with it.
|
|
1059
|
+
*/
|
|
1060
|
+
export function replaceClipMedia(
|
|
1061
|
+
sources: VideoSources,
|
|
1062
|
+
file: string,
|
|
1063
|
+
clip: VideoTarget,
|
|
1064
|
+
src: string
|
|
1065
|
+
): VideoEditResult {
|
|
1066
|
+
return run(sources, (work) => {
|
|
1067
|
+
const found = clipOf(work, file, clip)
|
|
1068
|
+
const kind = found.kind
|
|
1069
|
+
if (!found.src || (kind !== "video" && kind !== "audio" && kind !== "image")) {
|
|
1070
|
+
throw new EditError(`'${found.ref}' plays no file to replace.`)
|
|
1071
|
+
}
|
|
1072
|
+
// The file, however it is named: from the clip's file, or from the project's root.
|
|
1073
|
+
const path = videoFilePath(file, src, (each) => work.has(each)) ?? src
|
|
1074
|
+
if (sources.get(path) !== null) {
|
|
1075
|
+
throw new EditError(`'${src}' is not one of the video's recordings or pictures.`)
|
|
1076
|
+
}
|
|
1077
|
+
const replacing = mediaKind(path)
|
|
1078
|
+
if (replacing !== undefined && replacing !== kind) {
|
|
1079
|
+
throw new EditError(
|
|
1080
|
+
`'${src}' is no ${kind === "image" ? "picture" : kind}: choose one that is.`
|
|
1081
|
+
)
|
|
1082
|
+
}
|
|
1083
|
+
|
|
1084
|
+
const parts =
|
|
1085
|
+
file === VIDEO_ENTRY
|
|
1086
|
+
? [{ file, clip: found }]
|
|
1087
|
+
: (chainsOf(work).get(`${file}\n${found.ref}`)?.parts ?? [{ file, clip: found }])
|
|
1088
|
+
let intoMs = 0
|
|
1089
|
+
for (const part of parts) {
|
|
1090
|
+
const now = clipOf(work, part.file, part.clip.ref)
|
|
1091
|
+
setAttributes(work, part.file, now.source, {
|
|
1092
|
+
src: writtenPath(work, part.file, path, now.src?.startsWith("../") ?? false),
|
|
1093
|
+
...(playsFile(now) ? { "data-media-start": seconds(intoMs) } : {}),
|
|
1094
|
+
})
|
|
1095
|
+
intoMs += now.durationMs ?? 0
|
|
1096
|
+
}
|
|
1097
|
+
})
|
|
1098
|
+
}
|
|
1099
|
+
|
|
1100
|
+
/** What a file of the video is by its name: footage, a sound or a picture. */
|
|
1101
|
+
function mediaKind(path: string): "video" | "audio" | "image" | undefined {
|
|
1102
|
+
const extension = /\.([^./]+)$/.exec(path)?.[1]?.toLowerCase() ?? ""
|
|
1103
|
+
if (["mp4", "mov", "m4v", "webm"].includes(extension)) return "video"
|
|
1104
|
+
if (["mp3", "m4a", "wav", "aac", "ogg", "oga", "flac"].includes(extension)) return "audio"
|
|
1105
|
+
if (["png", "jpg", "jpeg", "webp", "gif", "avif"].includes(extension)) return "image"
|
|
1106
|
+
return undefined
|
|
1107
|
+
}
|
|
1108
|
+
|
|
1109
|
+
/**
|
|
1110
|
+
* A file of the video as `file` names it: from the project's root, else from beside it, or from
|
|
1111
|
+
* beside it first, as the file named the one it replaces.
|
|
1112
|
+
*/
|
|
1113
|
+
function writtenPath(work: Working, file: string, path: string, beside = false): string {
|
|
1114
|
+
const exists = (each: string) => work.has(each)
|
|
1115
|
+
const up = `${"../".repeat(file.split("/").length - 1)}${path}`
|
|
1116
|
+
const spellings = beside ? [up, path] : [path, up]
|
|
1117
|
+
return spellings.find((each) => videoFilePath(file, each, exists) === path) ?? path
|
|
1118
|
+
}
|
|
1119
|
+
|
|
1120
|
+
/** The files an edit leaves: `sources` with the edit applied. */
|
|
1121
|
+
export function applyVideoEdit(sources: VideoSources, edit: VideoEdit): Map<string, string | null> {
|
|
1122
|
+
const files = new Map(sources)
|
|
1123
|
+
for (const each of edit) {
|
|
1124
|
+
if ("remove" in each) files.delete(each.path)
|
|
1125
|
+
else if ("create" in each) files.set(each.path, each.create)
|
|
1126
|
+
else files.set(each.path, applyTextChanges(files.get(each.path) ?? "", each.changes))
|
|
1127
|
+
}
|
|
1128
|
+
return files
|
|
1129
|
+
}
|
|
1130
|
+
|
|
1131
|
+
/** A text with its changes applied, from the last, so each index names its place. */
|
|
1132
|
+
function applyTextChanges(text: string, changes: readonly VideoTextChange[]): string {
|
|
1133
|
+
let result = text
|
|
1134
|
+
for (const change of [...changes].sort((a, b) => b.index - a.index)) {
|
|
1135
|
+
result =
|
|
1136
|
+
result.slice(0, change.index) + change.insert + result.slice(change.index + change.remove)
|
|
1137
|
+
}
|
|
1138
|
+
return result
|
|
1139
|
+
}
|
|
1140
|
+
|
|
1141
|
+
/** A file's clips and texts, as an editor lays them out: `index.html`'s own, or a scene's. */
|
|
1142
|
+
export function readVideoFileTimeline(file: string, html: string): VideoFileTimeline | null {
|
|
1143
|
+
return file === VIDEO_ENTRY ? readRootTimeline(html) : readSceneTimeline(html)
|
|
1144
|
+
}
|
|
1145
|
+
|
|
1146
|
+
// The working copy --------------------------------------------------------------------------------
|
|
1147
|
+
|
|
1148
|
+
class EditError extends Error {}
|
|
1149
|
+
|
|
1150
|
+
/** The files as an operation changes them. */
|
|
1151
|
+
class Working {
|
|
1152
|
+
private readonly files: Map<string, string | null>
|
|
1153
|
+
/**
|
|
1154
|
+
* The root as it was before the operation moved a scene, once it has: from then on the root
|
|
1155
|
+
* writes room for any length, so each step reads, and the last writes the length it ends with.
|
|
1156
|
+
*/
|
|
1157
|
+
opened: VideoRoot | undefined
|
|
1158
|
+
/**
|
|
1159
|
+
* The clips that go on, as the operation found them before it cut any, and what each cut left
|
|
1160
|
+
* of their parts, by `file\nref`: the pieces' ids, none for a part cut whole.
|
|
1161
|
+
*/
|
|
1162
|
+
marking:
|
|
1163
|
+
| {
|
|
1164
|
+
readonly chains: ReadonlyMap<string, VideoChain>
|
|
1165
|
+
readonly pieces: Map<string, readonly string[]>
|
|
1166
|
+
}
|
|
1167
|
+
| undefined
|
|
1168
|
+
/** Text each file's edit writes whole, though it holds it as it did: see {@link wholeAgain}. */
|
|
1169
|
+
private readonly whole = new Map<string, Set<string>>()
|
|
1170
|
+
/** Each file's timeline as last read, kept while its text is the one it was read from. */
|
|
1171
|
+
readonly timelines = new Map<
|
|
1172
|
+
string,
|
|
1173
|
+
{ readonly text: string; readonly timeline: VideoFileTimeline | null }
|
|
1174
|
+
>()
|
|
1175
|
+
|
|
1176
|
+
constructor(readonly sources: VideoSources) {
|
|
1177
|
+
this.files = new Map(sources)
|
|
1178
|
+
}
|
|
1179
|
+
|
|
1180
|
+
text(path: string): string {
|
|
1181
|
+
const text = this.files.get(path)
|
|
1182
|
+
if (typeof text !== "string") {
|
|
1183
|
+
throw new EditError(`'${path}' is not one of the video's HTML files.`)
|
|
1184
|
+
}
|
|
1185
|
+
return text
|
|
1186
|
+
}
|
|
1187
|
+
|
|
1188
|
+
has(path: string): boolean {
|
|
1189
|
+
return this.files.has(path)
|
|
1190
|
+
}
|
|
1191
|
+
|
|
1192
|
+
/** A text file's text, undefined for one that is not the video's or holds bytes. */
|
|
1193
|
+
textOf(path: string): string | undefined {
|
|
1194
|
+
const text = this.files.get(path)
|
|
1195
|
+
return typeof text === "string" ? text : undefined
|
|
1196
|
+
}
|
|
1197
|
+
|
|
1198
|
+
paths(): string[] {
|
|
1199
|
+
return [...this.files.keys()]
|
|
1200
|
+
}
|
|
1201
|
+
|
|
1202
|
+
replace(path: string, start: number, end: number, insert: string): void {
|
|
1203
|
+
const text = this.text(path)
|
|
1204
|
+
this.files.set(path, text.slice(0, start) + insert + text.slice(end))
|
|
1205
|
+
}
|
|
1206
|
+
|
|
1207
|
+
create(path: string, text: string): void {
|
|
1208
|
+
this.files.set(path, text)
|
|
1209
|
+
}
|
|
1210
|
+
|
|
1211
|
+
remove(path: string): void {
|
|
1212
|
+
this.files.delete(path)
|
|
1213
|
+
}
|
|
1214
|
+
|
|
1215
|
+
/** The paths whose text the operation changed so far. */
|
|
1216
|
+
changed(): string[] {
|
|
1217
|
+
return [...this.files].flatMap(([path, text]) =>
|
|
1218
|
+
typeof text === "string" && this.sources.get(path) !== text ? [path] : []
|
|
1219
|
+
)
|
|
1220
|
+
}
|
|
1221
|
+
|
|
1222
|
+
/** Writes `text`, which the file holds as it did, whole in its edit. */
|
|
1223
|
+
writeWhole(path: string, text: string): void {
|
|
1224
|
+
this.whole.set(path, (this.whole.get(path) ?? new Set()).add(text))
|
|
1225
|
+
}
|
|
1226
|
+
|
|
1227
|
+
/** The files now, and the edit from what they were: each text's changes, as `diffText` finds them. */
|
|
1228
|
+
result(): { readonly edit: VideoEdit; readonly files: ReadonlyMap<string, string | null> } {
|
|
1229
|
+
const edit: VideoFileEdit[] = []
|
|
1230
|
+
for (const [path, text] of this.files) {
|
|
1231
|
+
const before = this.sources.get(path)
|
|
1232
|
+
if (text === null || before === text) continue
|
|
1233
|
+
if (typeof before !== "string") {
|
|
1234
|
+
edit.push({ path, create: text })
|
|
1235
|
+
continue
|
|
1236
|
+
}
|
|
1237
|
+
edit.push({
|
|
1238
|
+
path,
|
|
1239
|
+
changes: wholeAgain(before, text, textChanges(before, text), this.whole.get(path)),
|
|
1240
|
+
})
|
|
1241
|
+
}
|
|
1242
|
+
for (const path of this.sources.keys()) {
|
|
1243
|
+
if (!this.files.has(path)) edit.push({ path, remove: true })
|
|
1244
|
+
}
|
|
1245
|
+
return { edit, files: this.files }
|
|
1246
|
+
}
|
|
1247
|
+
}
|
|
1248
|
+
|
|
1249
|
+
/**
|
|
1250
|
+
* A file's changes, each text of `whole` it holds as it did written again whole among them: what
|
|
1251
|
+
* no change touches is then this edit's too, so another edit that removes it at once leaves this
|
|
1252
|
+
* one's copy. An insertion just before it goes with it; one inside, and it was written anyway.
|
|
1253
|
+
*/
|
|
1254
|
+
function wholeAgain(
|
|
1255
|
+
before: string,
|
|
1256
|
+
after: string,
|
|
1257
|
+
changes: readonly VideoTextChange[],
|
|
1258
|
+
whole: ReadonlySet<string> | undefined
|
|
1259
|
+
): VideoTextChange[] {
|
|
1260
|
+
const result = [...changes]
|
|
1261
|
+
for (const text of whole ?? []) {
|
|
1262
|
+
const start = before.indexOf(text)
|
|
1263
|
+
const end = start + text.length
|
|
1264
|
+
if (start < 0 || !after.includes(text)) continue
|
|
1265
|
+
|
|
1266
|
+
const inside = result.some((change) =>
|
|
1267
|
+
change.remove > 0
|
|
1268
|
+
? change.index < end && change.index + change.remove > start
|
|
1269
|
+
: change.index > start && change.index < end
|
|
1270
|
+
)
|
|
1271
|
+
if (inside) continue
|
|
1272
|
+
const at = result.findIndex((change) => change.remove === 0 && change.index === start)
|
|
1273
|
+
const ahead = result[at]
|
|
1274
|
+
if (ahead) result[at] = { index: start, remove: text.length, insert: ahead.insert + text }
|
|
1275
|
+
else result.push({ index: start, remove: text.length, insert: text })
|
|
1276
|
+
}
|
|
1277
|
+
return result.sort((a, b) => a.index - b.index)
|
|
1278
|
+
}
|
|
1279
|
+
|
|
1280
|
+
/**
|
|
1281
|
+
* The changes from `before` to `after`: the live layer's line diff, and within a run of lines
|
|
1282
|
+
* changed one for one, each line narrowed to the attributes and words that changed in it, so two
|
|
1283
|
+
* values written again on one line are two changes. A run that adds or removes lines, such as a host
|
|
1284
|
+
* moved or a clip split, stays whole: its lines are what changed.
|
|
1285
|
+
*/
|
|
1286
|
+
function textChanges(before: string, after: string): VideoTextChange[] {
|
|
1287
|
+
return diffText(before, after).flatMap((edit) => {
|
|
1288
|
+
// The diff narrows each run to its characters: the lines around them are read whole.
|
|
1289
|
+
const start = before.lastIndexOf("\n", edit.start - 1) + 1
|
|
1290
|
+
const lineEnd = before.indexOf("\n", edit.end)
|
|
1291
|
+
const end = lineEnd < 0 ? before.length : lineEnd
|
|
1292
|
+
const insert = before.slice(start, edit.start) + edit.insert + before.slice(edit.end, end)
|
|
1293
|
+
|
|
1294
|
+
const removed = textLines(before.slice(start, end))
|
|
1295
|
+
const added = textLines(insert)
|
|
1296
|
+
if (removed.length !== added.length || removed.length === 0) {
|
|
1297
|
+
return [{ index: edit.start, remove: edit.end - edit.start, insert: edit.insert }]
|
|
1298
|
+
}
|
|
1299
|
+
|
|
1300
|
+
const changes: VideoTextChange[] = []
|
|
1301
|
+
let at = start
|
|
1302
|
+
removed.forEach((line, index) => {
|
|
1303
|
+
const next = added[index] ?? ""
|
|
1304
|
+
// Two lines that share little are two elements, one in the other's place: the line goes
|
|
1305
|
+
// whole, rather than one element's characters becoming another's.
|
|
1306
|
+
if (!alike(line, next)) {
|
|
1307
|
+
changes.push({ index: at, remove: line.length, insert: next })
|
|
1308
|
+
at += line.length
|
|
1309
|
+
return
|
|
1310
|
+
}
|
|
1311
|
+
// Token by token: an attribute written again is one change, never a few of its letters.
|
|
1312
|
+
const from = tokens(line)
|
|
1313
|
+
const to = tokens(next)
|
|
1314
|
+
const fromAt = offsets(from)
|
|
1315
|
+
const toAt = offsets(to)
|
|
1316
|
+
for (const hunk of diffSequences(from, to)) {
|
|
1317
|
+
const index = fromAt[hunk.aStart] ?? 0
|
|
1318
|
+
changes.push({
|
|
1319
|
+
index: at + index,
|
|
1320
|
+
remove: (fromAt[hunk.aEnd] ?? line.length) - index,
|
|
1321
|
+
insert: next.slice(toAt[hunk.bStart] ?? 0, toAt[hunk.bEnd] ?? next.length),
|
|
1322
|
+
})
|
|
1323
|
+
}
|
|
1324
|
+
at += line.length
|
|
1325
|
+
})
|
|
1326
|
+
return changes
|
|
1327
|
+
})
|
|
1328
|
+
}
|
|
1329
|
+
|
|
1330
|
+
/**
|
|
1331
|
+
* A line's words, spaces, and each mark between them, which together write it again. An attribute
|
|
1332
|
+
* is one token with the space before it: two people writing its value at once then leave it twice,
|
|
1333
|
+
* `data-duration="8" data-duration="12"`, which reads as the first, rather than `"812"`.
|
|
1334
|
+
*/
|
|
1335
|
+
function tokens(line: string): string[] {
|
|
1336
|
+
return (
|
|
1337
|
+
line.match(/\s+[^\s"'<>/=]+=(?:"[^"]*"|'[^']*')|\s+|[A-Za-z0-9_.:+-]+|[^\sA-Za-z0-9_.:+-]/g) ??
|
|
1338
|
+
[]
|
|
1339
|
+
)
|
|
1340
|
+
}
|
|
1341
|
+
|
|
1342
|
+
/** Where each token starts, and the end past the last. */
|
|
1343
|
+
function offsets(parts: readonly string[]): number[] {
|
|
1344
|
+
const at = [0]
|
|
1345
|
+
for (const part of parts) at.push((at.at(-1) ?? 0) + part.length)
|
|
1346
|
+
return at
|
|
1347
|
+
}
|
|
1348
|
+
|
|
1349
|
+
/** Whether two lines are one written again: what they start and end alike is most of them. */
|
|
1350
|
+
function alike(left: string, right: string): boolean {
|
|
1351
|
+
let head = 0
|
|
1352
|
+
while (head < left.length && head < right.length && left[head] === right[head]) head += 1
|
|
1353
|
+
let tail = 0
|
|
1354
|
+
while (
|
|
1355
|
+
tail < left.length - head &&
|
|
1356
|
+
tail < right.length - head &&
|
|
1357
|
+
left[left.length - 1 - tail] === right[right.length - 1 - tail]
|
|
1358
|
+
) {
|
|
1359
|
+
tail += 1
|
|
1360
|
+
}
|
|
1361
|
+
return (head + tail) * 2 >= Math.max(left.length, right.length)
|
|
1362
|
+
}
|
|
1363
|
+
|
|
1364
|
+
/** Runs an operation on a working copy, and checks that what it leaves is still a video. */
|
|
1365
|
+
function run(sources: VideoSources, operation: (work: Working) => void): VideoEditResult {
|
|
1366
|
+
const work = new Working(sources)
|
|
1367
|
+
try {
|
|
1368
|
+
operation(work)
|
|
1369
|
+
markCutChains(work)
|
|
1370
|
+
// Scenes moved, or a file holding parts of a clip that goes on changed: its parts agree again.
|
|
1371
|
+
if (work.opened || work.changed().some((path) => holdsParts(work, path))) settleChains(work)
|
|
1372
|
+
if (work.opened) fitRoot(work, work.opened)
|
|
1373
|
+
} catch (error) {
|
|
1374
|
+
if (error instanceof EditError) return { error: error.message }
|
|
1375
|
+
throw error
|
|
1376
|
+
}
|
|
1377
|
+
|
|
1378
|
+
const { edit, files } = work.result()
|
|
1379
|
+
const index = files.get(VIDEO_ENTRY)
|
|
1380
|
+
if (typeof index !== "string") return { error: `A video needs '${VIDEO_ENTRY}'.` }
|
|
1381
|
+
|
|
1382
|
+
const read = readVideoComposition(index, (path) => {
|
|
1383
|
+
const text = files.get(path)
|
|
1384
|
+
return typeof text === "string" ? text : undefined
|
|
1385
|
+
})
|
|
1386
|
+
if ("error" in read) return { error: read.error }
|
|
1387
|
+
|
|
1388
|
+
for (const scene of read.composition.scenes) {
|
|
1389
|
+
if (!readSceneTimeline(files.get(scene.src) as string)) {
|
|
1390
|
+
return { error: `${scene.src}: a scene file is one <template> holding its composition.` }
|
|
1391
|
+
}
|
|
1392
|
+
}
|
|
1393
|
+
|
|
1394
|
+
// No HTML file grows past what publishing reads, as a look carried in for many clips might.
|
|
1395
|
+
const limit = ARTIFACT_LIMITS.maxSceneBytes
|
|
1396
|
+
for (const change of edit) {
|
|
1397
|
+
if (!/\.html?$/i.test(change.path) || "remove" in change) continue
|
|
1398
|
+
const after = new TextEncoder().encode(files.get(change.path) ?? "").length
|
|
1399
|
+
const before = new TextEncoder().encode(sources.get(change.path) ?? "").length
|
|
1400
|
+
if (after > limit && after > before) {
|
|
1401
|
+
return {
|
|
1402
|
+
error: `'${change.path}' would be ${after} bytes: each of a video's HTML files holds at most ${limit}.`,
|
|
1403
|
+
refusal: "too_large",
|
|
1404
|
+
}
|
|
1405
|
+
}
|
|
1406
|
+
}
|
|
1407
|
+
return { edit: [...edit, ...unplayed(sources, files, edit)] }
|
|
1408
|
+
}
|
|
1409
|
+
|
|
1410
|
+
/**
|
|
1411
|
+
* The recordings and pictures an edit took the last clip of: each leaves the version with it, and
|
|
1412
|
+
* an undo of the edit brings it back. Only those a file the edit changed played, and no text file
|
|
1413
|
+
* of the version names now, under any spelling: what nothing played before it stays, as the Agent
|
|
1414
|
+
* may have put it there for later, and when unsure, a file stays.
|
|
1415
|
+
*/
|
|
1416
|
+
function unplayed(
|
|
1417
|
+
sources: VideoSources,
|
|
1418
|
+
files: ReadonlyMap<string, string | null>,
|
|
1419
|
+
edit: VideoEdit
|
|
1420
|
+
): VideoFileEdit[] {
|
|
1421
|
+
const named = (path: string, text: string | null | undefined) =>
|
|
1422
|
+
new Set(
|
|
1423
|
+
[...(text ?? "").matchAll(/\ssrc\s*=\s*(["'])([^"']+)\1/gi)].flatMap((match) => {
|
|
1424
|
+
const found = videoFilePath(path, match[2] ?? "", (each) => sources.has(each))
|
|
1425
|
+
return found === undefined ? [] : [found]
|
|
1426
|
+
})
|
|
1427
|
+
)
|
|
1428
|
+
|
|
1429
|
+
const gone = new Set<string>()
|
|
1430
|
+
for (const change of edit) {
|
|
1431
|
+
if (!/\.html?$/i.test(change.path)) continue
|
|
1432
|
+
const now = named(change.path, files.get(change.path))
|
|
1433
|
+
for (const path of named(change.path, sources.get(change.path))) {
|
|
1434
|
+
if (!now.has(path) && sources.get(path) === null && files.has(path)) gone.add(path)
|
|
1435
|
+
}
|
|
1436
|
+
}
|
|
1437
|
+
if (gone.size === 0) return []
|
|
1438
|
+
|
|
1439
|
+
// Still named anywhere, it stays: by any text file of the version, hosted or not, in a style, a
|
|
1440
|
+
// script, a picture's `href`, under any spelling that reaches it, whose name ends with its own.
|
|
1441
|
+
const texts = [...files.values()].filter((text): text is string => typeof text === "string")
|
|
1442
|
+
return [...gone]
|
|
1443
|
+
.filter((path) => {
|
|
1444
|
+
const name = path.slice(path.lastIndexOf("/") + 1)
|
|
1445
|
+
const spellings = new Set(
|
|
1446
|
+
[name, encodeURIComponent(name), encodeURI(name)].flatMap((each) => [
|
|
1447
|
+
each,
|
|
1448
|
+
// As HTML writes it, `a&b.png`, and as CSS escapes it, `my\ clip.png`.
|
|
1449
|
+
each.replace(/[&<>"']/g, (char) => HTML_ENTITIES[char] ?? char),
|
|
1450
|
+
each.replace(/[^\w.-]/g, (char) => `\\${char}`),
|
|
1451
|
+
])
|
|
1452
|
+
)
|
|
1453
|
+
return !texts.some((text) => [...spellings].some((spelling) => text.includes(spelling)))
|
|
1454
|
+
})
|
|
1455
|
+
.map((path) => ({ path, remove: true as const }))
|
|
1456
|
+
}
|
|
1457
|
+
|
|
1458
|
+
/** How HTML writes the characters it escapes. */
|
|
1459
|
+
const HTML_ENTITIES: Readonly<Record<string, string>> = {
|
|
1460
|
+
"&": "&",
|
|
1461
|
+
"<": "<",
|
|
1462
|
+
">": ">",
|
|
1463
|
+
'"': """,
|
|
1464
|
+
"'": "'",
|
|
1465
|
+
}
|
|
1466
|
+
|
|
1467
|
+
/** Runs an operation that places clips, and returns where it placed them with its edit. */
|
|
1468
|
+
function placing(
|
|
1469
|
+
sources: VideoSources,
|
|
1470
|
+
operation: (work: Working) => Placement | undefined
|
|
1471
|
+
): VideoPlaceResult {
|
|
1472
|
+
let placement: Placement | undefined
|
|
1473
|
+
const result = run(sources, (work) => {
|
|
1474
|
+
placement = operation(work)
|
|
1475
|
+
})
|
|
1476
|
+
return "edit" in result && placement ? { ...result, ...placement } : result
|
|
1477
|
+
}
|
|
1478
|
+
|
|
1479
|
+
// Scenes, inside ----------------------------------------------------------------------------------
|
|
1480
|
+
|
|
1481
|
+
function rootOf(work: Working): VideoRoot {
|
|
1482
|
+
const read = parseVideoRoot(work.text(VIDEO_ENTRY))
|
|
1483
|
+
if (!("error" in read)) return read.root
|
|
1484
|
+
|
|
1485
|
+
// Once open, the root has room for any length a video may have: a scene past it is too long.
|
|
1486
|
+
if (work.opened && read.error.includes("after the root composition's data-duration")) {
|
|
1487
|
+
throw new EditError(`A video lasts at most ${ARTIFACT_LIMITS.maxVideoSeconds} s.`)
|
|
1488
|
+
}
|
|
1489
|
+
throw new EditError(read.error)
|
|
1490
|
+
}
|
|
1491
|
+
|
|
1492
|
+
/**
|
|
1493
|
+
* The root before the operation moves its first scene, read once: the root then writes room for
|
|
1494
|
+
* any length, so that each step after reads as a video whatever the scenes' end, until
|
|
1495
|
+
* {@link fitRoot} writes the length it ends with.
|
|
1496
|
+
*/
|
|
1497
|
+
function openRoot(work: Working): VideoRoot {
|
|
1498
|
+
if (work.opened) return work.opened
|
|
1499
|
+
|
|
1500
|
+
const before = rootOf(work)
|
|
1501
|
+
work.opened = before
|
|
1502
|
+
if (before.root.attributes["data-duration"]) {
|
|
1503
|
+
setAttributes(work, VIDEO_ENTRY, before.root, {
|
|
1504
|
+
"data-duration": String(ARTIFACT_LIMITS.maxVideoSeconds),
|
|
1505
|
+
})
|
|
1506
|
+
}
|
|
1507
|
+
return before
|
|
1508
|
+
}
|
|
1509
|
+
|
|
1510
|
+
function hostOf(work: Working, scene: string): VideoRoot["scenes"][number] {
|
|
1511
|
+
const found = rootOf(work).scenes.find((each) => each.id === scene)
|
|
1512
|
+
if (!found) throw new EditError(noScene(scene))
|
|
1513
|
+
return found
|
|
1514
|
+
}
|
|
1515
|
+
|
|
1516
|
+
/** The scene a file is, if it is one. */
|
|
1517
|
+
function sceneOf(work: Working, file: string): VideoRoot["scenes"][number] | undefined {
|
|
1518
|
+
if (file === VIDEO_ENTRY) return undefined
|
|
1519
|
+
return rootOf(work).scenes.find((each) => each.src === file)
|
|
1520
|
+
}
|
|
1521
|
+
|
|
1522
|
+
function noScene(scene: string): string {
|
|
1523
|
+
return `The video has no scene '${scene}'.`
|
|
1524
|
+
}
|
|
1525
|
+
|
|
1526
|
+
/** Writes a scene's length, and moves the scenes at a number of seconds after it as much. */
|
|
1527
|
+
function lengthenScene(work: Working, scene: string, ms: number): void {
|
|
1528
|
+
openRoot(work)
|
|
1529
|
+
const now = rootOf(work)
|
|
1530
|
+
const host = now.scenes.find((each) => each.id === scene)
|
|
1531
|
+
if (!host) throw new EditError(noScene(scene))
|
|
1532
|
+
|
|
1533
|
+
const delta = ms - host.durationMs
|
|
1534
|
+
if (delta === 0) return
|
|
1535
|
+
|
|
1536
|
+
const end = host.startMs + host.durationMs
|
|
1537
|
+
setAttributes(work, VIDEO_ENTRY, host.host, { "data-duration": seconds(ms) })
|
|
1538
|
+
for (const each of now.scenes) {
|
|
1539
|
+
if (each.id === scene || each.start.type !== "at" || each.startMs < end - SAME_MS) continue
|
|
1540
|
+
setAttributes(work, VIDEO_ENTRY, hostOf(work, each.id).host, {
|
|
1541
|
+
"data-start": seconds(Math.max(0, each.startMs + delta)),
|
|
1542
|
+
})
|
|
1543
|
+
}
|
|
1544
|
+
}
|
|
1545
|
+
|
|
1546
|
+
/**
|
|
1547
|
+
* Removes a scene from the root as it is now: its host, and its file unless another scene plays
|
|
1548
|
+
* it. The scenes that followed it start where it started, keeping their own overlap or gap.
|
|
1549
|
+
*/
|
|
1550
|
+
function dropScene(work: Working, scene: string): void {
|
|
1551
|
+
const now = rootOf(work)
|
|
1552
|
+
const removed = now.scenes.find((each) => each.id === scene)
|
|
1553
|
+
if (!removed) throw new EditError(noScene(scene))
|
|
1554
|
+
if (now.scenes.length === 1) throw new EditError("A video keeps at least one scene.")
|
|
1555
|
+
|
|
1556
|
+
const end = removed.startMs + removed.durationMs
|
|
1557
|
+
for (const each of now.scenes) {
|
|
1558
|
+
if (each.id === scene) continue
|
|
1559
|
+
|
|
1560
|
+
if (startsAfter(each.start, removed)) {
|
|
1561
|
+
const offset = each.start.type === "after" ? each.start.offset : 0
|
|
1562
|
+
const start =
|
|
1563
|
+
removed.start.type === "at"
|
|
1564
|
+
? seconds(Math.max(0, removed.startMs + offset * 1000))
|
|
1565
|
+
: chainedStart(removed.start.scene, removed.start.offset + offset)
|
|
1566
|
+
setAttributes(work, VIDEO_ENTRY, hostOf(work, each.id).host, { "data-start": start })
|
|
1567
|
+
} else if (each.start.type === "at" && each.startMs >= end - SAME_MS) {
|
|
1568
|
+
setAttributes(work, VIDEO_ENTRY, hostOf(work, each.id).host, {
|
|
1569
|
+
"data-start": seconds(each.startMs - removed.durationMs),
|
|
1570
|
+
})
|
|
1571
|
+
}
|
|
1572
|
+
}
|
|
1573
|
+
|
|
1574
|
+
const block = lineBlock(work.text(VIDEO_ENTRY), hostOf(work, scene).host.element)
|
|
1575
|
+
work.replace(VIDEO_ENTRY, block.start, block.end, "")
|
|
1576
|
+
if (!now.scenes.some((each) => each.id !== scene && each.src === removed.src)) {
|
|
1577
|
+
work.remove(removed.src)
|
|
1578
|
+
}
|
|
1579
|
+
}
|
|
1580
|
+
|
|
1581
|
+
/** Whether a scene's start names `scene`, by its id or its host's own. */
|
|
1582
|
+
function startsAfter(start: VideoStart, scene: { readonly id: string; readonly hostId?: string }) {
|
|
1583
|
+
return start.type === "after" && (start.scene === scene.id || start.scene === scene.hostId)
|
|
1584
|
+
}
|
|
1585
|
+
|
|
1586
|
+
/**
|
|
1587
|
+
* How long after the scene before it a scene starts, in seconds, sooner when negative: its own
|
|
1588
|
+
* offset when it names that scene, else the gap it leaves after it.
|
|
1589
|
+
*/
|
|
1590
|
+
function gapBefore(scenes: VideoRoot["scenes"], scene: VideoRoot["scenes"][number]): number {
|
|
1591
|
+
if (scene.start.type === "after") return scene.start.offset
|
|
1592
|
+
|
|
1593
|
+
const previous = scenes[scenes.findIndex((each) => each.id === scene.id) - 1]
|
|
1594
|
+
if (!previous) return 0
|
|
1595
|
+
return (scene.startMs - previous.startMs - previous.durationMs) / 1000
|
|
1596
|
+
}
|
|
1597
|
+
|
|
1598
|
+
/** `data-start` for a scene that starts when `after` ends, `offset` seconds later or sooner. */
|
|
1599
|
+
function chainedStart(after: string, offset: number): string {
|
|
1600
|
+
const amount = Math.round(Math.abs(offset) * 1000) / 1000
|
|
1601
|
+
if (amount === 0) return after
|
|
1602
|
+
return `${after} ${offset < 0 ? "-" : "+"} ${amount}`
|
|
1603
|
+
}
|
|
1604
|
+
|
|
1605
|
+
/**
|
|
1606
|
+
* Stacks the scenes on screen together in play order, the later on top, as a crossfade shows it.
|
|
1607
|
+
* HyperFrames positions each host and paints them in the file's order unless a `z-index` says
|
|
1608
|
+
* otherwise. A host never moves its line, which someone else may be editing at once; each host that
|
|
1609
|
+
* must sit above one later in the file writes the least `z-index` that puts it there, and drops one
|
|
1610
|
+
* it no longer needs. Scenes never on screen together cover nothing, so their order does not count.
|
|
1611
|
+
*
|
|
1612
|
+
* What the root paints of its own over the scenes, a captions bar, stays over them: once a scene
|
|
1613
|
+
* has a layer, each writes one above the highest, and drops it with the last.
|
|
1614
|
+
*/
|
|
1615
|
+
function restack(work: Working): void {
|
|
1616
|
+
const text = work.text(VIDEO_ENTRY)
|
|
1617
|
+
const play = rootOf(work).scenes
|
|
1618
|
+
const line = new Map(
|
|
1619
|
+
[...play]
|
|
1620
|
+
.sort((a, b) => a.host.element.start - b.host.element.start)
|
|
1621
|
+
.map((scene, index) => [scene.id, index])
|
|
1622
|
+
)
|
|
1623
|
+
|
|
1624
|
+
// From the first to start, each scene's least layer above every scene on screen with it before.
|
|
1625
|
+
const wanted = new Map<string, number>()
|
|
1626
|
+
play.forEach((each, index) => {
|
|
1627
|
+
let least = 0
|
|
1628
|
+
for (const below of play.slice(0, index)) {
|
|
1629
|
+
if (!together(below, each)) continue
|
|
1630
|
+
const under = wanted.get(below.id) ?? 0
|
|
1631
|
+
const later = (line.get(each.id) ?? 0) > (line.get(below.id) ?? 0)
|
|
1632
|
+
least = Math.max(least, later ? under : under + 1)
|
|
1633
|
+
}
|
|
1634
|
+
wanted.set(each.id, least)
|
|
1635
|
+
})
|
|
1636
|
+
|
|
1637
|
+
const writes: { readonly source: VideoElementSource; readonly layer: number }[] = []
|
|
1638
|
+
for (const scene of play) {
|
|
1639
|
+
const layer = wanted.get(scene.id) ?? 0
|
|
1640
|
+
if (layer !== layerOf(text, scene.host)) writes.push({ source: scene.host, layer })
|
|
1641
|
+
}
|
|
1642
|
+
|
|
1643
|
+
// The root's own layer is one above the highest scene's, so one it wrote is that, and one
|
|
1644
|
+
// written higher is someone's own, kept.
|
|
1645
|
+
const was = Math.max(0, ...play.map((scene) => layerOf(text, scene.host)))
|
|
1646
|
+
const top = Math.max(0, ...wanted.values())
|
|
1647
|
+
for (const source of readRootLayers(text)) {
|
|
1648
|
+
const layer = layerOf(text, source)
|
|
1649
|
+
const own = was > 0 && layer === was + 1
|
|
1650
|
+
if (top === 0 && own) writes.push({ source, layer: 0 })
|
|
1651
|
+
else if (top > 0 && layer !== top + 1 && (own || layer <= top)) {
|
|
1652
|
+
writes.push({ source, layer: top + 1 })
|
|
1653
|
+
}
|
|
1654
|
+
}
|
|
1655
|
+
|
|
1656
|
+
// From the last in the file, so each span read still names what it named.
|
|
1657
|
+
for (const { source, layer } of writes.sort((a, b) => b.source.tag.start - a.source.tag.start)) {
|
|
1658
|
+
const style = source.attributes.style
|
|
1659
|
+
setAttributes(work, VIDEO_ENTRY, source, {
|
|
1660
|
+
style: withLayer(style ? attributeValue(text, style) : undefined, layer),
|
|
1661
|
+
})
|
|
1662
|
+
}
|
|
1663
|
+
}
|
|
1664
|
+
|
|
1665
|
+
type Stacked = VideoRoot["scenes"][number]
|
|
1666
|
+
|
|
1667
|
+
/** Whether two scenes are on screen together, for longer than a rounding. */
|
|
1668
|
+
function together(a: Stacked, b: Stacked): boolean {
|
|
1669
|
+
return (
|
|
1670
|
+
a.startMs < b.startMs + b.durationMs - SAME_MS && b.startMs < a.startMs + a.durationMs - SAME_MS
|
|
1671
|
+
)
|
|
1672
|
+
}
|
|
1673
|
+
|
|
1674
|
+
/** An element's `z-index`, as its `style` writes it: 0 for none. */
|
|
1675
|
+
function layerOf(text: string, source: Pick<VideoElementSource, "attributes">): number {
|
|
1676
|
+
const at = source.attributes.style
|
|
1677
|
+
const style = at ? attributeValue(text, at) : undefined
|
|
1678
|
+
const written = style && /(?:^|;)\s*z-index\s*:\s*(-?\d+)\s*(?:;|$)/i.exec(style)
|
|
1679
|
+
return written ? Number(written[1]) : 0
|
|
1680
|
+
}
|
|
1681
|
+
|
|
1682
|
+
/** A `style` whose `z-index` is `layer`, or has none at 0: null once nothing else is left. */
|
|
1683
|
+
function withLayer(style: string | undefined, layer: number): string | null {
|
|
1684
|
+
const rest = (style ?? "")
|
|
1685
|
+
.split(";")
|
|
1686
|
+
.map((each) => each.trim())
|
|
1687
|
+
.filter((each) => each !== "" && !/^z-index\s*:/i.test(each))
|
|
1688
|
+
if (layer > 0) rest.push(`z-index: ${layer}`)
|
|
1689
|
+
return rest.length > 0 ? rest.join("; ") : null
|
|
1690
|
+
}
|
|
1691
|
+
|
|
1692
|
+
/**
|
|
1693
|
+
* Writes the root's length once its scenes moved: what it played past the last scene, it still
|
|
1694
|
+
* plays past it, and a sound of its own that ran to its end runs to the new one. What the root
|
|
1695
|
+
* plays during one scene, a line of voice-over, moves with that scene, and goes with it; so does
|
|
1696
|
+
* the poster, kept within it. A poster past the end moves to its last frame.
|
|
1697
|
+
*/
|
|
1698
|
+
function fitRoot(work: Working, before: VideoRoot): void {
|
|
1699
|
+
const now = rootOf(work)
|
|
1700
|
+
const end = (root: VideoRoot) =>
|
|
1701
|
+
Math.max(...root.scenes.map((scene) => scene.startMs + scene.durationMs))
|
|
1702
|
+
const tail = Math.max(0, before.durationMs - end(before))
|
|
1703
|
+
const duration = end(now) + tail
|
|
1704
|
+
|
|
1705
|
+
// Where each scene of the root before now is: none for one removed.
|
|
1706
|
+
const moved = (scene: VideoRoot["scenes"][number] | undefined) =>
|
|
1707
|
+
scene && now.scenes.find((each) => each.id === scene.id)
|
|
1708
|
+
|
|
1709
|
+
// A clip of the root's own goes with the scene it belongs to, and keeps its time into it. Once
|
|
1710
|
+
// that scene is gone, what followed it keeps its time, as when a clip is removed.
|
|
1711
|
+
const owners = new Map<string, VideoRoot["scenes"][number]>()
|
|
1712
|
+
for (const clip of readRootTimeline(work.text(VIDEO_ENTRY))?.clips ?? []) {
|
|
1713
|
+
const owner = numericStart(work, VIDEO_ENTRY, clip) ? sceneOfClip(before, clip) : undefined
|
|
1714
|
+
if (owner) owners.set(clip.ref, owner)
|
|
1715
|
+
}
|
|
1716
|
+
freezeFollowers(work, VIDEO_ENTRY, (clip) => {
|
|
1717
|
+
const owner = owners.get(clip.ref)
|
|
1718
|
+
return owner !== undefined && !moved(owner)
|
|
1719
|
+
})
|
|
1720
|
+
|
|
1721
|
+
const plan = new Map<string, ClipChange>()
|
|
1722
|
+
for (const clip of readRootTimeline(work.text(VIDEO_ENTRY))?.clips ?? []) {
|
|
1723
|
+
const owner = owners.get(clip.ref)
|
|
1724
|
+
const scene = moved(owner)
|
|
1725
|
+
if (owner && !scene) {
|
|
1726
|
+
plan.set(clip.ref, "remove")
|
|
1727
|
+
continue
|
|
1728
|
+
}
|
|
1729
|
+
|
|
1730
|
+
const start = scene && owner ? clip.startMs + scene.startMs - owner.startMs : clip.startMs
|
|
1731
|
+
const change: Record<string, string> = {}
|
|
1732
|
+
if (start !== clip.startMs) change["data-start"] = seconds(start)
|
|
1733
|
+
|
|
1734
|
+
// A sound that ran to the video's end runs to its new one.
|
|
1735
|
+
if (
|
|
1736
|
+
duration !== before.durationMs &&
|
|
1737
|
+
clip.durationMs !== undefined &&
|
|
1738
|
+
Math.abs(clip.startMs + clip.durationMs - before.durationMs) <= SAME_MS &&
|
|
1739
|
+
duration - start >= MIN_CLIP_MS
|
|
1740
|
+
) {
|
|
1741
|
+
change["data-duration"] = seconds(duration - start)
|
|
1742
|
+
}
|
|
1743
|
+
if (Object.keys(change).length > 0) plan.set(clip.ref, change)
|
|
1744
|
+
}
|
|
1745
|
+
rewriteClips(work, VIDEO_ENTRY, plan)
|
|
1746
|
+
pruneLooks(work, VIDEO_ENTRY)
|
|
1747
|
+
|
|
1748
|
+
// Its length and its poster at once: either alone may not read as a video.
|
|
1749
|
+
const poster = posterAfter(before, now, duration)
|
|
1750
|
+
setAttributes(work, VIDEO_ENTRY, rootOf(work).root, {
|
|
1751
|
+
...(before.root.attributes["data-duration"] ? { "data-duration": seconds(duration) } : {}),
|
|
1752
|
+
...(poster === undefined || poster === before.posterMs
|
|
1753
|
+
? {}
|
|
1754
|
+
: { "data-poster": seconds(poster) }),
|
|
1755
|
+
})
|
|
1756
|
+
}
|
|
1757
|
+
|
|
1758
|
+
/**
|
|
1759
|
+
* The scene a clip of the root belongs to: the one it plays most during, when it lasts no longer
|
|
1760
|
+
* than that scene. A line of voice-over belongs to the scene it is said over; a soundtrack under
|
|
1761
|
+
* every scene belongs to none.
|
|
1762
|
+
*/
|
|
1763
|
+
function sceneOfClip(root: VideoRoot, clip: VideoClip): VideoRoot["scenes"][number] | undefined {
|
|
1764
|
+
if (clip.durationMs === undefined) return undefined
|
|
1765
|
+
|
|
1766
|
+
const clipEnd = clip.startMs + clip.durationMs
|
|
1767
|
+
let owner: VideoRoot["scenes"][number] | undefined
|
|
1768
|
+
let most = 0
|
|
1769
|
+
for (const scene of root.scenes) {
|
|
1770
|
+
const during =
|
|
1771
|
+
Math.min(clipEnd, scene.startMs + scene.durationMs) - Math.max(clip.startMs, scene.startMs)
|
|
1772
|
+
if (during > 0 && during >= most) {
|
|
1773
|
+
owner = scene
|
|
1774
|
+
most = during
|
|
1775
|
+
}
|
|
1776
|
+
}
|
|
1777
|
+
return owner && clip.durationMs <= owner.durationMs + SAME_MS ? owner : undefined
|
|
1778
|
+
}
|
|
1779
|
+
|
|
1780
|
+
/**
|
|
1781
|
+
* Where the poster is once the scenes moved: as far into its scene as it was, the scene on top at
|
|
1782
|
+
* its instant, and within it; where it was without one. Past the end, the last frame.
|
|
1783
|
+
*/
|
|
1784
|
+
function posterAfter(before: VideoRoot, now: VideoRoot, duration: number): number | undefined {
|
|
1785
|
+
const posterMs = before.posterMs
|
|
1786
|
+
if (posterMs === undefined) return undefined
|
|
1787
|
+
|
|
1788
|
+
const frame = Math.ceil(1000 / before.fps)
|
|
1789
|
+
const owner = before.scenes
|
|
1790
|
+
.filter((scene) => scene.startMs <= posterMs && posterMs < scene.startMs + scene.durationMs)
|
|
1791
|
+
.at(-1)
|
|
1792
|
+
const scene = owner && now.scenes.find((each) => each.id === owner.id)
|
|
1793
|
+
const at =
|
|
1794
|
+
owner && scene
|
|
1795
|
+
? scene.startMs + Math.min(posterMs - owner.startMs, Math.max(0, scene.durationMs - frame))
|
|
1796
|
+
: posterMs
|
|
1797
|
+
return at >= duration ? Math.max(0, duration - frame) : at
|
|
1798
|
+
}
|
|
1799
|
+
|
|
1800
|
+
/**
|
|
1801
|
+
* A scene file copied under a new id: its composition's id, its timeline's name, and the id of
|
|
1802
|
+
* every element, with what names it.
|
|
1803
|
+
*/
|
|
1804
|
+
function copySceneFile(text: string, from: string, to: string, taken: Set<string>): string {
|
|
1805
|
+
const quoted = (name: string) => name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
|
|
1806
|
+
let copy = text
|
|
1807
|
+
.replace(
|
|
1808
|
+
new RegExp(`(data-composition-id\\s*=\\s*)(["'])${quoted(from)}\\2`, "g"),
|
|
1809
|
+
(_, name, quote) => `${name}${quote}${to}${quote}`
|
|
1810
|
+
)
|
|
1811
|
+
.replace(
|
|
1812
|
+
new RegExp(`(__timelines\\s*\\[\\s*)(["'])${quoted(from)}\\2(\\s*\\])`, "g"),
|
|
1813
|
+
(_, open, quote, close) => `${open}${quote}${to}${quote}${close}`
|
|
1814
|
+
)
|
|
1815
|
+
|
|
1816
|
+
// A copy goes on from nothing: the clip a part of it went on from is the original's.
|
|
1817
|
+
copy = copy.replace(/\s+data-continues\s*=\s*(["'])[^"']*\1/g, "")
|
|
1818
|
+
|
|
1819
|
+
// Every element takes an id of its own, and whatever names it in the file follows, each read
|
|
1820
|
+
// once: the attribute, the look carried for it, a start chained to it by its whole name, and a
|
|
1821
|
+
// selector naming it, `#id` or `[id=…]`, in a style's preludes and a script's selector strings,
|
|
1822
|
+
// never a value, such as a colour `#fade`.
|
|
1823
|
+
const renamed = new Map<string, string>()
|
|
1824
|
+
for (const match of copy.matchAll(/\sid\s*=\s*(["'])([^"']+)\1/g)) {
|
|
1825
|
+
const id = match[2]
|
|
1826
|
+
if (!id || renamed.has(id)) continue
|
|
1827
|
+
const fresh = uniqueName(id, taken)
|
|
1828
|
+
taken.add(fresh)
|
|
1829
|
+
renamed.set(id, fresh)
|
|
1830
|
+
}
|
|
1831
|
+
if (renamed.size === 0) return copy
|
|
1832
|
+
const name = (id: string) => renamed.get(id) ?? id
|
|
1833
|
+
|
|
1834
|
+
return copy
|
|
1835
|
+
.replace(
|
|
1836
|
+
/(\sid\s*=\s*)(["'])([^"']+)\2/g,
|
|
1837
|
+
(_, attribute, quote, id) => `${attribute}${quote}${name(id)}${quote}`
|
|
1838
|
+
)
|
|
1839
|
+
.replace(
|
|
1840
|
+
/(\sdata-start\s*=\s*(["'])\s*)([A-Za-z][\w.:-]*)(?=\s+[+-]\s|\s*\2)/g,
|
|
1841
|
+
(_, start, _quote, id) => `${start}${name(id)}`
|
|
1842
|
+
)
|
|
1843
|
+
.replace(
|
|
1844
|
+
/(<style\b[^>]*>)([\s\S]*?)(<\/style>)/gi,
|
|
1845
|
+
(_, open, css, close) => `${open}${renameInCss(css, name)}${close}`
|
|
1846
|
+
)
|
|
1847
|
+
.replace(
|
|
1848
|
+
/(<script\b[^>]*>)([\s\S]*?)(<\/script>)/gi,
|
|
1849
|
+
(_, open, script, close) => `${open}${renameInScript(script, name)}${close}`
|
|
1850
|
+
)
|
|
1851
|
+
}
|
|
1852
|
+
|
|
1853
|
+
/** A selector with each id it names, `#id` or `[id=…]`, named as `to` says. */
|
|
1854
|
+
function renameInSelector(selector: string, to: (id: string) => string): string {
|
|
1855
|
+
return selector
|
|
1856
|
+
.replace(/#([A-Za-z_][\w-]*)/g, (_, id) => `#${to(id)}`)
|
|
1857
|
+
.replace(
|
|
1858
|
+
/(\[\s*id\s*=\s*)(["']?)([^"'\]\s]+)\2(\s*\])/g,
|
|
1859
|
+
(_, open, quote, id, close) => `${open}${quote}${to(id)}${quote}${close}`
|
|
1860
|
+
)
|
|
1861
|
+
}
|
|
1862
|
+
|
|
1863
|
+
/**
|
|
1864
|
+
* A stylesheet with the ids its selectors name renamed, and nothing else: a rule's prelude and a
|
|
1865
|
+
* nested rule's are selectors; a declaration's value, an at-rule's prelude, a string and a comment
|
|
1866
|
+
* are not.
|
|
1867
|
+
*/
|
|
1868
|
+
function renameInCss(css: string, to: (id: string) => string): string {
|
|
1869
|
+
let out = ""
|
|
1870
|
+
let segment = ""
|
|
1871
|
+
for (let at = 0; at < css.length; at += 1) {
|
|
1872
|
+
const char = css[at] ?? ""
|
|
1873
|
+
if (char === "/" && css[at + 1] === "*") {
|
|
1874
|
+
const end = css.indexOf("*/", at + 2)
|
|
1875
|
+
const stop = end < 0 ? css.length : end + 2
|
|
1876
|
+
segment += css.slice(at, stop)
|
|
1877
|
+
at = stop - 1
|
|
1878
|
+
continue
|
|
1879
|
+
}
|
|
1880
|
+
if (char === '"' || char === "'") {
|
|
1881
|
+
let end = at + 1
|
|
1882
|
+
while (end < css.length && css[end] !== char) end += css[end] === "\\" ? 2 : 1
|
|
1883
|
+
segment += css.slice(at, end + 1)
|
|
1884
|
+
at = end
|
|
1885
|
+
continue
|
|
1886
|
+
}
|
|
1887
|
+
// What ends at a `{` is a prelude, a rule's or a nested one's; at a `;` or a `}`, a
|
|
1888
|
+
// declaration or a statement.
|
|
1889
|
+
if (char === "{") {
|
|
1890
|
+
out += segment.trimStart().startsWith("@") ? segment : renameInSelector(segment, to)
|
|
1891
|
+
out += char
|
|
1892
|
+
segment = ""
|
|
1893
|
+
continue
|
|
1894
|
+
}
|
|
1895
|
+
if (char === ";" || char === "}") {
|
|
1896
|
+
out += segment + char
|
|
1897
|
+
segment = ""
|
|
1898
|
+
continue
|
|
1899
|
+
}
|
|
1900
|
+
segment += char
|
|
1901
|
+
}
|
|
1902
|
+
return out + segment
|
|
1903
|
+
}
|
|
1904
|
+
|
|
1905
|
+
/**
|
|
1906
|
+
* A script with the ids its selector strings name renamed, as `querySelector('#fade')` and
|
|
1907
|
+
* `\`${scene} #talk\``, and `getElementById`'s; a string that is a property's value, such as a
|
|
1908
|
+
* colour `{ color: "#fade" }`, is not a selector.
|
|
1909
|
+
*/
|
|
1910
|
+
function renameInScript(script: string, to: (id: string) => string): string {
|
|
1911
|
+
return script.replace(
|
|
1912
|
+
/(["'`])((?:\\.|(?!\1)[^\\])*)\1/g,
|
|
1913
|
+
(whole, quote, inner, offset: number) => {
|
|
1914
|
+
const before = script.slice(Math.max(0, offset - 40), offset)
|
|
1915
|
+
if (/:\s*$/.test(before)) return whole
|
|
1916
|
+
if (/getElementById\(\s*$/.test(before)) return `${quote}${to(inner)}${quote}`
|
|
1917
|
+
return `${quote}${renameInSelector(inner, to)}${quote}`
|
|
1918
|
+
}
|
|
1919
|
+
)
|
|
1920
|
+
}
|
|
1921
|
+
|
|
1922
|
+
/** Every name an id may not take: the scenes', and every element's id in every HTML file. */
|
|
1923
|
+
function takenNames(work: Working): Set<string> {
|
|
1924
|
+
return new Set(nameCounts(work).keys())
|
|
1925
|
+
}
|
|
1926
|
+
|
|
1927
|
+
/** How many times each id and each scene's name is written, in every HTML file. */
|
|
1928
|
+
function nameCounts(work: Working): Map<string, number> {
|
|
1929
|
+
const counts = new Map<string, number>()
|
|
1930
|
+
for (const path of work.paths()) {
|
|
1931
|
+
if (!/\.html?$/i.test(path)) continue
|
|
1932
|
+
const text = work.text(path)
|
|
1933
|
+
for (const match of text.matchAll(/\s(?:id|data-composition-id)\s*=\s*(["'])([^"']*)\1/g)) {
|
|
1934
|
+
if (match[2]) counts.set(match[2], (counts.get(match[2]) ?? 0) + 1)
|
|
1935
|
+
}
|
|
1936
|
+
}
|
|
1937
|
+
return counts
|
|
1938
|
+
}
|
|
1939
|
+
|
|
1940
|
+
/** `base`, or `base-2`, `base-3`… until one is not taken. */
|
|
1941
|
+
function uniqueName(base: string, taken: ReadonlySet<string>): string {
|
|
1942
|
+
const stem = /^[A-Za-z]/.test(base) ? base : `x-${base}`
|
|
1943
|
+
const plain = stem.replace(/-\d+$/, "")
|
|
1944
|
+
// The id a carried look's rules weigh as is no element's.
|
|
1945
|
+
const free = (name: string) => !taken.has(name) && name !== LOOK_ID
|
|
1946
|
+
if (free(stem)) return stem
|
|
1947
|
+
for (let n = 2; ; n += 1) {
|
|
1948
|
+
const name = `${plain}-${n}`
|
|
1949
|
+
if (free(name)) return name
|
|
1950
|
+
}
|
|
1951
|
+
}
|
|
1952
|
+
|
|
1953
|
+
function uniquePath(work: Working, stem: string, extension: string): string {
|
|
1954
|
+
if (!work.has(`${stem}${extension}`)) return `${stem}${extension}`
|
|
1955
|
+
for (let n = 2; ; n += 1) {
|
|
1956
|
+
if (!work.has(`${stem}-${n}${extension}`)) return `${stem}-${n}${extension}`
|
|
1957
|
+
}
|
|
1958
|
+
}
|
|
1959
|
+
|
|
1960
|
+
/** A name an id may take: letters, digits and dashes, from a file's name. */
|
|
1961
|
+
function slug(name: string): string {
|
|
1962
|
+
return name
|
|
1963
|
+
.normalize("NFKD")
|
|
1964
|
+
.replace(/[̀-ͯ]/g, "")
|
|
1965
|
+
.replace(/[^A-Za-z0-9_-]+/g, "-")
|
|
1966
|
+
.replace(/^[^A-Za-z]+/, "")
|
|
1967
|
+
.replace(/-+$/, "")
|
|
1968
|
+
.slice(0, 40)
|
|
1969
|
+
}
|
|
1970
|
+
|
|
1971
|
+
// Clips, inside -----------------------------------------------------------------------------------
|
|
1972
|
+
|
|
1973
|
+
function timelineOf(work: Working, file: string): VideoFileTimeline {
|
|
1974
|
+
const timeline = readTimeline(work, file)
|
|
1975
|
+
if (!timeline) throw new EditError(`${file} holds no composition.`)
|
|
1976
|
+
return timeline
|
|
1977
|
+
}
|
|
1978
|
+
|
|
1979
|
+
/** A file's timeline, read once for each text it holds; none for a file holding no composition. */
|
|
1980
|
+
function readTimeline(work: Working, file: string): VideoFileTimeline | null {
|
|
1981
|
+
const text = work.text(file)
|
|
1982
|
+
const read = work.timelines.get(file)
|
|
1983
|
+
if (read?.text === text) return read.timeline
|
|
1984
|
+
|
|
1985
|
+
const timeline = readVideoFileTimeline(file, text)
|
|
1986
|
+
work.timelines.set(file, { text, timeline })
|
|
1987
|
+
return timeline
|
|
1988
|
+
}
|
|
1989
|
+
|
|
1990
|
+
function clipOf(work: Working, file: string, target: VideoTarget): VideoClip {
|
|
1991
|
+
return resolveTarget(timelineOf(work, file).clips, target, file, "clip")
|
|
1992
|
+
}
|
|
1993
|
+
|
|
1994
|
+
/**
|
|
1995
|
+
* The clips a group names, each once, as the files are before any of them changes: one gone, or
|
|
1996
|
+
* no longer alone like it, refuses the whole edit. `index` is its place among those named.
|
|
1997
|
+
*/
|
|
1998
|
+
function clipsOf(
|
|
1999
|
+
work: Working,
|
|
2000
|
+
clips: readonly VideoClipTarget[]
|
|
2001
|
+
): { readonly file: string; readonly clip: VideoClip; readonly index: number }[] {
|
|
2002
|
+
if (clips.length === 0) throw new EditError("Choose the clips first.")
|
|
2003
|
+
|
|
2004
|
+
const seen = new Set<string>()
|
|
2005
|
+
return clips.flatMap(({ file, clip }, index) => {
|
|
2006
|
+
const found = clipOf(work, file, clip)
|
|
2007
|
+
const key = `${file}\n${found.ref}`
|
|
2008
|
+
if (seen.has(key)) return []
|
|
2009
|
+
seen.add(key)
|
|
2010
|
+
return [{ file, clip: found, index }]
|
|
2011
|
+
})
|
|
2012
|
+
}
|
|
2013
|
+
|
|
2014
|
+
/**
|
|
2015
|
+
* Clips by their file, `index.html` first: what an edit of the video's own moves, a scene's
|
|
2016
|
+
* edit then moves with its scene, as when each is edited alone.
|
|
2017
|
+
*/
|
|
2018
|
+
function byFile<T extends { readonly file: string }>(found: readonly T[]): Map<string, T[]> {
|
|
2019
|
+
const files = new Map<string, T[]>()
|
|
2020
|
+
const ordered = [...found].sort(
|
|
2021
|
+
(a, b) => Number(b.file === VIDEO_ENTRY) - Number(a.file === VIDEO_ENTRY)
|
|
2022
|
+
)
|
|
2023
|
+
for (const each of ordered) files.set(each.file, [...(files.get(each.file) ?? []), each])
|
|
2024
|
+
return files
|
|
2025
|
+
}
|
|
2026
|
+
|
|
2027
|
+
/**
|
|
2028
|
+
* Splits a clip found in its file at `atMs` from its composition's start, and returns the second
|
|
2029
|
+
* part's id: see {@link splitClip}.
|
|
2030
|
+
*/
|
|
2031
|
+
function splitFound(
|
|
2032
|
+
work: Working,
|
|
2033
|
+
file: string,
|
|
2034
|
+
found: VideoClip,
|
|
2035
|
+
atMs: number,
|
|
2036
|
+
endMs: number | undefined
|
|
2037
|
+
): string | undefined {
|
|
2038
|
+
const at = Math.round(atMs)
|
|
2039
|
+
const end = clipEnd(found, endMs)
|
|
2040
|
+
if (at - found.startMs < MIN_CLIP_MS || end - at < MIN_CLIP_MS) {
|
|
2041
|
+
throw new EditError(`Each part of a split clip lasts at least ${MIN_CLIP_MS} ms.`)
|
|
2042
|
+
}
|
|
2043
|
+
|
|
2044
|
+
const parts = clipParts(work, file, found, [
|
|
2045
|
+
{ startMs: found.startMs, mediaStartMs: found.mediaStartMs, durationMs: at - found.startMs },
|
|
2046
|
+
{
|
|
2047
|
+
startMs: at,
|
|
2048
|
+
mediaStartMs: found.mediaStartMs + (at - found.startMs),
|
|
2049
|
+
durationMs: end - at,
|
|
2050
|
+
},
|
|
2051
|
+
])
|
|
2052
|
+
const second = parts.refs[1]
|
|
2053
|
+
replaceClip(work, file, found, parts.html)
|
|
2054
|
+
|
|
2055
|
+
// What followed the clip's end follows the second part's, where that end now is.
|
|
2056
|
+
if (found.id && second) followInstead(work, file, found.id, second)
|
|
2057
|
+
return second
|
|
2058
|
+
}
|
|
2059
|
+
|
|
2060
|
+
/**
|
|
2061
|
+
* Splits a clip as {@link splitFound} does, and when it goes on in later scenes, as two: the
|
|
2062
|
+
* second part heads the parts after it, which go on from it.
|
|
2063
|
+
*/
|
|
2064
|
+
function splitChained(
|
|
2065
|
+
work: Working,
|
|
2066
|
+
chains: ReadonlyMap<string, VideoChain>,
|
|
2067
|
+
file: string,
|
|
2068
|
+
found: VideoClip,
|
|
2069
|
+
atMs: number,
|
|
2070
|
+
endMs: number | undefined
|
|
2071
|
+
): string | undefined {
|
|
2072
|
+
const chain = chains.get(`${file}\n${found.ref}`)
|
|
2073
|
+
const at = videoStartOf(work, file, found) - found.startMs + Math.round(atMs)
|
|
2074
|
+
const second = splitFound(work, file, found, atMs, endMs)
|
|
2075
|
+
if (!chain || !second) return second
|
|
2076
|
+
|
|
2077
|
+
for (const part of chain.parts) {
|
|
2078
|
+
if ((part.file === file && part.clip.ref === found.ref) || part.startMs < at) continue
|
|
2079
|
+
const now = clipOf(work, part.file, videoTarget(part.clip))
|
|
2080
|
+
setAttributes(work, part.file, now.source, { "data-continues": second })
|
|
2081
|
+
}
|
|
2082
|
+
return second
|
|
2083
|
+
}
|
|
2084
|
+
|
|
2085
|
+
/** The element a target names in a file, or why none is: see {@link findVideoTarget}. */
|
|
2086
|
+
function resolveTarget<T extends { readonly ref: string; readonly tag: string }>(
|
|
2087
|
+
items: readonly T[],
|
|
2088
|
+
target: VideoTarget,
|
|
2089
|
+
file: string,
|
|
2090
|
+
what: "clip" | "text"
|
|
2091
|
+
): T {
|
|
2092
|
+
const found = findVideoTarget(items, target)
|
|
2093
|
+
if (!Array.isArray(found)) return found as T
|
|
2094
|
+
|
|
2095
|
+
const ref = typeof target === "string" ? target : target.ref
|
|
2096
|
+
if (typeof target === "string" || isIdRef(ref)) {
|
|
2097
|
+
throw new EditError(`${file} has no ${what} '${ref}'.`)
|
|
2098
|
+
}
|
|
2099
|
+
throw new EditError(
|
|
2100
|
+
found.length === 0
|
|
2101
|
+
? `The ${what} you chose in ${file} changed or was removed meanwhile: choose it again.`
|
|
2102
|
+
: `${file} now has ${found.length} ${what}s like the one you chose: choose it again.`
|
|
2103
|
+
)
|
|
2104
|
+
}
|
|
2105
|
+
|
|
2106
|
+
/**
|
|
2107
|
+
* The element a target names among a file's clips or texts, as the file is now: the one at its
|
|
2108
|
+
* place when it is still what the editor saw there, else the one element that is, wherever it
|
|
2109
|
+
* moved. Otherwise the elements alike, none or several: an edit refuses rather than land on
|
|
2110
|
+
* another element than the one the reader chose, and a selection lets go.
|
|
2111
|
+
*/
|
|
2112
|
+
export function findVideoTarget<T extends { readonly ref: string; readonly tag: string }>(
|
|
2113
|
+
items: readonly T[],
|
|
2114
|
+
target: VideoTarget
|
|
2115
|
+
): T | readonly T[] {
|
|
2116
|
+
const ref = typeof target === "string" ? target : target.ref
|
|
2117
|
+
const placed = items.find((item) => item.ref === ref)
|
|
2118
|
+
if (typeof target === "string" || isIdRef(ref)) return placed ?? []
|
|
2119
|
+
|
|
2120
|
+
if (placed && matches(placed, target)) return placed
|
|
2121
|
+
const alike = items.filter((item) => !isIdRef(item.ref) && matches(item, target))
|
|
2122
|
+
const [only] = alike
|
|
2123
|
+
return only && alike.length === 1 ? only : alike
|
|
2124
|
+
}
|
|
2125
|
+
|
|
2126
|
+
/** Whether an element is still what the editor saw: its tag, words and file. */
|
|
2127
|
+
function matches(
|
|
2128
|
+
item: { readonly tag: string; readonly text?: string; readonly src?: string },
|
|
2129
|
+
target: Exclude<VideoTarget, string>
|
|
2130
|
+
): boolean {
|
|
2131
|
+
return (
|
|
2132
|
+
item.tag === target.tag &&
|
|
2133
|
+
(target.words === undefined || item.text === target.words) &&
|
|
2134
|
+
(target.src === undefined || item.src === target.src)
|
|
2135
|
+
)
|
|
2136
|
+
}
|
|
2137
|
+
|
|
2138
|
+
/** Whether a ref is an element's own id, which no edit around it changes. */
|
|
2139
|
+
function isIdRef(ref: string): boolean {
|
|
2140
|
+
return !/^#\d+$|^[a-z][\w-]*:\d+$/.test(ref)
|
|
2141
|
+
}
|
|
2142
|
+
|
|
2143
|
+
function playsFile(clip: VideoClip): boolean {
|
|
2144
|
+
return clip.kind === "video" || clip.kind === "audio"
|
|
2145
|
+
}
|
|
2146
|
+
|
|
2147
|
+
/** Where a clip ends: its length, else where its file or scene ends it, as `endMs` says. */
|
|
2148
|
+
function clipEnd(clip: VideoClip, endMs: number | undefined): number {
|
|
2149
|
+
if (clip.durationMs !== undefined) return clip.startMs + clip.durationMs
|
|
2150
|
+
if (endMs !== undefined && endMs > clip.startMs) return Math.round(endMs)
|
|
2151
|
+
throw new EditError(`'${clip.ref}' writes no length: give it data-duration first.`)
|
|
2152
|
+
}
|
|
2153
|
+
|
|
2154
|
+
/**
|
|
2155
|
+
* Moves what starts at `fromMs` or after by `deltaMs`, but the clips `except` names, and the
|
|
2156
|
+
* scene's end with it when it ends there or after: a gap closed or opened at `fromMs`.
|
|
2157
|
+
*/
|
|
2158
|
+
function ripple(
|
|
2159
|
+
work: Working,
|
|
2160
|
+
file: string,
|
|
2161
|
+
fromMs: number,
|
|
2162
|
+
deltaMs: number,
|
|
2163
|
+
except: ReadonlySet<string>
|
|
2164
|
+
): void {
|
|
2165
|
+
if (deltaMs === 0) return
|
|
2166
|
+
|
|
2167
|
+
const after = timelineOf(work, file).clips.filter(
|
|
2168
|
+
(clip) =>
|
|
2169
|
+
!except.has(clip.ref) && clip.startMs >= fromMs - SAME_MS && numericStart(work, file, clip)
|
|
2170
|
+
)
|
|
2171
|
+
rewriteClips(
|
|
2172
|
+
work,
|
|
2173
|
+
file,
|
|
2174
|
+
new Map(
|
|
2175
|
+
after.map((clip) => [
|
|
2176
|
+
clip.ref,
|
|
2177
|
+
{ "data-start": seconds(Math.max(0, clip.startMs + deltaMs)) },
|
|
2178
|
+
])
|
|
2179
|
+
)
|
|
2180
|
+
)
|
|
2181
|
+
|
|
2182
|
+
const scene = sceneOf(work, file)
|
|
2183
|
+
if (scene && scene.durationMs >= fromMs - SAME_MS) {
|
|
2184
|
+
lengthenScene(work, scene.id, Math.max(MIN_CLIP_MS, scene.durationMs + deltaMs))
|
|
2185
|
+
}
|
|
2186
|
+
}
|
|
2187
|
+
|
|
2188
|
+
/**
|
|
2189
|
+
* Takes stretches out of a composition's timeline: each clip moves back by what was cut before it
|
|
2190
|
+
* and loses what was cut of it. A recording cut through is split around the cut, each part
|
|
2191
|
+
* playing on from where its file was cut; another clip is only shortened, and one with nothing
|
|
2192
|
+
* left is removed. The scene ends as much sooner: by whole frame steps, as each stretch it shows
|
|
2193
|
+
* is made ({@link inFrameSteps}).
|
|
2194
|
+
*/
|
|
2195
|
+
function rippleRemove(work: Working, file: string, cut: readonly VideoCut[]): void {
|
|
2196
|
+
// The clips that go on, as they were before the operation cut any: what it leaves of them
|
|
2197
|
+
// goes on as one once it ends (see `marking`).
|
|
2198
|
+
work.marking ??= { chains: chainsOf(work), pieces: new Map() }
|
|
2199
|
+
const chains = work.marking
|
|
2200
|
+
const shown = sceneOf(work, file)
|
|
2201
|
+
const spans = shown
|
|
2202
|
+
? inFrameSteps(cut, shown.durationMs, frameStepMs(rootOf(work).fps))
|
|
2203
|
+
: mergeRanges(cut)
|
|
2204
|
+
const map = (t: number) =>
|
|
2205
|
+
t - spans.reduce((sum, span) => sum + Math.max(0, Math.min(t, span.endMs) - span.startMs), 0)
|
|
2206
|
+
|
|
2207
|
+
// A clip that follows one the cut moves, shortens or takes keeps the time it starts at, which
|
|
2208
|
+
// the cut then moves as any other's: its name may name a part, or nothing, once it is cut.
|
|
2209
|
+
const from = spans[0]?.startMs ?? Number.POSITIVE_INFINITY
|
|
2210
|
+
freezeFollowers(
|
|
2211
|
+
work,
|
|
2212
|
+
file,
|
|
2213
|
+
(each) => each.durationMs === undefined || each.startMs + each.durationMs > from
|
|
2214
|
+
)
|
|
2215
|
+
|
|
2216
|
+
const taken = takenNames(work)
|
|
2217
|
+
const plan = new Map<string, ClipChange>()
|
|
2218
|
+
const chained = (clip: VideoClip) =>
|
|
2219
|
+
clip.id !== undefined && chains.chains.has(`${file}\n${clip.ref}`)
|
|
2220
|
+
for (const clip of timelineOf(work, file).clips) {
|
|
2221
|
+
if (!numericStart(work, file, clip)) continue
|
|
2222
|
+
|
|
2223
|
+
const start = map(clip.startMs)
|
|
2224
|
+
if (clip.durationMs === undefined) {
|
|
2225
|
+
if (start !== clip.startMs) plan.set(clip.ref, { "data-start": seconds(start) })
|
|
2226
|
+
continue
|
|
2227
|
+
}
|
|
2228
|
+
|
|
2229
|
+
// What is left of it, in the timeline before the cut.
|
|
2230
|
+
const kept = subtractRanges(
|
|
2231
|
+
{ startMs: clip.startMs, endMs: clip.startMs + clip.durationMs },
|
|
2232
|
+
spans
|
|
2233
|
+
).filter((piece) => piece.endMs - piece.startMs >= MIN_CLIP_MS)
|
|
2234
|
+
const first = kept[0]
|
|
2235
|
+
if (!first) {
|
|
2236
|
+
plan.set(clip.ref, "remove")
|
|
2237
|
+
if (chained(clip)) chains.pieces.set(`${file}\n${clip.ref}`, [])
|
|
2238
|
+
continue
|
|
2239
|
+
}
|
|
2240
|
+
|
|
2241
|
+
if (playsFile(clip) && kept.length > 1) {
|
|
2242
|
+
const parts = clipParts(
|
|
2243
|
+
work,
|
|
2244
|
+
file,
|
|
2245
|
+
clip,
|
|
2246
|
+
kept.map((piece) => ({
|
|
2247
|
+
startMs: map(piece.startMs),
|
|
2248
|
+
mediaStartMs: clip.mediaStartMs + piece.startMs - clip.startMs,
|
|
2249
|
+
durationMs: piece.endMs - piece.startMs,
|
|
2250
|
+
})),
|
|
2251
|
+
taken
|
|
2252
|
+
)
|
|
2253
|
+
plan.set(clip.ref, { html: parts.html })
|
|
2254
|
+
if (chained(clip)) chains.pieces.set(`${file}\n${clip.ref}`, parts.refs)
|
|
2255
|
+
continue
|
|
2256
|
+
}
|
|
2257
|
+
|
|
2258
|
+
const length = map(clip.startMs + clip.durationMs) - start
|
|
2259
|
+
if (start === clip.startMs && length === clip.durationMs) continue
|
|
2260
|
+
const into = first.startMs - clip.startMs
|
|
2261
|
+
plan.set(clip.ref, {
|
|
2262
|
+
"data-start": seconds(start),
|
|
2263
|
+
"data-duration": seconds(length),
|
|
2264
|
+
...(into > 0 && playsFile(clip)
|
|
2265
|
+
? { "data-media-start": seconds(clip.mediaStartMs + into) }
|
|
2266
|
+
: {}),
|
|
2267
|
+
})
|
|
2268
|
+
}
|
|
2269
|
+
rewriteClips(work, file, plan)
|
|
2270
|
+
pruneLooks(work, file)
|
|
2271
|
+
|
|
2272
|
+
const scene = sceneOf(work, file)
|
|
2273
|
+
if (scene) lengthenScene(work, scene.id, Math.max(MIN_CLIP_MS, map(scene.durationMs)))
|
|
2274
|
+
}
|
|
2275
|
+
|
|
2276
|
+
/**
|
|
2277
|
+
* Places again each clip that goes on whose parts its scenes, moved, now play at once, as a scene
|
|
2278
|
+
* removed between two that crossfade: placed again, from where it starts for as long as its parts
|
|
2279
|
+
* show, it goes on in the next scene where the one before ends, and is heard once.
|
|
2280
|
+
*/
|
|
2281
|
+
function settleChains(work: Working): void {
|
|
2282
|
+
joinStrays(work)
|
|
2283
|
+
|
|
2284
|
+
// Each clip once: one placed again plays once, and leaves the others as they were.
|
|
2285
|
+
for (let placed = 0; ; placed += 1) {
|
|
2286
|
+
const scenes = rootOf(work).scenes
|
|
2287
|
+
const chains = chainsOf(work)
|
|
2288
|
+
if (placed > chains.size) return
|
|
2289
|
+
|
|
2290
|
+
const overlapping = [...new Set(chains.values())].find((chain) => {
|
|
2291
|
+
const seen = chain.parts.map((part) => {
|
|
2292
|
+
const scene = scenes.find((each) => each.src === part.file)
|
|
2293
|
+
return Math.min(part.endMs, scene ? scene.startMs + scene.durationMs : part.endMs)
|
|
2294
|
+
})
|
|
2295
|
+
return chain.parts
|
|
2296
|
+
.slice(1)
|
|
2297
|
+
.some((part, at) => part.startMs < (seen[at] ?? part.startMs) - SAME_MS)
|
|
2298
|
+
})
|
|
2299
|
+
const head = overlapping?.parts[0]
|
|
2300
|
+
if (!head) return
|
|
2301
|
+
placeFound(work, [logicalClip(work, chains, head)])
|
|
2302
|
+
}
|
|
2303
|
+
}
|
|
2304
|
+
|
|
2305
|
+
/**
|
|
2306
|
+
* Joins to its clip each part that goes on from that clip's last part but names another head, as
|
|
2307
|
+
* two people's edits merged may leave it, one cutting the clip's words and marking its pieces
|
|
2308
|
+
* again, the other placing it: such a part, and the parts that go on from it, name its head again.
|
|
2309
|
+
*/
|
|
2310
|
+
function joinStrays(work: Working): void {
|
|
2311
|
+
for (let joined = 0; joined <= MAX_STRAYS; joined += 1) {
|
|
2312
|
+
const scenes = rootOf(work).scenes
|
|
2313
|
+
const chains = [...new Set(chainsOf(work).values())]
|
|
2314
|
+
const linked = new Set(chains.flatMap((chain) => chain.parts.map((part) => keyOf(part))))
|
|
2315
|
+
|
|
2316
|
+
// A clip whose head is gone, or a part that names one and goes on from nothing.
|
|
2317
|
+
const strays: VideoChainPart[][] = chains
|
|
2318
|
+
.filter((chain) => chain.parts[0]?.clip.continues)
|
|
2319
|
+
.map((chain) => [...chain.parts])
|
|
2320
|
+
for (const scene of scenes) {
|
|
2321
|
+
if (work.textOf(scene.src) === undefined) continue
|
|
2322
|
+
for (const clip of readTimeline(work, scene.src)?.clips ?? []) {
|
|
2323
|
+
const part = { file: scene.src, clip }
|
|
2324
|
+
if (!clip.continues || clip.durationMs === undefined || linked.has(keyOf(part))) continue
|
|
2325
|
+
const startMs = scene.startMs + clip.startMs
|
|
2326
|
+
strays.push([{ ...part, startMs, endMs: startMs + clip.durationMs }])
|
|
2327
|
+
}
|
|
2328
|
+
}
|
|
2329
|
+
|
|
2330
|
+
const heads = chains.filter(
|
|
2331
|
+
(chain) => chain.parts[0]?.clip.id && !chain.parts[0].clip.continues
|
|
2332
|
+
)
|
|
2333
|
+
const found = strays
|
|
2334
|
+
.map((stray) => ({
|
|
2335
|
+
stray,
|
|
2336
|
+
chain: heads.find((chain) => {
|
|
2337
|
+
const last = chain.parts.at(-1)
|
|
2338
|
+
return last && stray[0] && goesOnFrom(scenes, last, stray[0])
|
|
2339
|
+
}),
|
|
2340
|
+
}))
|
|
2341
|
+
.find((each) => each.chain)
|
|
2342
|
+
const head = found?.chain?.parts[0]?.clip.id
|
|
2343
|
+
if (!found || !head) return
|
|
2344
|
+
|
|
2345
|
+
for (const part of found.stray) {
|
|
2346
|
+
const now = clipOf(work, part.file, videoTarget(part.clip))
|
|
2347
|
+
setAttributes(work, part.file, now.source, { "data-continues": head })
|
|
2348
|
+
}
|
|
2349
|
+
}
|
|
2350
|
+
}
|
|
2351
|
+
|
|
2352
|
+
/** Whether a file holds a part of a clip that goes on: one naming its head, or a head named. */
|
|
2353
|
+
function holdsParts(work: Working, path: string): boolean {
|
|
2354
|
+
const text = work.textOf(path)
|
|
2355
|
+
if (path === VIDEO_ENTRY || text === undefined || !/\.html?$/i.test(path)) return false
|
|
2356
|
+
if (text.includes("data-continues")) return true
|
|
2357
|
+
|
|
2358
|
+
const heads = new Set<string>()
|
|
2359
|
+
for (const file of work.paths()) {
|
|
2360
|
+
for (const match of (work.textOf(file) ?? "").matchAll(/\sdata-continues\s*=\s*"([^"]+)"/g)) {
|
|
2361
|
+
if (match[1]) heads.add(match[1])
|
|
2362
|
+
}
|
|
2363
|
+
}
|
|
2364
|
+
return [...heads].some((id) => text.includes(` id="${id}"`))
|
|
2365
|
+
}
|
|
2366
|
+
|
|
2367
|
+
/** How many strays an edit joins again at most: two people's edits leave a few, never more. */
|
|
2368
|
+
const MAX_STRAYS = 20
|
|
2369
|
+
|
|
2370
|
+
function keyOf(part: { readonly file: string; readonly clip: VideoClip }): string {
|
|
2371
|
+
return `${part.file}\n${part.clip.ref}`
|
|
2372
|
+
}
|
|
2373
|
+
|
|
2374
|
+
/**
|
|
2375
|
+
* Marks again each clip that goes on that an operation cut: what is left of its parts, in play
|
|
2376
|
+
* order, goes on from the first piece left, its head. A cut through a part leaves pieces back to
|
|
2377
|
+
* back that go on one from the other; a part cut whole leaves none, and a head cut whole, the
|
|
2378
|
+
* next piece to head the rest.
|
|
2379
|
+
*/
|
|
2380
|
+
function markCutChains(work: Working): void {
|
|
2381
|
+
const marking = work.marking
|
|
2382
|
+
if (!marking || marking.pieces.size === 0) return
|
|
2383
|
+
|
|
2384
|
+
for (const chain of new Set(marking.chains.values())) {
|
|
2385
|
+
const keys = chain.parts.map((part) => `${part.file}\n${part.clip.ref}`)
|
|
2386
|
+
if (!keys.some((key) => marking.pieces.has(key))) continue
|
|
2387
|
+
|
|
2388
|
+
const left = chain.parts.flatMap((part, at) => {
|
|
2389
|
+
const ids = marking.pieces.get(keys[at] as string) ?? (part.clip.id ? [part.clip.id] : [])
|
|
2390
|
+
const clips = work.textOf(part.file) === undefined ? [] : timelineOf(work, part.file).clips
|
|
2391
|
+
return ids.flatMap((id) => {
|
|
2392
|
+
const clip = clips.find((each) => each.id === id)
|
|
2393
|
+
return clip ? [{ file: part.file, clip }] : []
|
|
2394
|
+
})
|
|
2395
|
+
})
|
|
2396
|
+
const [head, ...rest] = left
|
|
2397
|
+
if (!head?.clip.id) continue
|
|
2398
|
+
if (head.clip.continues) {
|
|
2399
|
+
setAttributes(work, head.file, head.clip.source, { "data-continues": null })
|
|
2400
|
+
}
|
|
2401
|
+
for (const part of rest) {
|
|
2402
|
+
if (part.clip.continues === head.clip.id) continue
|
|
2403
|
+
const now = clipOf(work, part.file, videoTarget(part.clip))
|
|
2404
|
+
setAttributes(work, part.file, now.source, { "data-continues": head.clip.id })
|
|
2405
|
+
}
|
|
2406
|
+
}
|
|
2407
|
+
}
|
|
2408
|
+
|
|
2409
|
+
/**
|
|
2410
|
+
* The shortest stretch that is whole frames and whole milliseconds, which times are written in:
|
|
2411
|
+
* a frame at 25 and 50 fps, three at 24, 30 and 60 (125, 100 and 50 ms). A scene made longer or
|
|
2412
|
+
* shorter by steps moves those after it by whole frames: each still starts as far into its frame,
|
|
2413
|
+
* shows the same frames, and keeps its rendered segment.
|
|
2414
|
+
*/
|
|
2415
|
+
function frameStepMs(fps: number): number {
|
|
2416
|
+
let [a, b] = [fps, 1000]
|
|
2417
|
+
while (b > 0) [a, b] = [b, a % b]
|
|
2418
|
+
return 1000 / a
|
|
2419
|
+
}
|
|
2420
|
+
|
|
2421
|
+
/**
|
|
2422
|
+
* Stretches cut from a scene `lengthMs` long, sorted and apart, each kept to what the scene shows
|
|
2423
|
+
* and grown to the whole steps that cover it, so nothing marked to go stays. What it grows by goes
|
|
2424
|
+
* into its room first, as evenly as each side has some, then evenly beyond, and always within
|
|
2425
|
+
* the scene. Two that come to touch are joined, and grown again.
|
|
2426
|
+
*/
|
|
2427
|
+
function inFrameSteps(cut: readonly VideoCut[], lengthMs: number, step: number): VideoRange[] {
|
|
2428
|
+
const scene = (ms: number) => Math.min(lengthMs, Math.max(0, ms))
|
|
2429
|
+
let spans = mergeCuts(
|
|
2430
|
+
cut.map((each) => {
|
|
2431
|
+
const room = each.room ?? each
|
|
2432
|
+
return {
|
|
2433
|
+
startMs: scene(Math.floor(each.startMs)),
|
|
2434
|
+
endMs: scene(Math.ceil(each.endMs)),
|
|
2435
|
+
room: { startMs: scene(Math.ceil(room.startMs)), endMs: scene(Math.floor(room.endMs)) },
|
|
2436
|
+
}
|
|
2437
|
+
})
|
|
2438
|
+
)
|
|
2439
|
+
|
|
2440
|
+
for (;;) {
|
|
2441
|
+
const grown = mergeCuts(spans.map((span) => growToSteps(span, step, lengthMs)))
|
|
2442
|
+
if (grown.length === spans.length) return grown
|
|
2443
|
+
spans = grown
|
|
2444
|
+
}
|
|
2445
|
+
}
|
|
2446
|
+
|
|
2447
|
+
/** A cut grown to the whole steps that cover it: into its room, then beyond, within `lengthMs`. */
|
|
2448
|
+
function growToSteps(cut: VideoCut, step: number, lengthMs: number): VideoCut {
|
|
2449
|
+
const length = cut.endMs - cut.startMs
|
|
2450
|
+
const extra = Math.ceil(length / step) * step - length
|
|
2451
|
+
const room = cut.room ?? cut
|
|
2452
|
+
const roomBefore = Math.max(0, cut.startMs - room.startMs)
|
|
2453
|
+
const roomAfter = Math.max(0, room.endMs - cut.endMs)
|
|
2454
|
+
|
|
2455
|
+
// Half each side, as far as its room goes; what one side lacks, the other takes.
|
|
2456
|
+
let before = Math.min(roomBefore, Math.floor(extra / 2))
|
|
2457
|
+
const after = Math.min(roomAfter, extra - before)
|
|
2458
|
+
before = Math.min(roomBefore, extra - after)
|
|
2459
|
+
|
|
2460
|
+
// Past the room, half each side.
|
|
2461
|
+
const beyond = extra - before - after
|
|
2462
|
+
let startMs = cut.startMs - before - Math.floor(beyond / 2)
|
|
2463
|
+
let endMs = cut.endMs + after + Math.ceil(beyond / 2)
|
|
2464
|
+
|
|
2465
|
+
// Within the scene: what would fall before it or after it goes on the other side.
|
|
2466
|
+
if (startMs < 0) [startMs, endMs] = [0, endMs - startMs]
|
|
2467
|
+
if (endMs > lengthMs) [startMs, endMs] = [Math.max(0, startMs - (endMs - lengthMs)), lengthMs]
|
|
2468
|
+
|
|
2469
|
+
return {
|
|
2470
|
+
startMs,
|
|
2471
|
+
endMs,
|
|
2472
|
+
room: { startMs: Math.min(room.startMs, startMs), endMs: Math.max(room.endMs, endMs) },
|
|
2473
|
+
}
|
|
2474
|
+
}
|
|
2475
|
+
|
|
2476
|
+
/** Cuts sorted, and those that touch or overlap joined, with the room of each. */
|
|
2477
|
+
function mergeCuts(cuts: readonly VideoCut[]): VideoCut[] {
|
|
2478
|
+
const sorted = cuts.filter((cut) => cut.endMs > cut.startMs).sort((a, b) => a.startMs - b.startMs)
|
|
2479
|
+
|
|
2480
|
+
const merged: VideoCut[] = []
|
|
2481
|
+
for (const cut of sorted) {
|
|
2482
|
+
const room = cut.room ?? cut
|
|
2483
|
+
const last = merged.at(-1)
|
|
2484
|
+
if (last && cut.startMs <= last.endMs) {
|
|
2485
|
+
const lastRoom = last.room ?? last
|
|
2486
|
+
merged[merged.length - 1] = {
|
|
2487
|
+
startMs: last.startMs,
|
|
2488
|
+
endMs: Math.max(last.endMs, cut.endMs),
|
|
2489
|
+
room: {
|
|
2490
|
+
startMs: Math.min(lastRoom.startMs, room.startMs),
|
|
2491
|
+
endMs: Math.max(lastRoom.endMs, room.endMs),
|
|
2492
|
+
},
|
|
2493
|
+
}
|
|
2494
|
+
} else {
|
|
2495
|
+
merged.push({ startMs: cut.startMs, endMs: cut.endMs, room })
|
|
2496
|
+
}
|
|
2497
|
+
}
|
|
2498
|
+
return merged
|
|
2499
|
+
}
|
|
2500
|
+
|
|
2501
|
+
/** A range without the spans, sorted and apart: what is left of it, in pieces. */
|
|
2502
|
+
function subtractRanges(range: VideoRange, spans: readonly VideoRange[]): VideoRange[] {
|
|
2503
|
+
const pieces: VideoRange[] = []
|
|
2504
|
+
let cursor = range.startMs
|
|
2505
|
+
for (const span of spans) {
|
|
2506
|
+
if (span.endMs <= cursor || span.startMs >= range.endMs) continue
|
|
2507
|
+
if (span.startMs > cursor) pieces.push({ startMs: cursor, endMs: span.startMs })
|
|
2508
|
+
cursor = Math.max(cursor, span.endMs)
|
|
2509
|
+
}
|
|
2510
|
+
if (cursor < range.endMs) pieces.push({ startMs: cursor, endMs: range.endMs })
|
|
2511
|
+
return pieces
|
|
2512
|
+
}
|
|
2513
|
+
|
|
2514
|
+
/** Whether a clip's start is written as seconds, which a ripple moves; a named one follows its clip. */
|
|
2515
|
+
function numericStart(work: Working, file: string, clip: VideoClip): boolean {
|
|
2516
|
+
const at = clip.source.attributes["data-start"]
|
|
2517
|
+
if (!at) return true
|
|
2518
|
+
return Number.isFinite(Number(attributeValue(work.text(file), at) ?? Number.NaN))
|
|
2519
|
+
}
|
|
2520
|
+
|
|
2521
|
+
/** The clip a written start follows, by its name, and what comes after: `v` and ` + 0.5`. */
|
|
2522
|
+
function followed(written: string): { readonly name: string; readonly offset: string } | undefined {
|
|
2523
|
+
if (written.trim() === "" || Number.isFinite(Number(written))) return undefined
|
|
2524
|
+
const match = /^\s*(.*?)(\s*[+-]\s*(?:\d*\.)?\d+)?\s*$/.exec(written)
|
|
2525
|
+
return match?.[1] ? { name: match[1], offset: match[2] ?? "" } : undefined
|
|
2526
|
+
}
|
|
2527
|
+
|
|
2528
|
+
/**
|
|
2529
|
+
* Writes the start of each clip that follows one `moves` picks as the seconds it starts at now:
|
|
2530
|
+
* once what it follows is moved, split or gone, it starts where it did. One at a time, each read
|
|
2531
|
+
* again, as each write moves what comes after it in the file.
|
|
2532
|
+
*/
|
|
2533
|
+
function freezeFollowers(work: Working, file: string, moves: (clip: VideoClip) => boolean): void {
|
|
2534
|
+
for (;;) {
|
|
2535
|
+
const clips = timelineOf(work, file).clips
|
|
2536
|
+
const moving = new Set(clips.flatMap((clip) => (clip.id && moves(clip) ? [clip.id] : [])))
|
|
2537
|
+
const follower = clips.find((clip) => {
|
|
2538
|
+
const at = clip.source.attributes["data-start"]
|
|
2539
|
+
const name = at ? followed(attributeValue(work.text(file), at) ?? "")?.name : undefined
|
|
2540
|
+
return name !== undefined && moving.has(name)
|
|
2541
|
+
})
|
|
2542
|
+
if (!follower) return
|
|
2543
|
+
|
|
2544
|
+
setAttributes(work, file, follower.source, { "data-start": seconds(follower.startMs) })
|
|
2545
|
+
}
|
|
2546
|
+
}
|
|
2547
|
+
|
|
2548
|
+
/** Points each clip that follows `name` to `instead`, with its own offset. */
|
|
2549
|
+
function followInstead(work: Working, file: string, name: string, instead: string): void {
|
|
2550
|
+
for (;;) {
|
|
2551
|
+
const follower = timelineOf(work, file).clips.find((clip) => {
|
|
2552
|
+
const at = clip.source.attributes["data-start"]
|
|
2553
|
+
return at && followed(attributeValue(work.text(file), at) ?? "")?.name === name
|
|
2554
|
+
})
|
|
2555
|
+
const at = follower?.source.attributes["data-start"]
|
|
2556
|
+
if (!follower || !at) return
|
|
2557
|
+
|
|
2558
|
+
const written = followed(attributeValue(work.text(file), at) ?? "")
|
|
2559
|
+
setAttributes(work, file, follower.source, {
|
|
2560
|
+
"data-start": `${instead}${written?.offset ?? ""}`,
|
|
2561
|
+
})
|
|
2562
|
+
}
|
|
2563
|
+
}
|
|
2564
|
+
|
|
2565
|
+
/** What becomes of a clip: attributes set, removed, or written again as other HTML. */
|
|
2566
|
+
type ClipChange = Readonly<Record<string, string | null>> | "remove" | { readonly html: string }
|
|
2567
|
+
|
|
2568
|
+
/** Changes clips of one file, read once, from the last to the first. */
|
|
2569
|
+
function rewriteClips(work: Working, file: string, plan: ReadonlyMap<string, ClipChange>): void {
|
|
2570
|
+
if (plan.size === 0) return
|
|
2571
|
+
|
|
2572
|
+
const clips = timelineOf(work, file)
|
|
2573
|
+
.clips.filter((clip) => plan.has(clip.ref))
|
|
2574
|
+
.sort((a, b) => b.source.element.start - a.source.element.start)
|
|
2575
|
+
for (const clip of clips) {
|
|
2576
|
+
const change = plan.get(clip.ref)
|
|
2577
|
+
if (change === "remove") {
|
|
2578
|
+
const block = lineBlock(work.text(file), clip.source.element)
|
|
2579
|
+
work.replace(file, block.start, block.end, "")
|
|
2580
|
+
} else if (change && "html" in change && typeof change.html === "string") {
|
|
2581
|
+
work.replace(file, clip.source.element.start, clip.source.element.end, change.html)
|
|
2582
|
+
} else if (change) {
|
|
2583
|
+
setAttributes(work, file, clip.source, change as Readonly<Record<string, string | null>>)
|
|
2584
|
+
}
|
|
2585
|
+
}
|
|
2586
|
+
}
|
|
2587
|
+
|
|
2588
|
+
/**
|
|
2589
|
+
* The HTML of a clip's parts, each a copy of it with its own timing: the first keeps its id, the
|
|
2590
|
+
* others take new ones, so HyperFrames plays each.
|
|
2591
|
+
*/
|
|
2592
|
+
function clipParts(
|
|
2593
|
+
work: Working,
|
|
2594
|
+
file: string,
|
|
2595
|
+
clip: VideoClip,
|
|
2596
|
+
parts: readonly {
|
|
2597
|
+
readonly startMs: number
|
|
2598
|
+
readonly mediaStartMs: number
|
|
2599
|
+
readonly durationMs: number
|
|
2600
|
+
}[],
|
|
2601
|
+
taken: Set<string> = takenNames(work)
|
|
2602
|
+
): { readonly html: string; readonly refs: readonly string[] } {
|
|
2603
|
+
const text = work.text(file)
|
|
2604
|
+
const element = text.slice(clip.source.element.start, clip.source.element.end)
|
|
2605
|
+
const indent = lineIndent(text, clip.source.element.start)
|
|
2606
|
+
const base = clip.id ?? slug(clip.src?.replace(/^.*\//, "").replace(/\.[^.]*$/, "") ?? "")
|
|
2607
|
+
|
|
2608
|
+
const refs: string[] = []
|
|
2609
|
+
const html = parts.map((part, index) => {
|
|
2610
|
+
const id = index === 0 && clip.id ? clip.id : uniqueName(base || clip.kind, taken)
|
|
2611
|
+
taken.add(id)
|
|
2612
|
+
refs.push(id)
|
|
2613
|
+
return withAttributes(element, {
|
|
2614
|
+
id,
|
|
2615
|
+
"data-start": seconds(part.startMs),
|
|
2616
|
+
"data-duration": seconds(part.durationMs),
|
|
2617
|
+
...(playsFile(clip) ? { "data-media-start": seconds(part.mediaStartMs) } : {}),
|
|
2618
|
+
// Only the first piece goes on from the clip the whole did.
|
|
2619
|
+
...(index === 0 ? {} : { "data-continues": null }),
|
|
2620
|
+
})
|
|
2621
|
+
})
|
|
2622
|
+
return { html: html.join(`\n${indent}`), refs }
|
|
2623
|
+
}
|
|
2624
|
+
|
|
2625
|
+
function replaceClip(work: Working, file: string, clip: VideoClip, html: string): void {
|
|
2626
|
+
work.replace(file, clip.source.element.start, clip.source.element.end, html)
|
|
2627
|
+
}
|
|
2628
|
+
|
|
2629
|
+
/** A new clip's element, as the Agent's guide writes one. */
|
|
2630
|
+
function clipHtml(
|
|
2631
|
+
kind: VideoImportKind,
|
|
2632
|
+
values: {
|
|
2633
|
+
readonly id: string
|
|
2634
|
+
readonly src: string
|
|
2635
|
+
readonly start: string
|
|
2636
|
+
readonly duration: string
|
|
2637
|
+
readonly mediaStart: string
|
|
2638
|
+
/** Footage whose sound is not played. */
|
|
2639
|
+
readonly muted?: boolean
|
|
2640
|
+
}
|
|
2641
|
+
): string {
|
|
2642
|
+
const { id, src, start, duration, mediaStart } = values
|
|
2643
|
+
const fill = 'style="position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover"'
|
|
2644
|
+
const timing = `data-start="${start}" data-duration="${duration}"`
|
|
2645
|
+
const sound = values.muted ? "muted" : 'data-has-audio="true"'
|
|
2646
|
+
switch (kind) {
|
|
2647
|
+
case "video":
|
|
2648
|
+
return `<video id="${id}" src="${escapeAttribute(src)}" ${timing} data-media-start="${mediaStart}" ${sound} playsinline ${fill}></video>`
|
|
2649
|
+
case "audio":
|
|
2650
|
+
return `<audio id="${id}" src="${escapeAttribute(src)}" ${timing} data-media-start="${mediaStart}" data-volume="1"></audio>`
|
|
2651
|
+
case "image":
|
|
2652
|
+
return `<img id="${id}" src="${escapeAttribute(src)}" alt="" ${timing} ${fill}>`
|
|
2653
|
+
}
|
|
2654
|
+
}
|
|
2655
|
+
|
|
2656
|
+
/**
|
|
2657
|
+
* Puts clips' elements in a composition, on lines of their own, as children of the composition
|
|
2658
|
+
* itself: never inside one of its elements, a phone's frame or a lower third, whose look would be
|
|
2659
|
+
* theirs. A picture goes under the words: before the first element holding any, when that one
|
|
2660
|
+
* comes no later than the last holding a picture, as a card holding both; else after the last
|
|
2661
|
+
* holding a picture. Words and sounds go after the last element holding one of their track, else
|
|
2662
|
+
* over the pictures, after the last holding one; else last, before the closing styles and scripts.
|
|
2663
|
+
*/
|
|
2664
|
+
function insertInComposition(
|
|
2665
|
+
work: Working,
|
|
2666
|
+
file: string,
|
|
2667
|
+
timeline: VideoFileTimeline,
|
|
2668
|
+
elements: readonly string[],
|
|
2669
|
+
track: VideoTrack
|
|
2670
|
+
): void {
|
|
2671
|
+
const text = work.text(file)
|
|
2672
|
+
const children = timeline.children
|
|
2673
|
+
const holds = (child: (typeof children)[number], tracks: readonly VideoTrack[]) => {
|
|
2674
|
+
const { start, end } = child.source.element
|
|
2675
|
+
const inside = (span: VideoSourceSpan) => span.start >= start && span.end <= end
|
|
2676
|
+
return (
|
|
2677
|
+
timeline.clips.some((clip) => tracks.includes(clip.track) && inside(clip.source.element)) ||
|
|
2678
|
+
(tracks.includes("text") && timeline.texts.some((each) => inside(each.source.element)))
|
|
2679
|
+
)
|
|
2680
|
+
}
|
|
2681
|
+
const lastHolding = (tracks: readonly VideoTrack[]) =>
|
|
2682
|
+
children.filter((child) => holds(child, tracks)).at(-1)
|
|
2683
|
+
|
|
2684
|
+
// A picture never goes over words: before the first element holding any, when that one comes
|
|
2685
|
+
// no later than the last holding a picture, as a card holding both.
|
|
2686
|
+
const firstWords = children.findIndex((child) => holds(child, ["text"]))
|
|
2687
|
+
const lastPicture = children.reduce(
|
|
2688
|
+
(last, child, index) => (holds(child, ["picture"]) ? index : last),
|
|
2689
|
+
-1
|
|
2690
|
+
)
|
|
2691
|
+
const underWords =
|
|
2692
|
+
track === "picture" && firstWords >= 0 && (lastPicture < 0 || firstWords <= lastPicture)
|
|
2693
|
+
const after = underWords
|
|
2694
|
+
? undefined
|
|
2695
|
+
: (lastHolding([track]) ?? (track === "picture" ? undefined : lastHolding(["picture"])))
|
|
2696
|
+
const before = underWords ? children[firstWords] : undefined
|
|
2697
|
+
// Last, before the styles and scripts that close the composition.
|
|
2698
|
+
let closing = children.length
|
|
2699
|
+
while (closing > 0 && ["style", "script"].includes(children[closing - 1]?.tag ?? "")) closing -= 1
|
|
2700
|
+
const next = before ?? (after ? undefined : children[closing])
|
|
2701
|
+
const previous = after ?? (next ? undefined : children[closing - 1])
|
|
2702
|
+
|
|
2703
|
+
if (previous) {
|
|
2704
|
+
const block = lineBlock(text, previous.source.element)
|
|
2705
|
+
const indent = lineIndent(text, previous.source.element.start)
|
|
2706
|
+
const html = elements.join(`\n${indent}`)
|
|
2707
|
+
work.replace(file, block.end, block.end, `${indent}${html}${lineEnd(text, block)}`)
|
|
2708
|
+
return
|
|
2709
|
+
}
|
|
2710
|
+
|
|
2711
|
+
if (next) {
|
|
2712
|
+
const block = lineBlock(text, next.source.element)
|
|
2713
|
+
const indent = lineIndent(text, next.source.element.start)
|
|
2714
|
+
const html = elements.join(`\n${indent}`)
|
|
2715
|
+
if (block.start < next.source.element.start || aloneOnLine(text, next.source.element.start)) {
|
|
2716
|
+
work.replace(file, block.start, block.start, `${indent}${html}\n`)
|
|
2717
|
+
} else {
|
|
2718
|
+
work.replace(file, next.source.element.start, next.source.element.start, `${html} `)
|
|
2719
|
+
}
|
|
2720
|
+
return
|
|
2721
|
+
}
|
|
2722
|
+
|
|
2723
|
+
const content = timeline.composition.content
|
|
2724
|
+
if (!content) throw new EditError(`${file}: its composition holds nothing a clip can join.`)
|
|
2725
|
+
const indent = `${lineIndent(text, timeline.composition.element.start)} `
|
|
2726
|
+
work.replace(file, content.start, content.start, `\n${indent}${elements.join(`\n${indent}`)}`)
|
|
2727
|
+
}
|
|
2728
|
+
|
|
2729
|
+
// Placing clips ----------------------------------------------------------------------------------
|
|
2730
|
+
|
|
2731
|
+
/** Where a placement put each clip, as {@link VideoPlaceResult} says. */
|
|
2732
|
+
interface Placement {
|
|
2733
|
+
readonly clips: readonly VideoClipTarget[]
|
|
2734
|
+
readonly animationsLeft: readonly VideoAnimationLeft[]
|
|
2735
|
+
readonly animationsJoined?: readonly VideoAnimationJoined[]
|
|
2736
|
+
readonly looksMayDiffer?: readonly VideoClipTarget[]
|
|
2737
|
+
}
|
|
2738
|
+
|
|
2739
|
+
/** A clip found in its file, and where in the video it is to play. */
|
|
2740
|
+
interface Placing {
|
|
2741
|
+
readonly file: string
|
|
2742
|
+
readonly clip: VideoClip
|
|
2743
|
+
/** From the video's start. */
|
|
2744
|
+
readonly startMs: number
|
|
2745
|
+
/** How long it plays; none for a clip that writes no length, which plays until its scene ends. */
|
|
2746
|
+
readonly durationMs: number | undefined
|
|
2747
|
+
/** Where in its file it starts playing, when not where it does now: a start trimmed. */
|
|
2748
|
+
readonly mediaStartMs?: number
|
|
2749
|
+
/**
|
|
2750
|
+
* Where a recording cut by its words skips on in its file, by how far into the clip: from each,
|
|
2751
|
+
* it plays on from there. None for one that plays its file straight on.
|
|
2752
|
+
*/
|
|
2753
|
+
readonly jumps?: readonly MediaJump[]
|
|
2754
|
+
/** The parts it goes on in, in later scenes: each used again where one lands, else removed. */
|
|
2755
|
+
readonly others?: readonly { readonly file: string; readonly clip: VideoClip }[]
|
|
2756
|
+
}
|
|
2757
|
+
|
|
2758
|
+
/** Where a recording skips on in its file: from `intoMs` into the clip, it plays from `mediaMs`. */
|
|
2759
|
+
interface MediaJump {
|
|
2760
|
+
readonly intoMs: number
|
|
2761
|
+
readonly mediaMs: number
|
|
2762
|
+
}
|
|
2763
|
+
|
|
2764
|
+
type Scene = VideoRoot["scenes"][number]
|
|
2765
|
+
|
|
2766
|
+
/** A stretch of a placed clip, in the scene that plays it, from `startMs` in the video. */
|
|
2767
|
+
interface Part {
|
|
2768
|
+
readonly scene: Scene
|
|
2769
|
+
readonly startMs: number
|
|
2770
|
+
readonly durationMs: number | undefined
|
|
2771
|
+
/** How far into the clip it starts: what the parts before it play. */
|
|
2772
|
+
readonly intoMs: number
|
|
2773
|
+
}
|
|
2774
|
+
|
|
2775
|
+
/**
|
|
2776
|
+
* Marks each clip a placement writes, the first part of each wherever it lands, until the
|
|
2777
|
+
* placement reads where each ended up. Added and removed within one edit, it is never in one.
|
|
2778
|
+
*/
|
|
2779
|
+
const PLACING = "data-sixb-placing"
|
|
2780
|
+
|
|
2781
|
+
/** Where a clip of a file starts in the video: its scene's start and its own. */
|
|
2782
|
+
function videoStartOf(work: Working, file: string, clip: VideoClip): number {
|
|
2783
|
+
return (sceneOf(work, file)?.startMs ?? 0) + clip.startMs
|
|
2784
|
+
}
|
|
2785
|
+
|
|
2786
|
+
/**
|
|
2787
|
+
* Moves clips found in their files by `deltaMs`, as one placement: none before the video's start,
|
|
2788
|
+
* so the whole group moves less. Each keeps the length its scene shows of it: what played on past
|
|
2789
|
+
* the scene's end, never seen, stays behind rather than show in the next scene.
|
|
2790
|
+
*/
|
|
2791
|
+
function moveFound(
|
|
2792
|
+
work: Working,
|
|
2793
|
+
found: readonly { readonly file: string; readonly clip: VideoClip }[],
|
|
2794
|
+
deltaMs: number
|
|
2795
|
+
): Placement {
|
|
2796
|
+
const chains = chainsOf(work)
|
|
2797
|
+
const logical = [
|
|
2798
|
+
...new Map(
|
|
2799
|
+
found.map((each) => {
|
|
2800
|
+
const clip = logicalClip(work, chains, each)
|
|
2801
|
+
return [`${clip.file}\n${clip.clip.ref}`, clip] as const
|
|
2802
|
+
})
|
|
2803
|
+
).values(),
|
|
2804
|
+
]
|
|
2805
|
+
const delta = Math.max(Math.round(deltaMs), ...logical.map((clip) => -clip.startMs))
|
|
2806
|
+
|
|
2807
|
+
// A move that goes nowhere, as a group held at the video's start, changes nothing.
|
|
2808
|
+
if (delta === 0) {
|
|
2809
|
+
return {
|
|
2810
|
+
clips: logical.map(({ file, clip }) => ({ file, clip: videoTarget(clip) })),
|
|
2811
|
+
animationsLeft: [],
|
|
2812
|
+
animationsJoined: [],
|
|
2813
|
+
}
|
|
2814
|
+
}
|
|
2815
|
+
|
|
2816
|
+
return placeFound(
|
|
2817
|
+
work,
|
|
2818
|
+
logical.map((clip) => ({ ...clip, startMs: clip.startMs + delta }))
|
|
2819
|
+
)
|
|
2820
|
+
}
|
|
2821
|
+
|
|
2822
|
+
/** The chains of the video's scenes, each by every part of it, as `file\nref`. */
|
|
2823
|
+
function chainsOf(work: Working): Map<string, VideoChain> {
|
|
2824
|
+
const scenes = rootOf(work).scenes
|
|
2825
|
+
const chains = videoChains(scenes, (file) =>
|
|
2826
|
+
work.textOf(file) === undefined ? undefined : (readTimeline(work, file) ?? undefined)
|
|
2827
|
+
)
|
|
2828
|
+
const byPart = new Map<string, VideoChain>()
|
|
2829
|
+
for (const chain of chains) {
|
|
2830
|
+
for (const part of chain.parts) byPart.set(`${part.file}\n${part.clip.ref}`, chain)
|
|
2831
|
+
}
|
|
2832
|
+
return byPart
|
|
2833
|
+
}
|
|
2834
|
+
|
|
2835
|
+
/**
|
|
2836
|
+
* A clip as one, whatever part of it is named: its head, where it starts in the video, how long
|
|
2837
|
+
* it plays as its scenes show it, and the parts it goes on in. What played on past the last
|
|
2838
|
+
* part's scene's end, never seen, is not part of it.
|
|
2839
|
+
*/
|
|
2840
|
+
function logicalClip(
|
|
2841
|
+
work: Working,
|
|
2842
|
+
chains: ReadonlyMap<string, VideoChain>,
|
|
2843
|
+
found: { readonly file: string; readonly clip: VideoClip }
|
|
2844
|
+
): Placing {
|
|
2845
|
+
const chain = chains.get(`${found.file}\n${found.clip.ref}`)
|
|
2846
|
+
const [head, ...others] = chain?.parts ?? [
|
|
2847
|
+
{ ...found, startMs: videoStartOf(work, found.file, found.clip), endMs: 0 },
|
|
2848
|
+
]
|
|
2849
|
+
if (!head) throw new EditError(`'${found.clip.ref}' is not one of the video's clips.`)
|
|
2850
|
+
|
|
2851
|
+
// As long as its scenes show it: each part to its scene's end, a part past it, unseen, as it
|
|
2852
|
+
// is. A clip that goes on lasts as long as its parts show, so placed again it plays its file on
|
|
2853
|
+
// without the gap or the hidden end a scene made longer or shorter under it left.
|
|
2854
|
+
const shown = (part: { readonly file: string; readonly clip: VideoClip }) => {
|
|
2855
|
+
if (part.clip.durationMs === undefined) return undefined
|
|
2856
|
+
const scene = sceneOf(work, part.file)
|
|
2857
|
+
if (!scene || part.clip.startMs >= scene.durationMs) return part.clip.durationMs
|
|
2858
|
+
return Math.min(part.clip.durationMs, scene.durationMs - part.clip.startMs)
|
|
2859
|
+
}
|
|
2860
|
+
const lengths = [head, ...others].map(shown)
|
|
2861
|
+
const durationMs = lengths.some((length) => length === undefined)
|
|
2862
|
+
? undefined
|
|
2863
|
+
: lengths.reduce<number>((sum, length) => sum + (length ?? 0), 0)
|
|
2864
|
+
|
|
2865
|
+
// Where a part plays on from where the one before stops, as written, it is that one played on,
|
|
2866
|
+
// whatever its scene hides or leaves; elsewhere, its words were cut, and it skips on there.
|
|
2867
|
+
const jumps: MediaJump[] = []
|
|
2868
|
+
if (playsFile(head.clip)) {
|
|
2869
|
+
let intoMs = 0
|
|
2870
|
+
others.forEach((part, at) => {
|
|
2871
|
+
const before = [head, ...others][at] as typeof part
|
|
2872
|
+
intoMs += lengths[at] ?? 0
|
|
2873
|
+
const stops = before.clip.mediaStartMs + (before.clip.durationMs ?? 0)
|
|
2874
|
+
if (Math.abs(part.clip.mediaStartMs - stops) > SAME_MS) {
|
|
2875
|
+
jumps.push({ intoMs, mediaMs: part.clip.mediaStartMs })
|
|
2876
|
+
}
|
|
2877
|
+
})
|
|
2878
|
+
}
|
|
2879
|
+
return {
|
|
2880
|
+
file: head.file,
|
|
2881
|
+
clip: head.clip,
|
|
2882
|
+
startMs: head.startMs,
|
|
2883
|
+
durationMs,
|
|
2884
|
+
...(jumps.length > 0 ? { jumps } : {}),
|
|
2885
|
+
others: others.map(({ file, clip }) => ({ file, clip })),
|
|
2886
|
+
}
|
|
2887
|
+
}
|
|
2888
|
+
|
|
2889
|
+
/**
|
|
2890
|
+
* Where a clip placed from `mediaStartMs` plays its file `intoMs` into it: on from its start, or
|
|
2891
|
+
* from the last jump before that point.
|
|
2892
|
+
*/
|
|
2893
|
+
function mediaAt(
|
|
2894
|
+
mediaStartMs: number,
|
|
2895
|
+
jumps: readonly MediaJump[] | undefined,
|
|
2896
|
+
intoMs: number
|
|
2897
|
+
): number {
|
|
2898
|
+
const jump = jumps?.filter((each) => each.intoMs <= intoMs).at(-1)
|
|
2899
|
+
return jump ? jump.mediaMs + intoMs - jump.intoMs : mediaStartMs + intoMs
|
|
2900
|
+
}
|
|
2901
|
+
|
|
2902
|
+
/**
|
|
2903
|
+
* The jumps of a clip whose start moves `byMs` into it, or back out: each as far into the clip
|
|
2904
|
+
* as it is now, those it passes behind it, and where it then starts in its file.
|
|
2905
|
+
*/
|
|
2906
|
+
function jumpsFrom(
|
|
2907
|
+
mediaStartMs: number,
|
|
2908
|
+
jumps: readonly MediaJump[] | undefined,
|
|
2909
|
+
byMs: number
|
|
2910
|
+
): { readonly mediaStartMs: number; readonly jumps?: readonly MediaJump[] } {
|
|
2911
|
+
if (!jumps) return { mediaStartMs: mediaStartMs + byMs }
|
|
2912
|
+
const ahead = jumps
|
|
2913
|
+
.filter((jump) => jump.intoMs > byMs)
|
|
2914
|
+
.map((jump) => ({ intoMs: jump.intoMs - byMs, mediaMs: jump.mediaMs }))
|
|
2915
|
+
return {
|
|
2916
|
+
mediaStartMs: byMs < 0 ? mediaStartMs + byMs : mediaAt(mediaStartMs, jumps, byMs),
|
|
2917
|
+
...(ahead.length > 0 ? { jumps: ahead } : {}),
|
|
2918
|
+
}
|
|
2919
|
+
}
|
|
2920
|
+
|
|
2921
|
+
/**
|
|
2922
|
+
* Puts clips where they are to play in the video, as one step: each clip of a scene in the scene
|
|
2923
|
+
* that plays where it starts, its own while that holds it, and on past that scene's end as a copy
|
|
2924
|
+
* in the next (see {@link partsOf}); the last scene grows, in whole frame steps, to hold what
|
|
2925
|
+
* lands past it. The video's own clips only take their times. A clip that leaves its file takes
|
|
2926
|
+
* an id no other element of the video has, its styles and its recording's path; what followed it
|
|
2927
|
+
* there keeps its time. Every clip is found before any changes.
|
|
2928
|
+
*/
|
|
2929
|
+
function placeFound(work: Working, placing: readonly Placing[]): Placement {
|
|
2930
|
+
// Every scene read once: `partsOf` tells them apart by identity.
|
|
2931
|
+
const root = rootOf(work)
|
|
2932
|
+
const scenes = root.scenes
|
|
2933
|
+
const frameMs = 1000 / root.fps
|
|
2934
|
+
const plans = placing.map((each, index) => {
|
|
2935
|
+
const own =
|
|
2936
|
+
each.file === VIDEO_ENTRY ? undefined : scenes.find((scene) => scene.src === each.file)
|
|
2937
|
+
const parts = own
|
|
2938
|
+
? atJumps(partsOf(scenes, own, each.startMs, each.durationMs), each, frameMs)
|
|
2939
|
+
: []
|
|
2940
|
+
return { ...each, index, own, parts }
|
|
2941
|
+
})
|
|
2942
|
+
|
|
2943
|
+
// A clip leaves its file whole or not at all, and only for a file one scene plays.
|
|
2944
|
+
const read = new Map<string, readonly VideoClip[]>()
|
|
2945
|
+
const clipsIn = (file: string) => {
|
|
2946
|
+
const known = read.get(file)
|
|
2947
|
+
if (known) return known
|
|
2948
|
+
const clips = timelineOf(work, file).clips
|
|
2949
|
+
read.set(file, clips)
|
|
2950
|
+
return clips
|
|
2951
|
+
}
|
|
2952
|
+
for (const { file, clip, own, parts } of plans) {
|
|
2953
|
+
if (!own || parts.every((part) => part.scene.id === own.id)) continue
|
|
2954
|
+
|
|
2955
|
+
for (const src of new Set([file, ...parts.map((part) => part.scene.src)])) {
|
|
2956
|
+
if (scenes.filter((scene) => scene.src === src).length > 1) {
|
|
2957
|
+
throw new EditError(`More than one scene plays ${src}: a clip can't leave or join it.`)
|
|
2958
|
+
}
|
|
2959
|
+
}
|
|
2960
|
+
const { start, end } = clip.source.element
|
|
2961
|
+
const holds = clipsIn(file).some(
|
|
2962
|
+
(other) => other.source.element.start > start && other.source.element.end <= end
|
|
2963
|
+
)
|
|
2964
|
+
if (holds) throw new EditError(`'${clip.ref}' holds other clips: it moves within its scene.`)
|
|
2965
|
+
}
|
|
2966
|
+
|
|
2967
|
+
// Each clip marked, from the last in each file, so the spans read once still name what they did.
|
|
2968
|
+
for (const [file, each] of byFile(plans)) {
|
|
2969
|
+
const marks = new Map(each.map((plan) => [plan.clip.ref, String(plan.index)]))
|
|
2970
|
+
const clips = timelineOf(work, file)
|
|
2971
|
+
.clips.filter((clip) => marks.has(clip.ref))
|
|
2972
|
+
.sort((a, b) => b.source.element.start - a.source.element.start)
|
|
2973
|
+
for (const clip of clips) {
|
|
2974
|
+
setAttributes(work, file, clip.source, { [PLACING]: marks.get(clip.ref) ?? null })
|
|
2975
|
+
}
|
|
2976
|
+
}
|
|
2977
|
+
|
|
2978
|
+
// What followed a clip that leaves, or now ends sooner in its file, or a part of one that may
|
|
2979
|
+
// go, keeps its time.
|
|
2980
|
+
const cut = new Map<string, Set<string>>()
|
|
2981
|
+
const cuts = (file: string, id: string | undefined) => {
|
|
2982
|
+
if (!id) return
|
|
2983
|
+
cut.set(file, (cut.get(file) ?? new Set()).add(id))
|
|
2984
|
+
}
|
|
2985
|
+
for (const { file, clip, own, parts, durationMs, others } of plans) {
|
|
2986
|
+
const [first] = parts
|
|
2987
|
+
const whole =
|
|
2988
|
+
parts.length === 1 && first?.scene.id === own?.id && first?.durationMs === durationMs
|
|
2989
|
+
if (own && !whole) cuts(file, clip.id)
|
|
2990
|
+
for (const other of others ?? []) cuts(other.file, other.clip.id)
|
|
2991
|
+
}
|
|
2992
|
+
for (const [file, ids] of cut) {
|
|
2993
|
+
freezeFollowers(work, file, (clip) => clip.id !== undefined && ids.has(clip.id))
|
|
2994
|
+
}
|
|
2995
|
+
|
|
2996
|
+
// Each file's clips written in place or taken out, and what lands in another file kept for it.
|
|
2997
|
+
// Every file is read before any is written, so each clip's ref still names it.
|
|
2998
|
+
const counts = nameCounts(work)
|
|
2999
|
+
const taken = new Set(counts.keys())
|
|
3000
|
+
const carried: Carried[] = []
|
|
3001
|
+
const left: { readonly index: number; readonly scene: string }[] = []
|
|
3002
|
+
const joined: { readonly index: number; readonly scene: string }[] = []
|
|
3003
|
+
const uncertain = new Set<number>()
|
|
3004
|
+
const changes = new Map<string, Map<string, ClipChange>>()
|
|
3005
|
+
const change = (file: string, ref: string, what: ClipChange) => {
|
|
3006
|
+
const plan = changes.get(file) ?? new Map<string, ClipChange>()
|
|
3007
|
+
changes.set(file, plan)
|
|
3008
|
+
plan.set(ref, what)
|
|
3009
|
+
}
|
|
3010
|
+
// The files whose looks may have lost their last clip, the sheets every scene reads, and each
|
|
3011
|
+
// look worked out once: by its file's styles, and by what the clip is as a selector reads it.
|
|
3012
|
+
const touched = new Set<string>()
|
|
3013
|
+
const linked = linkedBlocks(work)
|
|
3014
|
+
const blocksOf = new Map<string, readonly CssBlock[]>()
|
|
3015
|
+
const parsed = (css: string) => {
|
|
3016
|
+
const known = blocksOf.get(css)
|
|
3017
|
+
if (known) return known
|
|
3018
|
+
const blocks = cssBlocks(css)
|
|
3019
|
+
blocksOf.set(css, blocks)
|
|
3020
|
+
return blocks
|
|
3021
|
+
}
|
|
3022
|
+
const looksOf = new Map<string, { look: Look; home: string }>()
|
|
3023
|
+
// What holds a clip landing in each scene, read once for all that land there.
|
|
3024
|
+
const contexts = new Map<string, LookContext>()
|
|
3025
|
+
const landing = (id: string) => {
|
|
3026
|
+
const known = contexts.get(id)
|
|
3027
|
+
if (known) return known
|
|
3028
|
+
// The scene as the files are now, which what is written in them may have moved.
|
|
3029
|
+
const scene = rootOf(work).scenes.find((each) => each.id === id)
|
|
3030
|
+
const context = scene ? sceneContext(work, scene) : undefined
|
|
3031
|
+
if (context) contexts.set(id, context)
|
|
3032
|
+
return context
|
|
3033
|
+
}
|
|
3034
|
+
// The parts each clip placed goes on in, as `file\nid`, by the ids its parts may name.
|
|
3035
|
+
const wrote = new Map<string, Set<string>>()
|
|
3036
|
+
|
|
3037
|
+
for (const [file, each] of byFile(plans)) {
|
|
3038
|
+
const text = work.text(file)
|
|
3039
|
+
const clips = timelineOf(work, file).clips
|
|
3040
|
+
const scripts = file === VIDEO_ENTRY ? "" : scriptsOf(text)
|
|
3041
|
+
const read = attributesRead([...linked, ...stylesIn(text).map((style) => parsed(style.css))])
|
|
3042
|
+
|
|
3043
|
+
for (const plan of each) {
|
|
3044
|
+
const { clip: was, own, parts, startMs, durationMs, index } = plan
|
|
3045
|
+
const clip = markedClip(text, clips, index)
|
|
3046
|
+
|
|
3047
|
+
if (!own) {
|
|
3048
|
+
change(file, clip.ref, {
|
|
3049
|
+
...(startMs === was.startMs ? {} : { "data-start": seconds(startMs) }),
|
|
3050
|
+
...(durationMs === undefined || durationMs === was.durationMs
|
|
3051
|
+
? {}
|
|
3052
|
+
: { "data-duration": seconds(durationMs) }),
|
|
3053
|
+
})
|
|
3054
|
+
continue
|
|
3055
|
+
}
|
|
3056
|
+
|
|
3057
|
+
const element = text.slice(clip.source.element.start, clip.source.element.end)
|
|
3058
|
+
const facts = lookElement(element, was.id)
|
|
3059
|
+
const stays = parts[0]?.scene.id === own.id
|
|
3060
|
+
const mediaStartMs = plan.mediaStartMs ?? was.mediaStartMs
|
|
3061
|
+
// How it looks where it is, for each part that lands elsewhere: its scene is its home,
|
|
3062
|
+
// unless it came with a look carried from another, which it keeps by its key.
|
|
3063
|
+
const carriedKey = facts.attributes.get("data-look")
|
|
3064
|
+
const known = parts.some((part) => part.scene.id !== own.id)
|
|
3065
|
+
? (() => {
|
|
3066
|
+
const signature = `${file}\n${carriedKey ?? ""}\n${lookSignature(facts, read)}`
|
|
3067
|
+
const cached = looksOf.get(signature)
|
|
3068
|
+
if (cached) return cached
|
|
3069
|
+
const styled = lookSheets(stylesIn(text), carriedKey, linked, parsed)
|
|
3070
|
+
const found = {
|
|
3071
|
+
look: lookOf(facts, own.id, styled.sheets),
|
|
3072
|
+
home: styled.home ?? own.id,
|
|
3073
|
+
}
|
|
3074
|
+
looksOf.set(signature, found)
|
|
3075
|
+
return found
|
|
3076
|
+
})()
|
|
3077
|
+
: undefined
|
|
3078
|
+
const look = known?.look
|
|
3079
|
+
const home = known?.home ?? own.id
|
|
3080
|
+
const key = look && look.rules.length > 0 ? lookKey(home, look) : undefined
|
|
3081
|
+
// It may look otherwise only where what it could not take along styles it otherwise.
|
|
3082
|
+
if (look?.uncertain) {
|
|
3083
|
+
const from = clipContext(work, own, text, clip)
|
|
3084
|
+
const elsewhere = new Set(
|
|
3085
|
+
parts.map((part) => part.scene).filter((scene) => scene.id !== home)
|
|
3086
|
+
)
|
|
3087
|
+
const differs = [...elsewhere].some((scene) =>
|
|
3088
|
+
lookDiffers(look, facts, { from, to: landing(scene.id) ?? [] })
|
|
3089
|
+
)
|
|
3090
|
+
if (differs) uncertain.add(index)
|
|
3091
|
+
}
|
|
3092
|
+
const base = was.id ?? slug(was.src?.replace(/^.*\//, "").replace(/\.[^.]*$/, "") ?? "")
|
|
3093
|
+
const fresh = () => {
|
|
3094
|
+
const id = uniqueName(base || was.kind, taken)
|
|
3095
|
+
taken.add(id)
|
|
3096
|
+
return id
|
|
3097
|
+
}
|
|
3098
|
+
|
|
3099
|
+
// Its parts in later scenes, each used again where a new one lands in its file.
|
|
3100
|
+
const others = [...(plan.others ?? [])]
|
|
3101
|
+
const reuse = (src: string) => {
|
|
3102
|
+
const at = others.findIndex((other) => other.file === src)
|
|
3103
|
+
return at < 0 ? undefined : others.splice(at, 1)[0]
|
|
3104
|
+
}
|
|
3105
|
+
|
|
3106
|
+
// The head: in place, or written for the file it lands in. It has an id once parts go on
|
|
3107
|
+
// from it, and its own id unless another element has it.
|
|
3108
|
+
const keepsId = was.id !== undefined && (stays || (counts.get(was.id) ?? 0) <= 1)
|
|
3109
|
+
const headId = keepsId ? was.id : was.id || parts.length > 1 ? fresh() : undefined
|
|
3110
|
+
if (headId && headId === was.id) taken.add(headId)
|
|
3111
|
+
const written = new Set<string>()
|
|
3112
|
+
for (const id of [headId, was.id]) if (id) wrote.set(id, written)
|
|
3113
|
+
|
|
3114
|
+
// As one clip, it fades in on its first part and out on its last: the fade out it ended on.
|
|
3115
|
+
const lastOther = plan.others?.at(-1)
|
|
3116
|
+
const fadeOutMs = lastOther ? lastOther.clip.fadeOutMs : was.fadeOutMs
|
|
3117
|
+
const fades = (n: number, now: VideoClip): Record<string, string | null> => ({
|
|
3118
|
+
...(n > 0 && now.fadeInMs !== undefined ? { "data-fade-in": null } : {}),
|
|
3119
|
+
...(n < parts.length - 1
|
|
3120
|
+
? now.fadeOutMs === undefined
|
|
3121
|
+
? {}
|
|
3122
|
+
: { "data-fade-out": null }
|
|
3123
|
+
: now.fadeOutMs === fadeOutMs
|
|
3124
|
+
? {}
|
|
3125
|
+
: { "data-fade-out": fadeOutMs === undefined ? null : seconds(fadeOutMs) }),
|
|
3126
|
+
})
|
|
3127
|
+
|
|
3128
|
+
parts.forEach((part, n) => {
|
|
3129
|
+
const media = mediaAt(mediaStartMs, plan.jumps, part.intoMs)
|
|
3130
|
+
const timing = {
|
|
3131
|
+
start: seconds(part.startMs - part.scene.startMs),
|
|
3132
|
+
duration: part.durationMs === undefined ? undefined : seconds(part.durationMs),
|
|
3133
|
+
media: playsFile(was) ? seconds(media) : undefined,
|
|
3134
|
+
}
|
|
3135
|
+
|
|
3136
|
+
if (n === 0 && stays) {
|
|
3137
|
+
change(file, clip.ref, {
|
|
3138
|
+
...(part.startMs === own.startMs + was.startMs ? {} : { "data-start": timing.start }),
|
|
3139
|
+
...(timing.duration === undefined || part.durationMs === was.durationMs
|
|
3140
|
+
? {}
|
|
3141
|
+
: { "data-duration": timing.duration }),
|
|
3142
|
+
...(timing.media === undefined || mediaStartMs === was.mediaStartMs
|
|
3143
|
+
? {}
|
|
3144
|
+
: { "data-media-start": timing.media }),
|
|
3145
|
+
...(headId && headId !== was.id ? { id: headId } : {}),
|
|
3146
|
+
...(was.continues ? { "data-continues": null } : {}),
|
|
3147
|
+
...fades(n, was),
|
|
3148
|
+
})
|
|
3149
|
+
return
|
|
3150
|
+
}
|
|
3151
|
+
|
|
3152
|
+
// A part that goes on, in a file holding one already: that one, written again.
|
|
3153
|
+
const kept = n > 0 ? reuse(part.scene.src) : undefined
|
|
3154
|
+
if (kept) {
|
|
3155
|
+
const old = kept.clip
|
|
3156
|
+
written.add(`${kept.file}\n${old.id ?? old.ref}`)
|
|
3157
|
+
change(kept.file, old.ref, {
|
|
3158
|
+
...(part.startMs - part.scene.startMs === old.startMs
|
|
3159
|
+
? {}
|
|
3160
|
+
: { "data-start": timing.start }),
|
|
3161
|
+
...(part.durationMs === old.durationMs || timing.duration === undefined
|
|
3162
|
+
? {}
|
|
3163
|
+
: { "data-duration": timing.duration }),
|
|
3164
|
+
...(timing.media === undefined || media === old.mediaStartMs
|
|
3165
|
+
? {}
|
|
3166
|
+
: { "data-media-start": timing.media }),
|
|
3167
|
+
...(old.continues === headId ? {} : { "data-continues": headId ?? null }),
|
|
3168
|
+
...fades(n, old),
|
|
3169
|
+
})
|
|
3170
|
+
return
|
|
3171
|
+
}
|
|
3172
|
+
|
|
3173
|
+
const id = n === 0 ? headId : fresh()
|
|
3174
|
+
if (n > 0 && id) written.add(`${part.scene.src}\n${id}`)
|
|
3175
|
+
// Its look, by its key, where it is not at home; at home, its scene's own rules.
|
|
3176
|
+
const styled = key !== undefined && part.scene.id !== home
|
|
3177
|
+
const src = attributeOf(text, clip, "src")
|
|
3178
|
+
const moved = src === undefined ? undefined : rebased(work, file, part.scene.src, src)
|
|
3179
|
+
const html = withAttributes(element, {
|
|
3180
|
+
...(id ? { id } : {}),
|
|
3181
|
+
"data-start": timing.start,
|
|
3182
|
+
...(timing.duration === undefined ? {} : { "data-duration": timing.duration }),
|
|
3183
|
+
// Where it plays its file from, written only where that changes.
|
|
3184
|
+
...(timing.media === undefined || media === was.mediaStartMs
|
|
3185
|
+
? {}
|
|
3186
|
+
: { "data-media-start": timing.media }),
|
|
3187
|
+
...(moved === undefined ? {} : { src: moved }),
|
|
3188
|
+
"data-continues": n === 0 ? null : (headId ?? null),
|
|
3189
|
+
"data-look": styled ? key : null,
|
|
3190
|
+
...fades(n, was),
|
|
3191
|
+
...(n === 0 ? {} : { [PLACING]: null }),
|
|
3192
|
+
})
|
|
3193
|
+
carried.push({
|
|
3194
|
+
file: part.scene.src,
|
|
3195
|
+
scene: part.scene.id,
|
|
3196
|
+
track: was.track,
|
|
3197
|
+
html,
|
|
3198
|
+
index,
|
|
3199
|
+
element: { ...facts, ...(id ? { id } : {}) },
|
|
3200
|
+
...(look && key && styled ? { look, key, home } : {}),
|
|
3201
|
+
})
|
|
3202
|
+
})
|
|
3203
|
+
|
|
3204
|
+
if (!stays) {
|
|
3205
|
+
change(file, clip.ref, "remove")
|
|
3206
|
+
touched.add(file)
|
|
3207
|
+
if (scriptNames(scripts, facts)) left.push({ index, scene: own.id })
|
|
3208
|
+
}
|
|
3209
|
+
|
|
3210
|
+
// The parts no new one lands beside go, with the looks they carried.
|
|
3211
|
+
for (const other of others) {
|
|
3212
|
+
change(other.file, other.clip.ref, "remove")
|
|
3213
|
+
touched.add(other.file)
|
|
3214
|
+
}
|
|
3215
|
+
}
|
|
3216
|
+
}
|
|
3217
|
+
|
|
3218
|
+
for (const [file, plan] of changes) rewriteClips(work, file, plan)
|
|
3219
|
+
|
|
3220
|
+
// What lands in a file goes after the last clip of its track there, in the order placed.
|
|
3221
|
+
for (const [file, each] of groupBy(carried, (one) => one.file)) {
|
|
3222
|
+
for (const track of ["picture", "text", "sound"] as const) {
|
|
3223
|
+
const batch = each.filter((one) => one.track === track)
|
|
3224
|
+
if (batch.length === 0) continue
|
|
3225
|
+
|
|
3226
|
+
insertInComposition(
|
|
3227
|
+
work,
|
|
3228
|
+
file,
|
|
3229
|
+
timelineOf(work, file),
|
|
3230
|
+
batch.map((one) => one.html),
|
|
3231
|
+
track
|
|
3232
|
+
)
|
|
3233
|
+
}
|
|
3234
|
+
|
|
3235
|
+
// Each look that came with a clip, in a style of its own that reaches that clip alone, and
|
|
3236
|
+
// each animation of this scene that reaches what landed.
|
|
3237
|
+
const scripts = scriptsOf(work.text(file))
|
|
3238
|
+
const seen = sceneKeyframes(work, file, linked)
|
|
3239
|
+
const carriedIn = new Map(seen.carried)
|
|
3240
|
+
const text = work.text(file)
|
|
3241
|
+
const present = lookBlocks(text)
|
|
3242
|
+
const written = new Set(present.map((block) => block.key))
|
|
3243
|
+
const looks: { readonly key: string; readonly home: string; readonly rules: string[] }[] = []
|
|
3244
|
+
for (const one of each) {
|
|
3245
|
+
// A look the file writes already goes in the edit whole all the same: someone removing it
|
|
3246
|
+
// at once, its last clip gone, leaves this one's for the clip it brings.
|
|
3247
|
+
const there = one.key ? present.find((block) => block.key === one.key) : undefined
|
|
3248
|
+
if (there) work.writeWhole(file, text.slice(...spanOf(lineBlock(text, there))))
|
|
3249
|
+
|
|
3250
|
+
if (one.look && one.key && one.home && !written.has(one.key)) {
|
|
3251
|
+
written.add(one.key)
|
|
3252
|
+
const rules = writtenLook(one.look, one.scene, one.key, {
|
|
3253
|
+
own: seen.own,
|
|
3254
|
+
carried: carriedIn,
|
|
3255
|
+
})
|
|
3256
|
+
if (rules.length > 0) looks.push({ key: one.key, home: one.home, rules })
|
|
3257
|
+
// What it wrote, the next look reads.
|
|
3258
|
+
for (const [name, body] of keyframesOf(cssBlocks(rules.join("\n")))) {
|
|
3259
|
+
carriedIn.set(name, [...(carriedIn.get(name) ?? []), body])
|
|
3260
|
+
}
|
|
3261
|
+
}
|
|
3262
|
+
const context = landing(one.scene)
|
|
3263
|
+
const reaches = context
|
|
3264
|
+
? scriptReachesIn(scripts, one.element, context)
|
|
3265
|
+
: scriptReaches(scripts, one.element)
|
|
3266
|
+
if (reaches && !joined.some((each) => each.index === one.index)) {
|
|
3267
|
+
joined.push({ index: one.index, scene: one.scene })
|
|
3268
|
+
}
|
|
3269
|
+
}
|
|
3270
|
+
writeLooks(work, file, looks)
|
|
3271
|
+
}
|
|
3272
|
+
for (const file of touched) pruneLooks(work, file)
|
|
3273
|
+
|
|
3274
|
+
// Whatever else names a clip placed, a part some edit left out of it, lets go of it: it is
|
|
3275
|
+
// a clip of its own, and no later placement links it again.
|
|
3276
|
+
if (wrote.size > 0) {
|
|
3277
|
+
for (const scene of rootOf(work).scenes) {
|
|
3278
|
+
const text = work.textOf(scene.src)
|
|
3279
|
+
if (text === undefined || !text.includes("data-continues")) continue
|
|
3280
|
+
for (;;) {
|
|
3281
|
+
const stale = readTimeline(work, scene.src)?.clips.find((clip) => {
|
|
3282
|
+
const parts = clip.continues ? wrote.get(clip.continues) : undefined
|
|
3283
|
+
return parts !== undefined && !parts.has(`${scene.src}\n${clip.id ?? clip.ref}`)
|
|
3284
|
+
})
|
|
3285
|
+
if (!stale) break
|
|
3286
|
+
setAttributes(work, scene.src, stale.source, { "data-continues": null })
|
|
3287
|
+
}
|
|
3288
|
+
}
|
|
3289
|
+
}
|
|
3290
|
+
|
|
3291
|
+
// The last scene grows to hold what lands past its end.
|
|
3292
|
+
const needed = new Map<string, number>()
|
|
3293
|
+
for (const { parts } of plans) {
|
|
3294
|
+
for (const part of parts) {
|
|
3295
|
+
if (part.durationMs === undefined) continue
|
|
3296
|
+
const end = part.startMs + part.durationMs - part.scene.startMs
|
|
3297
|
+
needed.set(part.scene.id, Math.max(needed.get(part.scene.id) ?? 0, end))
|
|
3298
|
+
}
|
|
3299
|
+
}
|
|
3300
|
+
const step = frameStepMs(rootOf(work).fps)
|
|
3301
|
+
for (const [id, end] of needed) {
|
|
3302
|
+
const scene = scenes.find((each) => each.id === id)
|
|
3303
|
+
if (scene && end > scene.durationMs + SAME_MS) {
|
|
3304
|
+
lengthenScene(work, id, Math.ceil(end / step) * step)
|
|
3305
|
+
}
|
|
3306
|
+
}
|
|
3307
|
+
|
|
3308
|
+
// Where each clip ended up, read from its mark, which then goes.
|
|
3309
|
+
const found = new Map<number, VideoClipTarget>()
|
|
3310
|
+
for (const file of new Set([
|
|
3311
|
+
...plans.map((plan) => plan.file),
|
|
3312
|
+
...carried.map((one) => one.file),
|
|
3313
|
+
])) {
|
|
3314
|
+
const text = work.text(file)
|
|
3315
|
+
const marked = timelineOf(work, file)
|
|
3316
|
+
.clips.filter((clip) => clip.source.attributes[PLACING])
|
|
3317
|
+
.sort((a, b) => b.source.element.start - a.source.element.start)
|
|
3318
|
+
for (const clip of marked) {
|
|
3319
|
+
const at = clip.source.attributes[PLACING]
|
|
3320
|
+
found.set(Number(at ? attributeValue(text, at) : Number.NaN), {
|
|
3321
|
+
file,
|
|
3322
|
+
clip: videoTarget(clip),
|
|
3323
|
+
})
|
|
3324
|
+
setAttributes(work, file, clip.source, { [PLACING]: null })
|
|
3325
|
+
}
|
|
3326
|
+
}
|
|
3327
|
+
|
|
3328
|
+
return {
|
|
3329
|
+
clips: plans.flatMap((plan) => {
|
|
3330
|
+
const at = found.get(plan.index)
|
|
3331
|
+
return at ? [at] : []
|
|
3332
|
+
}),
|
|
3333
|
+
animationsLeft: left.flatMap(({ index, scene }) => {
|
|
3334
|
+
const clip = found.get(index)
|
|
3335
|
+
return clip ? [{ clip, scene }] : []
|
|
3336
|
+
}),
|
|
3337
|
+
animationsJoined: joined.flatMap(({ index, scene }) => {
|
|
3338
|
+
const clip = found.get(index)
|
|
3339
|
+
return clip ? [{ clip, scene }] : []
|
|
3340
|
+
}),
|
|
3341
|
+
looksMayDiffer: [...uncertain].flatMap((index) => {
|
|
3342
|
+
const clip = found.get(index)
|
|
3343
|
+
return clip ? [clip] : []
|
|
3344
|
+
}),
|
|
3345
|
+
}
|
|
3346
|
+
}
|
|
3347
|
+
|
|
3348
|
+
/**
|
|
3349
|
+
* Where a clip of `own` placed at `startMs` for `durationMs` plays, part by part: from the scene
|
|
3350
|
+
* that holds its start, its own while that does, else the one on top there, the later in play
|
|
3351
|
+
* order, as the hosts stack in a crossfade; in a gap between scenes, from the next one's start;
|
|
3352
|
+
* past every scene, in the last. Past that scene's end, the rest goes on in the next scene, from
|
|
3353
|
+
* where the first ends, so nothing shows twice in a crossfade, and so on; the last holds whatever
|
|
3354
|
+
* is left. A clip that writes no length is never split. A part shorter than a clip may be, a few
|
|
3355
|
+
* frames past a scene's edge, is left out, unless it is all there is.
|
|
3356
|
+
*/
|
|
3357
|
+
function partsOf(
|
|
3358
|
+
every: readonly Scene[],
|
|
3359
|
+
own: Scene,
|
|
3360
|
+
startMs: number,
|
|
3361
|
+
durationMs: number | undefined
|
|
3362
|
+
): Part[] {
|
|
3363
|
+
// An overlay keeps its clips, where they are moved to in it; a chapter's go to chapters alone.
|
|
3364
|
+
const scenes = videoChapters(every)
|
|
3365
|
+
if (!scenes.includes(own)) {
|
|
3366
|
+
return [{ scene: own, startMs: Math.max(startMs, own.startMs), durationMs, intoMs: 0 }]
|
|
3367
|
+
}
|
|
3368
|
+
|
|
3369
|
+
const end = (scene: Scene) => scene.startMs + scene.durationMs
|
|
3370
|
+
const holds = (scene: Scene, ms: number) => scene.startMs <= ms && ms < end(scene)
|
|
3371
|
+
const onTop = (ms: number, except?: Scene) =>
|
|
3372
|
+
scenes.filter((scene) => scene !== except && holds(scene, ms)).at(-1)
|
|
3373
|
+
const from = (ms: number, except?: Scene) =>
|
|
3374
|
+
scenes.find((scene) => scene !== except && scene.startMs >= ms)
|
|
3375
|
+
const last = scenes.reduce((latest, scene) => (end(scene) >= end(latest) ? scene : latest), own)
|
|
3376
|
+
const next = (scene: Scene) => sceneAfter(scenes, scene)
|
|
3377
|
+
|
|
3378
|
+
let scene = (holds(own, startMs) ? own : undefined) ?? onTop(startMs) ?? from(startMs) ?? last
|
|
3379
|
+
const first = Math.max(startMs, scene.startMs)
|
|
3380
|
+
if (durationMs === undefined) return [{ scene, startMs: first, durationMs, intoMs: 0 }]
|
|
3381
|
+
|
|
3382
|
+
const clipEnd = first + durationMs
|
|
3383
|
+
const pieces: Part[] = []
|
|
3384
|
+
let at = first
|
|
3385
|
+
for (;;) {
|
|
3386
|
+
const after = clipEnd > end(scene) + SAME_MS ? next(scene) : undefined
|
|
3387
|
+
const until = after ? end(scene) : clipEnd
|
|
3388
|
+
pieces.push({ scene, startMs: at, durationMs: until - at, intoMs: at - first })
|
|
3389
|
+
if (!after) break
|
|
3390
|
+
|
|
3391
|
+
at = Math.max(end(scene), after.startMs)
|
|
3392
|
+
if (at >= clipEnd) break
|
|
3393
|
+
scene = after
|
|
3394
|
+
}
|
|
3395
|
+
|
|
3396
|
+
const kept = pieces.filter((piece) => (piece.durationMs ?? 0) >= MIN_CLIP_MS)
|
|
3397
|
+
if (kept.length > 0) return kept
|
|
3398
|
+
const longest = pieces.reduce((a, b) => ((b.durationMs ?? 0) > (a.durationMs ?? 0) ? b : a))
|
|
3399
|
+
return [longest]
|
|
3400
|
+
}
|
|
3401
|
+
|
|
3402
|
+
/**
|
|
3403
|
+
* A recording's parts cut again where it skips on in its file, so each plays one stretch of it:
|
|
3404
|
+
* two pieces back to back in a scene, which go on one from the other. A skip however close to a
|
|
3405
|
+
* part's edge cuts it there: what its words left out is never heard again. A piece shorter than a
|
|
3406
|
+
* frame, a skip that close to an edge, is left out, less than a frame of what they kept.
|
|
3407
|
+
*/
|
|
3408
|
+
function atJumps(parts: readonly Part[], placing: Placing, frameMs: number): Part[] {
|
|
3409
|
+
const jumps = placing.jumps ?? []
|
|
3410
|
+
if (jumps.length === 0) return [...parts]
|
|
3411
|
+
|
|
3412
|
+
return parts.flatMap((part) => {
|
|
3413
|
+
if (part.durationMs === undefined) return [part]
|
|
3414
|
+
const end = part.intoMs + part.durationMs
|
|
3415
|
+
const cuts = jumps.map((jump) => jump.intoMs).filter((at) => at > part.intoMs && at < end)
|
|
3416
|
+
const edges = [part.intoMs, ...cuts, end]
|
|
3417
|
+
const pieces = edges.slice(1).map((to, at) => {
|
|
3418
|
+
const from = edges[at] as number
|
|
3419
|
+
return {
|
|
3420
|
+
...part,
|
|
3421
|
+
startMs: part.startMs + from - part.intoMs,
|
|
3422
|
+
durationMs: to - from,
|
|
3423
|
+
intoMs: from,
|
|
3424
|
+
}
|
|
3425
|
+
})
|
|
3426
|
+
const whole = pieces.filter((piece) => piece.durationMs >= frameMs)
|
|
3427
|
+
return whole.length > 0 ? whole : pieces
|
|
3428
|
+
})
|
|
3429
|
+
}
|
|
3430
|
+
|
|
3431
|
+
/** A clip's element written for another file, the look it takes along, and what it is. */
|
|
3432
|
+
interface Carried {
|
|
3433
|
+
readonly file: string
|
|
3434
|
+
readonly scene: string
|
|
3435
|
+
readonly track: VideoTrack
|
|
3436
|
+
readonly html: string
|
|
3437
|
+
/** The clip it is a part of, by its place among those placed. */
|
|
3438
|
+
readonly index: number
|
|
3439
|
+
readonly element: LookElement
|
|
3440
|
+
/** Its look, the key it carries it by, and the scene it is from, when it lands elsewhere. */
|
|
3441
|
+
readonly look?: Look
|
|
3442
|
+
readonly key?: string
|
|
3443
|
+
readonly home?: string
|
|
3444
|
+
}
|
|
3445
|
+
|
|
3446
|
+
/**
|
|
3447
|
+
* What holds a clip landing in a scene, as a selector reads it: the video's root, the scene's host
|
|
3448
|
+
* and its composition, of which the clip is a child.
|
|
3449
|
+
*/
|
|
3450
|
+
function sceneContext(work: Working, scene: Scene): LookContext {
|
|
3451
|
+
const index = work.text(VIDEO_ENTRY)
|
|
3452
|
+
const composition =
|
|
3453
|
+
work.textOf(scene.src) === undefined ? undefined : readTimeline(work, scene.src)
|
|
3454
|
+
return [
|
|
3455
|
+
startTagOf(index, rootOf(work).root),
|
|
3456
|
+
startTagOf(index, scene.host),
|
|
3457
|
+
...(composition ? [startTagOf(work.text(scene.src), composition.composition)] : []),
|
|
3458
|
+
]
|
|
3459
|
+
}
|
|
3460
|
+
|
|
3461
|
+
/** What holds a clip where it is: its scene's, and the elements of its file around it. */
|
|
3462
|
+
function clipContext(work: Working, scene: Scene, text: string, clip: VideoClip): LookContext {
|
|
3463
|
+
const timeline = readTimeline(work, scene.src)
|
|
3464
|
+
const content = timeline?.composition.content
|
|
3465
|
+
const wrappers = content ? wrappersOf(text, content.start, clip.source.element.start) : []
|
|
3466
|
+
return [...sceneContext(work, scene), ...wrappers]
|
|
3467
|
+
}
|
|
3468
|
+
|
|
3469
|
+
/** An element as a selector reads it, from its start tag alone. */
|
|
3470
|
+
function startTagOf(text: string, source: VideoElementSource): LookElement {
|
|
3471
|
+
const html = text.slice(source.tag.start, source.tag.end)
|
|
3472
|
+
const element = lookElement(html, undefined)
|
|
3473
|
+
const id = element.attributes.get("id")
|
|
3474
|
+
return id ? { ...element, id } : element
|
|
3475
|
+
}
|
|
3476
|
+
|
|
3477
|
+
/** The elements open at `to`, from `from` in a file, outermost first: what holds what is there. */
|
|
3478
|
+
function wrappersOf(text: string, from: number, to: number): LookElement[] {
|
|
3479
|
+
const open: { readonly tag: string; readonly html: string }[] = []
|
|
3480
|
+
const tags = /<!--[\s\S]*?-->|<(\/?)([a-zA-Z][\w-]*)\b[^>]*>/g
|
|
3481
|
+
tags.lastIndex = from
|
|
3482
|
+
for (let match = tags.exec(text); match && match.index < to; match = tags.exec(text)) {
|
|
3483
|
+
const [html, closing, name] = match
|
|
3484
|
+
if (!name) continue
|
|
3485
|
+
const tag = name.toLowerCase()
|
|
3486
|
+
if (closing) {
|
|
3487
|
+
const at = open.map((each) => each.tag).lastIndexOf(tag)
|
|
3488
|
+
if (at >= 0) open.length = at
|
|
3489
|
+
continue
|
|
3490
|
+
}
|
|
3491
|
+
if (VOID_TAGS.has(tag) || html.endsWith("/>")) continue
|
|
3492
|
+
// What a script or a style holds is no element.
|
|
3493
|
+
if (tag === "script" || tag === "style") {
|
|
3494
|
+
const end = text.indexOf(`</${tag}`, tags.lastIndex)
|
|
3495
|
+
tags.lastIndex = end < 0 ? text.length : end
|
|
3496
|
+
continue
|
|
3497
|
+
}
|
|
3498
|
+
open.push({ tag, html })
|
|
3499
|
+
}
|
|
3500
|
+
return open.map(({ html }) => {
|
|
3501
|
+
const element = lookElement(html, undefined)
|
|
3502
|
+
const id = element.attributes.get("id")
|
|
3503
|
+
return id ? { ...element, id } : element
|
|
3504
|
+
})
|
|
3505
|
+
}
|
|
3506
|
+
|
|
3507
|
+
const VOID_TAGS = new Set([
|
|
3508
|
+
"area",
|
|
3509
|
+
"base",
|
|
3510
|
+
"br",
|
|
3511
|
+
"col",
|
|
3512
|
+
"embed",
|
|
3513
|
+
"hr",
|
|
3514
|
+
"img",
|
|
3515
|
+
"input",
|
|
3516
|
+
"link",
|
|
3517
|
+
"meta",
|
|
3518
|
+
"source",
|
|
3519
|
+
"track",
|
|
3520
|
+
"wbr",
|
|
3521
|
+
])
|
|
3522
|
+
|
|
3523
|
+
/** An element as a selector reads it, from its HTML, by the id it has where it is. */
|
|
3524
|
+
function lookElement(html: string, id: string | undefined): LookElement {
|
|
3525
|
+
const source = readElement(html)
|
|
3526
|
+
const attributes = new Map<string, string>()
|
|
3527
|
+
for (const [name, at] of Object.entries(source.attributes)) {
|
|
3528
|
+
attributes.set(name, attributeValue(html, at) ?? "")
|
|
3529
|
+
}
|
|
3530
|
+
return {
|
|
3531
|
+
tag: (/^<([a-zA-Z][\w-]*)/.exec(html)?.[1] ?? "").toLowerCase(),
|
|
3532
|
+
...(id ? { id } : {}),
|
|
3533
|
+
classes: (attributes.get("class") ?? "").split(/\s+/).filter(Boolean),
|
|
3534
|
+
attributes,
|
|
3535
|
+
}
|
|
3536
|
+
}
|
|
3537
|
+
|
|
3538
|
+
/**
|
|
3539
|
+
* An element as the selectors of its file may read it, by its tag, id, classes and the attributes
|
|
3540
|
+
* they name: elements alike so have one look there, which a placement works out once for them all.
|
|
3541
|
+
*/
|
|
3542
|
+
function lookSignature(element: LookElement, read: ReadonlySet<string>): string {
|
|
3543
|
+
const attributes = [...element.attributes].filter(([name]) => read.has(name.toLowerCase()))
|
|
3544
|
+
return JSON.stringify([element.tag, element.id, element.classes, attributes])
|
|
3545
|
+
}
|
|
3546
|
+
|
|
3547
|
+
/** The attributes some selector of these sheets names, `[name…]`, at any depth. */
|
|
3548
|
+
function attributesRead(sheets: readonly (readonly CssBlock[])[]): Set<string> {
|
|
3549
|
+
const read = new Set<string>()
|
|
3550
|
+
for (const blocks of sheets) {
|
|
3551
|
+
for (const { prelude, body } of blocks) {
|
|
3552
|
+
for (const match of `${prelude}{${body}`.matchAll(/\[\s*([\w-]+)/g)) {
|
|
3553
|
+
read.add((match[1] ?? "").toLowerCase())
|
|
3554
|
+
}
|
|
3555
|
+
}
|
|
3556
|
+
}
|
|
3557
|
+
return read
|
|
3558
|
+
}
|
|
3559
|
+
|
|
3560
|
+
/** A file's `<style>` elements: each one's CSS, and the look it is, by its key, if it is one. */
|
|
3561
|
+
function stylesIn(
|
|
3562
|
+
text: string
|
|
3563
|
+
): { readonly css: string; readonly look?: string; readonly from?: string }[] {
|
|
3564
|
+
return [...text.matchAll(/<style\b([^>]*)>([\s\S]*?)<\/style>/gi)].map((match) => {
|
|
3565
|
+
const attributes = match[1] ?? ""
|
|
3566
|
+
const look = /\sdata-look\s*=\s*"([^"]*)"/.exec(attributes)?.[1]
|
|
3567
|
+
const from = /\sdata-from\s*=\s*"([^"]*)"/.exec(attributes)?.[1]
|
|
3568
|
+
return { css: match[2] ?? "", ...(look ? { look } : {}), ...(from ? { from } : {}) }
|
|
3569
|
+
})
|
|
3570
|
+
}
|
|
3571
|
+
|
|
3572
|
+
/** The stylesheets `index.html` links, each as its blocks. */
|
|
3573
|
+
function linkedBlocks(work: Working): CssBlock[][] {
|
|
3574
|
+
const exists = (path: string) => work.has(path)
|
|
3575
|
+
return rootOf(work).links.stylesheets.flatMap((link) => {
|
|
3576
|
+
const path = videoFilePath(VIDEO_ENTRY, link.value, exists)
|
|
3577
|
+
const css = path === undefined ? undefined : work.textOf(path)
|
|
3578
|
+
return css === undefined ? [] : [cssBlocks(css)]
|
|
3579
|
+
})
|
|
3580
|
+
}
|
|
3581
|
+
|
|
3582
|
+
/**
|
|
3583
|
+
* The sheets that give a clip of a scene's file its look, in the order the page reads them, and the
|
|
3584
|
+
* scene it is from when it came with a look of its own, by `key`: then that look, and the rules
|
|
3585
|
+
* naming it by its id; else every rule that styles it there.
|
|
3586
|
+
*/
|
|
3587
|
+
function lookSheets(
|
|
3588
|
+
styles: readonly { readonly css: string; readonly look?: string; readonly from?: string }[],
|
|
3589
|
+
key: string | undefined,
|
|
3590
|
+
linked: readonly (readonly CssBlock[])[],
|
|
3591
|
+
parsed: (css: string) => readonly CssBlock[]
|
|
3592
|
+
): { readonly sheets: LookSheet[]; readonly home?: string } {
|
|
3593
|
+
const own = key ? styles.find((style) => style.look === key) : undefined
|
|
3594
|
+
const byId = own !== undefined
|
|
3595
|
+
// In the order the page reads them: the linked sheets in its head, the scene's own styles,
|
|
3596
|
+
// then the look carried in, written after them.
|
|
3597
|
+
const sheets: LookSheet[] = [
|
|
3598
|
+
...linked.map((blocks) => ({ blocks, byId, shared: true })),
|
|
3599
|
+
...styles
|
|
3600
|
+
.filter((style) => style.look === undefined)
|
|
3601
|
+
.map((style) => ({ blocks: parsed(style.css), byId })),
|
|
3602
|
+
...(own ? [{ blocks: parsed(own.css), carried: true }] : []),
|
|
3603
|
+
]
|
|
3604
|
+
return { sheets, ...(own?.from ? { home: own.from } : {}) }
|
|
3605
|
+
}
|
|
3606
|
+
|
|
3607
|
+
/** The keyframes a scene's file reads by name: its own, its linked sheets', and its looks'. */
|
|
3608
|
+
function sceneKeyframes(
|
|
3609
|
+
work: Working,
|
|
3610
|
+
file: string,
|
|
3611
|
+
linked: readonly (readonly CssBlock[])[]
|
|
3612
|
+
): SceneKeyframes {
|
|
3613
|
+
const own = new Map<string, string>()
|
|
3614
|
+
const carried = new Map<string, string[]>()
|
|
3615
|
+
const styles = stylesIn(work.text(file))
|
|
3616
|
+
for (const blocks of [
|
|
3617
|
+
...styles.filter((style) => !style.look).map((style) => cssBlocks(style.css)),
|
|
3618
|
+
...linked,
|
|
3619
|
+
]) {
|
|
3620
|
+
for (const [name, body] of keyframesOf(blocks)) if (!own.has(name)) own.set(name, body)
|
|
3621
|
+
}
|
|
3622
|
+
for (const style of styles.filter((each) => each.look)) {
|
|
3623
|
+
for (const [name, body] of keyframesOf(cssBlocks(style.css))) {
|
|
3624
|
+
carried.set(name, [...(carried.get(name) ?? []), body])
|
|
3625
|
+
}
|
|
3626
|
+
}
|
|
3627
|
+
return { own, carried }
|
|
3628
|
+
}
|
|
3629
|
+
|
|
3630
|
+
/** The keys of the looks a file writes, `<style data-look="<key>">`. */
|
|
3631
|
+
function looksIn(text: string): string[] {
|
|
3632
|
+
return stylesIn(text).flatMap((style) => (style.look ? [style.look] : []))
|
|
3633
|
+
}
|
|
3634
|
+
|
|
3635
|
+
/** The looks a file writes, `<style data-look="<key>">`, each by its key and where it is. */
|
|
3636
|
+
function lookBlocks(
|
|
3637
|
+
text: string
|
|
3638
|
+
): { readonly key: string; readonly start: number; readonly end: number }[] {
|
|
3639
|
+
return [...text.matchAll(/<style\b([^>]*)>[\s\S]*?<\/style>/gi)].flatMap((match) => {
|
|
3640
|
+
const key = /\sdata-look\s*=\s*"([^"]*)"/.exec(match[1] ?? "")?.[1]
|
|
3641
|
+
return key ? [{ key, start: match.index, end: match.index + match[0].length }] : []
|
|
3642
|
+
})
|
|
3643
|
+
}
|
|
3644
|
+
|
|
3645
|
+
/**
|
|
3646
|
+
* Removes each look of a file no element of it carries any more, and a look written twice, as two
|
|
3647
|
+
* people carrying it in at once may leave it, but once.
|
|
3648
|
+
*/
|
|
3649
|
+
function pruneLooks(work: Working, file: string): void {
|
|
3650
|
+
if (!work.textOf(file)?.includes("data-look")) return
|
|
3651
|
+
for (const key of new Set(looksIn(work.text(file)))) {
|
|
3652
|
+
const name = key.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
|
|
3653
|
+
for (;;) {
|
|
3654
|
+
const text = work.text(file)
|
|
3655
|
+
const blocks = lookBlocks(text).filter((block) => block.key === key)
|
|
3656
|
+
// Each style names its key once itself; each element carrying it, once more.
|
|
3657
|
+
const named = text.match(new RegExp(`\\sdata-look\\s*=\\s*"${name}"`, "g"))?.length ?? 0
|
|
3658
|
+
const extra = named > blocks.length ? blocks.slice(1) : blocks
|
|
3659
|
+
const last = extra.at(-1)
|
|
3660
|
+
if (!last) break
|
|
3661
|
+
work.replace(file, ...spanOf(lineBlock(text, last)), "")
|
|
3662
|
+
}
|
|
3663
|
+
}
|
|
3664
|
+
}
|
|
3665
|
+
|
|
3666
|
+
/**
|
|
3667
|
+
* Writes each look carried into a file from the scene `home` in a style of its own, once for all
|
|
3668
|
+
* the clips that carry it, `<style data-look="<key>" data-from="<home>">`, a child of the
|
|
3669
|
+
* composition before the scripts that close it, which a later move finds and takes along. All at
|
|
3670
|
+
* once, the file read once.
|
|
3671
|
+
*/
|
|
3672
|
+
function writeLooks(
|
|
3673
|
+
work: Working,
|
|
3674
|
+
file: string,
|
|
3675
|
+
looks: readonly {
|
|
3676
|
+
readonly key: string
|
|
3677
|
+
readonly home: string
|
|
3678
|
+
readonly rules: readonly string[]
|
|
3679
|
+
}[]
|
|
3680
|
+
): void {
|
|
3681
|
+
if (looks.length === 0) return
|
|
3682
|
+
|
|
3683
|
+
const text = work.text(file)
|
|
3684
|
+
const timeline = timelineOf(work, file)
|
|
3685
|
+
const children = timeline.children
|
|
3686
|
+
let closing = children.length
|
|
3687
|
+
while (closing > 0 && children[closing - 1]?.tag === "script") closing -= 1
|
|
3688
|
+
const next = children[closing]
|
|
3689
|
+
const previous = children[closing - 1]
|
|
3690
|
+
const indent = next
|
|
3691
|
+
? lineIndent(text, next.source.element.start)
|
|
3692
|
+
: previous
|
|
3693
|
+
? lineIndent(text, previous.source.element.start)
|
|
3694
|
+
: `${lineIndent(text, timeline.composition.element.start)} `
|
|
3695
|
+
|
|
3696
|
+
const styles = looks
|
|
3697
|
+
.map(({ key, home, rules }) => {
|
|
3698
|
+
const body = rules.map((rule) => `${indent} ${rule.replace(/\n/g, `\n${indent} `)}`)
|
|
3699
|
+
return `<style data-look="${escapeAttribute(key)}" data-from="${escapeAttribute(home)}">\n${body.join("\n")}\n${indent}</style>`
|
|
3700
|
+
})
|
|
3701
|
+
.join(`\n${indent}`)
|
|
3702
|
+
|
|
3703
|
+
if (next && aloneOnLine(text, next.source.element.start)) {
|
|
3704
|
+
const lineStart = text.lastIndexOf("\n", next.source.element.start - 1) + 1
|
|
3705
|
+
work.replace(file, lineStart, lineStart, `${indent}${styles}\n`)
|
|
3706
|
+
} else if (previous) {
|
|
3707
|
+
const block = lineBlock(text, previous.source.element)
|
|
3708
|
+
work.replace(file, block.end, block.end, `${indent}${styles}${lineEnd(text, block)}`)
|
|
3709
|
+
} else {
|
|
3710
|
+
const content = timeline.composition.content
|
|
3711
|
+
if (!content) throw new EditError(`${file}: its composition holds nothing a style can join.`)
|
|
3712
|
+
work.replace(file, content.start, content.start, `\n${indent}${styles}`)
|
|
3713
|
+
}
|
|
3714
|
+
}
|
|
3715
|
+
|
|
3716
|
+
/** Whether only spaces come before `at` on its line. */
|
|
3717
|
+
function aloneOnLine(text: string, at: number): boolean {
|
|
3718
|
+
return text.slice(text.lastIndexOf("\n", at - 1) + 1, at).trim() === ""
|
|
3719
|
+
}
|
|
3720
|
+
|
|
3721
|
+
/** A file's inline scripts, as one text. */
|
|
3722
|
+
function scriptsOf(text: string): string {
|
|
3723
|
+
return [...text.matchAll(/<script\b[^>]*>([\s\S]*?)<\/script>/gi)]
|
|
3724
|
+
.map((match) => match[1] ?? "")
|
|
3725
|
+
.join("\n")
|
|
3726
|
+
}
|
|
3727
|
+
|
|
3728
|
+
function spanOf(span: VideoSourceSpan): [number, number] {
|
|
3729
|
+
return [span.start, span.end]
|
|
3730
|
+
}
|
|
3731
|
+
|
|
3732
|
+
/** A file's path as a scene's file writes it, rebased for another; undefined when unchanged. */
|
|
3733
|
+
function rebased(work: Working, from: string, to: string, written: string): string | undefined {
|
|
3734
|
+
const exists = (path: string) => work.has(path)
|
|
3735
|
+
const path = videoFilePath(from, written, exists)
|
|
3736
|
+
if (path === undefined || videoFilePath(to, written, exists) === path) return undefined
|
|
3737
|
+
|
|
3738
|
+
const up = "../".repeat(to.split("/").length - 1)
|
|
3739
|
+
return [path, `${up}${path}`].find((each) => videoFilePath(to, each, exists) === path)
|
|
3740
|
+
}
|
|
3741
|
+
|
|
3742
|
+
/** The clip a placement marked with `index`. */
|
|
3743
|
+
function markedClip(text: string, clips: readonly VideoClip[], index: number): VideoClip {
|
|
3744
|
+
const found = clips.find((clip) => {
|
|
3745
|
+
const at = clip.source.attributes[PLACING]
|
|
3746
|
+
return at !== undefined && attributeValue(text, at) === String(index)
|
|
3747
|
+
})
|
|
3748
|
+
if (!found) throw new EditError("A clip changed while it was placed: try again.")
|
|
3749
|
+
return found
|
|
3750
|
+
}
|
|
3751
|
+
|
|
3752
|
+
/** An attribute a clip writes, as written. */
|
|
3753
|
+
function attributeOf(text: string, clip: VideoClip, name: string): string | undefined {
|
|
3754
|
+
const at = clip.source.attributes[name]
|
|
3755
|
+
return at ? attributeValue(text, at) : undefined
|
|
3756
|
+
}
|
|
3757
|
+
|
|
3758
|
+
function groupBy<T>(items: readonly T[], key: (item: T) => string): Map<string, T[]> {
|
|
3759
|
+
const groups = new Map<string, T[]>()
|
|
3760
|
+
for (const item of items) groups.set(key(item), [...(groups.get(key(item)) ?? []), item])
|
|
3761
|
+
return groups
|
|
3762
|
+
}
|
|
3763
|
+
|
|
3764
|
+
// Text, inside ------------------------------------------------------------------------------------
|
|
3765
|
+
|
|
3766
|
+
/**
|
|
3767
|
+
* Sets attributes of an element, read from `source`: a value written again in place, one it lacks
|
|
3768
|
+
* added at the end of its start tag, null removed. From the last to the first, so each span still
|
|
3769
|
+
* names what it named.
|
|
3770
|
+
*/
|
|
3771
|
+
function setAttributes(
|
|
3772
|
+
work: Working,
|
|
3773
|
+
file: string,
|
|
3774
|
+
source: VideoElementSource,
|
|
3775
|
+
values: Readonly<Record<string, string | null>>
|
|
3776
|
+
): void {
|
|
3777
|
+
const text = work.text(file)
|
|
3778
|
+
const steps: { readonly at: number; readonly run: () => void }[] = []
|
|
3779
|
+
const added: string[] = []
|
|
3780
|
+
|
|
3781
|
+
for (const [name, value] of Object.entries(values)) {
|
|
3782
|
+
// Two people writing one value at once leave it twice: the first counts, as in a browser,
|
|
3783
|
+
// and the next write keeps it alone.
|
|
3784
|
+
for (const copy of laterCopies(text, source, name)) {
|
|
3785
|
+
steps.push({ at: copy.start, run: () => work.replace(file, copy.start, copy.end, "") })
|
|
3786
|
+
}
|
|
3787
|
+
|
|
3788
|
+
const at = source.attributes[name]
|
|
3789
|
+
if (!at) {
|
|
3790
|
+
if (value !== null) added.push(` ${name}="${escapeAttribute(value)}"`)
|
|
3791
|
+
continue
|
|
3792
|
+
}
|
|
3793
|
+
|
|
3794
|
+
if (value === null) {
|
|
3795
|
+
// The attribute and the space before it.
|
|
3796
|
+
let start = at.start
|
|
3797
|
+
while (start > 0 && /[ \t\n\r]/.test(text[start - 1] ?? "")) start -= 1
|
|
3798
|
+
steps.push({ at: at.start, run: () => work.replace(file, start, at.end, "") })
|
|
3799
|
+
continue
|
|
3800
|
+
}
|
|
3801
|
+
|
|
3802
|
+
const span = valueSpan(text, at)
|
|
3803
|
+
if (!span) {
|
|
3804
|
+
steps.push({
|
|
3805
|
+
at: at.start,
|
|
3806
|
+
run: () => work.replace(file, at.start, at.end, `${name}="${escapeAttribute(value)}"`),
|
|
3807
|
+
})
|
|
3808
|
+
continue
|
|
3809
|
+
}
|
|
3810
|
+
const written = span.quoted ? escapeAttribute(value) : `"${escapeAttribute(value)}"`
|
|
3811
|
+
if (attributeValue(text, at) === value) continue
|
|
3812
|
+
steps.push({ at: at.start, run: () => work.replace(file, span.start, span.end, written) })
|
|
3813
|
+
}
|
|
3814
|
+
|
|
3815
|
+
if (added.length > 0) {
|
|
3816
|
+
const close = text.slice(source.tag.start, source.tag.end).endsWith("/>") ? 2 : 1
|
|
3817
|
+
let at = source.tag.end - close
|
|
3818
|
+
while (at > source.tag.start && /[ \t\n\r]/.test(text[at - 1] ?? "")) at -= 1
|
|
3819
|
+
steps.push({ at, run: () => work.replace(file, at, at, added.join("")) })
|
|
3820
|
+
}
|
|
3821
|
+
|
|
3822
|
+
for (const step of steps.sort((a, b) => b.at - a.at)) step.run()
|
|
3823
|
+
}
|
|
3824
|
+
|
|
3825
|
+
/** Each copy of an attribute its start tag writes after the first, with the space before it. */
|
|
3826
|
+
function laterCopies(text: string, source: VideoElementSource, name: string): VideoSourceSpan[] {
|
|
3827
|
+
const first = source.attributes[name]
|
|
3828
|
+
if (!first) return []
|
|
3829
|
+
|
|
3830
|
+
const copies: VideoSourceSpan[] = []
|
|
3831
|
+
const pattern = /\s+([^\s"'>/=]+)(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s"'=<>`]+))?/y
|
|
3832
|
+
let at = first.end
|
|
3833
|
+
for (;;) {
|
|
3834
|
+
pattern.lastIndex = at
|
|
3835
|
+
const match = pattern.exec(text)
|
|
3836
|
+
if (!match?.[1] || match.index + match[0].length > source.tag.end) break
|
|
3837
|
+
if (match[1].toLowerCase() === name) {
|
|
3838
|
+
copies.push({ start: match.index, end: match.index + match[0].length })
|
|
3839
|
+
}
|
|
3840
|
+
at = match.index + match[0].length
|
|
3841
|
+
}
|
|
3842
|
+
return copies
|
|
3843
|
+
}
|
|
3844
|
+
|
|
3845
|
+
/** An element's HTML, written alone, with attributes set as {@link setAttributes} sets them. */
|
|
3846
|
+
function withAttributes(html: string, values: Readonly<Record<string, string | null>>): string {
|
|
3847
|
+
const scratch = new Working(new Map([["element.html", html]]))
|
|
3848
|
+
const source = readElement(html)
|
|
3849
|
+
setAttributes(scratch, "element.html", source, values)
|
|
3850
|
+
return scratch.text("element.html")
|
|
3851
|
+
}
|
|
3852
|
+
|
|
3853
|
+
/** The first element of a piece of HTML, where it writes its attributes. */
|
|
3854
|
+
function readElement(html: string): VideoElementSource {
|
|
3855
|
+
const tag = /^<([a-zA-Z][\w-]*)/.exec(html)
|
|
3856
|
+
if (!tag) throw new EditError("An element was expected.")
|
|
3857
|
+
|
|
3858
|
+
const attributes: Record<string, VideoSourceSpan> = {}
|
|
3859
|
+
const pattern = /\s+([^\s"'>/=]+)(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s"'=<>`]+))?/y
|
|
3860
|
+
let at = tag[0].length
|
|
3861
|
+
for (;;) {
|
|
3862
|
+
pattern.lastIndex = at
|
|
3863
|
+
const match = pattern.exec(html)
|
|
3864
|
+
if (!match?.[1]) break
|
|
3865
|
+
const name = match[1].toLowerCase()
|
|
3866
|
+
const start = match.index + match[0].length - match[0].trimStart().length
|
|
3867
|
+
attributes[name] ??= { start, end: match.index + match[0].length }
|
|
3868
|
+
at = match.index + match[0].length
|
|
3869
|
+
}
|
|
3870
|
+
|
|
3871
|
+
const close = html.indexOf(">", at)
|
|
3872
|
+
if (close < 0) throw new EditError("An element's start tag was not closed.")
|
|
3873
|
+
return { element: { start: 0, end: html.length }, tag: { start: 0, end: close + 1 }, attributes }
|
|
3874
|
+
}
|
|
3875
|
+
|
|
3876
|
+
/** Where an attribute's value is written, inside its quotes when it has them; none for a bare name. */
|
|
3877
|
+
function valueSpan(
|
|
3878
|
+
text: string,
|
|
3879
|
+
at: VideoSourceSpan
|
|
3880
|
+
): { readonly start: number; readonly end: number; readonly quoted: boolean } | undefined {
|
|
3881
|
+
const written = text.slice(at.start, at.end)
|
|
3882
|
+
const equals = written.indexOf("=")
|
|
3883
|
+
if (equals < 0) return undefined
|
|
3884
|
+
|
|
3885
|
+
let start = at.start + equals + 1
|
|
3886
|
+
while (/[ \t\n\r]/.test(text[start] ?? "")) start += 1
|
|
3887
|
+
const quote = text[start]
|
|
3888
|
+
if (quote === '"' || quote === "'") return { start: start + 1, end: at.end - 1, quoted: true }
|
|
3889
|
+
return { start, end: at.end, quoted: false }
|
|
3890
|
+
}
|
|
3891
|
+
|
|
3892
|
+
/** An attribute's value as written, its entities decoded. */
|
|
3893
|
+
function attributeValue(text: string, at: VideoSourceSpan): string | undefined {
|
|
3894
|
+
const span = valueSpan(text, at)
|
|
3895
|
+
if (!span) return undefined
|
|
3896
|
+
return decodeEntities(text.slice(span.start, span.end))
|
|
3897
|
+
}
|
|
3898
|
+
|
|
3899
|
+
function decodeEntities(value: string): string {
|
|
3900
|
+
return value
|
|
3901
|
+
.replace(/"/g, '"')
|
|
3902
|
+
.replace(/'/g, "'")
|
|
3903
|
+
.replace(/</g, "<")
|
|
3904
|
+
.replace(/>/g, ">")
|
|
3905
|
+
.replace(/&/g, "&")
|
|
3906
|
+
}
|
|
3907
|
+
|
|
3908
|
+
function escapeAttribute(value: string): string {
|
|
3909
|
+
return value.replace(/&/g, "&").replace(/"/g, """)
|
|
3910
|
+
}
|
|
3911
|
+
|
|
3912
|
+
function escapeText(value: string): string {
|
|
3913
|
+
return value.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">")
|
|
3914
|
+
}
|
|
3915
|
+
|
|
3916
|
+
/** Milliseconds written as seconds, to the millisecond: `4200` as `4.2`. */
|
|
3917
|
+
function seconds(ms: number): string {
|
|
3918
|
+
return String(Math.round(ms) / 1000)
|
|
3919
|
+
}
|
|
3920
|
+
|
|
3921
|
+
/** Ranges sorted, and those that touch or overlap joined. */
|
|
3922
|
+
function mergeRanges(ranges: readonly VideoRange[]): VideoRange[] {
|
|
3923
|
+
const sorted = ranges
|
|
3924
|
+
.filter((range) => range.endMs > range.startMs)
|
|
3925
|
+
.sort((a, b) => a.startMs - b.startMs)
|
|
3926
|
+
const merged: VideoRange[] = []
|
|
3927
|
+
for (const range of sorted) {
|
|
3928
|
+
const last = merged.at(-1)
|
|
3929
|
+
if (last && range.startMs <= last.endMs) {
|
|
3930
|
+
merged[merged.length - 1] = {
|
|
3931
|
+
startMs: last.startMs,
|
|
3932
|
+
endMs: Math.max(last.endMs, range.endMs),
|
|
3933
|
+
}
|
|
3934
|
+
} else {
|
|
3935
|
+
merged.push(range)
|
|
3936
|
+
}
|
|
3937
|
+
}
|
|
3938
|
+
return merged
|
|
3939
|
+
}
|
|
3940
|
+
|
|
3941
|
+
/**
|
|
3942
|
+
* An element with its line: from the line's start when only spaces come before it, through its
|
|
3943
|
+
* line's end when only spaces follow, so removing it leaves no blank line.
|
|
3944
|
+
*/
|
|
3945
|
+
function lineBlock(text: string, span: VideoSourceSpan): VideoSourceSpan {
|
|
3946
|
+
let start = span.start
|
|
3947
|
+
while (start > 0 && (text[start - 1] === " " || text[start - 1] === "\t")) start -= 1
|
|
3948
|
+
const alone = start === 0 || text[start - 1] === "\n"
|
|
3949
|
+
if (!alone) return span
|
|
3950
|
+
|
|
3951
|
+
let end = span.end
|
|
3952
|
+
while (end < text.length && (text[end] === " " || text[end] === "\t")) end += 1
|
|
3953
|
+
if (text[end] === "\r") end += 1
|
|
3954
|
+
if (text[end] === "\n") return { start, end: end + 1 }
|
|
3955
|
+
return end === text.length ? { start, end } : span
|
|
3956
|
+
}
|
|
3957
|
+
|
|
3958
|
+
/** What ends a line block: nothing when it took its line's end with it, else a line break. */
|
|
3959
|
+
function lineEnd(text: string, block: VideoSourceSpan): string {
|
|
3960
|
+
return text[block.end - 1] === "\n" ? "\n" : ""
|
|
3961
|
+
}
|
|
3962
|
+
|
|
3963
|
+
/** The spaces a line starts with, before `at`. */
|
|
3964
|
+
function lineIndent(text: string, at: number): string {
|
|
3965
|
+
const line = text.lastIndexOf("\n", at - 1) + 1
|
|
3966
|
+
return /^[ \t]*/.exec(text.slice(line, at))?.[0] ?? ""
|
|
3967
|
+
}
|