-
Notifications
You must be signed in to change notification settings - Fork 0
/
youtube.js
95 lines (70 loc) · 2.52 KB
/
youtube.js
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* Created by I327247 on 15/04/2017.
*/
const fs = require('fs');
const youtubedl = require('youtube-dl');
const yss = require('youtube-simple-search');
const request = require('request')
const config = require('./config.json');
const express = require('express');
function searchSong (searchString, callback) {
let urlRoot = 'https://www.youtube.com/watch?v=';
yss({
key: config.youtube.apiKey,
query: searchString,
maxResults: 10
},
getVideoUrl
);
function getVideoUrl(results) {
//console.log(result);
for (let result of results) {
//console.log(result);
if(result.id.kind === "youtube#video") {
var url = urlRoot + result.id.videoId;
console.log(url)
getYoutubeAudioURL(url, callback);
break;
}
}
}
}
function getYoutubeAudioURL(youtubeUrl, callback) {
//const options = ['--format=bestaudio/best', '--extract-audio', '--skip-download', '--get-url']
const options = ['-x', '--format=bestaudio/best', '--audio-format=mp3', '--skip-download'];
// Optional arguments passed to youtube-dl.
//var options = ['--username=user', '--password=hunter2'];
youtubedl.getInfo(youtubeUrl, options, function(err, info) {
if (err) throw err;
//console.log(info)
console.log('Download started');
console.log('filename: ' + info._filename);
console.log('size: ' + info.size);
//const download_base_url = "https://www.youtubeinmp3.com/fetch/?format=JSON&video=https://www.youtube.com/watch?v="
const download_base_url = "https://www.youtube.com/watch?v=";
// console.log(download_base_url+info.id)
request(download_base_url+info.id, function(err, response, body){
if (err) throw err;
//body_json = JSON.parse(body)
let results = null;
results = {
songName: info.title,
//donwloadUrl: body_json.link+'&v='+info.id,
donwloadUrl: youtubeUrl,
youtubeUrl: youtubeUrl,
streamUrl: info.formats[0].url,
imageUrl: info.thumbnails[0].url
};
callback(results)
});
});
}
// searchSong("Essengo", function(results) {
// console.log(results);
// });
function getYoutubeToMp3(youtubeID) {
}
module.exports = {
searchSong,
getYoutubeAudioURL,
};