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();
const main = async () => {
const videoId = "2gigEGxnsmo";
const videoId = "l6gPxDSNbVk";
const videoId2 = "oM-JneFEdBk";
// Providers
@@ -21,7 +21,11 @@ const main = async () => {
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}`);
};

View File

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

View File

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

View File

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

View File

@@ -43,6 +43,24 @@ export class YtDlpProvider extends Provider {
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 {
url: ytDlpWrapInfo.url,
fileSize: ytDlpWrapInfo.filesize,
@@ -52,6 +70,10 @@ export class YtDlpProvider extends Provider {
bitrate: ytDlpWrapInfo.asr, //bitrate: Math.ceil((ytDlpWrapInfo.tbr || 128) * 1000),
livestream: ytDlpWrapInfo.is_live,
refreshInfoFunction,
metadata: {
title: ytDlpWrapInfo.title,
thumbnail: bestThumbnail,
},
} as AudioInformation;
};