Skip to content

Commit 0a66de7

Browse files
committed
Add progress
1 parent 55733e3 commit 0a66de7

File tree

6 files changed

+39
-34
lines changed

6 files changed

+39
-34
lines changed

docs/api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ createWorker is a factory function that creates a ffmpeg worker, a worker is bas
2424
- `workerPath` path for downloading worker script
2525
- `workerBlobURL` a boolean to define whether to use Blob URL for worker script, default: true
2626
- `logger` a function to log the progress, a quick example is `m => console.log(m)`
27+
- `progress` a function to trace the progress, a quick example is `p => console.log(p)`
2728

2829

2930
**Examples:**

package-lock.json

Lines changed: 14 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
},
3939
"homepage": "https://github.com/ffmpegjs/ffmpeg.js#readme",
4040
"dependencies": {
41-
"@ffmpeg/core": "^0.3.0",
41+
"@ffmpeg/core": "^0.3.1",
4242
"is-url": "^1.2.4",
4343
"node-fetch": "^2.6.0",
4444
"regenerator-runtime": "^0.13.3",

src/constants/defaultOptions.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
module.exports = {
22
logger: () => {},
3+
progress: () => {},
34
};

src/createWorker.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const createJob = require('./createJob');
22
const { log } = require('./utils/log');
33
const getId = require('./utils/getId');
4+
const extractProgress = require('./utils/extractProgress');
45
const resolvePaths = require('./utils/resolvePaths');
56
const {
67
defaultOptions,
@@ -17,6 +18,7 @@ module.exports = (_options = {}) => {
1718
const id = getId('Worker', workerCounter);
1819
const {
1920
logger,
21+
progress,
2022
...options
2123
} = resolvePaths({
2224
...defaultOptions,
@@ -131,6 +133,7 @@ module.exports = (_options = {}) => {
131133
rejects[action](data);
132134
throw Error(data);
133135
} else if (status === 'progress') {
136+
extractProgress(data, progress);
134137
logger(data);
135138
}
136139
});

src/utils/extractProgress.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
let duration = 0;
2+
3+
const ts2sec = (ts) => {
4+
const [h, m, s] = ts.split(':');
5+
return (parseFloat(h) * 60 * 60) + (parseFloat(m) * 60) + parseFloat(s);
6+
};
7+
8+
module.exports = ({ message }, progress) => {
9+
if (message.startsWith(' Duration')) {
10+
const ts = message.split(', ')[0].split(': ')[1];
11+
duration = ts2sec(ts);
12+
} else if (message.startsWith('frame')) {
13+
const ts = message.split('time=')[1].split(' ')[0];
14+
const t = ts2sec(ts);
15+
progress({ ratio: t / duration });
16+
} else if (message.startsWith('video:')) {
17+
progress({ ratio: 1 });
18+
}
19+
};

0 commit comments

Comments
 (0)