-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
269 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
dist | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name": "@nahara/motion-video", | ||
"version": "1.0.0", | ||
"description": "Nahara Motion Video", | ||
"type": "module", | ||
"main": "dist/index.js", | ||
"scripts": { | ||
"build": "tsc --project tsconfig.json", | ||
"watch": "tsc --watch --project tsconfig.json", | ||
"test": "echo \"Tests are not available for this package.\" && exit 0" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "MIT", | ||
"dependencies": { | ||
"@nahara/motion": "workspace:*", | ||
"mp4-muxer": "^5.1.3" | ||
}, | ||
"devDependencies": { | ||
"typescript": "^5.6.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { IPipeline } from "./pipeline.js"; | ||
|
||
export class EncoderPipeline implements IPipeline<VideoFrame, [EncodedVideoChunk, EncodedVideoChunkMetadata | undefined], any> { | ||
next?: (output: [EncodedVideoChunk, EncodedVideoChunkMetadata | undefined]) => any; | ||
encoder?: VideoEncoder; | ||
|
||
constructor(public readonly options: VideoEncoderConfig) {} | ||
|
||
async initialize(next: (output: [EncodedVideoChunk, EncodedVideoChunkMetadata | undefined]) => any): Promise<void> { | ||
this.next = next; | ||
this.encoder = new VideoEncoder({ | ||
output: (chunk, meta) => next([chunk, meta]), | ||
error() {} | ||
}); | ||
this.encoder.configure(this.options); | ||
} | ||
|
||
consume(input: VideoFrame): void { | ||
this.encoder!.encode(input); | ||
input.close(); | ||
} | ||
|
||
async finalize(): Promise<any> { | ||
await this.encoder!.flush(); | ||
console.log("EncoderPipeline: Flushed"); | ||
this.encoder!.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export * from "./encoder.js"; | ||
export * from "./muxer.js"; | ||
export * from "./pipeline.js"; | ||
export * from "./render.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { ArrayBufferTarget, Muxer, MuxerOptions } from "mp4-muxer"; | ||
import { IPipeline } from "./pipeline.js"; | ||
|
||
export class MuxerPipeline implements IPipeline<[EncodedVideoChunk, EncodedVideoChunkMetadata | undefined], void, ArrayBuffer> { | ||
muxer?: Muxer<ArrayBufferTarget>; | ||
|
||
constructor(public readonly options: (MuxerOptions<any>["video"] & {})) {} | ||
|
||
async initialize(): Promise<void> { | ||
this.muxer = new Muxer({ | ||
target: new ArrayBufferTarget(), | ||
video: this.options, | ||
fastStart: "fragmented" | ||
}); | ||
} | ||
|
||
consume(input: [EncodedVideoChunk, EncodedVideoChunkMetadata | undefined]): void { | ||
this.muxer!.addVideoChunk(...input); | ||
} | ||
|
||
async finalize(): Promise<ArrayBuffer> { | ||
console.log("MuxerPipeline: Finalizing..."); | ||
this.muxer!.finalize(); | ||
return this.muxer!.target.buffer; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* Represent a pipeline in the system. | ||
* @param I The input chunk type. | ||
* @param O The output chunk type, which will be passed to next part of pipeline or collected by consumer. | ||
* @param R The result of the pipeline, which will be collected by consumer (does not pass to other pipelines!). Mainly | ||
* used for video file. | ||
*/ | ||
export interface IPipeline<I, O, R> { | ||
initialize(next: (output: O) => any): Promise<void>; | ||
consume(input: I): void; | ||
finalize(): Promise<R>; | ||
} | ||
|
||
export type AnyPipeline = IPipeline<any, any, any>; | ||
|
||
export function concat<I, M, O, R1, R2>(a: IPipeline<I, M, R1>, b: IPipeline<M, O, R2>): IPipeline<I, O, [R1, R2]>; | ||
export function concat(...pipelines: AnyPipeline[]): AnyPipeline; | ||
export function concat(...pipelines: AnyPipeline[]): AnyPipeline { | ||
let consumer: (inputs: any) => void; | ||
|
||
return { | ||
async initialize(next) { | ||
for (let i = pipelines.length - 1; i >= 0; i--) { | ||
const pipeline = pipelines[i]; | ||
await pipeline.initialize(next); | ||
next = inputs => pipeline.consume(inputs); | ||
} | ||
|
||
consumer = next; | ||
}, | ||
consume: i => consumer(i), | ||
async finalize() { | ||
const out: any[] = []; | ||
for (const p of pipelines) out.push(await p.finalize()); | ||
return out; | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { BaseCanvasRenderingContext2D, IScene } from "@nahara/motion"; | ||
import { IPipeline } from "./pipeline.js"; | ||
|
||
export class SceneRenderPipeline implements IPipeline<IScene, VideoFrame, any> { | ||
next?: (output: VideoFrame) => any; | ||
canvas?: OffscreenCanvas; | ||
ctx?: BaseCanvasRenderingContext2D; | ||
|
||
constructor( | ||
public readonly width: number, | ||
public readonly height: number, | ||
public readonly frameRate: number, | ||
public readonly frames: number | ||
) {} | ||
|
||
async initialize(next: (output: VideoFrame) => any): Promise<void> { | ||
this.next = next; | ||
this.canvas = new OffscreenCanvas(this.width, this.height); | ||
this.ctx = this.canvas.getContext("2d")!; | ||
} | ||
|
||
consume(input: IScene): void { | ||
for (let f = 0; f < this.frames; f++) { | ||
const timestamp = Math.floor(f * 1000000 / this.frameRate); | ||
const delta = Math.floor((f + 1) * 1000000 / this.frameRate) - timestamp; | ||
|
||
this.ctx!.reset(); | ||
this.ctx!.fillStyle = "#000"; | ||
this.ctx!.fillRect(0, 0, this.width, this.height); | ||
input.renderFrame({ | ||
canvas: this.ctx!, | ||
containerSize: { x: this.width, y: this.height }, | ||
time: timestamp / 1000, | ||
timeDelta: delta / 1000 | ||
}); | ||
const frame = new VideoFrame(this.canvas!, { timestamp, duration: delta }); | ||
this.next!(frame); | ||
} | ||
} | ||
|
||
async finalize(): Promise<any> { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "NodeNext", | ||
"target": "ESNext", | ||
"outDir": "dist", | ||
"incremental": true, | ||
"strict": true, | ||
"declaration": true, | ||
"skipLibCheck": true | ||
}, | ||
"include": [ | ||
"src" | ||
], | ||
"exclude": [ | ||
"node_modules" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
packages: | ||
- 'nahara-motion' | ||
- 'nahara-motion-ui' | ||
- 'nahara-motion-video' |