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";
|
import { Stream } from "./utils/stream";
|
||||||
|
|
||||||
const ytDlpWrap = new YTDlpWrap();
|
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(
|
return JSON.parse(
|
||||||
await ytDlpWrap.execPromise([
|
await ytDlpWrap.execPromise([
|
||||||
url,
|
url,
|
||||||
@@ -21,22 +50,40 @@ export const stream = async (url: string): Promise<Readable> => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const ytDlpWrapInfo = await getInfo();
|
const ytDlpWrapInfo = await getYtDlpWrapInfo();
|
||||||
const refreshStreamUrlFunction = async () => {
|
const playDlInfo = await getPlayDlInfo();
|
||||||
const info = await getInfo();
|
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;
|
return info.url;
|
||||||
};
|
};
|
||||||
|
|
||||||
const ytDlpWrapStream = new Stream(
|
const ytDlpWrapStream = new Stream(
|
||||||
ytDlpWrapInfo.url,
|
ytDlpWrapInfo.url,
|
||||||
url,
|
url,
|
||||||
//playdlData.type,
|
|
||||||
ytDlpWrapInfo.filesize,
|
ytDlpWrapInfo.filesize,
|
||||||
ytDlpWrapInfo.duration,
|
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) => {
|
// stream.on("error", (err) => {
|
||||||
// console.error("An error occurred:", err.message);
|
// console.error("An error occurred:", err.message);
|
||||||
|
|||||||
@@ -125,7 +125,7 @@ export class Stream {
|
|||||||
`Buffer Remaining: ${(this.stream.readableLength / (1024 * 1024)).toFixed(3)} MB | ` +
|
`Buffer Remaining: ${(this.stream.readableLength / (1024 * 1024)).toFixed(3)} MB | ` +
|
||||||
`${!this.fetchCompleted ? `Buffer Sufficient: ${isBufferSufficient} | ` : ``}` +
|
`${!this.fetchCompleted ? `Buffer Sufficient: ${isBufferSufficient} | ` : ``}` +
|
||||||
`Locked: ${this.locked} | ` +
|
`Locked: ${this.locked} | ` +
|
||||||
`Fetch Completed: ${this.fetchCompleted} | `,
|
`Fetch Completed: ${this.fetchCompleted}`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user