Skip to content

Commit a443794

Browse files
committed
Code updates
1 parent 77eb41e commit a443794

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

docs/guides/frameworks/supabase-edge-functions-database-webhooks.mdx

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ npx supabase gen types --lang=typescript --project-id "vuzgnncyyceddococcgj" --s
185185
You will need to install some additional dependencies for this task:
186186

187187
```bash
188-
npm install @deepgram/sdk @supabase/supabase-js fluent-ffmpeg node-fetch
188+
npm install @deepgram/sdk @supabase/supabase-js fluent-ffmpeg
189189
```
190190

191191
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";
197197
import { logger, task } from "@trigger.dev/sdk/v3";
198198
import ffmpeg from "fluent-ffmpeg";
199199
import fs from "fs";
200-
import fetch from "node-fetch";
201200
import { Readable } from "node:stream";
202201
import os from "os";
203202
import path from "path";
@@ -215,15 +214,20 @@ const deepgram = createDeepgramClient(process.env.DEEPGRAM_SECRET_KEY);
215214

216215
export const videoProcessAndUpdate = task({
217216
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: () => {
223218
// Generate temporary file names
224219
const tempDirectory = os.tmpdir();
225220
const outputPath = path.join(tempDirectory, `audio_${Date.now()}.wav`);
226221

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+
227231
// Fetch the video
228232
const response = await fetch(videoUrl);
229233

@@ -240,7 +244,7 @@ export const videoProcessAndUpdate = task({
240244
"-ar 44100", // Set audio sample rate to 44.1 kHz
241245
"-ac 2", // Set audio channels to stereo
242246
])
243-
.output(outputPath)
247+
.output(init.outputPath)
244248
.on("end", resolve)
245249
.on("error", reject)
246250
.run();
@@ -264,22 +268,16 @@ export const videoProcessAndUpdate = task({
264268
throw error;
265269
}
266270

267-
console.dir(result, { depth: null });
268-
269271
// Convert the result object to a string
270272
const transcription = result.results.channels[0].alternatives[0].paragraphs?.transcript;
271273

272274
logger.log(`Transcription: ${transcription}`);
273275

274-
// Delete the temporary audio file
275-
fs.unlinkSync(outputPath);
276-
logger.log(`Temporary audio file deleted`, { outputPath });
277-
278276
const { error: updateError } = await supabase
279277
.from("video_transcriptions")
280278
// 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);
283281

284282
// If there was an error updating the subscription, throw an error
285283
if (updateError) {
@@ -291,6 +289,11 @@ export const videoProcessAndUpdate = task({
291289
result,
292290
};
293291
},
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+
},
294297
});
295298
```
296299

@@ -357,8 +360,7 @@ Finally, we need to replace your `video-processing-handler` Edge Function code w
357360
```ts functions/video-processing-handler/index.ts
358361
// Setup type definitions for built-in Supabase Runtime APIs
359362
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";
362364
// Import the videoProcessAndUpdate task from the trigger folder
363365
import type { videoProcessAndUpdate } from "../../../src/trigger/videoProcessAndUpdate.ts";
364366
// 👆 type only import

0 commit comments

Comments
 (0)