@pipedream/openai 0.4.2 → 0.4.3

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.
@@ -17,13 +17,14 @@ import constants from "../common/constants.mjs";
17
17
  import lang from "../common/lang.mjs";
18
18
 
19
19
  const COMMON_AUDIO_FORMATS_TEXT = "Your audio file must be in one of these formats: mp3, mp4, mpeg, mpga, m4a, wav, or webm.";
20
+ const CHUNK_SIZE_MB = 20;
20
21
 
21
22
  const execAsync = promisify(exec);
22
23
  const pipelineAsync = promisify(stream.pipeline);
23
24
 
24
25
  export default {
25
26
  name: "Create Transcription",
26
- version: "0.1.5",
27
+ version: "0.1.6",
27
28
  key: "openai-create-transcription",
28
29
  description: "Transcribes audio into the input language. [See docs here](https://platform.openai.com/docs/api-reference/audio/create).",
29
30
  type: "action",
@@ -104,6 +105,18 @@ export default {
104
105
  form.append("file", readStream);
105
106
  return form;
106
107
  },
108
+ async splitLargeChunks(files, outputDir) {
109
+ for (const file of files) {
110
+ if (fs.statSync(`${outputDir}/${file}`).size / (1024 * 1024) > CHUNK_SIZE_MB) {
111
+ await this.chunkFile({
112
+ file: `${outputDir}/${file}`,
113
+ outputDir,
114
+ index: file.slice(6, 9),
115
+ });
116
+ await execAsync(`rm -f "${outputDir}/${file}"`);
117
+ }
118
+ }
119
+ },
107
120
  async chunkFileAndTranscribe({
108
121
  file, $,
109
122
  }) {
@@ -116,16 +129,20 @@ export default {
116
129
  outputDir,
117
130
  });
118
131
 
119
- const files = await fs.promises.readdir(outputDir);
132
+ let files = await fs.promises.readdir(outputDir);
133
+ // ffmpeg will sometimes return chunks larger than the allowed size,
134
+ // so we need to identify large chunks and break them down further
135
+ await this.splitLargeChunks(files, outputDir);
136
+ files = await fs.promises.readdir(outputDir);
120
137
 
121
- return await this.transcribeFiles({
138
+ return this.transcribeFiles({
122
139
  files,
123
140
  outputDir,
124
141
  $,
125
142
  });
126
143
  },
127
144
  async chunkFile({
128
- file, outputDir,
145
+ file, outputDir, index,
129
146
  }) {
130
147
  const ffmpegPath = ffmpegInstaller.path;
131
148
  const ext = extname(file);
@@ -133,8 +150,10 @@ export default {
133
150
  const fileSizeInMB = fs.statSync(file).size / (1024 * 1024);
134
151
  // We're limited to 26MB per request. Because of how ffmpeg splits files,
135
152
  // we need to be conservative in the number of chunks we create
136
- const conservativeChunkSizeMB = 20;
137
- const numberOfChunks = Math.ceil(fileSizeInMB / conservativeChunkSizeMB);
153
+ const conservativeChunkSizeMB = CHUNK_SIZE_MB;
154
+ const numberOfChunks = !index
155
+ ? Math.ceil(fileSizeInMB / conservativeChunkSizeMB)
156
+ : 2;
138
157
 
139
158
  if (numberOfChunks === 1) {
140
159
  await execAsync(`cp "${file}" "${outputDir}/chunk-000${ext}"`);
@@ -152,7 +171,9 @@ export default {
152
171
  const totalSeconds = (hours * 60 * 60) + (minutes * 60) + seconds;
153
172
  const segmentTime = Math.ceil(totalSeconds / numberOfChunks);
154
173
 
155
- const command = `${ffmpegPath} -i "${file}" -f segment -segment_time ${segmentTime} -c copy "${outputDir}/chunk-%03d${ext}"`;
174
+ const command = `${ffmpegPath} -i "${file}" -f segment -segment_time ${segmentTime} -c copy "${outputDir}/chunk-${index
175
+ ? `${index}-`
176
+ : ""}%03d${ext}"`;
156
177
  await execAsync(command);
157
178
  },
158
179
  transcribeFiles({
@@ -194,14 +215,14 @@ export default {
194
215
  } = this;
195
216
 
196
217
  if (!url && !path) {
197
- throw new Error("Must specify either File URL or File Path");
218
+ throw new ConfigurationError("Must specify either File URL or File Path");
198
219
  }
199
220
 
200
221
  let file;
201
222
 
202
223
  if (path) {
203
224
  if (!fs.existsSync(path)) {
204
- throw new Error(`${path} does not exist`);
225
+ throw new ConfigurationError(`${path} does not exist`);
205
226
  }
206
227
 
207
228
  file = path;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pipedream/openai",
3
- "version": "0.4.2",
3
+ "version": "0.4.3",
4
4
  "description": "Pipedream OpenAI Components",
5
5
  "main": "openai.app.mjs",
6
6
  "keywords": [