ffmpeg-progress 0.0.0 → 1.0.1
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/LICENSE +25 -0
- package/README.md +95 -0
- package/core.d.ts +28 -0
- package/core.js +85 -0
- package/package.json +45 -6
package/LICENSE
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
BSD 2-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) [2023], [Beeno Tung (Tung Cheung Leong)]
|
|
4
|
+
All rights reserved.
|
|
5
|
+
|
|
6
|
+
Redistribution and use in source and binary forms, with or without
|
|
7
|
+
modification, are permitted provided that the following conditions are met:
|
|
8
|
+
|
|
9
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
10
|
+
list of conditions and the following disclaimer.
|
|
11
|
+
|
|
12
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
13
|
+
this list of conditions and the following disclaimer in the documentation
|
|
14
|
+
and/or other materials provided with the distribution.
|
|
15
|
+
|
|
16
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
17
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
18
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
19
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
20
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
21
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
22
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
23
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
24
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
25
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# ffmpeg-progress
|
|
2
|
+
|
|
3
|
+
Extract progress from ffmpeg child_process output stream
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/ffmpeg-progress)
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install ffmpeg-progress
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage Example
|
|
14
|
+
|
|
15
|
+
**Get video duration**:
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { scanVideo } from 'ffmpeg-progress'
|
|
19
|
+
|
|
20
|
+
console.log(await scanVideo('test/in.mp4'))
|
|
21
|
+
// { duration: '00:52:04.78', seconds: 3124.78 }
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
**Convert video and monitor progress**:
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { startTimer } from '@beenotung/tslib/timer'
|
|
28
|
+
import { convertFile, parseToSeconds } from 'ffmpeg-progress'
|
|
29
|
+
|
|
30
|
+
let timer = startTimer('estimate duration')
|
|
31
|
+
await convertFile({
|
|
32
|
+
inFile: 'test/in.mp4',
|
|
33
|
+
outFile: 'test/out.mp4',
|
|
34
|
+
onDuration(duration) {
|
|
35
|
+
timer.next('convert video')
|
|
36
|
+
timer.setEstimateProgress(parseToSeconds(duration))
|
|
37
|
+
},
|
|
38
|
+
onProgress(args) {
|
|
39
|
+
timer.tick(args.deltaSeconds)
|
|
40
|
+
},
|
|
41
|
+
})
|
|
42
|
+
timer.end()
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Typescript Types
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
import { ChildProcessWithoutNullStreams } from 'child_process'
|
|
49
|
+
|
|
50
|
+
export function parseToSeconds(str: string): number
|
|
51
|
+
|
|
52
|
+
export function scanVideo(file: string): Promise<ScanVideoResult>
|
|
53
|
+
|
|
54
|
+
export type ScanVideoResult = {
|
|
55
|
+
duration: string
|
|
56
|
+
seconds: number
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export type ProgressArgs = {
|
|
60
|
+
onData?: (chunk: Buffer) => void
|
|
61
|
+
onDuration?: (duration: string) => void
|
|
62
|
+
onTime?: (time: string) => void
|
|
63
|
+
onProgress?: (args: {
|
|
64
|
+
deltaSeconds: number
|
|
65
|
+
currentSeconds: number
|
|
66
|
+
totalSeconds: number
|
|
67
|
+
time: string
|
|
68
|
+
duration: string
|
|
69
|
+
}) => void
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function convertFile(
|
|
73
|
+
args: {
|
|
74
|
+
inFile: string
|
|
75
|
+
outFile: string
|
|
76
|
+
} & ProgressArgs,
|
|
77
|
+
): Promise<void>
|
|
78
|
+
|
|
79
|
+
export function attachChildProcess(
|
|
80
|
+
args: {
|
|
81
|
+
childProcess: ChildProcessWithoutNullStreams
|
|
82
|
+
} & ProgressArgs,
|
|
83
|
+
): Promise<void>
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## License
|
|
87
|
+
|
|
88
|
+
This project is licensed with [BSD-2-Clause](./LICENSE)
|
|
89
|
+
|
|
90
|
+
This is free, libre, and open-source software. It comes down to four essential freedoms [[ref]](https://seirdy.one/2021/01/27/whatsapp-and-the-domestication-of-users.html#fnref:2):
|
|
91
|
+
|
|
92
|
+
- The freedom to run the program as you wish, for any purpose
|
|
93
|
+
- The freedom to study how the program works, and change it so it does your computing as you wish
|
|
94
|
+
- The freedom to redistribute copies so you can help others
|
|
95
|
+
- The freedom to distribute copies of your modified versions to others
|
package/core.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
import { ChildProcessWithoutNullStreams } from 'child_process';
|
|
4
|
+
export declare function parseToSeconds(str: string): number;
|
|
5
|
+
export type ScanVideoResult = {
|
|
6
|
+
duration: string;
|
|
7
|
+
seconds: number;
|
|
8
|
+
};
|
|
9
|
+
export declare function scanVideo(file: string): Promise<ScanVideoResult>;
|
|
10
|
+
export type ProgressArgs = {
|
|
11
|
+
onData?: (chunk: Buffer) => void;
|
|
12
|
+
onDuration?: (duration: string) => void;
|
|
13
|
+
onTime?: (time: string) => void;
|
|
14
|
+
onProgress?: (args: {
|
|
15
|
+
deltaSeconds: number;
|
|
16
|
+
currentSeconds: number;
|
|
17
|
+
totalSeconds: number;
|
|
18
|
+
time: string;
|
|
19
|
+
duration: string;
|
|
20
|
+
}) => void;
|
|
21
|
+
};
|
|
22
|
+
export declare function convertFile(args: {
|
|
23
|
+
inFile: string;
|
|
24
|
+
outFile: string;
|
|
25
|
+
} & ProgressArgs): Promise<void>;
|
|
26
|
+
export declare function attachChildProcess(args: {
|
|
27
|
+
childProcess: ChildProcessWithoutNullStreams;
|
|
28
|
+
} & ProgressArgs): Promise<void>;
|
package/core.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.attachChildProcess = exports.convertFile = exports.scanVideo = exports.parseToSeconds = void 0;
|
|
4
|
+
const child_process_1 = require("child_process");
|
|
5
|
+
function parseToSeconds(str) {
|
|
6
|
+
let parts = str.split(':');
|
|
7
|
+
let h = +parts[0];
|
|
8
|
+
let m = +parts[1];
|
|
9
|
+
let s = +parts[2];
|
|
10
|
+
return (h * 60 + m) * 60 + s;
|
|
11
|
+
}
|
|
12
|
+
exports.parseToSeconds = parseToSeconds;
|
|
13
|
+
function scanVideo(file) {
|
|
14
|
+
return new Promise((resolve, reject) => {
|
|
15
|
+
(0, child_process_1.exec)(`ffmpeg -i ${JSON.stringify(file)} 2>&1`, (err, stdout, stderr) => {
|
|
16
|
+
let match = stdout.match(/Duration: ([0-9:.]+),/);
|
|
17
|
+
if (match) {
|
|
18
|
+
let duration = match[1];
|
|
19
|
+
let seconds = parseToSeconds(duration);
|
|
20
|
+
resolve({ duration, seconds });
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
let error = new Error('failed to find video duration');
|
|
24
|
+
error.cause = err;
|
|
25
|
+
Object.assign(error, { stdout });
|
|
26
|
+
reject(error);
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
exports.scanVideo = scanVideo;
|
|
31
|
+
async function convertFile(args) {
|
|
32
|
+
let childProcess = (0, child_process_1.spawn)('ffmpeg', ['-y', '-i', args.inFile, args.outFile]);
|
|
33
|
+
return attachChildProcess({ childProcess, ...args });
|
|
34
|
+
}
|
|
35
|
+
exports.convertFile = convertFile;
|
|
36
|
+
async function attachChildProcess(args) {
|
|
37
|
+
let { code, signal } = await new Promise((resolve, reject) => {
|
|
38
|
+
let { childProcess } = args;
|
|
39
|
+
let duration = '';
|
|
40
|
+
let time = '';
|
|
41
|
+
let totalSeconds = 0;
|
|
42
|
+
let lastSeconds = 0;
|
|
43
|
+
if (args.onData) {
|
|
44
|
+
childProcess.stdout.on('data', args.onData);
|
|
45
|
+
}
|
|
46
|
+
childProcess.stderr.on('data', (data) => {
|
|
47
|
+
let str = data.toString();
|
|
48
|
+
let match = str.match(/Duration: ([0-9:.]+),/);
|
|
49
|
+
if (match) {
|
|
50
|
+
duration = match[1];
|
|
51
|
+
totalSeconds = parseToSeconds(duration);
|
|
52
|
+
if (args.onDuration) {
|
|
53
|
+
args.onDuration(duration);
|
|
54
|
+
}
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
match = str.match(/frame=\s*\d+\s+fps=.*time=([0-9:.]+)\s/);
|
|
58
|
+
if (match) {
|
|
59
|
+
time = match[1];
|
|
60
|
+
if (args.onTime) {
|
|
61
|
+
args.onTime(time);
|
|
62
|
+
}
|
|
63
|
+
if (args.onProgress) {
|
|
64
|
+
let currentSeconds = parseToSeconds(time);
|
|
65
|
+
let deltaSeconds = currentSeconds - lastSeconds;
|
|
66
|
+
lastSeconds = currentSeconds;
|
|
67
|
+
args.onProgress({
|
|
68
|
+
deltaSeconds,
|
|
69
|
+
currentSeconds,
|
|
70
|
+
totalSeconds,
|
|
71
|
+
time,
|
|
72
|
+
duration,
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
childProcess.on('exit', (code, signal) => {
|
|
78
|
+
resolve({ code, signal });
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
if (code == 0)
|
|
82
|
+
return;
|
|
83
|
+
throw new Error(`ffmpeg exit abnormally, exit code: ${code}, signal: ${signal}`);
|
|
84
|
+
}
|
|
85
|
+
exports.attachChildProcess = attachChildProcess;
|
package/package.json
CHANGED
|
@@ -1,11 +1,50 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ffmpeg-progress",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "",
|
|
5
|
-
"
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "Extract progress from ffmpeg child_process",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"ffmpeg",
|
|
7
|
+
"progress",
|
|
8
|
+
"child_process",
|
|
9
|
+
"stream",
|
|
10
|
+
"typescript",
|
|
11
|
+
"video",
|
|
12
|
+
"convert",
|
|
13
|
+
"scan",
|
|
14
|
+
"duration"
|
|
15
|
+
],
|
|
16
|
+
"author": {
|
|
17
|
+
"name": "Beeno Tung",
|
|
18
|
+
"email": "aabbcc1241@yahoo.com.hk",
|
|
19
|
+
"url": "https://beeno-tung.surge.sh"
|
|
20
|
+
},
|
|
21
|
+
"license": "BSD-2-Clause",
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "git+https://github.com/beenotung/ffmpeg-progress.git"
|
|
25
|
+
},
|
|
26
|
+
"homepage": "https://github.com/beenotung/ffmpeg-progress#readme",
|
|
27
|
+
"bugs": {
|
|
28
|
+
"url": "https://github.com/beenotung/ffmpeg-progress/issues"
|
|
29
|
+
},
|
|
30
|
+
"main": "core.js",
|
|
31
|
+
"types": "./core.d.ts",
|
|
32
|
+
"directories": {
|
|
33
|
+
"test": "test"
|
|
34
|
+
},
|
|
35
|
+
"files": [
|
|
36
|
+
"core.d.ts",
|
|
37
|
+
"core.js"
|
|
38
|
+
],
|
|
6
39
|
"scripts": {
|
|
40
|
+
"test": "tsc --noEmit",
|
|
41
|
+
"build": "tsc -p ."
|
|
7
42
|
},
|
|
8
|
-
"
|
|
9
|
-
|
|
10
|
-
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@beenotung/tslib": "^22.1.1",
|
|
45
|
+
"@types/node": "^18.18.5",
|
|
46
|
+
"ts-node": "^10.9.1",
|
|
47
|
+
"ts-node-dev": "^2.0.0",
|
|
48
|
+
"typescript": "^5.2.2"
|
|
49
|
+
}
|
|
11
50
|
}
|