mirror of
https://github.com/wukko/cobalt.git
synced 2024-11-15 12:50:01 +00:00
web/remux: add basic progress example
This commit is contained in:
parent
f93d84c457
commit
4c2acc595e
3 changed files with 28 additions and 4 deletions
|
@ -162,11 +162,14 @@ export default class LibAVWrapper {
|
||||||
return "unknown";
|
return "unknown";
|
||||||
})();
|
})();
|
||||||
|
|
||||||
const tryNumber = (str: string) => {
|
const tryNumber = (str: string, transform?: (n: number) => number) => {
|
||||||
if (str) {
|
if (str) {
|
||||||
const num = Number(str);
|
const num = Number(str);
|
||||||
if (!isNaN(num)) {
|
if (!isNaN(num)) {
|
||||||
return num;
|
if (transform)
|
||||||
|
return transform(num);
|
||||||
|
else
|
||||||
|
return num;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -179,6 +182,7 @@ export default class LibAVWrapper {
|
||||||
dup_frames: tryNumber(entries.dup_frames),
|
dup_frames: tryNumber(entries.dup_frames),
|
||||||
drop_frames: tryNumber(entries.drop_frames),
|
drop_frames: tryNumber(entries.drop_frames),
|
||||||
speed: tryNumber(entries.speed?.trim()?.replace('x', '')),
|
speed: tryNumber(entries.speed?.trim()?.replace('x', '')),
|
||||||
|
out_time_sec: tryNumber(entries.out_time_us, n => Math.floor(n / 1e6))
|
||||||
};
|
};
|
||||||
|
|
||||||
this.onProgress(progress);
|
this.onProgress(progress);
|
||||||
|
|
|
@ -22,6 +22,7 @@ export type FFmpegProgressEvent = {
|
||||||
dup_frames?: number,
|
dup_frames?: number,
|
||||||
drop_frames?: number,
|
drop_frames?: number,
|
||||||
speed?: number,
|
speed?: number,
|
||||||
|
out_time_sec?: number,
|
||||||
}
|
}
|
||||||
|
|
||||||
export type FFmpegProgressCallback = (info: FFmpegProgressEvent) => void;
|
export type FFmpegProgressCallback = (info: FFmpegProgressEvent) => void;
|
||||||
|
|
|
@ -9,15 +9,34 @@
|
||||||
let draggedOver = false;
|
let draggedOver = false;
|
||||||
let file: File | undefined;
|
let file: File | undefined;
|
||||||
|
|
||||||
|
let totalDuration: number | undefined;
|
||||||
|
let processedDuration: number | undefined;
|
||||||
|
let progress = '';
|
||||||
|
|
||||||
|
$: {
|
||||||
|
if (totalDuration && processedDuration && processing) {
|
||||||
|
const percentage = ((processedDuration / totalDuration) * 100).toFixed(2);
|
||||||
|
progress = `(${percentage}%)`;
|
||||||
|
} else progress = '';
|
||||||
|
}
|
||||||
|
|
||||||
let processing = false;
|
let processing = false;
|
||||||
|
|
||||||
const ff = new LibAVWrapper(console.log);
|
const ff = new LibAVWrapper(progress => {
|
||||||
|
if (progress.out_time_sec) {
|
||||||
|
processedDuration = progress.out_time_sec;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
ff.init();
|
ff.init();
|
||||||
|
|
||||||
const render = async () => {
|
const render = async () => {
|
||||||
if (!file || processing) return;
|
if (!file || processing) return;
|
||||||
await ff.init();
|
await ff.init();
|
||||||
|
|
||||||
|
const file_info = await ff.probe(file);
|
||||||
|
totalDuration = Number(file_info.format.duration);
|
||||||
|
|
||||||
processing = true;
|
processing = true;
|
||||||
|
|
||||||
const render = await ff.render({
|
const render = await ff.render({
|
||||||
|
@ -64,7 +83,7 @@
|
||||||
|
|
||||||
<div id="remux-processing" class:processing>
|
<div id="remux-processing" class:processing>
|
||||||
{#if processing}
|
{#if processing}
|
||||||
processing...
|
processing... {progress}
|
||||||
{:else}
|
{:else}
|
||||||
done!
|
done!
|
||||||
{/if}
|
{/if}
|
||||||
|
|
Loading…
Reference in a new issue