forked from fent/node-ytdl-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
191 lines (164 loc) · 4.8 KB
/
index.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
const PassThrough = require('stream').PassThrough;
const getInfo = require('./info');
const util = require('./util');
const cache = require('./cache');
const request = require('miniget');
const m3u8stream = require('m3u8stream');
/**
* @param {String} link
* @param {!Object} options
* @return {ReadableStream}
*/
var ytdl = module.exports = function ytdl(link, options) {
var stream = createStream(options);
getInfo(link, options, function(err, info) {
if (err) {
stream.emit('error', err);
return;
}
downloadFromInfoCallback(stream, info, options);
});
return stream;
};
ytdl.getInfo = getInfo;
ytdl.chooseFormat = util.chooseFormat;
ytdl.filterFormats = util.filterFormats;
ytdl.validateId = util.validateLink;
ytdl.cache = cache;
function createStream(options) {
var stream = new PassThrough({
highWaterMark: options && options.highWaterMark || null,
});
stream.destroy = function() { stream._isDestroyed = true; };
return stream;
}
/**
* Chooses a format to download.
*
* @param {stream.Readable} stream
* @param {Object} info
* @param {Object} options
*/
function downloadFromInfoCallback(stream, info, options) {
options = options || {};
var format = util.chooseFormat(info.formats, options);
if (format instanceof Error) {
// The caller expects this function to be async.
setImmediate(function() {
stream.emit('error', format);
});
return;
}
stream.emit('info', info, format);
if (stream._isDestroyed) { return; }
var url = format.url;
if (format.live) {
var req = m3u8stream(url, {
chunkReadahead: +info.live_chunk_readahead,
requestOptions: options.requestOptions,
});
req.on('error', stream.emit.bind(stream, 'error'));
stream.destroy = req.end.bind(req);
req.pipe(stream);
} else {
if (options.begin) {
url += '&begin=' + util.fromHumanTime(options.begin);
}
doDownload(stream, url, options, {
trys: options.retries || 5,
range: {
start: options.range && options.range.start ? options.range.start : 0,
end: options.range && options.range.end ? options.range.end : -1,
},
downloaded: 0,
});
}
}
/**
* Tries to download the video. If the download stops halfway, attempts to
* reconnect, continuing the download where it left off.
*
* @param {stream.Readable} stream
* @param {String} url
* @param {Object} options
* @param {Object} reconnectInfo
*/
function doDownload(stream, url, options, reconnectInfo) {
if (reconnectInfo.trys === 0) {
stream.emit('error', new Error('Too many reconnects'));
return;
}
// Start downloading the video.
var rangedUrl = url;
if (reconnectInfo.downloaded !== 0 ||
reconnectInfo.range.start !== 0 ||
reconnectInfo.range.end !== -1) {
var start = reconnectInfo.range.start + reconnectInfo.downloaded;
var end = reconnectInfo.range.end != -1 ? reconnectInfo.range.end : '';
rangedUrl += '&range=' + start + '-' + end;
}
var req = request(rangedUrl, options.requestOptions);
function cleanup() {
req.removeListener('end', onend);
req.removeListener('data', ondata);
req.unpipe();
}
var myres = false;
stream.destroy = function() {
stream._isDestroyed = true;
req.abort();
cleanup();
};
// Forward events from the request to the stream.
['abort', 'request', 'response'].forEach(function(event) {
req.on(event, function(arg) {
stream.emit.call(stream, event, arg);
});
});
function onend() {
cleanup();
// Reconnect if there is more to be downloaded.
if (reconnectInfo.downloaded < reconnectInfo.total) {
reconnectInfo.trys = reconnectInfo.trys - 1;
doDownload(stream, url, options, reconnectInfo);
} else {
stream.end();
}
}
req.on('error', function(err) {
if (stream._isDestroyed || !myres) {
stream.emit('error', err);
} else {
onend();
}
});
req.on('response', function(res) {
myres = true;
if (stream._isDestroyed) { return; }
if (reconnectInfo.downloaded === 0) {
reconnectInfo.total = parseInt(res.headers['content-length']);
}
});
function ondata(chunk) {
var downloaded = reconnectInfo.downloaded += chunk.length;
stream.emit('progress', chunk.length, downloaded, reconnectInfo.total);
}
req.on('data', ondata);
req.on('end', onend);
req.pipe(stream, { end: false });
}
/**
* Can be used to download video after its `info` is gotten through
* `ytdl.getInfo()`. In case the user might want to look at the
* `info` object before deciding to download.
*
* @param {Object} info
* @param {!Object} options
*/
ytdl.downloadFromInfo = function(info, options) {
var stream = createStream(options);
setImmediate(function() {
downloadFromInfoCallback(stream, info, options);
});
return stream;
};