1.0.3 • Published 3 years ago

sliceplayer-js v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

SlicePlayer

SlicePlayer is an audio player library written in JavaScript which can load audio tracks from YouTube and convert them into a stream for use with Discord.js music bots.

NPM License

Features

  • Load YouTube musics and playlists.
  • Load Spotify tracks, top 10 from an artist, playlists, and albums.

Installation

# yarn
$ yarn add sliceplayer-js

# npm
$ npm install sliceplayer-js

Usage

Creating a player instance.

# ES Modules
import SlicePlayer from 'sliceplayer-js';
# CommonJS
const SlicePlayer = require('sliceplayer-js');

const player = new SlicePlayer({
  spotifyConfig: {
    clientId: 'client_id',
    clientSecret: 'client_secret',
    splitSongsInParallel: 1,
  },
});

Loading musics and playlists

// Valid URLs
const ytbeVideo = 'https://youtu.be/7iTCAirmYro';
const ytVideo = 'https://www.youtube.com/watch?v=DYcUt0DGK6w';
const ytMusicVideo = 'https://music.youtube.com/watch?v=DYcUt0DGK6w';
const ytPlaylist =
  'https://youtube.com/playlist?list=PL40m373_h6Fk52Q9Ih0zkA9zk9XcCziXT';
const ytMusicPlaylist =
  'https://music.youtube.com/playlist?list=PL40m373_h6Fk52Q9Ih0zkA9zk9XcCziXT';
const spfTrack = 'https://open.spotify.com/track/2HrXRO1Z8xOpirCjqfqoKr';
const spfArtist = 'https://open.spotify.com/artist/1GfHpetDFhvqfm5pceDTrX';
const spfAlbum = 'https://open.spotify.com/album/02pWrAza7AhDJyIdrLSJoI';
const spfPlaylist = 'https://open.spotify.com/playlist/4RYqald16pnbRfcz6gkLeP';

async function load() {
  try {
    // By default the player doesn't search for a music in YouTube, if you want to search a music set the second parameter to true: loadItem('my search', true);
    const item = await player.loadItem(ytVideo);
    // Result: YoutubeVideo
    console.log(JSON.stringify(item, null, 4));
  } catch (error) {
    // Handle errors
    console.error(error);
  }
}

Converting a YoutubeVideo to OpusStream

const item = await player.loadItem(
  'https://www.youtube.com/watch?v=DYcUt0DGK6w',
);
const opusStream = player.videoToStream(item);

You can find more examples right here.

API

SlicePlayer

new SlicePlayer(playerOptions);

playerOptions

SlicePlayer options.

NameTypeDefaultDescription
spotifyConfigobjectnullSpotify credentials and config for the player.

spotifyConfig

Spotify configuration.

NameTypeDefaultDescription
clientIdstringRequiredSpotify Web API Client ID.
clientSecretstringRequiredSpotify Web API Client Secret.
splitSongsInParallelnumber20The number of songs per list loaded in parallel when loading a playlist.

Members

NameTypeInformation
youtubeYoutubeAPIYoutubeAPI instance used.
spotifySpotifyAPISpotifyAPI instance used.

Functions

loadItem(arg, search)

Load a item from URL or Search string.

ParamTypeDefaultDescription
argstringRequiredURL or Search string if search is enabled.
searchbooleanfalseEnable or disable YouTube search.

throws Error if nothing has found.\ returns Promise<SliceItem>.

videoToStream(video)

Convert a video into a Stream.

ParamTypeDefaultDescription
videoYoutubeVideoRequiredThe Video to convert into a Stream.

throws Error if the video doesn't have formats or suitable audio formats.\ returns OpusStream.

YoutubeAPI

Functions

searchVideos(search, limit)

Load search videos using https://www.youtube.com/results

ParamTypeDefaultDescription
searchstringRequiredVideo title to search.
limitnumber5Results parsed limit.

throws Error if unexpected error occurred while parsing data.\ returns Promise<YoutubeSearch>.

searchPlaylists(search, limit)

Load search playlists using https://www.youtube.com/results

ParamTypeDefaultDescription
searchstringRequiredPlaylist title to search.
limitnumber5Results parsed limit.

throws Error if unexpected error occurred while parsing data.\ returns Promise<YoutubeSearch>.

getVideo(videoId)

Get a Youtube Video data from videoId

ParamTypeDefaultDescription
videoIdstringRequiredYoutube Video ID. e.G: DYcUt0DGK6w

throws Error if unexpected error occurred while parsing data.\ returns Promise<YoutubeVideo>.

getPlaylist(playlistId)

Get a Youtube Playlist data from playlistId

ParamTypeDefaultDescription
playlistIdstringRequiredYoutube Playlist ID. e.G: PL40m373_h6Fk52Q9Ih0zkA9zk9XcCziXT

throws Error if unexpected error occurred while parsing data.\ returns Promise<YoutubePlaylist>.

SpotifyAPI

Functions

getTrack(trackId)

Load a track from Spotify using Spotify Web API.

ParamTypeDefaultDescription
trackIdstringRequiredSpotify Track ID. e.G: 2HrXRO1Z8xOpirCjqfqoKr

throws Error if unexpected error occurred while parsing data.\ returns Promise<SpotifyTrack>.

getArtist(artistId)

Load top 10 musics from a Spotify artist using Spotify Web API.

ParamTypeDefaultDescription
artistIdstringRequiredSpotify Artist ID. e.G: 1GfHpetDFhvqfm5pceDTrX

throws Error if unexpected error occurred while parsing data.\ returns Promise<SpotifyArtist>.

getAlbum(albumId)

Load musics from a Spotify album using Spotify Web API.

ParamTypeDefaultDescription
albumIdstringRequiredSpotify Album ID. e.G: 02pWrAza7AhDJyIdrLSJoI

throws Error if unexpected error occurred while parsing data.\ returns Promise<SpotifyAlbum>.

getPlaylist(playlistId)

Load musics from a Spotify playlist using Spotify Web API.

ParamTypeDefaultDescription
playlistIdstringRequiredSpotify Playlist ID. e.G: 4RYqald16pnbRfcz6gkLeP

throws Error if unexpected error occurred while parsing data.\ returns Promise<SpotifyPlaylist>.

YoutubeVideo

Members

NameTypeDefaultDescription
typestringvideoThe type of SliceItem
detailsYoutubeVideoDetailsVideo details
formatsYoutubeVideoFormat[]Video formats

YoutubePlaylist

Members

NameTypeDefaultDescription
typestringplaylistThe type of SliceItem
detailsYoutubePlaylistDetailsPlaylist details
videosYoutubePlaylistVideo[]Playlist videos

YoutubeVideoDetails

Members

NameType
videoIdstring
urlstring
lengthSecondsnumber
thumbnailsYoutubeThumbnail[]
titlestring
authorstring
keywordsstring
shortDescriptionstring
averageRatingnumber
viewCountnumber
isLiveboolean
isLiveContentboolean
liveUrlstring or null

YoutubeVideoFormat

Members

NameType
mimeTypestring
qualityLabelstring or null
bitratenumber
audioBitratenumber or null
urlstring
isAdaptiveboolean
hasVideoboolean
hasAudioboolean
containernumber
codecsstring
isLiveboolean
isHLSboolean
isDashMPDboolean
liveUrlstring or null
videoInfoobject
audioInfoobject

YoutubePlaylistDetails

Members

NameType
idstring
urlstring
thumbnailsYoutubeThumbnail[]
titlestring
authorstring
videoCountnumber
viewCountnumber

YoutubePlaylistVideo

Members

NameType
idstring
urlstring
authorstring
titlestring
lengthSecondsnumber
thumbnailsYoutubeThumbnail[]
isLiveboolean
isPlayableboolean

FAQ

How can i get my Spotify credentials?

You need to create a Spotify Application right here.

The YoutubeVideo from a playlist doesn't have formats, how i can play that?

You need to call the method player.youtube.getVideo(playlistVideo.details.id) to get a video with formats, youtube doesn't provide video formats until the watch page.

Authors

License

SlicePlayer is MIT licensed.

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago