Skip to content

Commit

Permalink
feat(Stream): support playing local files
Browse files Browse the repository at this point in the history
  • Loading branch information
skick1234 committed Jun 5, 2024
1 parent dcd8a45 commit 8101244
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/DisTube.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ export class DisTube extends TypedEmitter<TypedDisTubeEvents> {
const queue = this.getQueue(voiceChannel) || (await this.queues.create(voiceChannel, textChannel));
await queue._taskQueue.queuing();
try {
this.debug(`[${queue.id}] Resolving song: ${typeof song === "string" ? song : JSON.stringify(song)}`);
this.debug(`[${queue.id}] Playing input: ${song}`);
const resolved = await this.handler.resolve(song, { member, metadata });
const isNsfw = isNsfwChannel(queue?.textChannel || textChannel);
if (resolved instanceof Playlist) {
Expand Down
2 changes: 1 addition & 1 deletion src/core/DisTubeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class DisTubeHandler extends DisTubeBase {
const plugin =
(await this._getPluginFromURL(input)) || (await this._getPluginFromURL(await this.followRedirectLink(input)));
if (!plugin) throw new DisTubeError("NOT_SUPPORTED_URL");
this.debug(`[${plugin.constructor.name}] Resolving song from url: ${input}`);
this.debug(`[${plugin.constructor.name}] Resolving from url: ${input}`);
return plugin.resolve(input, options);
}
try {
Expand Down
16 changes: 9 additions & 7 deletions src/core/DisTubeStream.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Transform } from "stream";
import { DisTubeError, Events } from "..";
import { spawn, spawnSync } from "child_process";
import { DisTubeError, Events, isURL } from "..";
import { TypedEmitter } from "tiny-typed-emitter";
import { StreamType, createAudioResource } from "@discordjs/voice";
import type { TransformCallback } from "stream";
Expand Down Expand Up @@ -63,12 +63,6 @@ export class DisTubeStream extends TypedEmitter<{
* @param options - Stream options
*/
constructor(url: string, options: StreamOptions) {
if (typeof url !== "string" || !isURL(url)) {
throw new DisTubeError("INVALID_TYPE", "an URL", url);
}
if (!options || typeof options !== "object" || Array.isArray(options)) {
throw new DisTubeError("INVALID_TYPE", "object", options, "options");
}
super();
const { ffmpeg, seek } = options;
const opts: FFmpegArg = {
Expand All @@ -88,6 +82,14 @@ export class DisTubeStream extends TypedEmitter<{

if (typeof seek === "number" && seek > 0) opts.ss = seek.toString();

const fileUrl = new URL(url);
if (fileUrl.protocol === "file:") {
opts.reconnect = null;
opts.reconnect_streamed = null;
opts.reconnect_delay_max = null;
opts.i = fileUrl.hostname + fileUrl.pathname;
}

this.#ffmpegPath = ffmpeg.path;
this.#opts = [
...Object.entries(opts)
Expand Down
4 changes: 4 additions & 0 deletions src/struct/Playlist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,8 @@ export class Playlist<T = unknown> implements PlaylistInfo {
this.#metadata = metadata;
this.songs.forEach(s => (s.metadata = metadata));
}

toString() {
return `${this.name} (${this.songs.length} songs)`;
}
}

0 comments on commit 8101244

Please sign in to comment.