diff --git a/example/test.ts b/example/test.ts new file mode 100644 index 0000000..38903ff --- /dev/null +++ b/example/test.ts @@ -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(); diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..7413d40 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,5 @@ +import { stream } from "./stream"; + +export default { + stream, +}; diff --git a/src/stream.ts b/src/stream.ts new file mode 100644 index 0000000..e616599 --- /dev/null +++ b/src/stream.ts @@ -0,0 +1,7 @@ +import { Readable } from "stream"; +import playdl from "play-dl/play-dl"; + +export const stream = async (url: string): Promise => { + let playdlStream = await playdl.stream(url); + return playdlStream.stream; +};