Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const ytsr = require('youtube-sr').default
- const ytdl = require('ytdl-core-discord')
- const prism_media = require('prism-media')
- const FFMPEG_OPUS_ARGUMENTS = [
- '-analyzeduration',
- '0',
- '-loglevel',
- '0',
- '-acodec',
- 'libopus',
- '-f',
- 'opus',
- '-ar',
- '48000',
- '-ac',
- '2',
- ];
- class Search {
- /**
- *
- * @param {string} search
- * @returns Stream to be played in djs voice
- */
- async splay(search){
- this.search = search
- this.result = await ytsr.search(this.search, { limit: 1 }).then(x => x)
- this.info = await ytdl.getInfo(this.result[0].url)
- this.format_url = filter(this.info.formats)[0].url
- return [ffmpeg_reconnect(this.format_url), this.result]
- }
- /**
- *
- * @param {string} url
- * @returns
- */
- async uplay(url){
- this.url = url
- this.info = await ytdl.getInfo(this.url)
- this.format_url = filter(this.info.formats)[0].url
- return [ffmpeg_reconnect(this.format_url), {name : this.info.videoDetails.title, url : this.info.videoDetails.video_url, duration : time_convert(this.info.videoDetails.lengthSeconds)}]
- }
- /**
- *
- * @param {string} search
- * @param {number} limit
- * @returns List of songs
- */
- async search(search, limit){
- this.limit = limit
- this.search = search
- return await ytsr.search(this.search, { limit: this.limit }).then(x => x)
- }
- /**
- *
- * @param {string} search
- * @returns stream of 1st to be played
- */
- async playlist_play(search){
- this.search = search
- this.result = await ytsr.getPlaylist(this.search).then(x => x)
- this.info = await ytdl.getInfo(this.result.videos[0].url)
- this.format_url = filter(this.info.formats)[0].url
- return [ffmpeg_reconnect(this.format_url), this.result]
- }
- /**
- *
- * @param {string} search
- * @returns playlist videos
- */
- async playlist_list_full(search){
- this.search = search
- return await ytsr.getPlaylist(search).then(x => x.fetch()).then(x => x)
- }
- }
- /**
- *
- * @param {string} search_term
- * @returns search song and then play topmost
- */
- exports.splay = async (search_term) => {
- var cl = new Search
- return await cl.splay(search_term)
- }
- /**
- *
- * @param {string} url
- * @returns url playback
- */
- exports.uplay = async (url) => {
- var cl = new Search
- return await cl.uplay(url)
- }
- /**
- *
- * @param {string} term
- * @param {number} limit
- * @returns list of all songs
- */
- exports.search = async (term, limit) => {
- var cl = new Search
- return await cl.search(term, limit)
- }
- /**
- *
- * @param {string} url
- * @returns [stream, list]
- */
- exports.playlist_play = async (url) => {
- var cl = new Search
- return await cl.playlist_play(url)
- }
- /**
- *
- * @param {string} url
- * @returns all songs in playlist
- */
- exports.playlist_list_full = async (url) => {
- var cl = new Search
- return await cl.playlist_list_full(url)
- }
- function filter(format) {
- var results = []
- format.forEach((ele) => {
- if(ele.codecs === 'opus' && ele.container === 'webm' && ele.audioSampleRate === '48000' && ele.audioQuality === 'AUDIO_QUALITY_MEDIUM'){
- results.push(ele)
- }
- })
- return results
- }
- function ffmpeg_reconnect(url){
- return new prism_media.FFmpeg({
- args : ['-reconnect', '1', '-reconnect_streamed', '1', '-reconnect_delay_max', '5','-i', url, ...FFMPEG_OPUS_ARGUMENTS ]
- })
- }
- function time_convert(seconds){
- let mins = Math.floor(seconds / 60)
- let sec = seconds - (mins * 60)
- if(mins < 10) mins = `${mins}`
- if(sec < 10) sec = `0${sec}`
- return `${mins}:${sec}`
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement