How to download an OGA file, convert it to MP3 using ffmpeg from an npm package, save in /tmp directory, and return the file path?

This topic was automatically generated from Slack. You can find the original thread here.

What’s the code to download a file in OGA format, convert it to mp3 with ffmpeg, save the file to the /tmp directory, then return the path to the saved file? ffmpeg is not installed to the environment, so I need to download and include it via an npm package

To download a file in OGA format, convert it to MP3 using ffmpeg, and save it to the /tmp directory, you can use the following code. This code uses the fluent-ffmpeg package, which requires you to install @ffmpeg-installer/ffmpeg as well. First, add these imports at the top of your Node.js code: import { axios } from "@pipedream/platform"; import ffmpeg from "fluent-ffmpeg"; import ffmpegInstaller from "@ffmpeg-installer/ffmpeg"; Then, use this code within your Pipedream component: export default defineComponent({ props: { url: { type: "string", label: "URL of the OGA file", }, }, async run({ steps, $ }) { // Download the OGA file const response = await axios($, { method: "GET", url: this.url, responseType: "arraybuffer", }); // Set up ffmpeg with the installed package ffmpeg.setFfmpegPath(ffmpegInstaller.path); // Convert OGA to MP3 and save to /tmp directory const inputPath = "/tmp/input.oga"; const outputPath = "/tmp/output.mp3"; // Write the downloaded OGA file to the /tmp directory await require("fs").promises.writeFile(inputPath, response.data); // Convert the OGA file to MP3 using ffmpeg await new Promise((resolve, reject) => { ffmpeg(inputPath) .output(outputPath) .on("end", resolve) .on("error", reject) .run(); }); // Return the path to the saved MP3 file return outputPath; }, }); This code downloads the OGA file from the provided URL, converts it to MP3 using the fluent-ffmpeg package, and saves the converted file to the /tmp directory. Finally, it returns the path to the saved MP3 file.

I got the error ReferenceError
require is not defined

DETAILS
at Object.run (file:///tmp/pdg/dist/code/12911a610ebf49ea1fa6f8d2093c82870d42f364621cffcaa08a12e571557ec6/component.mjs:28:5)
at process.processTicksAndRejections (internal/process/task_queues.js:95:5)
at global.executeComponent (/var/task/launch_worker.js:139:22)
at MessagePort.messageHandler (/var/task/launch_worker.js:598:28)
ReferenceError
require is not defined

DETAILS
at Object.run (file:///tmp/pdg/dist/code/12911a610ebf49ea1fa6f8d2093c82870d42f364621cffcaa08a12e571557ec6/component.mjs:28:5)
at process.processTicksAndRejections (internal/process/task_queues.js:95:5)
at global.executeComponent (/var/task/launch_worker.js:139:22)
at MessagePort.messageHandler (/var/task/launch_worker.js:598:28)

I apologize for the confusion. In Pipedream, you should use ESM imports instead of CommonJS require. Here’s the updated code: ```
import { axios } from “@pipedream/platform”; import ffmpeg from “fluent-ffmpeg”; import ffmpegInstaller from “@ffmpeg-installer/ffmpeg”; import { promises as fs } from “fs”; export default defineComponent({ props: { url: { type: “string”, label: “URL of the OGA file”, }, }, async run({ $ }) { // Download the OGA file const response = await axios($, { method: “GET”, url: this.url, responseType: “arraybuffer”, }); // Set up ffmpeg with the installed package ffmpeg.setFfmpegPath(ffmpegInstaller.path); // Convert OGA to MP3 and save to /tmp directory const inputPath = “/tmp/input.oga”; const outputPath = “/tmp/output.mp3”; // Write the downloaded OGA file to the /tmp directory await fs.writeFile(inputPath, response.data); // Convert the OGA file to MP3 using ffmpeg await new Promise((resolve, reject) => { ffmpeg(inputPath) .output(outputPath) .on(“end”, resolve) .on(“error”, reject) .run(); }); // Return the path to the saved MP3 file return outputPath; }, });