Lucas Sierota

Lucas Sierota

// Name: download-youtube-video
// Description: Downloads an youtube video using the url
// Author: Lucas Sierota
// Twitter: @lucasemanuelss
import "@johnlindquist/kit";
import youtubeDlExec from "youtube-dl-exec";
const response = await widget(
`
<div class="w-full h-full text-center flex items-center justify-center">
<h1 :class="responseClass">{{ content }}</h1>
<div>
`
);
async function downloadVideo() {
try {
const videoSrc = await arg("Video url:");
const videoName = await arg("File name: (don't include extension)");
const videoPath = "Downloads"
const fileName = videoName !== "" ? videoName : videoSrc;
// setting path to be downloaded and including file extension
const path = home(videoPath, path.basename(fileName) + ".mp4");
// set loading state on widget
response.setState({
content: `Downloading...`,
responseClass: "text-yellow-600",
});
const res = await youtubeDlExec(videoSrc, { output: path });
console.log(res);
response.setState({
content: `${fileName} downloaded at ${path}`,
responseClass: "text-green-600",
});
} catch (err) {
console.log(err);
if (
err.stderr.includes("'NoneType' object has no attribute 'decompress'") ||
err.stderr.includes("is not a valid URL")
) {
response.setState({
content: `🔴 Error: The url provided is not an youtube video 🔴`,
responseClass: "text-red-600",
});
} else {
response.setState({
content: `🔴 Error ${err} 🔴`,
responseClass: "w-full h-full text-red-600",
});
}
}
}
await downloadVideo();