我正在使用 ytdl-core 执行一些测试。 下载包含音乐的视频时,只下载没有音频的视频。 你能帮我解决这个问题吗? 另一件事,我添加了两个按钮也只能下载音频(mp3),但我无法让它工作,你能帮我吗? 我添加了两个按钮,用于选择视频和音频选项。
Code.js
const express = require("express");
const ytdl = require("ytdl-core");
const app = express();
app.use(express.json());
app.use(express.static("public"));
app.get("/",function(request,response){
response.sendFile(__dirname + "public/index.html");
});
app.get("/videoInfo",async function(request,response){
const videoURL = request.query.videoURL;
const info = await ytdl.getInfo(videoURL);
response.status(200).json(info);
});
app.get("/download",function(request,response){
const videoURL = request.query.videoURL;
const itag = request.query.itag;
response.header("Content-Disposition",'attachment;\ filename="video.mp4"');
ytdl(videoURL,{
filter: format => format.itag == itag,
}).pipe(response);
});
app.listen(5000);
App.js
const host = "http://localhost:5000/";
document.querySelector("#get-video-info-btn").addEventListener("click", function () {
let videoURL = document.querySelector("#videoURL").value.trim();
if (videoURL.length == 0) {
alert("Please enter youtube video link");
return;
}
fetch(host + "videoInfo?videoURL=" + videoURL)
.then(function (response) {
return response.json();
})
.then(function (data) {
console.log(data);
let detailsNodes = {
thumbnail: document.querySelector(".video-data .thumbnail img"),
title: document.querySelector(".video-data .info h2"),
videoURL: document.querySelector(
".video-data .controls #video-url"
),
downloadOptions: document.querySelector(
".video-data .controls #download-options"
),
};
let html = "";
for (let i = 0; i < data.formats.length; i++) {
if (data.formats[i].container != "mp4") {
continue;
}
html += `
<option value="${data.formats[i].itag}">
${data.formats[i].container} - ${data.formats[i].qualityLabel}
</option>
`;
detailsNodes.thumbnail.src =
data.videoDetails.thumbnails[
data.videoDetails.thumbnails.length - 1
].url; // get HD thumbnail img
detailsNodes.title.innerText = data.videoDetails.title;
detailsNodes.videoURL.value = videoURL;
detailsNodes.downloadOptions.innerHTML = html;
document.querySelector(".card-detail").style.display = "flex";
document.querySelector(".video-data").style.display = "block";
document.querySelector(".video-data").scrollIntoView({
behavior: "smooth",
});
}
})
.catch(function (error) {
alert("Something went wrong");
});
});
document.querySelector("#download-btn").addEventListener("click", function () {
let videoURL = document.querySelector("#video-url").value;
let itag = document.querySelector("#download-options").value;
window.open(host + "download?videoURL=" + videoURL + "&itag=" + itag);
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>particles.js</title>
<meta name="description" content="particles.js is a lightweight JavaScript library for creating particles.">
<meta name="author" content="Vincent Garreau" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" media="screen" href="css/style.css">
<link
href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;700&display=swap"
rel="stylesheet"
/>
</head>
<body>
<!-- particles.js container -->
<div id="particles-js"></div>
<header>
<div class="logo">
<p>Downloader.js</p>
</div>
</header>
<main>
<section>
<div class="card">
<div class="form">
<div class="form-element">
<label for="videoURL">Enter Youtube Video Link</label></br>
<input type="text" id="videoURL" placeholder="https://www.youtube.com/watch?v=ae7vzA80rVo">
</div>
<div class="form-element">
<button id="get-video-info-btn">Download</button>
</div>
</div>
</div>
</section>
<section>
<div class="card-detail" >
<div class="video-data">
<div class="data">
<div class="thumbnail">
<img>
</div>
<div class="info">
<h2>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod</h2>
</div>
</div>
<div class="controls">
<input type="hidden" id="video-url">
<select id="download-options"></select>
<button id="download-btn">Download</button>
<select id="download-options2"></select>
<button id="download-btn2">Download</button>
</div>
</div>
</div>
</section>
</main>
<!-- scripts -->
<script src="js/particles.js"></script>
<script src="js/app.js"></script>
<script src="js/yt.js"></script>
</body>
</html>