ffmpeg-progress 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -8
- package/core.d.ts +16 -10
- package/core.js +19 -9
- package/package.json +6 -5
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# ffmpeg-progress
|
|
2
2
|
|
|
3
|
-
Extract progress from ffmpeg child_process
|
|
3
|
+
Extract progress from ffmpeg child_process output stream
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/ffmpeg-progress)
|
|
6
6
|
|
|
@@ -60,13 +60,16 @@ export type ProgressArgs = {
|
|
|
60
60
|
onData?: (chunk: Buffer) => void
|
|
61
61
|
onDuration?: (duration: string) => void
|
|
62
62
|
onTime?: (time: string) => void
|
|
63
|
-
onProgress?: (args:
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
63
|
+
onProgress?: (args: OnProgressArgs) => void
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export type OnProgressArgs = {
|
|
67
|
+
deltaSeconds: number
|
|
68
|
+
currentSeconds: number
|
|
69
|
+
totalSeconds: number
|
|
70
|
+
time: string
|
|
71
|
+
duration: string
|
|
72
|
+
abort: () => void
|
|
70
73
|
}
|
|
71
74
|
|
|
72
75
|
export function convertFile(
|
package/core.d.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
/// <reference types="node" />
|
|
3
1
|
import { ChildProcessWithoutNullStreams } from 'child_process';
|
|
4
|
-
export declare function
|
|
2
|
+
export declare function parseToSeconds(str: string): number;
|
|
5
3
|
export type ScanVideoResult = {
|
|
6
4
|
duration: string;
|
|
7
5
|
seconds: number;
|
|
@@ -11,13 +9,15 @@ export type ProgressArgs = {
|
|
|
11
9
|
onData?: (chunk: Buffer) => void;
|
|
12
10
|
onDuration?: (duration: string) => void;
|
|
13
11
|
onTime?: (time: string) => void;
|
|
14
|
-
onProgress?: (args:
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
12
|
+
onProgress?: (args: OnProgressArgs) => void;
|
|
13
|
+
};
|
|
14
|
+
export type OnProgressArgs = {
|
|
15
|
+
deltaSeconds: number;
|
|
16
|
+
currentSeconds: number;
|
|
17
|
+
totalSeconds: number;
|
|
18
|
+
time: string;
|
|
19
|
+
duration: string;
|
|
20
|
+
abort: () => void;
|
|
21
21
|
};
|
|
22
22
|
export declare function convertFile(args: {
|
|
23
23
|
inFile: string;
|
|
@@ -26,3 +26,9 @@ export declare function convertFile(args: {
|
|
|
26
26
|
export declare function attachChildProcess(args: {
|
|
27
27
|
childProcess: ChildProcessWithoutNullStreams;
|
|
28
28
|
} & ProgressArgs): Promise<void>;
|
|
29
|
+
export declare function estimateOutSize(args: {
|
|
30
|
+
inSize: number;
|
|
31
|
+
currentOutSize: number;
|
|
32
|
+
currentSeconds: number;
|
|
33
|
+
totalSeconds: number;
|
|
34
|
+
}): number;
|
package/core.js
CHANGED
|
@@ -1,22 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.parseToSeconds = parseToSeconds;
|
|
4
|
+
exports.scanVideo = scanVideo;
|
|
5
|
+
exports.convertFile = convertFile;
|
|
6
|
+
exports.attachChildProcess = attachChildProcess;
|
|
7
|
+
exports.estimateOutSize = estimateOutSize;
|
|
4
8
|
const child_process_1 = require("child_process");
|
|
5
|
-
function
|
|
9
|
+
function parseToSeconds(str) {
|
|
6
10
|
let parts = str.split(':');
|
|
7
11
|
let h = +parts[0];
|
|
8
12
|
let m = +parts[1];
|
|
9
13
|
let s = +parts[2];
|
|
10
14
|
return (h * 60 + m) * 60 + s;
|
|
11
15
|
}
|
|
12
|
-
exports.parseTime = parseTime;
|
|
13
16
|
function scanVideo(file) {
|
|
14
17
|
return new Promise((resolve, reject) => {
|
|
15
18
|
(0, child_process_1.exec)(`ffmpeg -i ${JSON.stringify(file)} 2>&1`, (err, stdout, stderr) => {
|
|
16
19
|
let match = stdout.match(/Duration: ([0-9:.]+),/);
|
|
17
20
|
if (match) {
|
|
18
21
|
let duration = match[1];
|
|
19
|
-
let seconds =
|
|
22
|
+
let seconds = parseToSeconds(duration);
|
|
20
23
|
resolve({ duration, seconds });
|
|
21
24
|
return;
|
|
22
25
|
}
|
|
@@ -27,12 +30,10 @@ function scanVideo(file) {
|
|
|
27
30
|
});
|
|
28
31
|
});
|
|
29
32
|
}
|
|
30
|
-
exports.scanVideo = scanVideo;
|
|
31
33
|
async function convertFile(args) {
|
|
32
34
|
let childProcess = (0, child_process_1.spawn)('ffmpeg', ['-y', '-i', args.inFile, args.outFile]);
|
|
33
35
|
return attachChildProcess({ childProcess, ...args });
|
|
34
36
|
}
|
|
35
|
-
exports.convertFile = convertFile;
|
|
36
37
|
async function attachChildProcess(args) {
|
|
37
38
|
let { code, signal } = await new Promise((resolve, reject) => {
|
|
38
39
|
let { childProcess } = args;
|
|
@@ -40,6 +41,9 @@ async function attachChildProcess(args) {
|
|
|
40
41
|
let time = '';
|
|
41
42
|
let totalSeconds = 0;
|
|
42
43
|
let lastSeconds = 0;
|
|
44
|
+
function abort() {
|
|
45
|
+
childProcess.kill('SIGKILL');
|
|
46
|
+
}
|
|
43
47
|
if (args.onData) {
|
|
44
48
|
childProcess.stdout.on('data', args.onData);
|
|
45
49
|
}
|
|
@@ -48,7 +52,7 @@ async function attachChildProcess(args) {
|
|
|
48
52
|
let match = str.match(/Duration: ([0-9:.]+),/);
|
|
49
53
|
if (match) {
|
|
50
54
|
duration = match[1];
|
|
51
|
-
totalSeconds =
|
|
55
|
+
totalSeconds = parseToSeconds(duration);
|
|
52
56
|
if (args.onDuration) {
|
|
53
57
|
args.onDuration(duration);
|
|
54
58
|
}
|
|
@@ -61,7 +65,7 @@ async function attachChildProcess(args) {
|
|
|
61
65
|
args.onTime(time);
|
|
62
66
|
}
|
|
63
67
|
if (args.onProgress) {
|
|
64
|
-
let currentSeconds =
|
|
68
|
+
let currentSeconds = parseToSeconds(time);
|
|
65
69
|
let deltaSeconds = currentSeconds - lastSeconds;
|
|
66
70
|
lastSeconds = currentSeconds;
|
|
67
71
|
args.onProgress({
|
|
@@ -70,6 +74,7 @@ async function attachChildProcess(args) {
|
|
|
70
74
|
totalSeconds,
|
|
71
75
|
time,
|
|
72
76
|
duration,
|
|
77
|
+
abort,
|
|
73
78
|
});
|
|
74
79
|
}
|
|
75
80
|
}
|
|
@@ -82,4 +87,9 @@ async function attachChildProcess(args) {
|
|
|
82
87
|
return;
|
|
83
88
|
throw new Error(`ffmpeg exit abnormally, exit code: ${code}, signal: ${signal}`);
|
|
84
89
|
}
|
|
85
|
-
|
|
90
|
+
function estimateOutSize(args) {
|
|
91
|
+
let estimatedRate = args.currentOutSize / args.currentSeconds;
|
|
92
|
+
let remindSeconds = args.totalSeconds - args.currentSeconds;
|
|
93
|
+
let estimatedOutSize = args.currentOutSize + estimatedRate * remindSeconds;
|
|
94
|
+
return estimatedOutSize;
|
|
95
|
+
}
|
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ffmpeg-progress",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Extract progress from ffmpeg child_process",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ffmpeg",
|
|
7
7
|
"progress",
|
|
8
8
|
"child_process",
|
|
9
|
+
"stream",
|
|
9
10
|
"typescript",
|
|
10
11
|
"video",
|
|
11
12
|
"convert",
|
|
@@ -40,10 +41,10 @@
|
|
|
40
41
|
"build": "tsc -p ."
|
|
41
42
|
},
|
|
42
43
|
"devDependencies": {
|
|
43
|
-
"@beenotung/tslib": "^
|
|
44
|
-
"@types/node": "^
|
|
45
|
-
"ts-node": "^10.9.
|
|
44
|
+
"@beenotung/tslib": "^24.1.0",
|
|
45
|
+
"@types/node": "^22.9.0",
|
|
46
|
+
"ts-node": "^10.9.2",
|
|
46
47
|
"ts-node-dev": "^2.0.0",
|
|
47
|
-
"typescript": "^5.
|
|
48
|
+
"typescript": "^5.6.3"
|
|
48
49
|
}
|
|
49
50
|
}
|