Refactor to worker version
This commit is contained in:
36
examples/browser/transcode.html
Normal file
36
examples/browser/transcode.html
Normal file
@@ -0,0 +1,36 @@
|
||||
<html>
|
||||
<head>
|
||||
<script src="/dist/ffmpeg.dev.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<input type="file" id="uploader">
|
||||
<video controls>
|
||||
<source id="video-src" type="video/mp4">
|
||||
</video>
|
||||
<script>
|
||||
const { createWorker } = FFmpeg;
|
||||
const worker = createWorker({
|
||||
corePath: '../../node_modules/@ffmpeg/core/ffmpeg-core.js',
|
||||
});
|
||||
|
||||
const bufferToBase64 = (buf) => {
|
||||
const binstr = Array.prototype.map.call(buf, function (ch) {
|
||||
return String.fromCharCode(ch);
|
||||
}).join('');
|
||||
return btoa(binstr);
|
||||
}
|
||||
|
||||
const transcode = async ({ target: { files } }) => {
|
||||
await worker.load();
|
||||
console.log('Start transcoding');
|
||||
const { data } = await worker.transcode('/tests/assets/test.avi', 'mp4');
|
||||
console.log(data);
|
||||
console.log('Complete transcoding');
|
||||
const b64 = bufferToBase64(data);
|
||||
document.getElementById('video-src').setAttribute('src', `data:video/mp4;base64,${b64}`);
|
||||
}
|
||||
const elm = document.getElementById('uploader');
|
||||
elm.addEventListener('change', transcode);
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
12
examples/node/transcode.js
Executable file
12
examples/node/transcode.js
Executable file
@@ -0,0 +1,12 @@
|
||||
const { createWorker } = require('../../src');
|
||||
|
||||
const { argv } = process;
|
||||
const [,, inputPath, outputPath] = argv;
|
||||
|
||||
const worker = createWorker();
|
||||
|
||||
(async () => {
|
||||
await worker.load();
|
||||
const { data } = await worker.transcode(inputPath, outputPath.split('.').pop());
|
||||
console.log(data.length);
|
||||
})();
|
||||
@@ -1,10 +0,0 @@
|
||||
const fs = require('fs');
|
||||
const ffmpeg = require('../src');
|
||||
const { argv } = process;
|
||||
const [,, inputPath, outputPath] = argv;
|
||||
|
||||
(async () => {
|
||||
await ffmpeg.load();
|
||||
const data = ffmpeg.transcode(inputPath, outputPath.split('.').pop());
|
||||
fs.writeFileSync(outputPath, data);
|
||||
})();
|
||||
Reference in New Issue
Block a user