mirror of
https://github.com/wukko/cobalt.git
synced 2024-11-15 12:50:01 +00:00
web/ffmpeg: universal render function for all needs
This commit is contained in:
parent
7044100aed
commit
1113ddd9c5
1 changed files with 21 additions and 8 deletions
|
@ -4,6 +4,19 @@ import ffmpegCoreWASM from "@imput/ffmpeg-core/wasm?url";
|
||||||
import { FFmpeg } from "@imput/ffmpeg.wasm";
|
import { FFmpeg } from "@imput/ffmpeg.wasm";
|
||||||
import { fetchFile } from "@imput/ffmpeg-util";
|
import { fetchFile } from "@imput/ffmpeg-util";
|
||||||
|
|
||||||
|
type renderParams = {
|
||||||
|
url: string,
|
||||||
|
input: {
|
||||||
|
type: string,
|
||||||
|
format: string,
|
||||||
|
},
|
||||||
|
output: {
|
||||||
|
type: string,
|
||||||
|
format: string,
|
||||||
|
},
|
||||||
|
args: string[],
|
||||||
|
}
|
||||||
|
|
||||||
export default class FFmpegWrapper {
|
export default class FFmpegWrapper {
|
||||||
initialized: boolean;
|
initialized: boolean;
|
||||||
ffmpeg: FFmpeg;
|
ffmpeg: FFmpeg;
|
||||||
|
@ -38,24 +51,24 @@ export default class FFmpegWrapper {
|
||||||
return this.ffmpeg.terminate();
|
return this.ffmpeg.terminate();
|
||||||
}
|
}
|
||||||
|
|
||||||
async renderFile(url: string, type: string, format: string) {
|
async renderFile({ url, input, output, args }: renderParams) {
|
||||||
const input = `input.${format}`;
|
const inputFile = `input.${input.format}`;
|
||||||
|
|
||||||
await this.ffmpeg.writeFile(
|
await this.ffmpeg.writeFile(
|
||||||
input,
|
inputFile,
|
||||||
await fetchFile(url)
|
await fetchFile(url)
|
||||||
)
|
)
|
||||||
|
|
||||||
await this.ffmpeg.exec([
|
await this.ffmpeg.exec([
|
||||||
'-threads', this.concurrency.toString(),
|
'-threads', this.concurrency.toString(),
|
||||||
'-i', input,
|
'-i', inputFile,
|
||||||
'-c', 'copy',
|
...args,
|
||||||
`output.${format}`
|
`output.${output.format}`
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const data = await this.ffmpeg.readFile(`output.${format}`);
|
const data = await this.ffmpeg.readFile(`output.${output.format}`);
|
||||||
const finalBlob = URL.createObjectURL(
|
const finalBlob = URL.createObjectURL(
|
||||||
new Blob([data], { type: `${type}/${format}` })
|
new Blob([data], { type: `${output.type}/${output.format}` })
|
||||||
);
|
);
|
||||||
|
|
||||||
return finalBlob
|
return finalBlob
|
||||||
|
|
Loading…
Reference in a new issue