download.js
1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
var request = require('request');
var optionParams = {
q:"염따",
part:"snippet",
key:"AIzaSyCgGa6aM7taXs4bajtYukbc_EQAKTLVTNc",
type:"video",
maxResult:2,
regionCode:"KR",
videoDuration:"short"
};
optionParams.q = encodeURI(optionParams.q);
var url = "https://www.googleapis.com/youtube/v3/search?";
for (var option in optionParams)
{
url+=option+"="+optionParams[option]+"&";
}
url = url.substr(0,url.length-1);
var VideoIds = new Array();
var idx = 0;
request(url, function(err,res,body){
//console.log(body)
var data = JSON.parse(body).items;
for(var content in data)
{
VideoIds[idx]=(data[content].id.videoId);
idx++;
}
console.log(VideoIds.length);
console.log(VideoIds[0]);
console.log(typeof(VideoIds[1]));
//다운로드 부분
var fs = require('fs');
var youtubedl = require('youtube-dl');
var video = youtubedl('http://www.youtube.com/watch?v='.concat(VideoIds[1]));
video.on('info',function(info)
{
console.log('Download started');
console.log('filename : '+ info.filename);
console.log('size : '+info.size);
});
video.pipe(fs.createWriteStream('justlikethat.mp4'));
});