Skip to content

Instantly share code, notes, and snippets.

@Tim-Gabrikowski
Last active November 4, 2023 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tim-Gabrikowski/31cc8ae0fdaccfc1177e05519ba97199 to your computer and use it in GitHub Desktop.
Save Tim-Gabrikowski/31cc8ae0fdaccfc1177e05519ba97199 to your computer and use it in GitHub Desktop.
YT Downloader in under 50 lines
const fs = require("fs");
const ytdl = require("ytdl-core");
const ffmpeg = require("fluent-ffmpeg");
const { createSpinner } = require("nanospinner");
var videoId = "videoID";
let hvSpinner = createSpinner("Download highest Video").start();
ytdl(videoId, { quality: "highestvideo" })
.pipe(fs.createWriteStream(videoId + ".mp4"))
.on("error", () => {
hvSpinner.error();
console.log("error downloading video");
})
.on("finish", () => {
hvSpinner.success();
let scSpinner = createSpinner("Downloading audio and combining").start();
const audioFileStream = ytdl(videoId, { quality: "highestaudio" });
const outputFilePath = videoId + ".mkv";
const command = ffmpeg();
command
.input(videoId + ".mp4")
.input(audioFileStream)
.audioCodec("copy")
.videoCodec("copy")
.inputFormat("mp4")
.output(outputFilePath)
.on("end", () => {
scSpinner.success();
let dlSpinner = createSpinner("Delete temporary created file").start();
fs.rmSync(videoId + ".mp4");
dlSpinner.success();
})
.on("error", (err) => {
scSpinner.error();
console.error("Error:", err);
})
.run();
});
{
"name": "ytdl",
"version": "1.0.0",
"description": "Simple YouTube downloader in under 50 lines",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"fluent-ffmpeg": "^2.1.2",
"nanospinner": "^1.1.0",
"ytdl-core": "^4.11.5"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment