Untitled

mail@pastecode.io avatar
unknown
javascript
3 years ago
808 B
3
Indexable
Never
const fs = require("fs");
const fetch = require("node-fetch");
const { pipeline } = require("stream");
const { promisify } = require("util");

const DOWNLOAD_DIR = "./videos/"; // Onde o arquivo deve ser baixado a pasta deve existir
const DOWNLOAD_URL = "";

// Download video action
const downloadVideo = async (videoId, videoNum, page) => {
  try {
    const streamPipeline = promisify(pipeline);
    // const unsecLink = /(([^\/]*\/){5})/.exec(link);
    const res = await fetch(
      `${DOWNLOAD_URL}/${videoId}/playlist.mp4`
    );
    if (!res.ok) {
      throw new Error(`unexpected response ${res.statusText}`);
    }
    await streamPipeline(
      res.body,
      fs.createWriteStream(`${DOWNLOAD_DIR}/page${page}-video${videoNum}.mp4`)
    );
  } catch (err) {
    console.log("err", err);
  }
};