aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMax Isom <[email protected]>2020-03-14 21:48:08 -0500
committerMax Isom <[email protected]>2020-03-14 21:48:08 -0500
commit2f0acaa858a5b76fc4c38b7c1458c5f9e67c657b (patch)
tree95e9291efcd6110032366a96da2ac571db7ffaa1 /src
parentad4d49f763e28796ce586def0aa3b1765065b9e3 (diff)
downloadmuse-2f0acaa858a5b76fc4c38b7c1458c5f9e67c657b.tar.xz
muse-2f0acaa858a5b76fc4c38b7c1458c5f9e67c657b.zip
Add livestream support
Diffstat (limited to 'src')
-rw-r--r--src/services/player.ts44
1 files changed, 22 insertions, 22 deletions
diff --git a/src/services/player.ts b/src/services/player.ts
index 1970cc7..88457ce 100644
--- a/src/services/player.ts
+++ b/src/services/player.ts
@@ -1,12 +1,12 @@
import {inject, injectable} from 'inversify';
import {VoiceConnection, VoiceChannel} from 'discord.js';
import {promises as fs, createWriteStream} from 'fs';
-import {Readable} from 'stream';
+import {Readable, PassThrough} from 'stream';
import path from 'path';
import hasha from 'hasha';
import ytdl from 'ytdl-core';
import {WriteStream} from 'fs-capacitor';
-import prism from 'prism-media';
+import ffmpeg from 'fluent-ffmpeg';
import {TYPES} from '../types';
import Queue, {QueuedSong} from './queue';
@@ -185,7 +185,13 @@ export default class {
let format = formats.find(filter);
let canDirectPlay = true;
- const nextBestFormat = (formats: ytdl.videoFormat[]): ytdl.videoFormat => {
+ const nextBestFormat = (formats: ytdl.videoFormat[]): ytdl.videoFormat | undefined => {
+ if (formats[0].live) {
+ formats = formats.sort((a, b) => (b as any).audioBitrate - (a as any).audioBitrate); // Bad typings
+
+ return formats.find(format => [128, 127, 120, 96, 95, 94, 93].includes(parseInt(format.itag as unknown as string, 10))); // Bad typings
+ }
+
formats = formats
.filter(format => format.averageBitrate)
.sort((a, b) => b.averageBitrate - a.averageBitrate);
@@ -195,6 +201,11 @@ export default class {
if (!format) {
format = nextBestFormat(info.formats);
canDirectPlay = false;
+
+ if (!format) {
+ // If still no format is found, throw
+ throw new Error('Can\'t find suitable format.');
+ }
}
const cacheTempPath = this.getCachedPathTemp(url);
@@ -209,25 +220,14 @@ export default class {
if (canDirectPlay) {
youtubeStream = ytdl.downloadFromInfo(info, {format});
} else {
- youtubeStream = new prism.FFmpeg({
- args: [
- '-reconnect',
- '1',
- '-reconnect_streamed',
- '1',
- '-reconnect_delay_max',
- '5',
- '-i',
- format.url,
- '-loglevel',
- 'verbose',
- '-vn',
- '-acodec',
- 'libopus',
- '-f',
- 'webm'
- ]
- });
+ youtubeStream = ffmpeg(format.url).inputOptions([
+ '-reconnect',
+ '1',
+ '-reconnect_streamed',
+ '1',
+ '-reconnect_delay_max',
+ '5'
+ ]).noVideo().audioCodec('libopus').outputFormat('webm').pipe() as PassThrough;
}
const capacitor = new WriteStream();