feat: Audio metadata

This commit is contained in:
2024-08-17 16:04:30 +07:00
parent ebef80e9d4
commit 97050de99a
5 changed files with 35 additions and 3 deletions

View File

@@ -9,7 +9,7 @@ import dotenv from "dotenv";
dotenv.config(); dotenv.config();
const main = async () => { const main = async () => {
const videoId = "2gigEGxnsmo"; const videoId = "l6gPxDSNbVk";
const videoId2 = "oM-JneFEdBk"; const videoId2 = "oM-JneFEdBk";
// Providers // Providers
@@ -21,7 +21,11 @@ const main = async () => {
player.startCurrentStream(); player.startCurrentStream();
}); });
await player.enqueue(`https://www.youtube.com/watch?v=${videoId}`); const info = await player.enqueue(
`https://www.youtube.com/watch?v=${videoId}`,
);
console.log(info);
await player.enqueue(`https://www.youtube.com/watch?v=${videoId2}`); await player.enqueue(`https://www.youtube.com/watch?v=${videoId2}`);
}; };

View File

@@ -1,8 +1,10 @@
import { Player, createPlayer } from "./player"; import { Player, createPlayer } from "./player";
import { YtDlpProvider } from "./providers";
export { Player, createPlayer }; export { Player, createPlayer };
export default { export default {
Player, Player,
YtDlpProvider,
createPlayer, createPlayer,
}; };

View File

@@ -113,7 +113,7 @@ export class Player {
this.queue.push(information); this.queue.push(information);
} }
console.log("Enqueued", url); return information;
} }
public async seek(time: number) { public async seek(time: number) {

View File

@@ -8,6 +8,10 @@ export interface AudioInformation {
}; };
bitrate: number; bitrate: number;
livestream: boolean; livestream: boolean;
metadata: {
title: string | null;
thumbnail: string | null;
};
refreshInfoFunction: () => Promise<AudioInformation>; refreshInfoFunction: () => Promise<AudioInformation>;
} }

View File

@@ -43,6 +43,24 @@ export class YtDlpProvider extends Provider {
throw new Error("Failed to parse YouTube formats"); throw new Error("Failed to parse YouTube formats");
} }
let bestThumbnail = null;
type Thumbnail = { url: string; preference: number };
const sortedThumbnails = ytDlpWrapInfo.thumbnails.sort(
(a: Thumbnail, b: Thumbnail) => b.preference - a.preference,
);
for (let thumbnail of sortedThumbnails) {
try {
const response = await fetch(thumbnail.url);
if (response.status === 200) {
bestThumbnail = thumbnail.url;
break;
}
} catch (error) {}
}
return { return {
url: ytDlpWrapInfo.url, url: ytDlpWrapInfo.url,
fileSize: ytDlpWrapInfo.filesize, fileSize: ytDlpWrapInfo.filesize,
@@ -52,6 +70,10 @@ export class YtDlpProvider extends Provider {
bitrate: ytDlpWrapInfo.asr, //bitrate: Math.ceil((ytDlpWrapInfo.tbr || 128) * 1000), bitrate: ytDlpWrapInfo.asr, //bitrate: Math.ceil((ytDlpWrapInfo.tbr || 128) * 1000),
livestream: ytDlpWrapInfo.is_live, livestream: ytDlpWrapInfo.is_live,
refreshInfoFunction, refreshInfoFunction,
metadata: {
title: ytDlpWrapInfo.title,
thumbnail: bestThumbnail,
},
} as AudioInformation; } as AudioInformation;
}; };