ffmpeg-progress 1.0.1 → 1.1.0

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.
Files changed (4) hide show
  1. package/README.md +14 -7
  2. package/core.d.ts +19 -9
  3. package/core.js +34 -13
  4. package/package.json +5 -5
package/README.md CHANGED
@@ -52,21 +52,28 @@ export function parseToSeconds(str: string): number
52
52
  export function scanVideo(file: string): Promise<ScanVideoResult>
53
53
 
54
54
  export type ScanVideoResult = {
55
+ /** @description e.g. "00:03:00.03" */
55
56
  duration: string
57
+ /** @description e.g. 180.03 */
56
58
  seconds: number
59
+ /** @description e.g. "4032x3024" */
60
+ resolution: string
57
61
  }
58
62
 
59
63
  export type ProgressArgs = {
60
64
  onData?: (chunk: Buffer) => void
61
65
  onDuration?: (duration: string) => void
62
66
  onTime?: (time: string) => void
63
- onProgress?: (args: {
64
- deltaSeconds: number
65
- currentSeconds: number
66
- totalSeconds: number
67
- time: string
68
- duration: string
69
- }) => void
67
+ onProgress?: (args: OnProgressArgs) => void
68
+ }
69
+
70
+ export type OnProgressArgs = {
71
+ deltaSeconds: number
72
+ currentSeconds: number
73
+ totalSeconds: number
74
+ time: string
75
+ duration: string
76
+ abort: () => void
70
77
  }
71
78
 
72
79
  export function convertFile(
package/core.d.ts CHANGED
@@ -1,23 +1,27 @@
1
- /// <reference types="node" />
2
- /// <reference types="node" />
3
1
  import { ChildProcessWithoutNullStreams } from 'child_process';
4
2
  export declare function parseToSeconds(str: string): number;
5
3
  export type ScanVideoResult = {
4
+ /** @description e.g. "00:03:00.03" */
6
5
  duration: string;
6
+ /** @description e.g. 180.03 */
7
7
  seconds: number;
8
+ /** @description e.g. "4032x3024" */
9
+ resolution: string;
8
10
  };
9
11
  export declare function scanVideo(file: string): Promise<ScanVideoResult>;
10
12
  export type ProgressArgs = {
11
13
  onData?: (chunk: Buffer) => void;
12
14
  onDuration?: (duration: string) => void;
13
15
  onTime?: (time: string) => void;
14
- onProgress?: (args: {
15
- deltaSeconds: number;
16
- currentSeconds: number;
17
- totalSeconds: number;
18
- time: string;
19
- duration: string;
20
- }) => void;
16
+ onProgress?: (args: OnProgressArgs) => void;
17
+ };
18
+ export type OnProgressArgs = {
19
+ deltaSeconds: number;
20
+ currentSeconds: number;
21
+ totalSeconds: number;
22
+ time: string;
23
+ duration: string;
24
+ abort: () => void;
21
25
  };
22
26
  export declare function convertFile(args: {
23
27
  inFile: string;
@@ -26,3 +30,9 @@ export declare function convertFile(args: {
26
30
  export declare function attachChildProcess(args: {
27
31
  childProcess: ChildProcessWithoutNullStreams;
28
32
  } & ProgressArgs): Promise<void>;
33
+ export declare function estimateOutSize(args: {
34
+ inSize: number;
35
+ currentOutSize: number;
36
+ currentSeconds: number;
37
+ totalSeconds: number;
38
+ }): number;
package/core.js CHANGED
@@ -1,6 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.attachChildProcess = exports.convertFile = exports.scanVideo = exports.parseToSeconds = void 0;
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
9
  function parseToSeconds(str) {
6
10
  let parts = str.split(':');
@@ -9,30 +13,38 @@ function parseToSeconds(str) {
9
13
  let s = +parts[2];
10
14
  return (h * 60 + m) * 60 + s;
11
15
  }
12
- exports.parseToSeconds = parseToSeconds;
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
- let match = stdout.match(/Duration: ([0-9:.]+),/);
17
- if (match) {
19
+ try {
20
+ let match = stdout.match(/Duration: ([0-9:.]+),/);
21
+ if (!match) {
22
+ throw new Error('failed to find video duration');
23
+ }
18
24
  let duration = match[1];
19
25
  let seconds = parseToSeconds(duration);
20
- resolve({ duration, seconds });
21
- return;
26
+ match = stdout.match(/Stream #0:\d.+: Video: .+ (\d+x\d+),/);
27
+ if (!match) {
28
+ throw new Error('failed to find video resolution');
29
+ }
30
+ let resolution = match[1];
31
+ resolve({ duration, seconds, resolution });
32
+ }
33
+ catch (e) {
34
+ let error = e instanceof Error ? e : new Error(String(e));
35
+ if (err) {
36
+ error.cause = err;
37
+ }
38
+ Object.assign(error, { stdout });
39
+ reject(error);
22
40
  }
23
- let error = new Error('failed to find video duration');
24
- error.cause = err;
25
- Object.assign(error, { stdout });
26
- reject(error);
27
41
  });
28
42
  });
29
43
  }
30
- exports.scanVideo = scanVideo;
31
44
  async function convertFile(args) {
32
45
  let childProcess = (0, child_process_1.spawn)('ffmpeg', ['-y', '-i', args.inFile, args.outFile]);
33
46
  return attachChildProcess({ childProcess, ...args });
34
47
  }
35
- exports.convertFile = convertFile;
36
48
  async function attachChildProcess(args) {
37
49
  let { code, signal } = await new Promise((resolve, reject) => {
38
50
  let { childProcess } = args;
@@ -40,6 +52,9 @@ async function attachChildProcess(args) {
40
52
  let time = '';
41
53
  let totalSeconds = 0;
42
54
  let lastSeconds = 0;
55
+ function abort() {
56
+ childProcess.kill('SIGKILL');
57
+ }
43
58
  if (args.onData) {
44
59
  childProcess.stdout.on('data', args.onData);
45
60
  }
@@ -70,6 +85,7 @@ async function attachChildProcess(args) {
70
85
  totalSeconds,
71
86
  time,
72
87
  duration,
88
+ abort,
73
89
  });
74
90
  }
75
91
  }
@@ -82,4 +98,9 @@ async function attachChildProcess(args) {
82
98
  return;
83
99
  throw new Error(`ffmpeg exit abnormally, exit code: ${code}, signal: ${signal}`);
84
100
  }
85
- exports.attachChildProcess = attachChildProcess;
101
+ function estimateOutSize(args) {
102
+ let estimatedRate = args.currentOutSize / args.currentSeconds;
103
+ let remindSeconds = args.totalSeconds - args.currentSeconds;
104
+ let estimatedOutSize = args.currentOutSize + estimatedRate * remindSeconds;
105
+ return estimatedOutSize;
106
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ffmpeg-progress",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "description": "Extract progress from ffmpeg child_process",
5
5
  "keywords": [
6
6
  "ffmpeg",
@@ -41,10 +41,10 @@
41
41
  "build": "tsc -p ."
42
42
  },
43
43
  "devDependencies": {
44
- "@beenotung/tslib": "^22.1.1",
45
- "@types/node": "^18.18.5",
46
- "ts-node": "^10.9.1",
44
+ "@beenotung/tslib": "^24.1.0",
45
+ "@types/node": "^22.9.0",
46
+ "ts-node": "^10.9.2",
47
47
  "ts-node-dev": "^2.0.0",
48
- "typescript": "^5.2.2"
48
+ "typescript": "^5.6.3"
49
49
  }
50
50
  }