feat: Simple play-dl stream

This commit is contained in:
2024-06-19 11:55:55 +07:00
parent a4242badf8
commit 2c3382306b
3 changed files with 43 additions and 0 deletions

31
example/test.ts Normal file
View File

@@ -0,0 +1,31 @@
import NekoMelody from "../src";
import Speaker from "speaker";
import ffmpeg from "fluent-ffmpeg";
const main = async () => {
// Create the Speaker instance
const speaker = new Speaker();
// Get the stream from the URL
const stream = await NekoMelody.stream(
"https://www.youtube.com/watch?v=9PuudPiyma4",
);
// PCM data from stdin gets piped into the speaker
let audioStream = stream;
const ffmpegProcess = ffmpeg()
.input(audioStream)
.format("s16le") // Output format (PCM 16-bit little-endian)
//.audioChannels(2) // Number of audio channels
//.audioFrequency(44100) // Sample rate
.on("error", (err) => {
console.error("An error occurred:", err.message);
})
.pipe(speaker, { end: true })
.on("end", () => {
console.log("Audio playback finished.");
});
};
main();

5
src/index.ts Normal file
View File

@@ -0,0 +1,5 @@
import { stream } from "./stream";
export default {
stream,
};

7
src/stream.ts Normal file
View File

@@ -0,0 +1,7 @@
import { Readable } from "stream";
import playdl from "play-dl/play-dl";
export const stream = async (url: string): Promise<Readable> => {
let playdlStream = await playdl.stream(url);
return playdlStream.stream;
};