/ command.ts
command.ts
 1  import yargs from "yargs";
 2  import { hideBin } from "yargs/helpers";
 3  import { create as createYoutubeDl } from "youtube-dl-exec";
 4  import createLogger from "progress-estimator";
 5  
 6  const youtubedl = createYoutubeDl("yt-dlp");
 7  
 8  yargs(hideBin(process.argv))
 9    .command(
10      "$0 <link>",
11      "Downloads a video from YouTube",
12      (yargs) =>
13        yargs.positional("link", {
14          description: "a link to the video",
15          type: "string",
16          demandOption: true,
17        }),
18      async (argv) => {
19        console.log("Downloading video...");
20  
21        try {
22          const url = argv.link;
23  
24          const logger = createLogger();
25  
26          const output = youtubedl(
27            url,
28            {
29              noCheckCertificates: true,
30              noWarnings: true,
31              preferFreeFormats: true,
32              addHeader: ["referer:youtube.com", "user-agent:googlebot"],
33              output: "%(title)s.%(ext)s",
34            },
35            {
36              timeout: 60000,
37              killSignal: "SIGKILL",
38            },
39          );
40  
41          await logger(output, `Obtaining ${url}`);
42        } catch (error) {
43          console.error("Error downloading video:", error);
44          process.exit(1);
45        }
46      },
47    )
48    .help()
49    .parse();