mirror of
https://github.com/YuzuZensai/NekoMelody.git
synced 2026-01-31 14:57:58 +00:00
✨ feat: Make play-dl use custom stream logic
This commit is contained in:
@@ -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<Readable> => {
|
||||
const playdlData = await playdl.stream(url);
|
||||
const playdlStream = playdlData.stream;
|
||||
|
||||
const getInfo = async () => {
|
||||
export const stream = async (url: string): Promise<Readable> => {
|
||||
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<Readable> => {
|
||||
);
|
||||
};
|
||||
|
||||
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);
|
||||
|
||||
@@ -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}`,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user