@editframe/assets 0.11.0-beta.8 → 0.12.0-beta.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/dist/Probe.js CHANGED
@@ -84,8 +84,6 @@ class Probe {
84
84
  return new Probe(absolutePath, json);
85
85
  }
86
86
  static async probeStream(stream) {
87
- const probeCommand = "ffprobe -i pipe:0 -v error -show_format -show_streams -of json";
88
- log("Probing", probeCommand);
89
87
  const probe = spawn(
90
88
  "ffprobe",
91
89
  [
@@ -98,35 +96,52 @@ class Probe {
98
96
  "-of",
99
97
  "json"
100
98
  ],
101
- {
102
- stdio: ["pipe", "pipe", "pipe"]
103
- }
99
+ { stdio: ["pipe", "pipe", "pipe"] }
104
100
  );
105
101
  const chunks = [];
102
+ const processExit = new Promise((_, reject) => {
103
+ probe.on("exit", (code) => {
104
+ if (code !== 0) {
105
+ reject(new Error(`ffprobe exited with code ${code}`));
106
+ }
107
+ });
108
+ probe.on("error", (err) => reject(err));
109
+ });
106
110
  probe.stderr.on("data", (data) => {
107
111
  log(data.toString());
108
112
  });
109
113
  probe.stdout.on("data", (data) => {
110
114
  chunks.push(data);
111
115
  });
112
- stream.pipe(probe.stdin);
113
116
  probe.stdin.on("error", (error) => {
117
+ if (error.code === "EPIPE") {
118
+ log("ffprobe closed input pipe");
119
+ return;
120
+ }
114
121
  log("ffprobe stdin error", error);
115
122
  });
116
- const json = await new Promise((resolve, reject) => {
117
- probe.stdout.on("end", () => {
118
- stream.unpipe(probe.stdin);
119
- stream.destroy();
120
- try {
121
- const buffer = Buffer.concat(chunks).toString("utf8");
122
- log("Got probe from stream", buffer);
123
- resolve(JSON.parse(buffer));
124
- } catch (error) {
125
- reject(error);
126
- }
127
- });
128
- });
129
- return new Probe("pipe:0", json);
123
+ stream.pipe(probe.stdin);
124
+ try {
125
+ const json = await Promise.race([
126
+ new Promise((resolve, reject) => {
127
+ probe.stdout.on("end", () => {
128
+ try {
129
+ const buffer = Buffer.concat(chunks).toString("utf8");
130
+ log("Got probe from stream", buffer);
131
+ resolve(JSON.parse(buffer));
132
+ } catch (error) {
133
+ reject(error);
134
+ }
135
+ });
136
+ }),
137
+ processExit
138
+ ]);
139
+ return new Probe("pipe:0", json);
140
+ } finally {
141
+ stream.unpipe(probe.stdin);
142
+ probe.stdin.end();
143
+ stream.destroy();
144
+ }
130
145
  }
131
146
  get audioStreams() {
132
147
  return this.data.streams.filter(
@@ -10,20 +10,21 @@ const generateTrackFromPath = async (absolutePath, trackId) => {
10
10
  const probe = await Probe.probePath(absolutePath);
11
11
  const readStream = probe.createConformingReadstream();
12
12
  const mp4File = new MP4File();
13
+ const trackStream = new PassThrough();
13
14
  log(`Generating track for ${absolutePath}`);
14
15
  readStream.pipe(mp4FileWritable(mp4File));
15
- await new Promise((resolve, reject) => {
16
- readStream.on("end", resolve);
17
- readStream.on("error", reject);
18
- });
19
- const trackStream = new PassThrough();
20
- for await (const fragment of mp4File.fragmentIterator()) {
21
- if (fragment.track !== trackId) {
22
- continue;
16
+ (async () => {
17
+ try {
18
+ for await (const fragment of mp4File.fragmentIterator()) {
19
+ if (fragment.track === trackId) {
20
+ trackStream.write(Buffer.from(fragment.data), "binary");
21
+ }
22
+ }
23
+ trackStream.end();
24
+ } catch (error) {
25
+ trackStream.destroy(error);
23
26
  }
24
- trackStream.write(Buffer.from(fragment.data), "binary");
25
- }
26
- trackStream.end();
27
+ })();
27
28
  return trackStream;
28
29
  };
29
30
  const generateTrackTask = idempotentTask({
@@ -33,9 +34,9 @@ const generateTrackTask = idempotentTask({
33
34
  });
34
35
  const generateTrack = async (cacheRoot, absolutePath, url) => {
35
36
  try {
36
- const trackId = new URL(
37
- `http://localhost${url}` ?? "bad-url"
38
- ).searchParams.get("trackId");
37
+ const trackId = new URL(`http://localhost${url}`).searchParams.get(
38
+ "trackId"
39
+ );
39
40
  if (trackId === null) {
40
41
  throw new Error(
41
42
  "No trackId provided. IT must be specified in the query string: ?trackId=0"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@editframe/assets",
3
- "version": "0.11.0-beta.8",
3
+ "version": "0.12.0-beta.1",
4
4
  "description": "",
5
5
  "exports": {
6
6
  ".": {
@@ -14,24 +14,26 @@ export const generateTrackFromPath = async (
14
14
  const probe = await Probe.probePath(absolutePath);
15
15
  const readStream = probe.createConformingReadstream();
16
16
  const mp4File = new MP4File();
17
+ const trackStream = new PassThrough();
17
18
 
18
19
  log(`Generating track for ${absolutePath}`);
19
- readStream.pipe(mp4FileWritable(mp4File));
20
-
21
- await new Promise((resolve, reject) => {
22
- readStream.on("end", resolve);
23
- readStream.on("error", reject);
24
- });
25
20
 
26
- const trackStream = new PassThrough();
21
+ // Set up pipe and processing of fragments
22
+ readStream.pipe(mp4FileWritable(mp4File));
27
23
 
28
- for await (const fragment of mp4File.fragmentIterator()) {
29
- if (fragment.track !== trackId) {
30
- continue;
24
+ // Process fragments as they become available
25
+ (async () => {
26
+ try {
27
+ for await (const fragment of mp4File.fragmentIterator()) {
28
+ if (fragment.track === trackId) {
29
+ trackStream.write(Buffer.from(fragment.data), "binary");
30
+ }
31
+ }
32
+ trackStream.end();
33
+ } catch (error) {
34
+ trackStream.destroy(error as Error);
31
35
  }
32
- trackStream.write(Buffer.from(fragment.data), "binary");
33
- }
34
- trackStream.end();
36
+ })();
35
37
 
36
38
  return trackStream;
37
39
  };
@@ -49,9 +51,9 @@ export const generateTrack = async (
49
51
  url: string,
50
52
  ) => {
51
53
  try {
52
- const trackId = new URL(
53
- `http://localhost${url}` ?? "bad-url",
54
- ).searchParams.get("trackId");
54
+ const trackId = new URL(`http://localhost${url}`).searchParams.get(
55
+ "trackId",
56
+ );
55
57
  if (trackId === null) {
56
58
  throw new Error(
57
59
  "No trackId provided. IT must be specified in the query string: ?trackId=0",