From 237a0413017867b70ff1487412710b14e7827a9d Mon Sep 17 00:00:00 2001 From: Yuzu Date: Thu, 20 Jun 2024 10:02:39 +0700 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20Make=20play-dl=20use=20cust?= =?UTF-8?q?om=20stream=20logic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/stream.ts | 67 ++++++++++++++++++++++++++++++++++++++------- src/utils/stream.ts | 2 +- 2 files changed, 58 insertions(+), 11 deletions(-) diff --git a/src/stream.ts b/src/stream.ts index dba0845..0049466 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -4,11 +4,40 @@ import YTDlpWrap from "yt-dlp-wrap"; import { Stream } from "./utils/stream"; const ytDlpWrap = new YTDlpWrap(); -export const stream = async (url: string): Promise => { - const playdlData = await playdl.stream(url); - const playdlStream = playdlData.stream; - const getInfo = async () => { +export const stream = async (url: string): Promise => { + const getPlayDlInfo = async () => { + const info = await playdl.video_basic_info(url); + if (!info.format) { + throw new Error("No stream URL found"); + } + + for (const format of info.format) { + if (format.itag === 140) { + console.log("format", format); + if ( + !format.url || + !format.contentLength || + !format.approxDurationMs + ) { + continue; + } + + type DefinedFormat = typeof format & { + url: string; + contentLength: number; + approxDurationMs: number; + }; + + const newFormat: DefinedFormat = format as DefinedFormat; + return newFormat; + } + } + + throw new Error("No stream URL found"); + }; + + const getYtDlpWrapInfo = async () => { return JSON.parse( await ytDlpWrap.execPromise([ url, @@ -21,22 +50,40 @@ export const stream = async (url: string): Promise => { ); }; - const ytDlpWrapInfo = await getInfo(); - const refreshStreamUrlFunction = async () => { - const info = await getInfo(); + const ytDlpWrapInfo = await getYtDlpWrapInfo(); + const playDlInfo = await getPlayDlInfo(); + console.log("dlp", ytDlpWrapInfo.url); + console.log("play-dl", playDlInfo); + + const ytDlpRefreshStreamUrlFunction = async () => { + const info = await getYtDlpWrapInfo(); + return info.url; + }; + + const playDlRefreshStreamUrlFunction = async () => { + const info = await getPlayDlInfo(); + if (!info.url) throw new Error("No stream URL found"); + return info.url; }; const ytDlpWrapStream = new Stream( ytDlpWrapInfo.url, url, - //playdlData.type, ytDlpWrapInfo.filesize, ytDlpWrapInfo.duration, - refreshStreamUrlFunction, + ytDlpRefreshStreamUrlFunction, ); - const stream = ytDlpWrapStream.stream; + const playDlStream = new Stream( + playDlInfo.url, + url, + playDlInfo.contentLength, + Math.ceil(playDlInfo.approxDurationMs / 1000), + playDlRefreshStreamUrlFunction, + ); + + const stream = playDlStream.stream; // stream.on("error", (err) => { // console.error("An error occurred:", err.message); diff --git a/src/utils/stream.ts b/src/utils/stream.ts index 093efe4..5fa7368 100644 --- a/src/utils/stream.ts +++ b/src/utils/stream.ts @@ -125,7 +125,7 @@ export class Stream { `Buffer Remaining: ${(this.stream.readableLength / (1024 * 1024)).toFixed(3)} MB | ` + `${!this.fetchCompleted ? `Buffer Sufficient: ${isBufferSufficient} | ` : ``}` + `Locked: ${this.locked} | ` + - `Fetch Completed: ${this.fetchCompleted} | `, + `Fetch Completed: ${this.fetchCompleted}`, ); }