Skip to content

Commit

Permalink
fix: going back to using ytdl-core. It needs direct changes in librar…
Browse files Browse the repository at this point in the history
  • Loading branch information
RafRunner committed Jul 30, 2024
1 parent ed0840e commit a14669c
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 19 deletions.
72 changes: 70 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"spotify-web-api-node": "5.0.2",
"winston": "^3.13.0",
"winston-daily-rotate-file": "^5.0.0",
"yt-stream": "1.6.1"
"yt-stream": "1.6.1",
"ytdl-core": "^4.11.5"
},
"devDependencies": {
"nodemon": "^3.1.0"
Expand Down
41 changes: 25 additions & 16 deletions src/botfunctions/player.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const ytstream = require('yt-stream');
// const ytstream = require('yt-stream');
const ytdl = require('ytdl-core');
const fs = require('fs');
const path = require('path');
const {
Expand All @@ -12,6 +13,7 @@ const ServerPlayer = require('../domain/ServerPlayer');
const PlaylistEntry = require('../domain/PlaylistEntry');
const logger = require('../util/logger');
const { YouTubeVideo } = require('play-dl');
const { Readable } = require('stream');

/**
*
Expand Down Expand Up @@ -86,13 +88,8 @@ async function playReq(serverPlayer, playlistEntry, sendMessage) {

try {
// const filePath = await downloadAudio(selectedSong, serverPlayer.guildId);
const ytStream = await ytstream.stream(selectedSong.url, {
quality: 'high',
type: 'audio',
highWaterMark: 256 * 1024,
download: true,
});
ytStream.stream.on('error', (error) => {
const stream = await createStream(selectedSong.url);
stream.on('error', (error) => {
logger.error(`Erro em playstream música "${selectedSong.title}" server ${serverPlayer.guildId}.`, error);
});

Expand All @@ -118,7 +115,7 @@ async function playReq(serverPlayer, playlistEntry, sendMessage) {
);
}

let resource = createAudioResource(ytStream.stream, {
let resource = createAudioResource(stream, {
inputType: StreamType.Arbitrary,
});

Expand Down Expand Up @@ -171,13 +168,7 @@ async function downloadAudio(selectedSong, guildId) {
fs.rmSync(filePath);
}

const ytStream = await ytstream.stream(selectedSong.url, {
quality: 'high',
type: 'audio',
highWaterMark: 256 * 1024,
download: true
});
const stream = ytStream.stream;
const stream = await createStream(selectedSong.url);
const writeStream = fs.createWriteStream(filePath);
stream.pipe(writeStream);

Expand All @@ -196,4 +187,22 @@ async function downloadAudio(selectedSong, guildId) {
return filePath;
}

/**
* Creates a stream from url
* @param {string} songUrl url to stream
* @returns {Promise<Readable>} the stream
*/
async function createStream(songUrl) {
// const ytStream = await ytstream.stream(selectedSong.url, {
// quality: 'high',
// type: 'audio',
// highWaterMark: 256 * 1024,
// download: true
// });
// return ytStream.stream;
return ytdl(songUrl, {
filter: 'audioonly',
})
}

module.exports = radin;

0 comments on commit a14669c

Please sign in to comment.