如何输入文本框以粘贴YouTube网址ytdl-npm

时间:2019-03-23 08:22:31

标签: javascript html

我正在使用NPM中的ytdl,我想知道如何制作一个框来粘贴youtube视频中的链接以进行下载,实际上该代码已经设置为可以从youtube下载特定视频,但是我希望能够直接从我的应用中选择:这是我的代码

    <script>

      //Youtube Downloader script

      function youtubeDL(){
        var fs = require('fs');
            var youtubedl = require('youtube-dl');
            //here is the actual link, How can I change it from my application?
                var video = youtubedl('http://www.youtube.com/watch?v=90AiXO1pAiA',
// Will be called when the download starts.
video.on('info', function(info) {
  console.log('Download started');
  console.log('filename: ' + info._filename);
  console.log('size: ' + info.size);
});
 
video.pipe(fs.createWriteStream('myvideo.mp4'));

      }    
    </script>

1 个答案:

答案 0 :(得分:0)

您可以在javascript中使用DOM来查看此link

html

<input type="text" id="ytLink">

javascript

var youtubeLink = document.getElementById("ytLink").value;

function youtubeDL(){

    var fs = require('fs');
    var youtubedl = require('youtube-dl');
    //here is the actual link, How can I change it from my application?
    var video = youtubedl(youtubeLink,

    // Will be called when the download starts.
    video.on('info', function(info) {
    console.log('Download started');
    console.log('filename: ' + info._filename);
    console.log('size: ' + info.size);
});

video.pipe(fs.createWriteStream('myvideo.mp4'));