2024-06-21 12:17:32 +07:00
|
|
|
import NekoMelody, { Player } from "../src";
|
2024-06-19 11:55:55 +07:00
|
|
|
|
|
|
|
|
import Speaker from "speaker";
|
|
|
|
|
import ffmpeg from "fluent-ffmpeg";
|
2024-06-21 00:30:21 +07:00
|
|
|
import { YtDlpProvider } from "../src/providers";
|
2024-06-19 11:55:55 +07:00
|
|
|
|
|
|
|
|
const main = async () => {
|
2024-06-21 12:17:32 +07:00
|
|
|
const videoId = "2gigEGxnsmo";
|
2024-06-19 22:23:05 +07:00
|
|
|
|
2024-06-21 00:30:21 +07:00
|
|
|
// Providers
|
|
|
|
|
const providers = [new YtDlpProvider()];
|
|
|
|
|
const player = NekoMelody.createPlayer(providers);
|
|
|
|
|
|
|
|
|
|
await player.play(`https://www.youtube.com/watch?v=${videoId}`);
|
2024-06-21 12:17:32 +07:00
|
|
|
playSpeaker(player);
|
2024-06-21 00:30:21 +07:00
|
|
|
|
2024-06-21 12:17:32 +07:00
|
|
|
// setTimeout(async () => {
|
|
|
|
|
// await player.seek(100);
|
|
|
|
|
// playSpeaker(player);
|
|
|
|
|
// }, 5000);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// TODO: player end event to automate changing the stream
|
|
|
|
|
let lastFFmpeg: ffmpeg.FfmpegCommand | null = null;
|
|
|
|
|
let lastSpeaker: Speaker | null = null;
|
|
|
|
|
const playSpeaker = async (player: Player) => {
|
2024-06-21 00:30:21 +07:00
|
|
|
if (!player.stream) {
|
|
|
|
|
console.error("No input stream");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-06-19 11:55:55 +07:00
|
|
|
|
2024-06-21 12:17:32 +07:00
|
|
|
// A function that resolves when the speaker is closed and the ffmpeg process is killed
|
|
|
|
|
const closeSpeaker = () => {
|
|
|
|
|
return new Promise<void>((resolve) => {
|
|
|
|
|
if (lastSpeaker) {
|
|
|
|
|
lastSpeaker.on("close", () => {
|
|
|
|
|
resolve();
|
|
|
|
|
});
|
|
|
|
|
if (lastFFmpeg) lastFFmpeg.kill("SIGKILL");
|
|
|
|
|
lastSpeaker.close(true);
|
|
|
|
|
} else {
|
|
|
|
|
resolve();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await closeSpeaker();
|
|
|
|
|
|
|
|
|
|
// Create the Speaker instance
|
|
|
|
|
const speaker = new Speaker();
|
|
|
|
|
lastSpeaker = speaker;
|
|
|
|
|
|
2024-06-19 11:55:55 +07:00
|
|
|
// PCM data from stdin gets piped into the speaker
|
|
|
|
|
const ffmpegProcess = ffmpeg()
|
2024-06-21 00:30:21 +07:00
|
|
|
.input(player.stream)
|
2024-06-19 11:55:55 +07:00
|
|
|
.format("s16le") // Output format (PCM 16-bit little-endian)
|
2024-06-21 00:30:21 +07:00
|
|
|
.audioChannels(2)
|
|
|
|
|
.audioFrequency(44100)
|
2024-06-19 11:55:55 +07:00
|
|
|
.on("error", (err) => {
|
|
|
|
|
console.error("An error occurred:", err.message);
|
2024-06-21 12:17:32 +07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Pipe the ffmpeg output to the speaker
|
|
|
|
|
ffmpegProcess.pipe(speaker, { end: true });
|
|
|
|
|
|
|
|
|
|
lastFFmpeg = ffmpegProcess;
|
2024-06-19 11:55:55 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
main();
|