Cannot merge audio and video streams from ytdl-core

Viewed 56

I am trying to merge ytdl-core video only and audio only streams and sending the merged stream are response to user (for download in browser). With JavaScript only, its working perfectly fine. But typeScript is saying child process stdio is undefined

import express, { Request } from "express";
import ytdl from "ytdl-core";
import cp from "child_process";
import ffmpeg from "ffmpeg-static";    
app.get("/downloadmerged", (req, res) => {
res.setHeader("Content-Type", "video/mp4");
res.setHeader(
  `Content-Disposition`,
  `attachment; filename="Merged.mp4"`
);
  const ref = "https://www.youtube.com/watch?v=aR-KAldshAE";
  const audio = ytdl(ref, { filter: "audioonly", quality: "highestaudio" });
  const video = ytdl(ref, { filter: "videoonly", quality: "highestvideo" });
  const ffmpegProcess = cp.spawn(
    ffmpeg,
    [
      // Remove ffmpeg's console spamming
      "-loglevel",
      "0",
      "-hide_banner",
      // 3 second audio offset
      "-itsoffset",
      "3.0",
      "-i",
      "pipe:3",
      "-i",
      "pipe:4",
      // Rescale the video
      "-vf",
      "scale=320:240",
      // Choose some fancy codes
      "-c:v",
      "libx265",
      "-x265-params",
      "log-level=0",
      "-c:a",
      "flac",
      // Define output container
      "-f",
      "matroska",
      "pipe:5",
    ],
    {
      windowsHide: true,
      stdio: [
        /* Standard: stdin, stdout, stderr */
        "inherit",
        "inherit",
        "inherit",
        /* Custom: pipe:3, pipe:4, pipe:5 */
        "pipe",
        "pipe",
        "pipe",
      ],
    }
  );
  ffmpegProcess.on("close", () => {
    process.stdout.write("\n\n\n");
    console.log("done");
  });
 
  audio.pipe(ffmpegProcess.stdio[3]);
  video.pipe(ffmpegProcess.stdio[4]);
  ffmpegProcess.stdio[5].pipe(res);
});

line ffmpegProcess.stdio[3] and ffmpegProcess.stdio[4] are giving type error

Argument of type 'Readable | Writable | null | undefined' is not assignable to parameter of type 'WritableStream'.Type 'undefined' is not assignable to type 'WritableStream'

  
0 Answers
Related