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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
package server.cache;
import haxe.io.Path;
import js.node.child_process.ChildProcess as ChildProcessObject;
import sys.FileSystem;
import utils.YoutubeUtils;
import ytdlp_nodejs.VideoFormat;
import ytdlp_nodejs.VideoInfo;
class YoutubeCache {
final main:Main;
final cache:Cache;
final ytdlp:YtdlpCache;
public function new(main:Main, cache:Cache, ytdlp:YtdlpCache):Void {
this.main = main;
this.cache = cache;
this.ytdlp = ytdlp;
}
public function cacheYoutubeVideo(client:Client, url:String, cb:CacheCallbacks) {
if (!cache.isYtReady) {
trace("Do `npm i https://github.com/RblSb/ytdlp-nodejs` to use cache feature (you also need to install `ffmpeg` to build mp4 from downloaded audio/video tracks).");
return;
}
final clientName = client.name;
final videoId = YoutubeUtils.extractVideoId(url);
if (videoId == "") {
log(clientName, 'Error: youtube video id not found in url: $url');
return;
}
// to prevent playlist in url handlings
url = 'https://youtu.be/$videoId';
final outName = videoId + ".mp4";
if (cache.exists(outName)) {
cb.onResolved(cache.getFileUrl(outName));
cb.onComplete();
return;
}
final inVideoName = '__tmp-video-$videoId';
inline function removeInputFiles():Void {
ytdlp.cleanInputFiles(inVideoName);
}
inline function checkEnoughSpace(contentLength:Int):Bool {
final hasSpace = cache.removeOlderCache(contentLength + cache.freeSpaceBlock);
if (!hasSpace) {
removeInputFiles();
cb.onError();
log(clientName, cache.notEnoughSpaceErrorText);
}
return hasSpace;
}
if (cache.isFileExists(inVideoName)) {
log(clientName, 'Caching $outName already in progress');
return;
}
trace('Caching $url to $outName...');
cb.onResolved(cache.getFileUrl(outName));
var useCookies = false;
var lastSentRatio = 0.0;
var canceled = false;
var process:Null<ChildProcessObject> = null;
if (cb.registerCancel != null) cb.registerCancel(() -> {
canceled = true;
process?.kill();
removeInputFiles();
});
function onGetInfo(info:VideoInfo):Void {
if (canceled) return;
if (cb.onMetadata != null) cb.onMetadata(info.title, info.duration);
trace('Get info with ${info.formats.length} formats');
var aformats = info.formats.filter(f -> f.acodec != "none"
&& f.vcodec == "none" && f.format_note?.contains("original"));
if (aformats.length == 0) {
aformats = info.formats.filter(f -> f.acodec != "none" && f.vcodec == "none");
}
if (aformats.length == 0) {
aformats = info.formats.filter(f -> f.acodec != "none");
}
aformats.sort((a, b) -> (a?.filesize ?? 0) < (b?.filesize ?? 0) ? 1 : -1);
final audioFormat:VideoFormat = aformats[0] ?? {
log(clientName, "Error: format with audio not found");
for (format in aformats) trace(format);
return;
}
final vformats = info.formats.filter(f -> {
if (f.vcodec == "none") return false;
return f.width != null && f.height != null;
});
vformats.sort((a, b) -> (a?.filesize ?? 0) < (b?.filesize ?? 0) ? 1 : -1);
var videoFormat = getBestYoutubeVideoFormat(vformats) ?? {
log(clientName, "Error: video format not found");
for (format in vformats) trace(format);
return;
}
inline function getTotalFormatsSize():Int {
final videoSize:Int = cast(videoFormat.filesize ?? 0);
final audioSize:Int = cast(audioFormat.filesize ?? 0);
return videoSize + audioSize;
}
// check if we have space for formats and video build
final ignoreQualities:Array<Int> = [];
for (i in 0...3) {
final hasSpace = cache.removeOlderCache(getTotalFormatsSize() * 2
+ cache.freeSpaceBlock);
if (hasSpace) break;
// try fallback to worse video quality
ignoreQualities.push(videoFormatResolution(videoFormat));
videoFormat = getBestYoutubeVideoFormat(vformats, ignoreQualities) ?? break;
}
if (!checkEnoughSpace(getTotalFormatsSize() * 2)) return;
final isMuxed = videoFormat.format_id == audioFormat.format_id;
final formatIds = if (isMuxed) {
videoFormat.format_id;
} else {
'${videoFormat.format_id}+${audioFormat.format_id}';
}
final totalSize = getTotalFormatsSize().limitMin(10);
var videoSizeRatio = (videoFormat.filesize ?? 0).limitMin(8) / totalSize;
var audioSizeRatio = (audioFormat.filesize ?? 0).limitMin(2) / totalSize;
if (isMuxed) {
videoSizeRatio = 1;
audioSizeRatio = 0;
}
trace(formatIds, toMibString(totalSize), videoSizeRatio.toFixed(), audioSizeRatio.toFixed());
var videoRatioCache = 0.0;
var audioRatioCache = 0.0;
process = ytdlp.download(url, {
format: formatIds,
output: '${cache.cacheDir}/$inVideoName',
remuxVideo: "mp4",
additionalOptions: ytdlp.ytExtraOptions(),
cookies: useCookies ? ytdlp.getCookiesPathOrNull() : null,
forceIpv4: true,
socketTimeout: 2,
extractorRetries: 0,
}, {
onProgress: p -> {
final isFinished = p.status == "finished";
if (isFinished) {
final filename = Path.withoutDirectory(p.filename);
trace('$filename format file downloaded');
}
var ratio = if (isFinished) {
1;
} else {
(p.downloaded / p.total).clamp(0, 1);
}
final isVideo = p.filename.contains('f${videoFormat.format_id}');
if (isVideo) videoRatioCache = ratio;
else audioRatioCache = ratio;
ratio = videoRatioCache * videoSizeRatio + audioRatioCache * audioSizeRatio;
if (canceled) return;
if (ratio - lastSentRatio < 0.01 && ratio < 1) return;
lastSentRatio = ratio;
cb.onProgress(ratio.toFixed(4));
},
onComplete: () -> {
if (canceled) {
removeInputFiles();
return;
}
final name = cache.findFile(n -> n.startsWith(inVideoName)) ?? {
final err = 'Error: cannot find downloaded file with prefix $inVideoName';
cache.logWithAdmins(client, err);
cb.onError();
return;
};
FileSystem.rename('${cache.cacheDir}/$name', '${cache.cacheDir}/$outName');
removeInputFiles();
cache.add(outName);
cb.onComplete();
},
onError: err -> {
if (canceled) return;
final err = "Error during video download: " + err;
cache.logWithAdmins(client, err);
removeInputFiles();
cb.onError();
}
});
}
ytdlp.getInfoAsync(url, useCookies).then(onGetInfo).catchError(err -> {
trace(err);
useCookies = true;
ytdlp.getInfoAsync(url, useCookies).then(onGetInfo).catchError(err -> {
removeInputFiles();
cb.onError();
log(clientName, "" + err);
});
});
}
function getBestYoutubeVideoFormat(formats:Array<VideoFormat>, ?ignoreQualities:Array<Int>):Null<VideoFormat> {
final qPriority = [1080, 720, 480, 360, 240, 144];
if (ignoreQualities != null) {
for (q in ignoreQualities) qPriority.remove(q);
}
final format60 = findVideoFormat(formats, qPriority, true);
return format60 ?? findVideoFormat(formats, qPriority, false);
}
function findVideoFormat(formats:Array<VideoFormat>, qPriority:Array<Int>, is60fps:Bool):Null<VideoFormat> {
for (q in qPriority) {
final quality = '${q}p' + (is60fps ? "60" : "");
for (format in formats) {
final min = videoFormatResolution(format);
if (min > q) continue;
final format_note = formatVideoQuality(format);
if (format_note == quality) return format;
}
}
return null;
}
function videoFormatResolution(format:VideoFormat):Int {
final min = Math.min(format.width, format.height);
return Std.int(min);
}
function formatVideoQuality(format:VideoFormat):Null<String> {
final resolution = videoFormatResolution(format);
// when there is 720p and 720p60 formats
return format.format_note ?? '${resolution}p';
}
inline function toMibString(bytes:Int):String {
return '${(bytes / 1024 / 1024).toFixed()} MiB';
}
function log(clientName:String, msg:String):Void {
cache.logByName(clientName, msg);
}
}
|