-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement: decode multiple videos at a time in parallel
- Loading branch information
Showing
5 changed files
with
167 additions
and
162 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import {VideoEffect} from "../timeline/types.js" | ||
import {MP4Demuxer} from "../../../tools/mp4boxjs/demuxer.js" | ||
|
||
let timestamp = 0 | ||
let end_timestamp = 0 | ||
let wait_time = 0 | ||
let interval_number = 0 | ||
let decoded_effect: VideoEffect | ||
let frames = 0 | ||
|
||
const decoder = new VideoDecoder({ | ||
output(frame) { | ||
self.postMessage({action: "new-frame", frame: {timestamp, frame, effect_id: decoded_effect.id, frames_count: frames}}) | ||
frame.close() | ||
timestamp += decoded_effect.duration / frames | ||
wait_time = 0 | ||
}, | ||
error: (e) => console.log(e) | ||
}) | ||
|
||
const interval = () => setInterval(() => { | ||
wait_time += 100 | ||
if(wait_time === 200) { | ||
decoder.flush() | ||
} | ||
if(timestamp === end_timestamp) { | ||
clearInterval(interval_number) | ||
} | ||
}, 100) | ||
|
||
const demux = (file: File) => new MP4Demuxer(file, { | ||
async onConfig(config: VideoDecoderConfig) { | ||
decoder.configure({...config}) | ||
await decoder.flush() | ||
}, | ||
async onChunk(chunk: EncodedVideoChunk) { | ||
decoder.decode(chunk) | ||
end_timestamp += decoded_effect.duration / frames | ||
}, | ||
framesCount(frames_count) { | ||
frames = frames_count | ||
}, | ||
setStatus() {} | ||
}) | ||
|
||
self.addEventListener("message", async message => { | ||
if(message.data.action === "demux") { | ||
timestamp = message.data.starting_timestamp | ||
end_timestamp = message.data.starting_timestamp | ||
decoded_effect = message.data.effect | ||
interval_number = interval() | ||
demux(message.data.effect.file) | ||
} | ||
}) |
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,40 @@ | ||
import {BinaryAccumulator} from "./tools/BinaryAccumulator/tool.js" | ||
|
||
const binary_accumulator = new BinaryAccumulator() | ||
|
||
async function handleChunk(chunk: EncodedVideoChunk) { | ||
const chunkData = new Uint8Array(chunk.byteLength); | ||
chunk.copyTo(chunkData) | ||
binary_accumulator.addChunk(chunkData) | ||
} | ||
|
||
// for later: https://github.com/gpac/mp4box.js/issues/243 | ||
const config: VideoEncoderConfig = { | ||
codec: "avc1.4d002a", // avc1.42001E / avc1.4d002a / avc1.640034 | ||
avc: {format: "annexb"}, | ||
width: 1280, | ||
height: 720, | ||
bitrate: 4_000_000, // 2 Mbps | ||
framerate: 30, | ||
bitrateMode: "constant" | ||
} | ||
|
||
const encoder = new VideoEncoder({ | ||
output: handleChunk, | ||
error: (e: any) => { | ||
console.log(e.message) | ||
}, | ||
}) | ||
|
||
encoder.configure(config) | ||
|
||
self.addEventListener("message", async message => { | ||
if(message.data.action === "encode") { | ||
const frame = message.data.frame as VideoFrame | ||
encoder.encode(frame) | ||
frame.close() | ||
} | ||
if(message.data.action === "get-binary") { | ||
self.postMessage({action: "binary", binary: binary_accumulator.binary}) | ||
} | ||
}) |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.