How to Convert a Video URL into Base64 Using Pipedream?

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

How do I convert a video in a url into base64 using pipedream?

: Could you please write a node.js code step which transforms a video encoding from binary to base64?

Sure, here’s a Node.js code step that fetches a video from a URL and converts it to base64: import { axios } from "@pipedream/platform"; export default defineComponent({ props: { videoUrl: { type: "string", label: "Video URL", }, }, async run({ steps, $ }) { const response = await axios($, { method: "GET", url: this.videoUrl, responseType: "arraybuffer", }); const base64Video = Buffer.from(response, "binary").toString("base64"); return base64Video; }, }); This code step takes a videoUrl prop as input. It fetches the video using the axios library, and then converts the binary data to a base64-encoded string. The base64-encoded video is returned from the step.

: There ya go! :point_up_2: :+1:

: Could you rewrite that step to save the result to the /tmp directory instead?

Sure, here’s the modified Node.js code step that fetches a video from a URL and saves it to the /tmp directory: import { axios } from "@pipedream/platform"; import fs from "fs"; export default defineComponent({ props: { videoUrl: { type: "string", label: "Video URL", }, }, async run({ steps, $ }) { const response = await axios($, { method: "GET", url: this.videoUrl, responseType: "arraybuffer", }); const filePath = "/tmp/video"; await fs.promises.writeFile(filePath, response); return `Video saved to ${filePath}`; }, }); This code step fetches the video using the axios library, and then saves the binary data to a file in the /tmp directory. The file path is returned from the step.

If the videos are large, you might run out of memory though, so it might be better to use a stream.

Something like this:

import fs from "fs";
import { Readable } from "stream";
import { finished } from "stream/promises";

export default defineComponent({
  async run({ steps, $ }) {
    const res = await fetch(url);
    const fileStream = fs.createWriteStream('/tmp/file.ext', { flags: 'wx' });
    await finished(Readable.fromWeb(res.body).pipe(fileStream));
  }
});

Thanks a lot marco. Trying this out now

This is what I get as the ouput from the step suggested by pi - $return_value:Video saved to /tmp/video

How do i get the base64 value to use in next step? essentially i want to post the base64 value to an external endpoint in next step @U05LWJMUX0A

To read the saved video file and convert it to base64, you can add another code step after the one that saves the video to /tmp/video. Here’s a code step to read the file and convert it to base64: import fs from "fs"; export default defineComponent({ async run({ steps, $ }) { const filePath = "/tmp/video"; const videoBuffer = await fs.promises.readFile(filePath); const base64Video = videoBuffer.toString("base64"); return base64Video; }, }); This code step reads the video file from /tmp/video, converts it to a base64-encoded string, and returns it. You can then use the base64-encoded video in the next step to send it to an external endpoint.

I keep getting error - ENOENT: no such file or directory, open ‘/tmp/video’

I apologize for the confusion. It seems like the video file might not have been saved correctly to the /tmp directory. Let’s make sure the video is being saved correctly before attempting to read it. Please double-check the code step that saves the video to /tmp/video and ensure it’s working as expected. If you’re still facing issues, please provide more information about the code you’re using to save the video, and I’ll be happy to help you further.

here’s an action I use… only ever tried it for images and you may need to add the mime types (depending on your use case). It should work though.

something like

data:video/mpeg;base64,{b64string}

I get this error when I try it…

It’s probably a really large file that’s why suggested saving the base64 string to a file and reading the file contents in a future step.

btw what’s the usecase of converting a video to base64?

I want to upload this video to youtube. But the youtube API requires I provide the video in base64.

I have the video hosted in a URL. So trying to conver that to base64 so that I can pass it to the youtube data api

I followed Marco’s advise and can save it to a temp file. But then not sure how to use the content of the temp file to pass it to the api as a base64 value

Why not download the actual video to /tmp and pass the video file from tmp to the YouTube API?