ffmpeg-progress 1.2.2 → 1.3.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 (3) hide show
  1. package/core.d.ts +3 -1
  2. package/core.js +27 -3
  3. package/package.json +1 -1
package/core.d.ts CHANGED
@@ -16,7 +16,9 @@ export type ScanVideoResult = {
16
16
  /** @description e.g. 180.03 or 0 */
17
17
  seconds: number;
18
18
  /** @description e.g. "4032x3024" */
19
- resolution: string;
19
+ resolution: string | null;
20
+ /** @description e.g. 44100 for 44.1kHz */
21
+ audioSampleRate: number | null;
20
22
  };
21
23
  export declare function parseVideoMetadata(stdout: string): ScanVideoResult;
22
24
  export declare function scanVideo(file: string): Promise<ScanVideoResult>;
package/core.js CHANGED
@@ -68,9 +68,10 @@ function format_ms(ms) {
68
68
  let duration_regex = /Duration: ([0-9:.]+|N\/A),/;
69
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
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
71
  // e.g. " Stream #0:1: Video: flv1 (flv), yuv420p, 1080x1920, 200 kb/s, 60 fps, 60 tbr, 1k tbn"
73
72
  let resolution_regex = /Stream #0:\d[\w\[\]\(\)]*: Video: .+ (\d+x\d+)[\s|,]/;
73
+ // e.g. " Stream #0:0: Audio: mp3 (mp3float), 44100 Hz, stereo, fltp, 128 kb/s"
74
+ let audio_sample_rate_regex = /Stream #0:\d[\w\[\]\(\)]*: Audio: .+ (\d+) Hz/;
74
75
  function parseVideoMetadata(stdout) {
75
76
  let lines = stdout
76
77
  .split('\n')
@@ -82,13 +83,36 @@ function parseVideoMetadata(stdout) {
82
83
  }
83
84
  let duration = match[1];
84
85
  let seconds = parseToSeconds(duration);
86
+ let resolution = parseResolution(lines);
87
+ let audioSampleRate = parseAudioSampleRate(lines);
88
+ return { duration, seconds, resolution, audioSampleRate };
89
+ }
90
+ function parseResolution(lines) {
91
+ let has_video = lines.find(line => line.trim().startsWith('Stream #0:') && line.includes(' Video: '));
92
+ if (!has_video)
93
+ return null;
85
94
  let line = lines.find(line => resolution_regex.test(line));
86
- match = line?.match(resolution_regex);
95
+ let match = line?.match(resolution_regex);
87
96
  if (!match) {
88
97
  throw new Error('failed to find video resolution');
89
98
  }
90
99
  let resolution = match[1];
91
- return { duration, seconds, resolution };
100
+ return resolution;
101
+ }
102
+ function parseAudioSampleRate(lines) {
103
+ let has_audio = lines.find(line => line.trim().startsWith('Stream #0:') && line.includes(' Audio: '));
104
+ if (!has_audio)
105
+ return null;
106
+ let line = lines.find(line => audio_sample_rate_regex.test(line));
107
+ let match = line?.match(audio_sample_rate_regex);
108
+ if (!match) {
109
+ throw new Error('failed to find audio sample rate');
110
+ }
111
+ let audioSampleRate = +match[1];
112
+ if (!audioSampleRate) {
113
+ throw new Error(`failed to parse audio sample rate: ${JSON.stringify(match[1])}`);
114
+ }
115
+ return audioSampleRate;
92
116
  }
93
117
  function scanVideo(file) {
94
118
  return new Promise((resolve, reject) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ffmpeg-progress",
3
- "version": "1.2.2",
3
+ "version": "1.3.0",
4
4
  "description": "Extract progress from ffmpeg child_process",
5
5
  "keywords": [
6
6
  "ffmpeg",