@@ -185,7 +185,7 @@ npx supabase gen types --lang=typescript --project-id "vuzgnncyyceddococcgj" --s
185
185
You will need to install some additional dependencies for this task:
186
186
187
187
``` bash
188
- npm install @deepgram/sdk @supabase/supabase-js fluent-ffmpeg node-fetch
188
+ npm install @deepgram/sdk @supabase/supabase-js fluent-ffmpeg
189
189
```
190
190
191
191
These dependencies will allow you to interact with the Deepgram and Supabase APIs, extract audio from a video using FFmpeg, and fetch the video from a URL.
@@ -197,7 +197,6 @@ import { createClient as createSupabaseClient } from "@supabase/supabase-js";
197
197
import { logger , task } from " @trigger.dev/sdk/v3" ;
198
198
import ffmpeg from " fluent-ffmpeg" ;
199
199
import fs from " fs" ;
200
- import fetch from " node-fetch" ;
201
200
import { Readable } from " node:stream" ;
202
201
import os from " os" ;
203
202
import path from " path" ;
@@ -215,15 +214,20 @@ const deepgram = createDeepgramClient(process.env.DEEPGRAM_SECRET_KEY);
215
214
216
215
export const videoProcessAndUpdate = task ({
217
216
id: " video-process-and-update" ,
218
- run : async (payload : { videoUrl: string }) => {
219
- const { videoUrl } = payload ;
220
-
221
- logger .log (` Processing video at URL: ${videoUrl } ` );
222
-
217
+ init : () => {
223
218
// Generate temporary file names
224
219
const tempDirectory = os .tmpdir ();
225
220
const outputPath = path .join (tempDirectory , ` audio_${Date .now ()}.wav ` );
226
221
222
+ return {
223
+ outputPath ,
224
+ };
225
+ },
226
+ run : async (payload : { id: string ; videoUrl: string }, { init }) => {
227
+ const { videoUrl } = payload ;
228
+
229
+ logger .log (` Processing video at URL: ${videoUrl } ` );
230
+
227
231
// Fetch the video
228
232
const response = await fetch (videoUrl );
229
233
@@ -240,7 +244,7 @@ export const videoProcessAndUpdate = task({
240
244
" -ar 44100" , // Set audio sample rate to 44.1 kHz
241
245
" -ac 2" , // Set audio channels to stereo
242
246
])
243
- .output (outputPath )
247
+ .output (init . outputPath )
244
248
.on (" end" , resolve )
245
249
.on (" error" , reject )
246
250
.run ();
@@ -264,22 +268,16 @@ export const videoProcessAndUpdate = task({
264
268
throw error ;
265
269
}
266
270
267
- console .dir (result , { depth: null });
268
-
269
271
// Convert the result object to a string
270
272
const transcription = result .results .channels [0 ].alternatives [0 ].paragraphs ?.transcript ;
271
273
272
274
logger .log (` Transcription: ${transcription } ` );
273
275
274
- // Delete the temporary audio file
275
- fs .unlinkSync (outputPath );
276
- logger .log (` Temporary audio file deleted ` , { outputPath });
277
-
278
276
const { error : updateError } = await supabase
279
277
.from (" video_transcriptions" )
280
278
// Set the plan to the new plan and update the timestamp
281
- .update ({ transcription: transcription , video_url: videoUrl })
282
- .eq (" video_url " , videoUrl );
279
+ .update ({ transcription: transcription })
280
+ .eq (" id " , payload . id );
283
281
284
282
// If there was an error updating the subscription, throw an error
285
283
if (updateError ) {
@@ -291,6 +289,11 @@ export const videoProcessAndUpdate = task({
291
289
result ,
292
290
};
293
291
},
292
+ cleanup : ({ init }) => {
293
+ // Delete the temporary audio file
294
+ fs .unlinkSync (init .outputPath );
295
+ logger .log (` Temporary audio file deleted ` , { outputPath: init .outputPath });
296
+ },
294
297
});
295
298
```
296
299
@@ -357,8 +360,7 @@ Finally, we need to replace your `video-processing-handler` Edge Function code w
357
360
``` ts functions/video-processing-handler/index.ts
358
361
// Setup type definitions for built-in Supabase Runtime APIs
359
362
import " jsr:@supabase/functions-js/edge-runtime.d.ts" ;
360
- // Import the Trigger.dev SDK - replace "<your-sdk-version>" with the version of the SDK you are using, e.g. "3.0.0". You can find this in your package.json file.
361
- import { tasks } from " npm:@trigger.dev/@sdk<your-sdk-version>/v3" ;
363
+ import { tasks } from " npm:@trigger.dev/sdk@latest/v3" ;
362
364
// Import the videoProcessAndUpdate task from the trigger folder
363
365
import type { videoProcessAndUpdate } from " ../../../src/trigger/videoProcessAndUpdate.ts" ;
364
366
// 👆 type only import
0 commit comments