ffmpeg-progress 1.2.0 → 1.2.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/core.d.ts +1 -0
- package/core.js +54 -20
- package/package.json +3 -3
package/core.d.ts
CHANGED
|
@@ -18,6 +18,7 @@ export type ScanVideoResult = {
|
|
|
18
18
|
/** @description e.g. "4032x3024" */
|
|
19
19
|
resolution: string;
|
|
20
20
|
};
|
|
21
|
+
export declare function parseVideoMetadata(stdout: string): ScanVideoResult;
|
|
21
22
|
export declare function scanVideo(file: string): Promise<ScanVideoResult>;
|
|
22
23
|
export type ProgressArgs = {
|
|
23
24
|
onData?: (chunk: Buffer) => void;
|
package/core.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.parseToSeconds = parseToSeconds;
|
|
4
4
|
exports.secondsToString = secondsToString;
|
|
5
|
+
exports.parseVideoMetadata = parseVideoMetadata;
|
|
5
6
|
exports.scanVideo = scanVideo;
|
|
6
7
|
exports.convertFile = convertFile;
|
|
7
8
|
exports.attachChildProcess = attachChildProcess;
|
|
@@ -31,36 +32,69 @@ function secondsToString(seconds) {
|
|
|
31
32
|
let h = Math.floor(seconds / 3600);
|
|
32
33
|
let m = Math.floor((seconds % 3600) / 60);
|
|
33
34
|
let s = Math.floor(seconds % 60);
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
35
|
+
// handle precision edge case, e.g. 60.1234 % 1 = 0.12339999999999662
|
|
36
|
+
let ms;
|
|
37
|
+
let str = seconds.toString();
|
|
38
|
+
if (str.includes('.')) {
|
|
39
|
+
ms = str.split('.')[1];
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
ms = '';
|
|
43
|
+
}
|
|
44
|
+
if (!ms) {
|
|
45
|
+
return `${d2(h)}:${d2(m)}:${d2(s)}`;
|
|
46
|
+
}
|
|
47
|
+
return `${d2(h)}:${d2(m)}:${d2(s)}.${ms}`;
|
|
38
48
|
}
|
|
39
49
|
function d2(x) {
|
|
40
50
|
if (x < 10)
|
|
41
51
|
return '0' + x;
|
|
42
52
|
return x;
|
|
43
53
|
}
|
|
54
|
+
function format_ms(ms) {
|
|
55
|
+
// round 0.123.39999999999418 to 0.123
|
|
56
|
+
let seconds = ms / 1000;
|
|
57
|
+
let str = seconds.toFixed(3);
|
|
58
|
+
seconds = +str;
|
|
59
|
+
ms = seconds * 1000;
|
|
60
|
+
if (ms < 10)
|
|
61
|
+
return '00' + ms;
|
|
62
|
+
if (ms < 100)
|
|
63
|
+
return '0' + ms;
|
|
64
|
+
return ms;
|
|
65
|
+
}
|
|
66
|
+
// e.g. " Duration: 00:03:00.03, start: 0.000000, bitrate: 2234 kb/s"
|
|
67
|
+
// e.g. " Duration: N/A, start: 0.000000, bitrate: N/A"
|
|
68
|
+
let duration_regex = /Duration: ([0-9:.]+|N\/A),/;
|
|
69
|
+
// e.g. " Stream #0:0[0x1](und): Video: h264 (Baseline) (avc1 / 0x31637661), yuvj420p(pc, progressive), 4032x3024, 2045 kb/s, 29.73 fps, 600 tbr, 600 tbn (default)"
|
|
70
|
+
// e.g. " Stream #0:0[0x1](eng): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt470bg/unknown/unknown, progressive), 1920x1080 [SAR 1:1 DAR 16:9], 3958 kb/s, 29.49 fps, 29.83 tbr, 11456 tbn (default)"
|
|
71
|
+
// e.g. " Stream #0:0: Audio: mp3 (mp3float), 44100 Hz, stereo, fltp, 128 kb/s"
|
|
72
|
+
// e.g. " Stream #0:1: Video: flv1 (flv), yuv420p, 1080x1920, 200 kb/s, 60 fps, 60 tbr, 1k tbn"
|
|
73
|
+
let resolution_regex = /Stream #0:\d[\w\[\]\(\)]*: Video: .+ (\d+x\d+)[\s|,]/;
|
|
74
|
+
function parseVideoMetadata(stdout) {
|
|
75
|
+
let lines = stdout
|
|
76
|
+
.split('\n')
|
|
77
|
+
.map(line => line.trim())
|
|
78
|
+
.filter(line => line.length > 0);
|
|
79
|
+
let match = stdout.match(duration_regex);
|
|
80
|
+
if (!match) {
|
|
81
|
+
throw new Error('failed to find video duration');
|
|
82
|
+
}
|
|
83
|
+
let duration = match[1];
|
|
84
|
+
let seconds = parseToSeconds(duration);
|
|
85
|
+
let line = lines.find(line => resolution_regex.test(line));
|
|
86
|
+
match = line?.match(resolution_regex);
|
|
87
|
+
if (!match) {
|
|
88
|
+
throw new Error('failed to find video resolution');
|
|
89
|
+
}
|
|
90
|
+
let resolution = match[1];
|
|
91
|
+
return { duration, seconds, resolution };
|
|
92
|
+
}
|
|
44
93
|
function scanVideo(file) {
|
|
45
94
|
return new Promise((resolve, reject) => {
|
|
46
95
|
(0, child_process_1.exec)(`ffmpeg -i ${JSON.stringify(file)} 2>&1`, (err, stdout, stderr) => {
|
|
47
96
|
try {
|
|
48
|
-
|
|
49
|
-
// e.g. " Duration: N/A, start: 0.000000, bitrate: N/A"
|
|
50
|
-
let match = stdout.match(/Duration: ([0-9:.]+|N\/A),/);
|
|
51
|
-
if (!match) {
|
|
52
|
-
throw new Error('failed to find video duration');
|
|
53
|
-
}
|
|
54
|
-
let duration = match[1];
|
|
55
|
-
let seconds = parseToSeconds(duration);
|
|
56
|
-
// e.g. " Stream #0:0[0x1](und): Video: h264 (Baseline) (avc1 / 0x31637661), yuvj420p(pc, progressive), 4032x3024, 2045 kb/s, 29.73 fps, 600 tbr, 600 tbn (default)"
|
|
57
|
-
// e.g. " Stream #0:0[0x1](eng): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt470bg/unknown/unknown, progressive), 1920x1080 [SAR 1:1 DAR 16:9], 3958 kb/s, 29.49 fps, 29.83 tbr, 11456 tbn (default)"
|
|
58
|
-
match = stdout.match(/Stream #0:\d.+: Video: .+ (\d+x\d+)[\s|,]/);
|
|
59
|
-
if (!match) {
|
|
60
|
-
throw new Error('failed to find video resolution');
|
|
61
|
-
}
|
|
62
|
-
let resolution = match[1];
|
|
63
|
-
resolve({ duration, seconds, resolution });
|
|
97
|
+
resolve(parseVideoMetadata(stdout));
|
|
64
98
|
}
|
|
65
99
|
catch (e) {
|
|
66
100
|
let error = e instanceof Error ? e : new Error(String(e));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ffmpeg-progress",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.2",
|
|
4
4
|
"description": "Extract progress from ffmpeg child_process",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ffmpeg",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"core.js"
|
|
38
38
|
],
|
|
39
39
|
"scripts": {
|
|
40
|
-
"mocha": "ts-mocha *.test.ts",
|
|
40
|
+
"mocha": "rm -f *.js && ts-mocha \"*.{test,spec}.ts\"",
|
|
41
41
|
"test": "npm run mocha && tsc --noEmit",
|
|
42
42
|
"build": "tsc -p ."
|
|
43
43
|
},
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"@types/mocha": "^10.0.10",
|
|
48
48
|
"@types/node": "^22.9.0",
|
|
49
49
|
"chai": "^4.5.0",
|
|
50
|
-
"mocha": "^
|
|
50
|
+
"mocha": "^10.8.2",
|
|
51
51
|
"ts-mocha": "^10.0.0",
|
|
52
52
|
"ts-node": "^10.9.2",
|
|
53
53
|
"ts-node-dev": "^2.0.0",
|