@@ -98,14 +98,13 @@ export const ffmpegCompressVideo = task({
9898
9999    //  Read the compressed video
100100    const   compressedVideo =  await  fs .readFile (outputPath );
101- 
102101    const   compressedSize =  compressedVideo .length ;
103102
104103    //  Log compression results
105104    logger .log (` Compressed video size: ${compressedSize } bytes ` );
106-     logger .log (` Compressed  video saved at: ${ outputPath } ` );
105+     logger .log (` Temporary compressed  video file created ` , {  outputPath  } );
107106
108-     //  Upload  the compressed video to S3, replacing slashes with underscores 
107+     //  Create  the r2Key for the extracted audio, using the base name of the output path 
109108    const   r2Key =  ` processed-videos/${path .basename (outputPath )} ` ;
110109
111110    const   uploadParams =  {
@@ -116,22 +115,31 @@ export const ffmpegCompressVideo = task({
116115
117116    //  Upload the video to R2 and get the URL
118117    await  s3Client .send (new  PutObjectCommand (uploadParams ));
119-     const   r2Url =  ` https://${process .env .R2_ACCOUNT_ID }.r2.cloudflarestorage.com/${process .env .R2_BUCKET }/${r2Key } ` ;
120-     logger .log (" Compressed video uploaded to R2"  , { url: r2Url  });
118+     logger .log (` Compressed video saved to your r2 bucket ` , { r2Key  });
121119
122120    //  Delete the temporary compressed video file
123121    await  fs .unlink (outputPath );
122+     logger .log (` Temporary compressed video file deleted ` , { outputPath  });
124123
125-     //  Return the compressed video file path, compressed size,  and S3 URL 
124+     //  Return the compressed video buffer  and r2 key 
126125    return  {
127-       compressedVideoPath: outputPath ,
128-       compressedSize ,
129-       r2Url ,
126+       Bucket: process .env .R2_BUCKET ,
127+       r2Key ,
130128    };
131129  },
132130});
133131``` 
134132
133+ ### Testing:  
134+ 
135+ To test this task, use this payload structure:
136+ 
137+ ``` json 
138+ {
139+   "videoUrl" : " <video-url>" 
140+ }
141+ ``` 
142+ 
135143## Extract audio from a video using FFmpeg  
136144
137145This task demonstrates how to use FFmpeg to extract audio from a video, convert it to WAV format, and upload it to R2 storage.
@@ -145,11 +153,6 @@ This task demonstrates how to use FFmpeg to extract audio from a video, convert
145153
146154### Task code  
147155
148- <Warning >
149-   When testing, make sure to provide a video URL that contains audio. If the video does not have
150-   audio, the task will fail.
151- </Warning >
152- 
153156``` ts trigger/ffmpeg-extract-audio.ts 
154157import  { PutObjectCommand , S3Client  } from  " @aws-sdk/client-s3"  ;
155158import  { logger , task  } from  " @trigger.dev/sdk/v3"  ;
@@ -176,63 +179,81 @@ export const ffmpegExtractAudio = task({
176179  run : async  (payload :  { videoUrl:  string  }) =>  {
177180    const   { videoUrl } =  payload ;
178181
179-     //  Generate temporary and output  file names
182+     //  Generate temporary file names
180183    const   tempDirectory =  os .tmpdir ();
181-     const   outputPath =  path .join (tempDirectory , ` output_ ${Date .now ()}.wav` );
184+     const   outputPath =  path .join (tempDirectory , ` audio_ ${Date .now ()}.wav` );
182185
183186    //  Fetch the video
184187    const   response =  await  fetch (videoUrl );
185188
186-     //  Convert  the video to WAV 
189+     //  Extract  the audio 
187190    await  new  Promise ((resolve , reject ) =>  {
188191      if  (! response .body ) {
189192        return  reject (new  Error (" Failed to fetch video"  ));
190193      }
194+ 
191195      ffmpeg (Readable .from (response .body ))
192-         .toFormat (" wav"  )
193-         .save (outputPath )
194-         .on (" end"  , () =>  {
195-           logger .log (` WAV file saved to ${outputPath } ` );
196-           resolve (outputPath );
197-         })
198-         .on (" error"  , (err ) =>  {
199-           reject (err );
200-         });
196+         .outputOptions ([
197+           " -vn"  , //  Disable video output
198+           " -acodec pcm_s16le"  , //  Use PCM 16-bit little-endian encoding
199+           " -ar 44100"  , //  Set audio sample rate to 44.1 kHz
200+           " -ac 2"  , //  Set audio channels to stereo
201+         ])
202+         .output (outputPath )
203+         .on (" end"  , resolve )
204+         .on (" error"  , reject )
205+         .run ();
201206    });
202207
203-     //  Read the WAV file
204-     const   wavBuffer =  await  fs .readFile (outputPath );
208+     //  Read the extracted audio
209+     const   audioBuffer =  await  fs .readFile (outputPath );
210+     const   audioSize =  audioBuffer .length ;
205211
206-     //  Log the output file path
207-     logger .log (` Converted video saved at: ${outputPath } ` );
212+     //  Log audio extraction results
213+     logger .log (` Extracted audio size: ${audioSize } bytes ` );
214+     logger .log (` Temporary audio file created ` , { outputPath  });
208215
209-     //  Upload  the compressed video to S3, replacing slashes with underscores 
210-     const   r2Key =  ` processed -audio/${path .basename (outputPath )}` ;
216+     //  Create  the r2Key for the extracted audio, using the base name of the output path 
217+     const   r2Key =  ` extracted -audio/${path .basename (outputPath )}` ;
211218
212219    const   uploadParams =  {
213220      Bucket: process .env .R2_BUCKET ,
214221      Key: r2Key ,
215-       Body: wavBuffer ,
222+       Body: audioBuffer ,
216223    };
217224
218225    //  Upload the audio to R2 and get the URL
219226    await  s3Client .send (new  PutObjectCommand (uploadParams ));
220-     const   r2Url =  ` https://${process .env .R2_ACCOUNT_ID }.r2.cloudflarestorage.com/${process .env .R2_BUCKET }/${r2Key } ` ;
221-     logger .log (" Extracted audio uploaded to R2"  , { url: r2Url  });
227+     logger .log (` Extracted audio saved to your R2 bucket ` , { r2Key  });
222228
223-     //  Delete the temporary file
229+     //  Delete the temporary audio  file
224230    await  fs .unlink (outputPath );
231+     logger .log (` Temporary audio file deleted ` , { outputPath  });
225232
226-     //  Return the WAV buffer  and file path 
233+     //  Return the audio file path, size,  and R2 URL 
227234    return  {
228-       wavBuffer ,
229-       wavFilePath: outputPath ,
230-       r2Url ,
235+       Bucket: process .env .R2_BUCKET ,
236+       r2Key ,
231237    };
232238  },
233239});
234240``` 
235241
242+ ### Testing:  
243+ 
244+ To test this task, use this payload structure:
245+ 
246+ <Warning >
247+   Make sure to provide a video URL that contains audio. If the video does not have audio, the task
248+   will fail.
249+ </Warning >
250+ 
251+ ``` json 
252+ {
253+   "videoUrl" : " <video-url>" 
254+ }
255+ ``` 
256+ 
236257## Generate a thumbnail from a video using FFmpeg  
237258
238259This task demonstrates how to use FFmpeg to generate a thumbnail from a video at a specific time and upload the generated thumbnail to R2 storage.
@@ -298,7 +319,7 @@ export const ffmpegGenerateThumbnail = task({
298319    //  Read the generated thumbnail
299320    const   thumbnail =  await  fs .readFile (outputPath );
300321
301-     //  Upload  the compressed video to S3, replacing slashes with underscores 
322+     //  Create  the r2Key for the extracted audio, using the base name of the output path 
302323    const   r2Key =  ` thumbnails/${path .basename (outputPath )} ` ;
303324
304325    const   uploadParams =  {
@@ -318,7 +339,7 @@ export const ffmpegGenerateThumbnail = task({
318339    //  Log thumbnail generation results
319340    logger .log (` Thumbnail uploaded to S3: ${r2Url } ` );
320341
321-     //  Return the thumbnail buffer, file  path, sizes,  and S3  URL
342+     //  Return the thumbnail buffer, path, and R2  URL
322343    return  {
323344      thumbnailBuffer: thumbnail ,
324345      thumbnailPath: outputPath ,
@@ -327,3 +348,13 @@ export const ffmpegGenerateThumbnail = task({
327348  },
328349});
329350``` 
351+ 
352+ ## Testing your task  
353+ 
354+ To test this task in the dashboard, you can use the following payload:
355+ 
356+ ``` json 
357+ {
358+   "videoUrl" : " <video-url>" 
359+ }
360+ ``` 
0 commit comments