@twick/2d 0.13.0 → 0.14.2

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.
Files changed (171) hide show
  1. package/LICENSE +21 -0
  2. package/editor/editor/tsconfig.build.tsbuildinfo +1 -1
  3. package/lib/components/Audio.js +3 -3
  4. package/lib/components/Img.js +23 -23
  5. package/lib/components/Line.js +31 -31
  6. package/lib/components/Media.d.ts +1 -1
  7. package/lib/components/Media.d.ts.map +1 -1
  8. package/lib/components/Media.js +26 -26
  9. package/lib/components/Spline.js +25 -25
  10. package/lib/components/Video.js +3 -3
  11. package/lib/tsconfig.build.tsbuildinfo +1 -1
  12. package/package.json +5 -4
  13. package/src/editor/NodeInspectorConfig.tsx +76 -76
  14. package/src/editor/PreviewOverlayConfig.tsx +67 -67
  15. package/src/editor/Provider.tsx +93 -93
  16. package/src/editor/SceneGraphTabConfig.tsx +81 -81
  17. package/src/editor/icons/CircleIcon.tsx +7 -7
  18. package/src/editor/icons/CodeBlockIcon.tsx +8 -8
  19. package/src/editor/icons/CurveIcon.tsx +7 -7
  20. package/src/editor/icons/GridIcon.tsx +7 -7
  21. package/src/editor/icons/IconMap.ts +35 -35
  22. package/src/editor/icons/ImgIcon.tsx +8 -8
  23. package/src/editor/icons/LayoutIcon.tsx +9 -9
  24. package/src/editor/icons/LineIcon.tsx +7 -7
  25. package/src/editor/icons/NodeIcon.tsx +7 -7
  26. package/src/editor/icons/RayIcon.tsx +7 -7
  27. package/src/editor/icons/RectIcon.tsx +7 -7
  28. package/src/editor/icons/ShapeIcon.tsx +7 -7
  29. package/src/editor/icons/TxtIcon.tsx +8 -8
  30. package/src/editor/icons/VideoIcon.tsx +7 -7
  31. package/src/editor/icons/View2DIcon.tsx +10 -10
  32. package/src/editor/index.ts +17 -17
  33. package/src/editor/tree/DetachedRoot.tsx +23 -23
  34. package/src/editor/tree/NodeElement.tsx +74 -74
  35. package/src/editor/tree/TreeElement.tsx +72 -72
  36. package/src/editor/tree/TreeRoot.tsx +10 -10
  37. package/src/editor/tree/ViewRoot.tsx +20 -20
  38. package/src/editor/tree/index.module.scss +38 -38
  39. package/src/editor/tree/index.ts +3 -3
  40. package/src/editor/tsconfig.build.json +5 -5
  41. package/src/editor/tsconfig.json +12 -12
  42. package/src/editor/tsdoc.json +4 -4
  43. package/src/editor/vite-env.d.ts +1 -1
  44. package/src/lib/code/CodeCursor.ts +445 -445
  45. package/src/lib/code/CodeDiffer.ts +78 -78
  46. package/src/lib/code/CodeFragment.ts +97 -97
  47. package/src/lib/code/CodeHighlighter.ts +75 -75
  48. package/src/lib/code/CodeMetrics.ts +47 -47
  49. package/src/lib/code/CodeRange.test.ts +74 -74
  50. package/src/lib/code/CodeRange.ts +216 -216
  51. package/src/lib/code/CodeScope.ts +101 -101
  52. package/src/lib/code/CodeSelection.ts +24 -24
  53. package/src/lib/code/CodeSignal.ts +327 -327
  54. package/src/lib/code/CodeTokenizer.ts +54 -54
  55. package/src/lib/code/DefaultHighlightStyle.ts +98 -98
  56. package/src/lib/code/LezerHighlighter.ts +113 -113
  57. package/src/lib/code/diff.test.ts +311 -311
  58. package/src/lib/code/diff.ts +319 -319
  59. package/src/lib/code/extractRange.ts +126 -126
  60. package/src/lib/code/index.ts +13 -13
  61. package/src/lib/components/Audio.ts +168 -168
  62. package/src/lib/components/Bezier.ts +105 -105
  63. package/src/lib/components/Circle.ts +266 -266
  64. package/src/lib/components/Code.ts +526 -526
  65. package/src/lib/components/CodeBlock.ts +576 -576
  66. package/src/lib/components/CubicBezier.ts +112 -112
  67. package/src/lib/components/Curve.ts +455 -455
  68. package/src/lib/components/Grid.ts +135 -135
  69. package/src/lib/components/Icon.ts +96 -96
  70. package/src/lib/components/Img.ts +319 -319
  71. package/src/lib/components/Knot.ts +157 -157
  72. package/src/lib/components/Latex.ts +122 -122
  73. package/src/lib/components/Layout.ts +1092 -1092
  74. package/src/lib/components/Line.ts +429 -429
  75. package/src/lib/components/Media.ts +576 -576
  76. package/src/lib/components/Node.ts +1940 -1940
  77. package/src/lib/components/Path.ts +137 -137
  78. package/src/lib/components/Polygon.ts +171 -171
  79. package/src/lib/components/QuadBezier.ts +100 -100
  80. package/src/lib/components/Ray.ts +125 -125
  81. package/src/lib/components/Rect.ts +187 -187
  82. package/src/lib/components/Rive.ts +156 -156
  83. package/src/lib/components/SVG.ts +797 -797
  84. package/src/lib/components/Shape.ts +143 -143
  85. package/src/lib/components/Spline.ts +344 -344
  86. package/src/lib/components/Txt.test.tsx +81 -81
  87. package/src/lib/components/Txt.ts +203 -203
  88. package/src/lib/components/TxtLeaf.ts +205 -205
  89. package/src/lib/components/Video.ts +461 -461
  90. package/src/lib/components/View2D.ts +98 -98
  91. package/src/lib/components/__tests__/children.test.tsx +142 -142
  92. package/src/lib/components/__tests__/clone.test.tsx +126 -126
  93. package/src/lib/components/__tests__/generatorTest.ts +28 -28
  94. package/src/lib/components/__tests__/mockScene2D.ts +45 -45
  95. package/src/lib/components/__tests__/query.test.tsx +122 -122
  96. package/src/lib/components/__tests__/state.test.tsx +60 -60
  97. package/src/lib/components/index.ts +28 -28
  98. package/src/lib/components/types.ts +35 -35
  99. package/src/lib/curves/ArcSegment.ts +159 -159
  100. package/src/lib/curves/CircleSegment.ts +77 -77
  101. package/src/lib/curves/CubicBezierSegment.ts +78 -78
  102. package/src/lib/curves/CurveDrawingInfo.ts +11 -11
  103. package/src/lib/curves/CurvePoint.ts +15 -15
  104. package/src/lib/curves/CurveProfile.ts +7 -7
  105. package/src/lib/curves/KnotInfo.ts +10 -10
  106. package/src/lib/curves/LineSegment.ts +62 -62
  107. package/src/lib/curves/Polynomial.ts +355 -355
  108. package/src/lib/curves/Polynomial2D.ts +62 -62
  109. package/src/lib/curves/PolynomialSegment.ts +124 -124
  110. package/src/lib/curves/QuadBezierSegment.ts +64 -64
  111. package/src/lib/curves/Segment.ts +17 -17
  112. package/src/lib/curves/UniformPolynomialCurveSampler.ts +94 -94
  113. package/src/lib/curves/createCurveProfileLerp.ts +471 -471
  114. package/src/lib/curves/getBezierSplineProfile.ts +223 -223
  115. package/src/lib/curves/getCircleProfile.ts +86 -86
  116. package/src/lib/curves/getPathProfile.ts +178 -178
  117. package/src/lib/curves/getPointAtDistance.ts +21 -21
  118. package/src/lib/curves/getPolylineProfile.test.ts +21 -21
  119. package/src/lib/curves/getPolylineProfile.ts +89 -89
  120. package/src/lib/curves/getRectProfile.ts +139 -139
  121. package/src/lib/curves/index.ts +16 -16
  122. package/src/lib/decorators/canvasStyleSignal.ts +16 -16
  123. package/src/lib/decorators/colorSignal.ts +9 -9
  124. package/src/lib/decorators/compound.ts +72 -72
  125. package/src/lib/decorators/computed.ts +18 -18
  126. package/src/lib/decorators/defaultStyle.ts +18 -18
  127. package/src/lib/decorators/filtersSignal.ts +136 -136
  128. package/src/lib/decorators/index.ts +10 -10
  129. package/src/lib/decorators/initializers.ts +32 -32
  130. package/src/lib/decorators/nodeName.ts +13 -13
  131. package/src/lib/decorators/signal.test.ts +90 -90
  132. package/src/lib/decorators/signal.ts +345 -345
  133. package/src/lib/decorators/spacingSignal.ts +15 -15
  134. package/src/lib/decorators/vector2Signal.ts +30 -30
  135. package/src/lib/globals.d.ts +2 -2
  136. package/src/lib/index.ts +8 -8
  137. package/src/lib/jsx-dev-runtime.ts +2 -2
  138. package/src/lib/jsx-runtime.ts +46 -46
  139. package/src/lib/parse-svg-path.d.ts +14 -14
  140. package/src/lib/partials/Filter.ts +180 -180
  141. package/src/lib/partials/Gradient.ts +102 -102
  142. package/src/lib/partials/Pattern.ts +34 -34
  143. package/src/lib/partials/ShaderConfig.ts +117 -117
  144. package/src/lib/partials/index.ts +4 -4
  145. package/src/lib/partials/types.ts +58 -58
  146. package/src/lib/scenes/Scene2D.ts +242 -242
  147. package/src/lib/scenes/index.ts +3 -3
  148. package/src/lib/scenes/makeScene2D.ts +16 -16
  149. package/src/lib/scenes/useScene2D.ts +6 -6
  150. package/src/lib/tsconfig.build.json +5 -5
  151. package/src/lib/tsconfig.json +10 -10
  152. package/src/lib/tsdoc.json +4 -4
  153. package/src/lib/utils/CanvasUtils.ts +306 -306
  154. package/src/lib/utils/diff.test.ts +453 -453
  155. package/src/lib/utils/diff.ts +148 -148
  156. package/src/lib/utils/index.ts +2 -2
  157. package/src/lib/utils/is.ts +11 -11
  158. package/src/lib/utils/makeSignalExtensions.ts +30 -30
  159. package/src/lib/utils/video/declarations.d.ts +1 -1
  160. package/src/lib/utils/video/ffmpeg-client.ts +50 -50
  161. package/src/lib/utils/video/mp4-parser-manager.ts +72 -72
  162. package/src/lib/utils/video/parser/index.ts +1 -1
  163. package/src/lib/utils/video/parser/parser.ts +257 -257
  164. package/src/lib/utils/video/parser/sampler.ts +72 -72
  165. package/src/lib/utils/video/parser/segment.ts +302 -302
  166. package/src/lib/utils/video/parser/sink.ts +29 -29
  167. package/src/lib/utils/video/parser/utils.ts +31 -31
  168. package/src/tsconfig.base.json +19 -19
  169. package/src/tsconfig.build.json +8 -8
  170. package/src/tsconfig.json +5 -5
  171. package/tsconfig.project.json +7 -7
@@ -1,257 +1,257 @@
1
- import {createFile} from 'mp4box';
2
- import {FrameSampler} from './sampler';
3
- import {Segment} from './segment';
4
- import {MP4FileSink} from './sink';
5
- import type {Edit} from './utils';
6
- import {description, mp4BoxEditToEdit} from './utils';
7
-
8
- /**
9
- * Loads the file at the given URI until it finds the moov box.
10
- * Once found, it calls `setConfig` with the video configuration.
11
- * @param uri - The URI of the video file.
12
- * @param setConfig - Callback to set the video configuration.
13
- * @returns
14
- */
15
- async function getFileInfo(uri: string) {
16
- return new Promise<{
17
- file: any;
18
- edits: Edit[];
19
- config: VideoDecoderConfig;
20
- }>((res, rej) => {
21
- const file = createFile();
22
- let found = false;
23
- const controller = new AbortController();
24
-
25
- file.onReady = (info: any) => {
26
- found = true;
27
- controller.abort();
28
-
29
- const track = info.videoTracks[0];
30
-
31
- const config = {
32
- // Browser doesn't support parsing full vp8 codec (eg: `vp08.00.41.08`),
33
- // they only support `vp8`.
34
- codec: track.codec.startsWith('vp08') ? 'vp8' : track.codec,
35
- codedHeight: track.video.height,
36
- codedWidth: track.video.width,
37
- description: description(file, track),
38
- };
39
-
40
- const defaultEdit: Omit<Edit, 'fps'> = {
41
- mediaTime: 0,
42
- segmentDuration: track.duration,
43
- mediaRateInteger: 1,
44
- mediaRateFraction: 0,
45
- };
46
-
47
- const editsFromVideo: Omit<Edit, 'fps'>[] = track.edits?.map(
48
- (edit: any) => mp4BoxEditToEdit(edit),
49
- );
50
-
51
- // If there are no entries, use the default edit
52
- const editsWithoutFps = editsFromVideo?.length
53
- ? editsFromVideo
54
- : [defaultEdit];
55
-
56
- // Calculate FPS for each segment
57
- const edits = editsWithoutFps.map(edit => {
58
- const trackDurationInSec = track.duration / track.timescale;
59
- const segmentDurationInSec =
60
- edit.segmentDuration / track.movie_timescale;
61
- const segmentFrames =
62
- track.nb_samples * (segmentDurationInSec / trackDurationInSec);
63
- const mediaRate =
64
- edit.mediaRateInteger + edit.mediaRateFraction / 0xffff;
65
- const fps = (segmentFrames / segmentDurationInSec) * mediaRate;
66
- return {...edit, fps};
67
- });
68
-
69
- file.setExtractionOptions(track.id);
70
- file.start();
71
-
72
- res({file, edits, config});
73
- };
74
-
75
- return fetch(uri, {signal: controller.signal}).then(async response => {
76
- if (!response.body) {
77
- throw new Error('Response body is null');
78
- }
79
-
80
- const reader = response.body.getReader();
81
- const sink = new MP4FileSink(file, () => {});
82
-
83
- while (!found) {
84
- await reader.read().then(({done, value}) => {
85
- if (done) {
86
- file.flush();
87
- controller.abort();
88
- rej('Could not find moov');
89
- return;
90
- }
91
-
92
- sink.write(value);
93
- });
94
- }
95
- });
96
- });
97
- }
98
-
99
- export class Mp4Parser {
100
- private uri: string;
101
- private file: any;
102
- private edits: Edit[] = [];
103
- private decoderConfig?: VideoDecoderConfig;
104
-
105
- private startTime: number;
106
- private targetFps: number;
107
-
108
- private nextSegment = 0;
109
- private sampler?: FrameSampler;
110
-
111
- public constructor(uri: string, targetFps: number, startTime: number) {
112
- this.uri = uri;
113
- this.targetFps = targetFps;
114
- this.startTime = startTime;
115
- }
116
-
117
- public async start() {
118
- const {file, edits, config} = await getFileInfo(this.uri);
119
- this.file = file;
120
- this.edits = edits;
121
- this.decoderConfig = config;
122
- }
123
-
124
- public async getNextFrame() {
125
- // Start the first segment
126
- if (!this.sampler) {
127
- // Skip segments until the start time
128
- let startTimeWithinSegment = this.startTime;
129
- while (this.nextSegment < this.edits.length) {
130
- const segmentDurationInSeconds = this.getSecondDurationOfSegment(
131
- this.edits[this.nextSegment],
132
- );
133
- if (startTimeWithinSegment < segmentDurationInSeconds) {
134
- break;
135
- }
136
-
137
- startTimeWithinSegment -= segmentDurationInSeconds;
138
- this.nextSegment++;
139
- }
140
-
141
- // The timestamp is outside of the video
142
- if (this.nextSegment >= this.edits.length) {
143
- throw new Error(
144
- `Timestamp ${this.startTime} is outside of the video, max timestamp is ${this.getDuration()}`,
145
- );
146
- }
147
-
148
- const segment = new Segment(
149
- this.uri,
150
- this.file,
151
- this.edits,
152
- this.nextSegment,
153
- startTimeWithinSegment,
154
- );
155
- await segment.start(this.decoderConfig!);
156
-
157
- this.sampler = new FrameSampler(
158
- segment,
159
- this.edits[this.nextSegment].fps,
160
- this.targetFps,
161
- 0,
162
- );
163
- this.nextSegment++;
164
- }
165
-
166
- // Try to get the next frame
167
- let frame = await this.sampler.getNextFrame();
168
-
169
- // If there are no more frames in the current segment, start the next segment
170
- while (!frame && this.nextSegment < this.edits.length) {
171
- this.sampler.getSegment().close();
172
- this.sampler.getLastFrame()?.close();
173
- const segment = new Segment(
174
- this.uri,
175
- this.file,
176
- this.edits,
177
- this.nextSegment,
178
- 0,
179
- );
180
- await segment.start(this.decoderConfig!);
181
-
182
- this.sampler = new FrameSampler(
183
- segment,
184
- this.edits[this.nextSegment].fps,
185
- this.targetFps,
186
- this.sampler.getSum(), // Carry over the sum from the previous segment
187
- );
188
- this.nextSegment++;
189
-
190
- frame = await this.sampler.getNextFrame();
191
- }
192
-
193
- // If there are no more frames, return the last frame
194
- if (!frame) {
195
- frame = this.sampler.getLastFrame();
196
-
197
- // We can close the segment
198
- await this.sampler.getSegment().close();
199
-
200
- // If we still don't have a frame, throw an error
201
- if (!frame) {
202
- throw new Error('There are no frames in the video.');
203
- }
204
- }
205
-
206
- return frame;
207
- }
208
-
209
- private getSecondDurationOfSegment(edit: Edit) {
210
- const mediaRate = edit.mediaRateInteger + edit.mediaRateFraction / 0xffff;
211
- const duration =
212
- edit.segmentDuration / this.file.getInfo().videoTracks[0].movie_timescale;
213
- return duration / mediaRate;
214
- }
215
-
216
- public getDuration() {
217
- return this.edits.reduce(
218
- (acc, edit) => acc + this.getSecondDurationOfSegment(edit),
219
- 0,
220
- );
221
- }
222
-
223
- private getTimeSubtractingFrames(frames: number) {
224
- const durationOfPastSegmentsInSeconds = this.edits
225
- .slice(0, this.nextSegment - 1)
226
- .reduce((acc, edit) => acc + this.getSecondDurationOfSegment(edit), 0);
227
-
228
- if (!this.sampler) {
229
- throw new Error('No current segment');
230
- }
231
-
232
- const currentSegmentStartTime = this.sampler.getSegment().getStartTime();
233
- const samplerTime = this.sampler.getTime(frames);
234
-
235
- return (
236
- durationOfPastSegmentsInSeconds + currentSegmentStartTime + samplerTime
237
- );
238
- }
239
-
240
- public getTime() {
241
- return this.getTimeSubtractingFrames(0);
242
- }
243
-
244
- public getLastTime() {
245
- return this.getTimeSubtractingFrames(1);
246
- }
247
-
248
- public getLastFrame() {
249
- return this.sampler?.getLastFrame();
250
- }
251
-
252
- public close() {
253
- this.sampler?.getSegment().close();
254
- this.sampler?.getLastFrame()?.close();
255
- this.file.flush();
256
- }
257
- }
1
+ import {createFile} from 'mp4box';
2
+ import {FrameSampler} from './sampler';
3
+ import {Segment} from './segment';
4
+ import {MP4FileSink} from './sink';
5
+ import type {Edit} from './utils';
6
+ import {description, mp4BoxEditToEdit} from './utils';
7
+
8
+ /**
9
+ * Loads the file at the given URI until it finds the moov box.
10
+ * Once found, it calls `setConfig` with the video configuration.
11
+ * @param uri - The URI of the video file.
12
+ * @param setConfig - Callback to set the video configuration.
13
+ * @returns
14
+ */
15
+ async function getFileInfo(uri: string) {
16
+ return new Promise<{
17
+ file: any;
18
+ edits: Edit[];
19
+ config: VideoDecoderConfig;
20
+ }>((res, rej) => {
21
+ const file = createFile();
22
+ let found = false;
23
+ const controller = new AbortController();
24
+
25
+ file.onReady = (info: any) => {
26
+ found = true;
27
+ controller.abort();
28
+
29
+ const track = info.videoTracks[0];
30
+
31
+ const config = {
32
+ // Browser doesn't support parsing full vp8 codec (eg: `vp08.00.41.08`),
33
+ // they only support `vp8`.
34
+ codec: track.codec.startsWith('vp08') ? 'vp8' : track.codec,
35
+ codedHeight: track.video.height,
36
+ codedWidth: track.video.width,
37
+ description: description(file, track),
38
+ };
39
+
40
+ const defaultEdit: Omit<Edit, 'fps'> = {
41
+ mediaTime: 0,
42
+ segmentDuration: track.duration,
43
+ mediaRateInteger: 1,
44
+ mediaRateFraction: 0,
45
+ };
46
+
47
+ const editsFromVideo: Omit<Edit, 'fps'>[] = track.edits?.map(
48
+ (edit: any) => mp4BoxEditToEdit(edit),
49
+ );
50
+
51
+ // If there are no entries, use the default edit
52
+ const editsWithoutFps = editsFromVideo?.length
53
+ ? editsFromVideo
54
+ : [defaultEdit];
55
+
56
+ // Calculate FPS for each segment
57
+ const edits = editsWithoutFps.map(edit => {
58
+ const trackDurationInSec = track.duration / track.timescale;
59
+ const segmentDurationInSec =
60
+ edit.segmentDuration / track.movie_timescale;
61
+ const segmentFrames =
62
+ track.nb_samples * (segmentDurationInSec / trackDurationInSec);
63
+ const mediaRate =
64
+ edit.mediaRateInteger + edit.mediaRateFraction / 0xffff;
65
+ const fps = (segmentFrames / segmentDurationInSec) * mediaRate;
66
+ return {...edit, fps};
67
+ });
68
+
69
+ file.setExtractionOptions(track.id);
70
+ file.start();
71
+
72
+ res({file, edits, config});
73
+ };
74
+
75
+ return fetch(uri, {signal: controller.signal}).then(async response => {
76
+ if (!response.body) {
77
+ throw new Error('Response body is null');
78
+ }
79
+
80
+ const reader = response.body.getReader();
81
+ const sink = new MP4FileSink(file, () => {});
82
+
83
+ while (!found) {
84
+ await reader.read().then(({done, value}) => {
85
+ if (done) {
86
+ file.flush();
87
+ controller.abort();
88
+ rej('Could not find moov');
89
+ return;
90
+ }
91
+
92
+ sink.write(value);
93
+ });
94
+ }
95
+ });
96
+ });
97
+ }
98
+
99
+ export class Mp4Parser {
100
+ private uri: string;
101
+ private file: any;
102
+ private edits: Edit[] = [];
103
+ private decoderConfig?: VideoDecoderConfig;
104
+
105
+ private startTime: number;
106
+ private targetFps: number;
107
+
108
+ private nextSegment = 0;
109
+ private sampler?: FrameSampler;
110
+
111
+ public constructor(uri: string, targetFps: number, startTime: number) {
112
+ this.uri = uri;
113
+ this.targetFps = targetFps;
114
+ this.startTime = startTime;
115
+ }
116
+
117
+ public async start() {
118
+ const {file, edits, config} = await getFileInfo(this.uri);
119
+ this.file = file;
120
+ this.edits = edits;
121
+ this.decoderConfig = config;
122
+ }
123
+
124
+ public async getNextFrame() {
125
+ // Start the first segment
126
+ if (!this.sampler) {
127
+ // Skip segments until the start time
128
+ let startTimeWithinSegment = this.startTime;
129
+ while (this.nextSegment < this.edits.length) {
130
+ const segmentDurationInSeconds = this.getSecondDurationOfSegment(
131
+ this.edits[this.nextSegment],
132
+ );
133
+ if (startTimeWithinSegment < segmentDurationInSeconds) {
134
+ break;
135
+ }
136
+
137
+ startTimeWithinSegment -= segmentDurationInSeconds;
138
+ this.nextSegment++;
139
+ }
140
+
141
+ // The timestamp is outside of the video
142
+ if (this.nextSegment >= this.edits.length) {
143
+ throw new Error(
144
+ `Timestamp ${this.startTime} is outside of the video, max timestamp is ${this.getDuration()}`,
145
+ );
146
+ }
147
+
148
+ const segment = new Segment(
149
+ this.uri,
150
+ this.file,
151
+ this.edits,
152
+ this.nextSegment,
153
+ startTimeWithinSegment,
154
+ );
155
+ await segment.start(this.decoderConfig!);
156
+
157
+ this.sampler = new FrameSampler(
158
+ segment,
159
+ this.edits[this.nextSegment].fps,
160
+ this.targetFps,
161
+ 0,
162
+ );
163
+ this.nextSegment++;
164
+ }
165
+
166
+ // Try to get the next frame
167
+ let frame = await this.sampler.getNextFrame();
168
+
169
+ // If there are no more frames in the current segment, start the next segment
170
+ while (!frame && this.nextSegment < this.edits.length) {
171
+ this.sampler.getSegment().close();
172
+ this.sampler.getLastFrame()?.close();
173
+ const segment = new Segment(
174
+ this.uri,
175
+ this.file,
176
+ this.edits,
177
+ this.nextSegment,
178
+ 0,
179
+ );
180
+ await segment.start(this.decoderConfig!);
181
+
182
+ this.sampler = new FrameSampler(
183
+ segment,
184
+ this.edits[this.nextSegment].fps,
185
+ this.targetFps,
186
+ this.sampler.getSum(), // Carry over the sum from the previous segment
187
+ );
188
+ this.nextSegment++;
189
+
190
+ frame = await this.sampler.getNextFrame();
191
+ }
192
+
193
+ // If there are no more frames, return the last frame
194
+ if (!frame) {
195
+ frame = this.sampler.getLastFrame();
196
+
197
+ // We can close the segment
198
+ await this.sampler.getSegment().close();
199
+
200
+ // If we still don't have a frame, throw an error
201
+ if (!frame) {
202
+ throw new Error('There are no frames in the video.');
203
+ }
204
+ }
205
+
206
+ return frame;
207
+ }
208
+
209
+ private getSecondDurationOfSegment(edit: Edit) {
210
+ const mediaRate = edit.mediaRateInteger + edit.mediaRateFraction / 0xffff;
211
+ const duration =
212
+ edit.segmentDuration / this.file.getInfo().videoTracks[0].movie_timescale;
213
+ return duration / mediaRate;
214
+ }
215
+
216
+ public getDuration() {
217
+ return this.edits.reduce(
218
+ (acc, edit) => acc + this.getSecondDurationOfSegment(edit),
219
+ 0,
220
+ );
221
+ }
222
+
223
+ private getTimeSubtractingFrames(frames: number) {
224
+ const durationOfPastSegmentsInSeconds = this.edits
225
+ .slice(0, this.nextSegment - 1)
226
+ .reduce((acc, edit) => acc + this.getSecondDurationOfSegment(edit), 0);
227
+
228
+ if (!this.sampler) {
229
+ throw new Error('No current segment');
230
+ }
231
+
232
+ const currentSegmentStartTime = this.sampler.getSegment().getStartTime();
233
+ const samplerTime = this.sampler.getTime(frames);
234
+
235
+ return (
236
+ durationOfPastSegmentsInSeconds + currentSegmentStartTime + samplerTime
237
+ );
238
+ }
239
+
240
+ public getTime() {
241
+ return this.getTimeSubtractingFrames(0);
242
+ }
243
+
244
+ public getLastTime() {
245
+ return this.getTimeSubtractingFrames(1);
246
+ }
247
+
248
+ public getLastFrame() {
249
+ return this.sampler?.getLastFrame();
250
+ }
251
+
252
+ public close() {
253
+ this.sampler?.getSegment().close();
254
+ this.sampler?.getLastFrame()?.close();
255
+ this.file.flush();
256
+ }
257
+ }
@@ -1,72 +1,72 @@
1
- import type {Segment} from './segment';
2
-
3
- export class FrameSampler {
4
- private framesRequested = 0;
5
- private lastFrame?: VideoFrame;
6
-
7
- public constructor(
8
- private segment: Segment,
9
- private sourceFps: number,
10
- private targetFps: number,
11
- private sum: number = 0,
12
- ) {}
13
-
14
- public async getNextFrame() {
15
- const samplingRate = this.sourceFps / this.targetFps;
16
- this.sum += samplingRate;
17
- this.framesRequested += 1;
18
-
19
- // Return last frame if we haven't progressed enough to get a new frame.
20
- if (this.sum < 1 && this.lastFrame) {
21
- return this.lastFrame;
22
- }
23
-
24
- // Burn frames if we have too many.
25
- while (this.sum >= 2) {
26
- // Burn frame
27
- const frame = await this.segment.getNextFrame();
28
-
29
- // If there are no more frames, return
30
- if (!frame) {
31
- return;
32
- }
33
-
34
- frame.close();
35
- this.sum -= 1;
36
- }
37
-
38
- // Get a new frame.
39
- if (this.sum >= 1 || !this.lastFrame) {
40
- this.sum -= 1;
41
- const frame = await this.segment.getNextFrame();
42
-
43
- // If there are no more frames, return
44
- if (!frame) {
45
- return;
46
- }
47
-
48
- this.lastFrame?.close();
49
- this.lastFrame = frame;
50
- return frame;
51
- }
52
-
53
- // One of the three if statements above is always true.
54
- throw new Error('Unreachable code');
55
- }
56
-
57
- public getLastFrame() {
58
- return this.lastFrame;
59
- }
60
-
61
- public getSum() {
62
- return this.sum;
63
- }
64
-
65
- public getSegment() {
66
- return this.segment;
67
- }
68
-
69
- public getTime(framesToSubtract: number) {
70
- return (this.framesRequested - framesToSubtract) / this.targetFps;
71
- }
72
- }
1
+ import type {Segment} from './segment';
2
+
3
+ export class FrameSampler {
4
+ private framesRequested = 0;
5
+ private lastFrame?: VideoFrame;
6
+
7
+ public constructor(
8
+ private segment: Segment,
9
+ private sourceFps: number,
10
+ private targetFps: number,
11
+ private sum: number = 0,
12
+ ) {}
13
+
14
+ public async getNextFrame() {
15
+ const samplingRate = this.sourceFps / this.targetFps;
16
+ this.sum += samplingRate;
17
+ this.framesRequested += 1;
18
+
19
+ // Return last frame if we haven't progressed enough to get a new frame.
20
+ if (this.sum < 1 && this.lastFrame) {
21
+ return this.lastFrame;
22
+ }
23
+
24
+ // Burn frames if we have too many.
25
+ while (this.sum >= 2) {
26
+ // Burn frame
27
+ const frame = await this.segment.getNextFrame();
28
+
29
+ // If there are no more frames, return
30
+ if (!frame) {
31
+ return;
32
+ }
33
+
34
+ frame.close();
35
+ this.sum -= 1;
36
+ }
37
+
38
+ // Get a new frame.
39
+ if (this.sum >= 1 || !this.lastFrame) {
40
+ this.sum -= 1;
41
+ const frame = await this.segment.getNextFrame();
42
+
43
+ // If there are no more frames, return
44
+ if (!frame) {
45
+ return;
46
+ }
47
+
48
+ this.lastFrame?.close();
49
+ this.lastFrame = frame;
50
+ return frame;
51
+ }
52
+
53
+ // One of the three if statements above is always true.
54
+ throw new Error('Unreachable code');
55
+ }
56
+
57
+ public getLastFrame() {
58
+ return this.lastFrame;
59
+ }
60
+
61
+ public getSum() {
62
+ return this.sum;
63
+ }
64
+
65
+ public getSegment() {
66
+ return this.segment;
67
+ }
68
+
69
+ public getTime(framesToSubtract: number) {
70
+ return (this.framesRequested - framesToSubtract) / this.targetFps;
71
+ }
72
+ }