YTDL-core 下载视频没有声音

问题描述

我正在使用 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",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>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...