UNPKG

discord-player-music

Version:

A powerful music module for your Discord bot with lots of features

58 lines (57 loc) 2.05 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const ytdl_core_1 = __importDefault(require("ytdl-core")); const prism_media_1 = require("prism-media"); /** * Generating a link to play a song using FFmpeg * * @param {Array<ytdl.videoFormat>} data YTDL Video Formats * * @returns {Array<ytdl.videoFormat>} All possible link formats * @private */ function formatURL(data) { let results = []; data.forEach(videoFormat => { if (videoFormat.codecs === 'opus' && videoFormat.container === 'webm' && videoFormat.audioSampleRate === '48000' && videoFormat.audioQuality === 'AUDIO_QUALITY_MEDIUM') results.push(videoFormat); }); return results; } /** * Creating a stream for playing songs based on FFmpeg * * @param {string} inputURL Track URL * @param {StreamOptions} options Filter Value * * @returns {Promise<FFmpeg>} FFmpeg Stream */ async function createStream(inputURL, options) { const trackInfo = await ytdl_core_1.default.getInfo(inputURL); const outputURL = formatURL(trackInfo.formats)[0].url; const FFMPEG_OPUS_DEFAULT = [ '-analyzeduration', '0', '-loglevel', '0', '-acodec', 'libopus', '-f', 'opus', '-ar', '48000', '-ac', '2' ]; let FFmpegArgs = ['-reconnect', '1', '-reconnect_streamed', '1', '-reconnect_delay_max', '5', '-i', outputURL, ...FFMPEG_OPUS_DEFAULT]; if (options.seek && options.seek != null) FFmpegArgs = FFmpegArgs.concat(['-ss', options.seek.toString()]); if (options.filter && options.filter != null) FFmpegArgs = FFmpegArgs.concat(['-af', options.filter]); const output = new prism_media_1.FFmpeg({ args: FFmpegArgs }); return output; } exports.default = Object.assign(createStream, ytdl_core_1.default);