2022-02-05 23:16:17 +01:00
|
|
|
import {VoiceChannel, Snowflake} from 'discord.js';
|
2021-11-19 18:13:45 +01:00
|
|
|
import {Readable} from 'stream';
|
2020-03-14 02:36:42 +01:00
|
|
|
import hasha from 'hasha';
|
|
|
|
import ytdl from 'ytdl-core';
|
|
|
|
import {WriteStream} from 'fs-capacitor';
|
2020-03-15 03:48:08 +01:00
|
|
|
import ffmpeg from 'fluent-ffmpeg';
|
2020-03-21 02:47:04 +01:00
|
|
|
import shuffle from 'array-shuffle';
|
2022-03-13 23:30:36 +01:00
|
|
|
import {
|
|
|
|
AudioPlayer,
|
|
|
|
AudioPlayerState,
|
|
|
|
AudioPlayerStatus,
|
|
|
|
createAudioPlayer,
|
|
|
|
createAudioResource,
|
|
|
|
joinVoiceChannel,
|
|
|
|
StreamType,
|
|
|
|
VoiceConnection,
|
|
|
|
VoiceConnectionStatus,
|
|
|
|
} from '@discordjs/voice';
|
2021-11-19 18:13:45 +01:00
|
|
|
import FileCacheProvider from './file-cache.js';
|
2022-02-05 23:16:17 +01:00
|
|
|
import debug from '../utils/debug.js';
|
2022-02-12 03:05:02 +01:00
|
|
|
import {prisma} from '../utils/db.js';
|
2020-03-21 02:47:04 +01:00
|
|
|
|
2022-03-12 16:50:29 +01:00
|
|
|
export enum MediaSource {
|
|
|
|
Youtube,
|
|
|
|
HLS,
|
|
|
|
}
|
|
|
|
|
2020-03-21 02:47:04 +01:00
|
|
|
export interface QueuedPlaylist {
|
|
|
|
title: string;
|
|
|
|
source: string;
|
|
|
|
}
|
|
|
|
|
2022-03-12 16:50:29 +01:00
|
|
|
export interface SongMetadata {
|
2020-03-21 02:47:04 +01:00
|
|
|
title: string;
|
|
|
|
artist: string;
|
|
|
|
url: string;
|
|
|
|
length: number;
|
2022-03-10 03:47:52 +01:00
|
|
|
offset: number;
|
2020-03-21 02:47:04 +01:00
|
|
|
playlist: QueuedPlaylist | null;
|
|
|
|
isLive: boolean;
|
2022-01-21 19:50:57 +01:00
|
|
|
thumbnailUrl: string | null;
|
2022-03-12 16:50:29 +01:00
|
|
|
source: MediaSource;
|
|
|
|
}
|
|
|
|
export interface QueuedSong extends SongMetadata {
|
|
|
|
addedInChannelId: Snowflake;
|
2022-01-21 19:50:57 +01:00
|
|
|
requestedBy: string;
|
2020-03-21 02:47:04 +01:00
|
|
|
}
|
2020-03-13 04:41:26 +01:00
|
|
|
|
2020-03-15 20:36:59 +01:00
|
|
|
export enum STATUS {
|
|
|
|
PLAYING,
|
2021-09-20 04:24:46 +02:00
|
|
|
PAUSED,
|
2022-02-12 03:05:02 +01:00
|
|
|
IDLE,
|
2020-03-13 04:41:26 +01:00
|
|
|
}
|
|
|
|
|
2022-02-05 23:16:17 +01:00
|
|
|
export interface PlayerEvents {
|
|
|
|
statusChange: (oldStatus: STATUS, newStatus: STATUS) => void;
|
|
|
|
}
|
|
|
|
|
2020-03-13 04:41:26 +01:00
|
|
|
export default class {
|
2020-03-17 02:14:15 +01:00
|
|
|
public voiceConnection: VoiceConnection | null = null;
|
2022-02-05 23:16:17 +01:00
|
|
|
public status = STATUS.PAUSED;
|
|
|
|
public guildId: string;
|
|
|
|
|
2020-03-21 02:47:04 +01:00
|
|
|
private queue: QueuedSong[] = [];
|
|
|
|
private queuePosition = 0;
|
2021-11-12 20:30:18 +01:00
|
|
|
private audioPlayer: AudioPlayer | null = null;
|
2020-03-18 01:42:28 +01:00
|
|
|
private nowPlaying: QueuedSong | null = null;
|
2020-03-16 01:30:07 +01:00
|
|
|
private playPositionInterval: NodeJS.Timeout | undefined;
|
2020-03-19 04:29:43 +01:00
|
|
|
private lastSongURL = '';
|
2020-03-13 04:41:26 +01:00
|
|
|
|
2020-03-15 21:35:34 +01:00
|
|
|
private positionInSeconds = 0;
|
|
|
|
|
2021-11-19 18:13:45 +01:00
|
|
|
private readonly fileCache: FileCacheProvider;
|
2022-02-12 03:05:02 +01:00
|
|
|
private disconnectTimer: NodeJS.Timeout | null = null;
|
2021-09-18 22:55:50 +02:00
|
|
|
|
2022-02-05 23:16:17 +01:00
|
|
|
constructor(fileCache: FileCacheProvider, guildId: string) {
|
2021-11-19 18:13:45 +01:00
|
|
|
this.fileCache = fileCache;
|
2022-02-05 23:16:17 +01:00
|
|
|
this.guildId = guildId;
|
2020-03-13 04:41:26 +01:00
|
|
|
}
|
|
|
|
|
2020-03-15 20:36:59 +01:00
|
|
|
async connect(channel: VoiceChannel): Promise<void> {
|
2022-01-30 04:01:03 +01:00
|
|
|
this.voiceConnection = joinVoiceChannel({
|
2021-11-12 20:30:18 +01:00
|
|
|
channelId: channel.id,
|
|
|
|
guildId: channel.guild.id,
|
|
|
|
adapterCreator: channel.guild.voiceAdapterCreator,
|
|
|
|
});
|
2020-03-13 04:41:26 +01:00
|
|
|
}
|
|
|
|
|
2021-11-12 20:30:18 +01:00
|
|
|
disconnect(): void {
|
2020-03-15 20:36:59 +01:00
|
|
|
if (this.voiceConnection) {
|
2020-03-16 01:30:07 +01:00
|
|
|
if (this.status === STATUS.PLAYING) {
|
|
|
|
this.pause();
|
|
|
|
}
|
|
|
|
|
2021-11-12 20:30:18 +01:00
|
|
|
this.voiceConnection.destroy();
|
|
|
|
this.audioPlayer?.stop();
|
2020-03-17 23:59:26 +01:00
|
|
|
|
|
|
|
this.voiceConnection = null;
|
2021-11-12 20:30:18 +01:00
|
|
|
this.audioPlayer = null;
|
2020-03-13 04:41:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-15 20:36:59 +01:00
|
|
|
async seek(positionSeconds: number): Promise<void> {
|
2020-03-17 23:59:26 +01:00
|
|
|
this.status = STATUS.PAUSED;
|
|
|
|
|
2020-03-15 20:36:59 +01:00
|
|
|
if (this.voiceConnection === null) {
|
2020-03-14 02:36:42 +01:00
|
|
|
throw new Error('Not connected to a voice channel.');
|
|
|
|
}
|
|
|
|
|
2020-03-21 02:47:04 +01:00
|
|
|
const currentSong = this.getCurrent();
|
2020-03-14 02:36:42 +01:00
|
|
|
|
|
|
|
if (!currentSong) {
|
|
|
|
throw new Error('No song currently playing');
|
|
|
|
}
|
|
|
|
|
2020-03-18 18:40:31 +01:00
|
|
|
if (positionSeconds > currentSong.length) {
|
|
|
|
throw new Error('Seek position is outside the range of the song.');
|
|
|
|
}
|
|
|
|
|
2022-03-10 03:47:52 +01:00
|
|
|
let realPositionSeconds = positionSeconds;
|
|
|
|
let to: number | undefined;
|
|
|
|
if (currentSong.offset !== undefined) {
|
|
|
|
realPositionSeconds += currentSong.offset;
|
|
|
|
to = currentSong.length + currentSong.offset;
|
|
|
|
}
|
|
|
|
|
2022-03-12 16:50:29 +01:00
|
|
|
const stream = await this.getStream(currentSong, {seek: realPositionSeconds, to});
|
2022-01-30 04:01:03 +01:00
|
|
|
this.audioPlayer = createAudioPlayer({
|
|
|
|
behaviors: {
|
|
|
|
// Needs to be somewhat high for livestreams
|
|
|
|
maxMissedFrames: 50,
|
|
|
|
},
|
|
|
|
});
|
2021-11-12 20:30:18 +01:00
|
|
|
this.voiceConnection.subscribe(this.audioPlayer);
|
|
|
|
this.audioPlayer.play(createAudioResource(stream, {
|
|
|
|
inputType: StreamType.WebmOpus,
|
|
|
|
}));
|
2020-03-16 01:30:07 +01:00
|
|
|
this.attachListeners();
|
|
|
|
this.startTrackingPosition(positionSeconds);
|
2020-03-15 21:35:34 +01:00
|
|
|
|
2020-03-16 01:30:07 +01:00
|
|
|
this.status = STATUS.PLAYING;
|
2020-03-15 21:35:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async forwardSeek(positionSeconds: number): Promise<void> {
|
|
|
|
return this.seek(this.positionInSeconds + positionSeconds);
|
|
|
|
}
|
|
|
|
|
|
|
|
getPosition(): number {
|
|
|
|
return this.positionInSeconds;
|
2020-03-14 02:36:42 +01:00
|
|
|
}
|
|
|
|
|
2020-03-15 20:36:59 +01:00
|
|
|
async play(): Promise<void> {
|
|
|
|
if (this.voiceConnection === null) {
|
2020-03-13 04:41:26 +01:00
|
|
|
throw new Error('Not connected to a voice channel.');
|
|
|
|
}
|
|
|
|
|
2020-03-21 02:47:04 +01:00
|
|
|
const currentSong = this.getCurrent();
|
2020-03-17 23:59:26 +01:00
|
|
|
|
|
|
|
if (!currentSong) {
|
|
|
|
throw new Error('Queue empty.');
|
|
|
|
}
|
|
|
|
|
2022-02-12 03:05:02 +01:00
|
|
|
// Cancel any pending idle disconnection
|
|
|
|
if (this.disconnectTimer) {
|
|
|
|
clearInterval(this.disconnectTimer);
|
|
|
|
this.disconnectTimer = null;
|
|
|
|
}
|
|
|
|
|
2020-03-15 20:36:59 +01:00
|
|
|
// Resume from paused state
|
2020-03-21 02:47:04 +01:00
|
|
|
if (this.status === STATUS.PAUSED && currentSong.url === this.nowPlaying?.url) {
|
2021-11-12 20:30:18 +01:00
|
|
|
if (this.audioPlayer) {
|
|
|
|
this.audioPlayer.unpause();
|
2020-03-19 00:29:32 +01:00
|
|
|
this.status = STATUS.PLAYING;
|
2020-03-19 04:29:43 +01:00
|
|
|
this.startTrackingPosition();
|
2020-03-19 00:29:32 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Was disconnected, need to recreate stream
|
2020-03-24 01:40:54 +01:00
|
|
|
if (!currentSong.isLive) {
|
|
|
|
return this.seek(this.getPosition());
|
|
|
|
}
|
2020-03-13 04:41:26 +01:00
|
|
|
}
|
|
|
|
|
2020-03-21 02:47:04 +01:00
|
|
|
try {
|
2022-03-10 03:47:52 +01:00
|
|
|
let positionSeconds: number | undefined;
|
|
|
|
let to: number | undefined;
|
|
|
|
if (currentSong.offset !== undefined) {
|
|
|
|
positionSeconds = currentSong.offset;
|
|
|
|
to = currentSong.length + currentSong.offset;
|
|
|
|
}
|
|
|
|
|
2022-03-12 16:50:29 +01:00
|
|
|
const stream = await this.getStream(currentSong, {seek: positionSeconds, to});
|
2022-01-30 04:01:03 +01:00
|
|
|
this.audioPlayer = createAudioPlayer({
|
|
|
|
behaviors: {
|
|
|
|
// Needs to be somewhat high for livestreams
|
|
|
|
maxMissedFrames: 50,
|
|
|
|
},
|
|
|
|
});
|
2021-11-12 20:30:18 +01:00
|
|
|
this.voiceConnection.subscribe(this.audioPlayer);
|
2022-02-05 23:16:17 +01:00
|
|
|
const resource = createAudioResource(stream, {
|
2021-11-12 20:30:18 +01:00
|
|
|
inputType: StreamType.WebmOpus,
|
2022-02-05 23:16:17 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
this.audioPlayer.play(resource);
|
2020-03-13 04:41:26 +01:00
|
|
|
|
2020-03-21 02:47:04 +01:00
|
|
|
this.attachListeners();
|
2020-03-13 04:41:26 +01:00
|
|
|
|
2020-03-21 02:47:04 +01:00
|
|
|
this.status = STATUS.PLAYING;
|
|
|
|
this.nowPlaying = currentSong;
|
2020-03-16 01:30:07 +01:00
|
|
|
|
2020-03-21 02:47:04 +01:00
|
|
|
if (currentSong.url === this.lastSongURL) {
|
|
|
|
this.startTrackingPosition();
|
|
|
|
} else {
|
|
|
|
// Reset position counter
|
|
|
|
this.startTrackingPosition(0);
|
|
|
|
this.lastSongURL = currentSong.url;
|
|
|
|
}
|
2020-10-24 18:32:43 +02:00
|
|
|
} catch (error: unknown) {
|
2021-09-18 22:55:50 +02:00
|
|
|
await this.forward(1);
|
|
|
|
|
|
|
|
if ((error as {statusCode: number}).statusCode === 410 && currentSong) {
|
|
|
|
const channelId = currentSong.addedInChannelId;
|
|
|
|
|
|
|
|
if (channelId) {
|
2022-02-05 23:16:17 +01:00
|
|
|
debug(`${currentSong.title} is unavailable`);
|
2021-09-18 22:55:50 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-21 02:47:04 +01:00
|
|
|
throw error;
|
2020-03-19 04:29:43 +01:00
|
|
|
}
|
2020-03-13 04:41:26 +01:00
|
|
|
}
|
|
|
|
|
2020-03-15 20:36:59 +01:00
|
|
|
pause(): void {
|
2020-03-16 01:30:07 +01:00
|
|
|
if (this.status !== STATUS.PLAYING) {
|
2020-03-15 20:36:59 +01:00
|
|
|
throw new Error('Not currently playing.');
|
|
|
|
}
|
2020-03-13 04:41:26 +01:00
|
|
|
|
2020-03-16 01:30:07 +01:00
|
|
|
this.status = STATUS.PAUSED;
|
|
|
|
|
2021-11-12 20:30:18 +01:00
|
|
|
if (this.audioPlayer) {
|
|
|
|
this.audioPlayer.pause();
|
2020-03-16 01:30:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this.stopTrackingPosition();
|
2020-03-13 04:41:26 +01:00
|
|
|
}
|
|
|
|
|
2021-04-23 18:30:31 +02:00
|
|
|
async forward(skip: number): Promise<void> {
|
|
|
|
this.manualForward(skip);
|
2020-03-21 02:47:04 +01:00
|
|
|
|
2020-03-24 01:40:54 +01:00
|
|
|
try {
|
|
|
|
if (this.getCurrent() && this.status !== STATUS.PAUSED) {
|
|
|
|
await this.play();
|
|
|
|
} else {
|
2022-02-12 03:05:02 +01:00
|
|
|
this.audioPlayer?.stop();
|
|
|
|
this.status = STATUS.IDLE;
|
|
|
|
|
|
|
|
const settings = await prisma.setting.findUnique({where: {guildId: this.guildId}});
|
|
|
|
|
|
|
|
if (!settings) {
|
|
|
|
throw new Error('Could not find settings for guild');
|
|
|
|
}
|
|
|
|
|
|
|
|
const {secondsToWaitAfterQueueEmpties} = settings;
|
|
|
|
if (secondsToWaitAfterQueueEmpties !== 0) {
|
|
|
|
this.disconnectTimer = setTimeout(() => {
|
|
|
|
// Make sure we are not accidentally playing
|
|
|
|
// when disconnecting
|
|
|
|
if (this.status === STATUS.IDLE) {
|
|
|
|
this.disconnect();
|
|
|
|
}
|
|
|
|
}, secondsToWaitAfterQueueEmpties * 1000);
|
|
|
|
}
|
2020-03-21 02:47:04 +01:00
|
|
|
}
|
2020-10-24 18:32:43 +02:00
|
|
|
} catch (error: unknown) {
|
2020-03-24 01:40:54 +01:00
|
|
|
this.queuePosition--;
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-05 23:16:17 +01:00
|
|
|
canGoForward(skip: number) {
|
|
|
|
return (this.queuePosition + skip - 1) < this.queue.length;
|
|
|
|
}
|
|
|
|
|
2021-04-23 18:30:31 +02:00
|
|
|
manualForward(skip: number): void {
|
2022-02-05 23:16:17 +01:00
|
|
|
if (this.canGoForward(skip)) {
|
2021-04-23 18:30:31 +02:00
|
|
|
this.queuePosition += skip;
|
2020-03-25 23:32:43 +01:00
|
|
|
this.positionInSeconds = 0;
|
|
|
|
this.stopTrackingPosition();
|
2020-03-21 02:47:04 +01:00
|
|
|
} else {
|
|
|
|
throw new Error('No songs in queue to forward to.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-05 23:16:17 +01:00
|
|
|
canGoBack() {
|
|
|
|
return this.queuePosition - 1 >= 0;
|
|
|
|
}
|
|
|
|
|
2020-03-21 02:47:04 +01:00
|
|
|
async back(): Promise<void> {
|
2022-02-05 23:16:17 +01:00
|
|
|
if (this.canGoBack()) {
|
2020-03-21 02:47:04 +01:00
|
|
|
this.queuePosition--;
|
|
|
|
this.positionInSeconds = 0;
|
2020-03-25 23:32:43 +01:00
|
|
|
this.stopTrackingPosition();
|
2020-03-21 02:47:04 +01:00
|
|
|
|
|
|
|
if (this.status !== STATUS.PAUSED) {
|
|
|
|
await this.play();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new Error('No songs in queue to go back to.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getCurrent(): QueuedSong | null {
|
|
|
|
if (this.queue[this.queuePosition]) {
|
|
|
|
return this.queue[this.queuePosition];
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-12-12 19:20:36 +01:00
|
|
|
/**
|
|
|
|
* Returns queue, not including the current song.
|
|
|
|
* @returns {QueuedSong[]}
|
|
|
|
*/
|
2020-03-21 02:47:04 +01:00
|
|
|
getQueue(): QueuedSong[] {
|
|
|
|
return this.queue.slice(this.queuePosition + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
add(song: QueuedSong, {immediate = false} = {}): void {
|
2021-11-18 00:17:08 +01:00
|
|
|
if (song.playlist || !immediate) {
|
2020-03-21 02:47:04 +01:00
|
|
|
// Add to end of queue
|
|
|
|
this.queue.push(song);
|
|
|
|
} else {
|
2021-11-18 00:17:08 +01:00
|
|
|
// Add as the next song to be played
|
|
|
|
const insertAt = this.queuePosition + 1;
|
2020-03-21 02:47:04 +01:00
|
|
|
this.queue = [...this.queue.slice(0, insertAt), song, ...this.queue.slice(insertAt)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
shuffle(): void {
|
2020-03-25 23:59:09 +01:00
|
|
|
const shuffledSongs = shuffle(this.queue.slice(this.queuePosition + 1));
|
|
|
|
|
|
|
|
this.queue = [...this.queue.slice(0, this.queuePosition + 1), ...shuffledSongs];
|
2020-03-21 02:47:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
clear(): void {
|
|
|
|
const newQueue = [];
|
|
|
|
|
|
|
|
// Don't clear curently playing song
|
|
|
|
const current = this.getCurrent();
|
|
|
|
|
|
|
|
if (current) {
|
|
|
|
newQueue.push(current);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.queuePosition = 0;
|
|
|
|
this.queue = newQueue;
|
|
|
|
}
|
|
|
|
|
2021-11-20 21:10:39 +01:00
|
|
|
removeFromQueue(index: number, amount = 1): void {
|
|
|
|
this.queue.splice(this.queuePosition + index, amount);
|
|
|
|
}
|
|
|
|
|
2020-03-21 02:47:04 +01:00
|
|
|
removeCurrent(): void {
|
|
|
|
this.queue = [...this.queue.slice(0, this.queuePosition), ...this.queue.slice(this.queuePosition + 1)];
|
|
|
|
}
|
|
|
|
|
|
|
|
queueSize(): number {
|
|
|
|
return this.getQueue().length;
|
|
|
|
}
|
|
|
|
|
|
|
|
isQueueEmpty(): boolean {
|
|
|
|
return this.queueSize() === 0;
|
2020-03-20 01:16:07 +01:00
|
|
|
}
|
|
|
|
|
2022-02-25 00:24:45 +01:00
|
|
|
stop(): void {
|
|
|
|
this.disconnect();
|
|
|
|
this.queuePosition = 0;
|
|
|
|
this.queue = [];
|
|
|
|
}
|
|
|
|
|
2021-11-19 18:13:45 +01:00
|
|
|
private getHashForCache(url: string): string {
|
|
|
|
return hasha(url);
|
2020-03-14 02:36:42 +01:00
|
|
|
}
|
|
|
|
|
2022-03-12 16:50:29 +01:00
|
|
|
private async getStream(song: QueuedSong, options: {seek?: number; to?: number} = {}): Promise<Readable> {
|
|
|
|
if (song.source === MediaSource.HLS) {
|
|
|
|
return this.createReadStream(song.url);
|
|
|
|
}
|
|
|
|
|
2020-03-18 23:15:45 +01:00
|
|
|
let ffmpegInput = '';
|
2020-03-25 23:32:43 +01:00
|
|
|
const ffmpegInputOptions: string[] = [];
|
2020-03-18 23:15:45 +01:00
|
|
|
let shouldCacheVideo = false;
|
|
|
|
|
2020-03-28 00:28:50 +01:00
|
|
|
let format: ytdl.videoFormat | undefined;
|
|
|
|
|
2021-11-19 18:13:45 +01:00
|
|
|
try {
|
2022-03-12 16:50:29 +01:00
|
|
|
ffmpegInput = await this.fileCache.getPathFor(this.getHashForCache(song.url));
|
2020-03-28 00:28:50 +01:00
|
|
|
|
|
|
|
if (options.seek) {
|
|
|
|
ffmpegInputOptions.push('-ss', options.seek.toString());
|
|
|
|
}
|
2022-03-10 03:47:52 +01:00
|
|
|
|
|
|
|
if (options.to) {
|
|
|
|
ffmpegInputOptions.push('-to', options.to.toString());
|
|
|
|
}
|
2021-11-19 18:13:45 +01:00
|
|
|
} catch {
|
2020-03-18 23:15:45 +01:00
|
|
|
// Not yet cached, must download
|
2022-03-12 16:50:29 +01:00
|
|
|
const info = await ytdl.getInfo(song.url);
|
2020-03-14 02:36:42 +01:00
|
|
|
|
2020-03-18 23:15:45 +01:00
|
|
|
const {formats} = info;
|
2020-03-14 02:36:42 +01:00
|
|
|
|
2020-03-18 23:15:45 +01:00
|
|
|
const filter = (format: ytdl.videoFormat): boolean => format.codecs === 'opus' && format.container === 'webm' && format.audioSampleRate !== undefined && parseInt(format.audioSampleRate, 10) === 48000;
|
2020-03-14 02:36:42 +01:00
|
|
|
|
2020-03-28 00:28:50 +01:00
|
|
|
format = formats.find(filter);
|
2020-03-14 02:36:42 +01:00
|
|
|
|
2020-03-18 23:15:45 +01:00
|
|
|
const nextBestFormat = (formats: ytdl.videoFormat[]): ytdl.videoFormat | undefined => {
|
2020-10-24 18:32:43 +02:00
|
|
|
if (formats[0].isLive) {
|
2020-03-25 23:59:09 +01:00
|
|
|
formats = formats.sort((a, b) => (b as unknown as {audioBitrate: number}).audioBitrate - (a as unknown as {audioBitrate: number}).audioBitrate); // Bad typings
|
2020-03-14 02:36:42 +01:00
|
|
|
|
2020-03-18 23:15:45 +01:00
|
|
|
return formats.find(format => [128, 127, 120, 96, 95, 94, 93].includes(parseInt(format.itag as unknown as string, 10))); // Bad typings
|
|
|
|
}
|
2020-03-15 03:48:08 +01:00
|
|
|
|
2020-03-18 23:15:45 +01:00
|
|
|
formats = formats
|
|
|
|
.filter(format => format.averageBitrate)
|
2020-10-24 18:32:43 +02:00
|
|
|
.sort((a, b) => {
|
|
|
|
if (a && b) {
|
|
|
|
return b.averageBitrate! - a.averageBitrate!;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
});
|
2020-03-18 23:15:45 +01:00
|
|
|
return formats.find(format => !format.bitrate) ?? formats[0];
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!format) {
|
|
|
|
format = nextBestFormat(info.formats);
|
|
|
|
|
|
|
|
if (!format) {
|
|
|
|
// If still no format is found, throw
|
|
|
|
throw new Error('Can\'t find suitable format.');
|
|
|
|
}
|
2020-03-15 03:48:08 +01:00
|
|
|
}
|
|
|
|
|
2020-03-18 23:15:45 +01:00
|
|
|
ffmpegInput = format.url;
|
2020-03-14 02:36:42 +01:00
|
|
|
|
2020-03-18 23:15:45 +01:00
|
|
|
// Don't cache livestreams or long videos
|
|
|
|
const MAX_CACHE_LENGTH_SECONDS = 30 * 60; // 30 minutes
|
2022-03-10 03:47:52 +01:00
|
|
|
shouldCacheVideo = !info.player_response.videoDetails.isLiveContent && parseInt(info.videoDetails.lengthSeconds, 10) < MAX_CACHE_LENGTH_SECONDS && !options.seek && !options.to;
|
2020-03-15 03:48:08 +01:00
|
|
|
|
2020-03-18 23:15:45 +01:00
|
|
|
ffmpegInputOptions.push(...[
|
|
|
|
'-reconnect',
|
|
|
|
'1',
|
|
|
|
'-reconnect_streamed',
|
|
|
|
'1',
|
|
|
|
'-reconnect_delay_max',
|
2021-09-20 04:24:46 +02:00
|
|
|
'5',
|
2020-03-18 23:15:45 +01:00
|
|
|
]);
|
2020-03-14 02:36:42 +01:00
|
|
|
|
2020-03-28 00:28:50 +01:00
|
|
|
if (options.seek) {
|
2022-03-10 03:47:52 +01:00
|
|
|
ffmpegInputOptions.push('-ss', options.seek.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.to) {
|
|
|
|
ffmpegInputOptions.push('-to', options.to.toString());
|
2020-03-28 00:28:50 +01:00
|
|
|
}
|
2020-03-14 02:36:42 +01:00
|
|
|
}
|
|
|
|
|
2022-03-12 16:50:29 +01:00
|
|
|
return this.createReadStream(ffmpegInput, {ffmpegInputOptions, cache: shouldCacheVideo});
|
2020-03-14 02:36:42 +01:00
|
|
|
}
|
2020-03-15 20:36:59 +01:00
|
|
|
|
2020-03-16 01:30:07 +01:00
|
|
|
private startTrackingPosition(initalPosition?: number): void {
|
2020-03-19 04:29:43 +01:00
|
|
|
if (initalPosition !== undefined) {
|
2020-03-16 01:30:07 +01:00
|
|
|
this.positionInSeconds = initalPosition;
|
|
|
|
}
|
2020-03-15 21:35:34 +01:00
|
|
|
|
2020-03-16 01:30:07 +01:00
|
|
|
if (this.playPositionInterval) {
|
|
|
|
clearInterval(this.playPositionInterval);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.playPositionInterval = setInterval(() => {
|
|
|
|
this.positionInSeconds++;
|
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
private stopTrackingPosition(): void {
|
|
|
|
if (this.playPositionInterval) {
|
|
|
|
clearInterval(this.playPositionInterval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private attachListeners(): void {
|
|
|
|
if (!this.voiceConnection) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-17 22:16:00 +01:00
|
|
|
if (this.voiceConnection.listeners(VoiceConnectionStatus.Disconnected).length === 0) {
|
|
|
|
this.voiceConnection.on(VoiceConnectionStatus.Disconnected, this.onVoiceConnectionDisconnect.bind(this));
|
2021-10-03 18:59:26 +02:00
|
|
|
}
|
2020-03-16 01:30:07 +01:00
|
|
|
|
2021-11-12 20:30:18 +01:00
|
|
|
if (!this.audioPlayer) {
|
2020-03-16 01:30:07 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-17 22:16:00 +01:00
|
|
|
if (this.audioPlayer.listeners('stateChange').length === 0) {
|
2022-03-13 23:30:36 +01:00
|
|
|
this.audioPlayer.on(AudioPlayerStatus.Idle, this.onAudioPlayerIdle.bind(this));
|
2021-10-03 18:59:26 +02:00
|
|
|
}
|
2020-03-21 02:47:04 +01:00
|
|
|
}
|
2020-03-19 04:29:43 +01:00
|
|
|
|
2020-03-21 02:47:04 +01:00
|
|
|
private onVoiceConnectionDisconnect(): void {
|
2021-11-12 20:30:18 +01:00
|
|
|
this.disconnect();
|
2020-03-21 02:47:04 +01:00
|
|
|
}
|
2020-03-19 04:29:43 +01:00
|
|
|
|
2022-03-13 23:30:36 +01:00
|
|
|
private async onAudioPlayerIdle(_oldState: AudioPlayerState, newState: AudioPlayerState): Promise<void> {
|
2020-03-21 02:47:04 +01:00
|
|
|
// Automatically advance queued song at end
|
2021-11-12 20:30:18 +01:00
|
|
|
if (newState.status === AudioPlayerStatus.Idle && this.status === STATUS.PLAYING) {
|
2021-04-23 18:30:31 +02:00
|
|
|
await this.forward(1);
|
2020-03-21 02:47:04 +01:00
|
|
|
}
|
2020-03-15 20:36:59 +01:00
|
|
|
}
|
2022-03-12 16:50:29 +01:00
|
|
|
|
|
|
|
private async createReadStream(url: string, options: {ffmpegInputOptions?: string[]; cache?: boolean} = {}): Promise<Readable> {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const capacitor = new WriteStream();
|
|
|
|
|
|
|
|
if (options?.cache) {
|
|
|
|
const cacheStream = this.fileCache.createWriteStream(this.getHashForCache(url));
|
|
|
|
capacitor.createReadStream().pipe(cacheStream);
|
|
|
|
}
|
|
|
|
|
2022-03-18 00:17:22 +01:00
|
|
|
const returnedStream = capacitor.createReadStream();
|
|
|
|
let hasReturnedStreamClosed = false;
|
|
|
|
|
2022-03-12 16:50:29 +01:00
|
|
|
const stream = ffmpeg(url)
|
|
|
|
.inputOptions(options?.ffmpegInputOptions ?? ['-re'])
|
|
|
|
.noVideo()
|
|
|
|
.audioCodec('libopus')
|
|
|
|
.outputFormat('webm')
|
|
|
|
.on('error', error => {
|
2022-03-18 00:17:22 +01:00
|
|
|
if (!hasReturnedStreamClosed) {
|
|
|
|
reject(error);
|
|
|
|
}
|
2022-03-12 16:50:29 +01:00
|
|
|
})
|
|
|
|
.on('start', command => {
|
|
|
|
debug(`Spawned ffmpeg with ${command as string}`);
|
|
|
|
});
|
|
|
|
|
|
|
|
stream.pipe(capacitor);
|
|
|
|
|
2022-03-18 00:17:22 +01:00
|
|
|
returnedStream.on('close', () => {
|
|
|
|
stream.kill('SIGKILL');
|
|
|
|
hasReturnedStreamClosed = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
resolve(returnedStream);
|
2022-03-12 16:50:29 +01:00
|
|
|
});
|
|
|
|
}
|
2022-03-19 16:03:08 +01:00
|
|
|
|
|
|
|
move(from: number, to: number): void {
|
|
|
|
|
|
|
|
if (from > this.queueSize() || to > this.queueSize()){
|
|
|
|
throw new Error('Move index is outside the range of the queue.' )
|
|
|
|
}
|
|
|
|
|
|
|
|
this.queue.splice( this.queuePosition + to ,0, this.queue.splice(this.queuePosition + from, 1)[0]);
|
|
|
|
}
|
|
|
|
|
2020-03-13 04:41:26 +01:00
|
|
|
}
|