aboutsummaryrefslogtreecommitdiffstats
path: root/src/server/cache
diff options
context:
space:
mode:
Diffstat (limited to 'src/server/cache')
-rw-r--r--src/server/cache/Cache.hx39
-rw-r--r--src/server/cache/CacheCallbacks.hx15
-rw-r--r--src/server/cache/RawCache.hx158
-rw-r--r--src/server/cache/YoutubeCache.hx156
-rw-r--r--src/server/cache/YtdlpCache.hx227
5 files changed, 404 insertions, 191 deletions
diff --git a/src/server/cache/Cache.hx b/src/server/cache/Cache.hx
index cfe2f66..f0fe741 100644
--- a/src/server/cache/Cache.hx
+++ b/src/server/cache/Cache.hx
@@ -18,6 +18,7 @@ class Cache {
public final freeSpaceBlock = 10 * 1024 * 1024; // 10MB
final cachedFiles:Array<String> = [];
+ var ytdlpCache:YtdlpCache;
var youtubeCache:YoutubeCache;
var rawCache:RawCache;
@@ -25,12 +26,13 @@ class Cache {
this.main = main;
this.cacheDir = cacheDir;
Utils.ensureDir(cacheDir);
- youtubeCache = new YoutubeCache(main, this);
+ ytdlpCache = new YtdlpCache(main, this);
+ youtubeCache = new YoutubeCache(main, this, ytdlpCache);
rawCache = new RawCache(main, this);
- isYtReady = youtubeCache.checkYtDeps();
+ isYtReady = ytdlpCache.checkYtDeps();
if (isYtReady) {
- youtubeCache.cleanYtInputFiles();
- youtubeCache.checkUpdate();
+ ytdlpCache.cleanInputFiles();
+ ytdlpCache.checkUpdate();
}
}
@@ -75,12 +77,31 @@ class Cache {
}
}
- public function cacheYoutubeVideo(client:Client, url:String, callback:(name:String) -> Void) {
- youtubeCache.cacheYoutubeVideo(client, url, callback);
+ public function cacheYoutubeVideo(client:Client, url:String, callbacks:CacheCallbacks) {
+ youtubeCache.cacheYoutubeVideo(client, url, callbacks);
}
- public function cacheRawVideo(client:Client, url:String, callback:(name:String) -> Void) {
- rawCache.cacheRawVideo(client, url, callback);
+ /** Site-specific extractor download via yt-dlp, no fallback. **/
+ public function cacheYtdlpVideo(client:Client, url:String, callbacks:CacheCallbacks) {
+ if (!isYtReady) {
+ rawCache.cacheRawVideo(client, url, callbacks);
+ return;
+ }
+ ytdlpCache.cacheVideo(client, url, callbacks);
+ }
+
+ /**
+ use yt-dlp if a site-specific extractor supports the url, otherwise fallback to raw download.
+ **/
+ public function cacheRawVideo(client:Client, url:String, callbacks:CacheCallbacks) {
+ if (!isYtReady) {
+ rawCache.cacheRawVideo(client, url, callbacks);
+ return;
+ }
+ ytdlpCache.checkUrlSupported(url, supported -> {
+ if (supported) ytdlpCache.cacheVideo(client, url, callbacks);
+ else rawCache.cacheRawVideo(client, url, callbacks);
+ });
}
public function setStorageLimit(bytes:Int) {
@@ -105,7 +126,7 @@ class Cache {
callback(storageLimit);
return;
}
- statfs("/", (err, stats) -> {
+ statfs(cacheDir, (err, stats) -> {
if (err != null) {
trace(err);
callback(storageLimit);
diff --git a/src/server/cache/CacheCallbacks.hx b/src/server/cache/CacheCallbacks.hx
new file mode 100644
index 0000000..28ced3c
--- /dev/null
+++ b/src/server/cache/CacheCallbacks.hx
@@ -0,0 +1,15 @@
+package server.cache;
+
+typedef CacheCallbacks = {
+ /** Called once the final cache url is known, before download starts. **/
+ final onResolved:(url:String) -> Void;
+ final onProgress:(ratio:Float) -> Void;
+ final onComplete:() -> Void;
+ final onError:() -> Void;
+ final ?registerCancel:(cancel:() -> Void) -> Void;
+ /**
+ Called when the real title/duration become known (for items added
+ with a zero duration, e.g. when the client could not resolve them).
+ **/
+ final ?onMetadata:(title:Null<String>, duration:Float) -> Void;
+}
diff --git a/src/server/cache/RawCache.hx b/src/server/cache/RawCache.hx
index 1fb251d..3505119 100644
--- a/src/server/cache/RawCache.hx
+++ b/src/server/cache/RawCache.hx
@@ -27,7 +27,7 @@ class RawCache {
this.cache = cache;
}
- public function cacheRawVideo(client:Client, url:String, callback:(name:String) -> Void) {
+ public function cacheRawVideo(client:Client, url:String, cb:CacheCallbacks) {
final isM3U8 = url.contains(".m3u8");
final ext = isM3U8 ? "m3u8" : "mp4";
@@ -39,71 +39,72 @@ class RawCache {
outName = cache.getFreeFileName(outName);
if (cache.exists(outName)) {
- callback(outName);
+ cb.onResolved(cache.getFileUrl(outName));
+ cb.onComplete();
return;
}
trace('Caching $url to $outName...');
- main.send(client, {
- type: Progress,
- progress: {
- type: Caching,
- ratio: 0,
- data: outName
- }
- });
+ cb.onResolved(cache.getFileUrl(outName));
if (isM3U8) {
- handleM3u8(client, url, outName, callback);
+ handleM3u8(client, url, outName, cb);
} else {
- handleMp4(client, url, outName, callback);
+ handleMp4(client, url, outName, cb);
}
}
- function handleMp4(client:Client, url:String, outName:String, callback:(name:String) -> Void) {
+ function handleMp4(client:Client, url:String, outName:String, cb:CacheCallbacks) {
final clientName = client.name;
+ var lastSentRatio = 0.0;
+ var canceled = false;
downloadFile(client, url, outName, (downloaded, total) -> {
- main.sendByName(clientName, {
- type: Progress,
- progress: {
- type: Downloading,
- ratio: (downloaded / total).clamp(0, 1).toFixed(4)
- }
- });
+ if (canceled) return;
+ final ratio = (downloaded / total).clamp(0, 1);
+ if (ratio - lastSentRatio < 0.01 && ratio < 1) return;
+ lastSentRatio = ratio;
+ cb.onProgress(ratio.toFixed(4));
}, () -> {
- cache.add(outName);
- callback(outName);
+ if (canceled) return;
+ probeDuration(outName, duration -> {
+ if (canceled) return;
+ if (duration > 0 && cb.onMetadata != null) cb.onMetadata(null, duration);
+ cache.add(outName);
+ cb.onComplete();
+ });
}, (err) -> {
+ if (canceled) return;
log(clientName, 'Mp4 download failed: $err');
- cancelProgress(clientName);
+ cb.onError();
+ }, (cancel) -> {
+ if (cb.registerCancel == null) return;
+ cb.registerCancel(() -> {
+ canceled = true;
+ cancel();
+ });
});
}
- function handleM3u8(client:Client, url:String, outName:String, callback:(name:String) -> Void):Void {
+ function handleM3u8(client:Client, url:String, outName:String, cb:CacheCallbacks):Void {
final clientName = client.name;
final useProxy = true;
+ var canceled = false;
+ if (cb.registerCancel != null) cb.registerCancel(() -> canceled = true);
downloadM3u8Playlist(client, url, useProxy, (playlist, totalSize, segments) -> {
+ if (canceled) return;
// only playlist file donwloaded
if (useProxy) totalSize = playlist.length;
if (!cache.removeOlderCache(totalSize + cache.freeSpaceBlock)) {
log(clientName, cache.notEnoughSpaceErrorText);
- cancelProgress(clientName);
+ cb.onError();
return;
}
if (useProxy) {
- main.sendByName(clientName, {
- type: Progress,
- progress: {
- type: Caching,
- ratio: 1,
- data: outName
- }
- });
File.saveContent('${cache.cacheDir}/$outName', playlist);
cache.add(outName);
- callback(outName);
+ cb.onComplete();
return;
}
@@ -128,25 +129,19 @@ class RawCache {
downloaded++;
final progress = downloaded / segments.length;
- main.sendByName(clientName, {
- type: Progress,
- progress: {
- type: Downloading,
- ratio: progress.clamp(0, 1)
- }
- });
+ cb.onProgress(progress.clamp(0, 1));
if (downloaded == segments.length) {
trace('All ${downloaded}/${segments.length} segments downloaded');
File.saveContent('${cache.cacheDir}/$outName', playlist);
cache.add(outName);
- callback(outName);
+ cb.onComplete();
// buildTsFiles(
// segments.map(item -> item.name),
// outName,
// client,
- // callback
+ // cb
// );
} else {
downloadNextBatch();
@@ -157,7 +152,7 @@ class RawCache {
activeDownloads--;
downloaded++;
log(clientName, 'TS segment ${segment.i} download failed: $err');
- cancelProgress(clientName);
+ cb.onError();
cleanupFiles(segments.map(item -> item.name));
}
);
@@ -168,7 +163,7 @@ class RawCache {
downloadNextBatch();
}, (err) -> {
log(clientName, 'M3U8 processing failed: $err');
- cancelProgress(clientName);
+ cb.onError();
});
}
@@ -247,6 +242,7 @@ class RawCache {
// Head request can return full stream size, so lets do loose assumption
final req = request(segments[0].url, {method: Get}, (res:IncomingMessage) -> {
final contentLength = Std.parseInt(res.headers["content-length"]) ?? 0;
+ res.destroy();
final totalSize = contentLength * (segments.length + 1);
if (totalSize == 0) {
onError("Failed to get segment sizes: no content-length");
@@ -272,7 +268,8 @@ class RawCache {
client:Client, url:String, fileName:String,
onProgress:(downloaded:Int, total:Int) -> Void,
onComplete:() -> Void,
- onError:(err:String) -> Void
+ onError:(err:String) -> Void,
+ ?onCancelable:(cancel:() -> Void) -> Void
):Void {
final outPath = '${cache.cacheDir}/$fileName';
final file = Fs.createWriteStream(outPath);
@@ -325,16 +322,22 @@ class RawCache {
onError('Request failed: $err');
});
+ if (onCancelable != null) onCancelable(() -> {
+ req.destroy();
+ file.destroy();
+ if (FileSystem.exists(outPath)) FileSystem.deleteFile(outPath);
+ });
+
req.end();
}
- function buildTsFiles(tempFiles:Array<String>, outName:String, client:Client, callback:String->Void) {
+ function buildTsFiles(tempFiles:Array<String>, outName:String, client:Client, cb:CacheCallbacks) {
final clientName = client.name;
final missingFiles = tempFiles.filter(f ->
!FileSystem.exists('${cache.cacheDir}/$f'));
if (missingFiles.length > 0) {
log(clientName, 'Concatenation failed: ${missingFiles.length} segments are missing');
- cancelProgress(clientName);
+ cb.onError();
cleanupFiles(tempFiles);
return;
}
@@ -376,7 +379,7 @@ class RawCache {
final timeoutId = js.Node.setTimeout(() -> {
process.kill();
log(clientName, 'FFmpeg process timed out after ${timeout / 1000} seconds');
- cancelProgress(clientName);
+ cb.onError();
cleanupFiles(tempFiles.concat([concatFile]));
}, timeout);
@@ -393,28 +396,16 @@ class RawCache {
final admins = main.clients.filter(client -> client.isAdmin);
for (admin in admins) main.serverMessage(admin, ffmpegErr);
- main.send(client, {
- type: Progress,
- progress: {
- type: Canceled,
- ratio: 1
- }
- });
+ cb.onError();
} else {
// Verify the output file exists and has content
if (FileSystem.exists('${cache.cacheDir}/$outName')
&& FileSystem.stat('${cache.cacheDir}/$outName').size > 0) {
cache.add(outName);
- callback(outName);
+ cb.onComplete();
} else {
log(clientName, 'FFmpeg process completed but output file is missing or empty');
- main.send(client, {
- type: Progress,
- progress: {
- type: Canceled,
- ratio: 1
- }
- });
+ cb.onError();
}
}
@@ -426,34 +417,37 @@ class RawCache {
process.on("error", (err) -> {
js.Node.clearTimeout(timeoutId);
log(clientName, 'Failed to start FFmpeg: $err');
- main.send(client, {
- type: Progress,
- progress: {
- type: Canceled,
- ratio: 1
- }
- });
+ cb.onError();
cleanupFiles(tempFiles.concat([concatFile]));
});
}
+ function probeDuration(fileName:String, callback:(duration:Float) -> Void):Void {
+ final path = '${cache.cacheDir}/$fileName';
+ final args = [
+ "-v", "error",
+ "-show_entries", "format=duration",
+ "-of", "default=noprint_wrappers=1:nokey=1",
+ path
+ ];
+ ChildProcess.execFile("ffprobe", args, (err, stdout, _) -> {
+ if (err != null) {
+ callback(0);
+ return;
+ }
+ final duration = Std.parseFloat(Std.string(stdout).trim());
+ callback(Math.isNaN(duration) ? 0 : duration);
+ });
+ }
+
function cleanupFiles(files:Array<String>):Void {
for (file in files) {
- if (FileSystem.exists(file)) FileSystem.deleteFile(file);
+ final path = '${cache.cacheDir}/$file';
+ if (FileSystem.exists(path)) FileSystem.deleteFile(path);
}
}
function log(clientName:String, msg:String):Void {
cache.logByName(clientName, msg);
}
-
- function cancelProgress(clientName:String):Void {
- main.sendByName(clientName, {
- type: Progress,
- progress: {
- type: Canceled,
- ratio: 0
- }
- });
- }
}
diff --git a/src/server/cache/YoutubeCache.hx b/src/server/cache/YoutubeCache.hx
index 09ab67b..50bd37c 100644
--- a/src/server/cache/YoutubeCache.hx
+++ b/src/server/cache/YoutubeCache.hx
@@ -1,54 +1,24 @@
package server.cache;
import haxe.io.Path;
-import js.lib.Promise;
-import js.node.ChildProcess;
+import js.node.child_process.ChildProcess as ChildProcessObject;
import sys.FileSystem;
import utils.YoutubeUtils;
import ytdlp_nodejs.VideoFormat;
import ytdlp_nodejs.VideoInfo;
-import ytdlp_nodejs.YtDlp;
class YoutubeCache {
final main:Main;
final cache:Cache;
- var ytDlp:Null<YtDlp>;
+ final ytdlp:YtdlpCache;
- public function new(main:Main, cache:Cache):Void {
+ public function new(main:Main, cache:Cache, ytdlp:YtdlpCache):Void {
this.main = main;
this.cache = cache;
+ this.ytdlp = ytdlp;
}
- public function checkYtDeps():Bool {
- try {
- ChildProcess.execSync("ffmpeg -version", {stdio: "ignore", timeout: 5000});
- ytDlp = js.Syntax.code("new (require('ytdlp-nodejs')).YtDlp()");
- return true;
- } catch (e) {
- return false;
- }
- }
-
- public function checkUpdate():Void {
- ytDlp.execAsync("", {
- updateTo: main.config.ytDlp.channel,
- onData: d -> {
- trace(d);
- }
- }).catchError(e -> {
- trace(e);
- });
- }
-
- public function cleanYtInputFiles(prefix = "__tmp"):Void {
- final names = FileSystem.readDirectory(cache.cacheDir);
- for (name in names) {
- if (!name.startsWith(prefix)) continue;
- cache.remove(name);
- }
- }
-
- public function cacheYoutubeVideo(client:Client, url:String, callback:(name:String) -> Void) {
+ 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;
@@ -59,20 +29,23 @@ class YoutubeCache {
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)) {
- callback(outName);
+ cb.onResolved(cache.getFileUrl(outName));
+ cb.onComplete();
return;
}
final inVideoName = '__tmp-video-$videoId';
inline function removeInputFiles():Void {
- cleanYtInputFiles(inVideoName);
+ ytdlp.cleanInputFiles(inVideoName);
}
inline function checkEnoughSpace(contentLength:Int):Bool {
final hasSpace = cache.removeOlderCache(contentLength + cache.freeSpaceBlock);
if (!hasSpace) {
removeInputFiles();
- cancelProgress(clientName);
+ cb.onError();
log(clientName, cache.notEnoughSpaceErrorText);
}
return hasSpace;
@@ -83,18 +56,21 @@ class YoutubeCache {
return;
}
trace('Caching $url to $outName...');
- main.sendByName(clientName, {
- type: Progress,
- progress: {
- type: Caching,
- ratio: 0,
- data: 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"));
@@ -154,16 +130,16 @@ class YoutubeCache {
var videoRatioCache = 0.0;
var audioRatioCache = 0.0;
- final dlVideo:Promise<String> = ytDlp.downloadAsync(url, {
+ process = ytdlp.download(url, {
format: formatIds,
output: '${cache.cacheDir}/$inVideoName',
remuxVideo: "mp4",
- additionalOptions: ["--no-js-runtimes", "--js-runtimes", main.config.ytDlp.jsRuntime],
- // verbose: true,
- cookies: useCookies ? getCookiesPathOrNull() : null,
+ additionalOptions: ytdlp.ytExtraOptions(),
+ cookies: useCookies ? ytdlp.getCookiesPathOrNull() : null,
forceIpv4: true,
socketTimeout: 2,
extractorRetries: 0,
+ }, {
onProgress: p -> {
final isFinished = p.status == "finished";
if (isFinished) {
@@ -182,58 +158,48 @@ class YoutubeCache {
ratio = videoRatioCache * videoSizeRatio + audioRatioCache * audioSizeRatio;
- main.sendByName(clientName, {
- type: Progress,
- progress: {
- type: Downloading,
- ratio: ratio.toFixed(4)
- }
- });
- }
- }).catchError(err -> {
- final err = "Error during video download: " + err;
- cache.logWithAdmins(client, err);
- removeInputFiles();
- cancelProgress(clientName);
- });
-
- dlVideo.then((v) -> {
- final name = cache.findFile(n -> n.startsWith(inVideoName)) ?? {
- final err = 'Error: cannot find downloaded file with prefix $inVideoName';
+ 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);
- cancelProgress(clientName);
- return;
- };
- FileSystem.rename('${cache.cacheDir}/$name', '${cache.cacheDir}/$outName');
- removeInputFiles();
- cache.add(outName);
- callback(outName);
+ removeInputFiles();
+ cb.onError();
+ }
});
}
- getInfoAsync(url, useCookies).then(onGetInfo).catchError(err -> {
+ ytdlp.getInfoAsync(url, useCookies).then(onGetInfo).catchError(err -> {
trace(err);
useCookies = true;
- getInfoAsync(url, useCookies).then(onGetInfo).catchError(err -> {
+ ytdlp.getInfoAsync(url, useCookies).then(onGetInfo).catchError(err -> {
removeInputFiles();
- cancelProgress(clientName);
+ cb.onError();
log(clientName, "" + err);
});
});
}
- function getInfoAsync(url:String, useCookies = false):Promise<VideoInfo> {
- return cast ytDlp.getInfoAsync(url, cast {
- cookies: useCookies ? getCookiesPathOrNull() : null,
- additionalOptions: ["--no-js-runtimes", "--js-runtimes", main.config.ytDlp.jsRuntime],
- });
- }
-
- function getCookiesPathOrNull():Null<String> {
- final cookiesPath = '${main.userDir}/cookies.txt';
- return FileSystem.exists(cookiesPath) ? cookiesPath : null;
- }
-
function getBestYoutubeVideoFormat(formats:Array<VideoFormat>, ?ignoreQualities:Array<Int>):Null<VideoFormat> {
final qPriority = [1080, 720, 480, 360, 240, 144];
if (ignoreQualities != null) {
@@ -274,14 +240,4 @@ class YoutubeCache {
function log(clientName:String, msg:String):Void {
cache.logByName(clientName, msg);
}
-
- function cancelProgress(clientName:String):Void {
- main.sendByName(clientName, {
- type: Progress,
- progress: {
- type: Canceled,
- ratio: 0
- }
- });
- }
}
diff --git a/src/server/cache/YtdlpCache.hx b/src/server/cache/YtdlpCache.hx
new file mode 100644
index 0000000..31df7b2
--- /dev/null
+++ b/src/server/cache/YtdlpCache.hx
@@ -0,0 +1,227 @@
+package server.cache;
+
+import haxe.io.Path;
+import js.lib.Promise;
+import js.node.ChildProcess;
+import js.node.child_process.ChildProcess as ChildProcessObject;
+import sys.FileSystem;
+import ytdlp_nodejs.ArgsOptions;
+import ytdlp_nodejs.VideoInfo;
+import ytdlp_nodejs.VideoProgress;
+import ytdlp_nodejs.YtDlp;
+
+typedef YtdlpHandlers = {
+ final onProgress:(p:VideoProgress) -> Void;
+ final onComplete:() -> Void;
+ final onError:(err:String) -> Void;
+}
+
+class YtdlpCache {
+ final main:Main;
+ final cache:Cache;
+ var ytDlp:Null<YtDlp>;
+
+ public function new(main:Main, cache:Cache):Void {
+ this.main = main;
+ this.cache = cache;
+ }
+
+ public function checkYtDeps():Bool {
+ try {
+ ChildProcess.execSync("ffmpeg -version", {stdio: "ignore", timeout: 5000});
+ ytDlp = js.Syntax.code("new (require('ytdlp-nodejs')).YtDlp()");
+ return true;
+ } catch (e) {
+ return false;
+ }
+ }
+
+ public function checkUpdate():Void {
+ ytDlp.execAsync("", {
+ updateTo: main.config.ytDlp.channel,
+ onData: d -> {
+ trace(d);
+ }
+ }).catchError(e -> {
+ trace(e);
+ });
+ }
+
+ public function cleanInputFiles(prefix = "__tmp"):Void {
+ final names = FileSystem.readDirectory(cache.cacheDir);
+ for (name in names) {
+ if (!name.startsWith(prefix)) continue;
+ cache.remove(name);
+ }
+ }
+
+ public function getInfoAsync(url:String, useCookies = false):Promise<VideoInfo> {
+ return cast ytDlp.getInfoAsync(url, cast {
+ cookies: useCookies ? getCookiesPathOrNull() : null,
+ additionalOptions: ytExtraOptions(),
+ });
+ }
+
+ public function getCookiesPathOrNull():Null<String> {
+ final cookiesPath = '${main.userDir}/cookies.txt';
+ return FileSystem.exists(cookiesPath) ? cookiesPath : null;
+ }
+
+ public function ytExtraOptions():Array<String> {
+ return ["--no-js-runtimes", "--js-runtimes", main.config.ytDlp.jsRuntime];
+ }
+
+ /**
+ Runs `yt-dlp <url> --ies default,-generic` to check if a site-specific
+ extractor handles the url. Returns the process so it can be killed on cancel.
+ **/
+ public function checkUrlSupported(url:String, callback:(supported:Bool) -> Void):ChildProcessObject {
+ final process:ChildProcessObject = ytDlp.exec(url, {
+ useExtractors: ["default", "-generic"],
+ simulate: true,
+ quiet: true,
+ socketTimeout: 5,
+ extractorRetries: 0,
+ });
+ var done = false;
+ inline function finish(supported:Bool):Void {
+ if (done) return;
+ done = true;
+ callback(supported);
+ }
+ process.on("close", (code:Int, _signal:String) -> finish(code == 0));
+ process.on("error", (_err:Dynamic) -> finish(false));
+ return process;
+ }
+
+ /**
+ Spawns yt-dlp as a manual process and wires progress/completion to `handlers`.
+ Returns the process so the caller can kill it on cancel.
+ **/
+ public function download(url:String, options:ArgsOptions, handlers:YtdlpHandlers):ChildProcessObject {
+ final process:ChildProcessObject = ytDlp.exec(url, options);
+ var stderr = "";
+ var done = false;
+ process.stderr.on("data", (d:Dynamic) -> stderr += d);
+ process.on("progress", (p:VideoProgress) -> handlers.onProgress(p));
+ process.on("close", (code:Int, _signal:String) -> {
+ if (done) return;
+ done = true;
+ if (code == 0) handlers.onComplete();
+ else handlers.onError('yt-dlp exited with code $code: $stderr');
+ });
+ process.on("error", (err:Dynamic) -> {
+ if (done) return;
+ done = true;
+ handlers.onError('Failed to start yt-dlp process: $err');
+ });
+ return process;
+ }
+
+ /**
+ Generic best-quality download to mp4 for vk and other site-specific
+ extractors. Used after `checkUrlSupported` succeeds (or directly for vk).
+ **/
+ public function cacheVideo(client:Client, url:String, cb:CacheCallbacks):Void {
+ final clientName = client.name;
+ final outName = deriveOutName(url);
+ trace('Caching $url to $outName...');
+ cb.onResolved(cache.getFileUrl(outName));
+
+ final inName = '__tmp-${Std.random(0xFFFFFF)}-${Path.withoutExtension(outName)}';
+ var canceled = false;
+ var process:Null<ChildProcessObject> = null;
+ inline function cleanup():Void {
+ cleanInputFiles(inName);
+ }
+ if (cb.registerCancel != null) cb.registerCancel(() -> {
+ canceled = true;
+ process?.kill();
+ cleanup();
+ });
+
+ var useCookies = false;
+ var lastSentRatio = 0.0;
+
+ function onGetInfo(info:VideoInfo):Void {
+ if (canceled) return;
+ if (cb.onMetadata != null) cb.onMetadata(info.title, info.duration);
+ if (!cache.removeOlderCache(estimateSize(info) * 2 + cache.freeSpaceBlock)) {
+ cleanup();
+ cb.onError();
+ log(clientName, cache.notEnoughSpaceErrorText);
+ return;
+ }
+ process = download(url, {
+ output: '${cache.cacheDir}/$inName',
+ remuxVideo: "mp4",
+ additionalOptions: ytExtraOptions(),
+ cookies: useCookies ? getCookiesPathOrNull() : null,
+ forceIpv4: true,
+ socketTimeout: 5,
+ extractorRetries: 0,
+ }, {
+ onProgress: p -> {
+ if (canceled) return;
+ final ratio = p.status == "finished" ? 1.0 : (p.downloaded / p.total)
+ .clamp(0, 1);
+ if (ratio - lastSentRatio < 0.01 && ratio < 1) return;
+ lastSentRatio = ratio;
+ cb.onProgress(ratio.toFixed(4));
+ },
+ onComplete: () -> {
+ if (canceled) {
+ cleanup();
+ return;
+ }
+ final name = cache.findFile(n -> n.startsWith(inName)) ?? {
+ final err = 'Error: cannot find downloaded file with prefix $inName';
+ cache.logWithAdmins(client, err);
+ cb.onError();
+ return;
+ };
+ FileSystem.rename('${cache.cacheDir}/$name', '${cache.cacheDir}/$outName');
+ cleanup();
+ cache.add(outName);
+ cb.onComplete();
+ },
+ onError: err -> {
+ if (canceled) return;
+ cache.logWithAdmins(client, "Error during video download: " + err);
+ cleanup();
+ cb.onError();
+ }
+ });
+ }
+
+ getInfoAsync(url, useCookies).then(onGetInfo).catchError(err -> {
+ if (canceled) return;
+ useCookies = true;
+ getInfoAsync(url, useCookies).then(onGetInfo).catchError(err -> {
+ cleanup();
+ cb.onError();
+ log(clientName, "" + err);
+ });
+ });
+ }
+
+ function deriveOutName(url:String):String {
+ final decoded = try url.urlDecode() catch (e) url;
+ var last = decoded.substr(decoded.lastIndexOf("/") + 1);
+ final q = last.indexOf("?");
+ if (q != -1) last = last.substr(0, q);
+ last = Path.withoutExtension(last);
+ if (last == "") last = "video";
+ return cache.getFreeFileName('$last.mp4');
+ }
+
+ function estimateSize(info:VideoInfo):Int {
+ final d:Dynamic = info;
+ final size:Null<Float> = d.filesize_approx ?? d.filesize;
+ return Std.int(size ?? 0);
+ }
+
+ function log(clientName:String, msg:String):Void {
+ cache.logByName(clientName, msg);
+ }
+}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage