0.6.0 Update

[ HLS to Dash Manifest ]
Made the loop properly to 5 mins. [ for avoiding memory issues for long playback ]
This commit is contained in:
Killer069 2021-08-27 15:09:53 +05:30 committed by GitHub
commit ede1b2af8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 102 additions and 215 deletions

View File

@ -18,21 +18,6 @@ client.on('messageCreate', async message => {
let args = message.content.split('play ')[1] let args = message.content.split('play ')[1]
let yt_info = await youtube.search(args) let yt_info = await youtube.search(args)
let stream = await youtube.stream(yt_info[0].url) let stream = await youtube.stream(yt_info[0].url)
/*
OR if you want to stream Live Video have less delay
let stream = await youtube.stream(yt_info[0].url, { low_latency : true })
OR if you want higher quality audio Live Stream
let stream = await youtube.stream(yt_info[0].url, { preferred_quality : "480p"}) // You can have resolution upto 1080p
Default : preferred_quality : "144p"
OR both
let stream = await youtube.stream(yt_info[0].url, { low_latency : true ,preferred_quality : "480p"})
*/
let resource = createAudioResource(stream.stream, { let resource = createAudioResource(stream.stream, {
inputType : stream.type inputType : stream.type

View File

@ -17,21 +17,6 @@ client.on('messageCreate', async message => {
let args = message.content.split('play ')[1].split(' ')[0] let args = message.content.split('play ')[1].split(' ')[0]
let stream = await youtube.stream(args) let stream = await youtube.stream(args)
/*
OR if you want to stream Live Video have less delay
let stream = await youtube.stream(args, { low_latency : true })
OR if you want higher quality audio Live Stream
let stream = await youtube.stream(args, { preferred_quality : "480p"}) // You can have resolution upto 1080p
Default : preferred_quality : "144p"
OR both
let stream = await youtube.stream(args, { low_latency : true ,preferred_quality : "480p"})
*/
let resource = createAudioResource(stream.stream, { let resource = createAudioResource(stream.stream, {
inputType : stream.type inputType : stream.type

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{ {
"name": "play-dl", "name": "play-dl",
"version": "0.5.6", "version": "0.6.0",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "play-dl", "name": "play-dl",
"version": "0.5.6", "version": "0.6.0",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"got": "^11.8.2" "got": "^11.8.2"

View File

@ -1,6 +1,6 @@
{ {
"name": "play-dl", "name": "play-dl",
"version": "0.5.6", "version": "0.6.0",
"description": "YouTube, SoundCloud, Spotify streaming for discord.js bots", "description": "YouTube, SoundCloud, Spotify streaming for discord.js bots",
"main": "dist/index.js", "main": "dist/index.js",
"typings": "dist/index.d.ts", "typings": "dist/index.d.ts",

View File

@ -12,94 +12,45 @@ export interface FormatInterface{
export class LiveStreaming{ export class LiveStreaming{
type : StreamType type : StreamType
stream : PassThrough stream : PassThrough
private low_latency : boolean; private base_url : string
private format : FormatInterface private url : string
private interval : number private interval : number
private packet_count : number private packet_count : number
private timer : NodeJS.Timer | null private timer : NodeJS.Timer | null
private segments_urls : string[] private segments_urls : string[]
constructor(format : FormatInterface, low_latency : boolean){ constructor(dash_url : string, target_interval : number){
this.type = StreamType.Arbitrary this.type = StreamType.Arbitrary
this.low_latency = low_latency || false this.url = dash_url
this.format = format this.base_url = ''
this.stream = new PassThrough({ highWaterMark : 10 * 1000 * 1000 }) this.stream = new PassThrough({ highWaterMark : 10 * 1000 * 1000 })
this.segments_urls = [] this.segments_urls = []
this.packet_count = 0 this.packet_count = 0
this.interval = (this.format.targetDurationSec / 2) * 1000 || 0
this.timer = null this.timer = null
this.interval = target_interval * 1000 || 0
this.stream.on('close', () => { this.stream.on('close', () => {
this.cleanup() this.cleanup()
}); });
(this.low_latency) ? this.live_loop() :this.start() this.start()
} }
private async live_loop(){ private async dash_getter(){
if(this.stream.destroyed) { let response = await got(this.url)
this.cleanup() let audioFormat = response.body.split('<AdaptationSet id="0"')[1].split('</AdaptationSet>')[0].split('</Representation>')
return if(audioFormat[audioFormat.length - 1] === '') audioFormat.pop()
} this.base_url = audioFormat[audioFormat.length - 1].split('<BaseURL>')[1].split('</BaseURL>')[0]
await this.manifest_getter() let list = audioFormat[audioFormat.length - 1].split('<SegmentList>')[1].split('</SegmentList>')[0]
this.segments_urls.splice(0, this.segments_urls.length - 2) this.segments_urls = list.replace(new RegExp('<SegmentURL media="', 'g'), '').split('"/>')
if(this.packet_count === 0) this.packet_count = Number(this.segments_urls[0].split('index.m3u8/sq/')[1].split('/')[0]) if(this.segments_urls[this.segments_urls.length - 1] === '') this.segments_urls.pop()
for await (let url of this.segments_urls){
await (async () => {
return new Promise(async (resolve, reject) => {
if(Number(url.split('index.m3u8/sq/')[1].split('/')[0]) !== this.packet_count){
resolve('')
return
}
let stream = this.got_stream(url)
stream.on('data', (chunk) => this.stream.write(chunk))
stream.on('end', () => {
this.packet_count++
resolve('')
})
})
})()
}
this.timer = setTimeout(async () => {
await this.looping()
}, this.interval)
}
private async looping(){
if(this.stream.destroyed){
this.cleanup()
return
}
await this.manifest_getter()
this.segments_urls.splice(0, (this.segments_urls.length / 2))
for await (let url of this.segments_urls){
await (async () => {
return new Promise(async (resolve, reject) => {
if(Number(url.split('index.m3u8/sq/')[1].split('/')[0]) !== this.packet_count){
resolve('')
return
}
let stream = this.got_stream(url)
stream.on('data', (chunk) => this.stream.write(chunk))
stream.on('end', () => {
this.packet_count++
resolve('')
})
})
})()
}
this.timer = setTimeout(async () => {
await this.looping()
}, this.interval)
}
private async manifest_getter(){
let response = await got(this.format.url)
this.segments_urls = response.body.split('\n').filter((x) => x.startsWith('https'))
} }
private cleanup(){ private cleanup(){
clearTimeout(this.timer as NodeJS.Timer) clearTimeout(this.timer as NodeJS.Timer)
this.timer = null this.timer = null
this.url = ''
this.base_url = ''
this.segments_urls = [] this.segments_urls = []
this.packet_count = 0 this.packet_count = 0
this.interval = 0
} }
private async start(){ private async start(){
@ -107,16 +58,15 @@ export class LiveStreaming{
this.cleanup() this.cleanup()
return return
} }
await this.manifest_getter() await this.dash_getter()
if(this.packet_count === 0) this.packet_count = Number(this.segments_urls[0].split('index.m3u8/sq/')[1].split('/')[0]) if(this.packet_count === 0) this.packet_count = Number(this.segments_urls[0].split('sq/')[1].split('/')[0])
for await (let url of this.segments_urls){ for await (let segment of this.segments_urls){
if(Number(segment.split('sq/')[1].split('/')[0]) !== this.packet_count){
continue
}
await (async () => { await (async () => {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
if(Number(url.split('index.m3u8/sq/')[1].split('/')[0]) !== this.packet_count){ let stream = got.stream(this.base_url + segment)
resolve('')
return
}
let stream = this.got_stream(url)
stream.on('data', (chunk) => this.stream.write(chunk)) stream.on('data', (chunk) => this.stream.write(chunk))
stream.on('end', () => { stream.on('end', () => {
this.packet_count++ this.packet_count++
@ -125,25 +75,23 @@ export class LiveStreaming{
}) })
})() })()
} }
this.timer = setTimeout(async () => { this.timer = setTimeout(() => {
await this.start() this.start()
}, this.interval) }, this.interval)
} }
private got_stream(url: string){
return got.stream(url)
}
} }
export class LiveEnded{ export class LiveEnded{
type : StreamType type : StreamType
stream : PassThrough stream : PassThrough
private format : FormatInterface private url : string;
private base_url : string;
private packet_count : number private packet_count : number
private segments_urls : string[] private segments_urls : string[]
constructor(format : FormatInterface){ constructor(dash_url : string){
this.type = StreamType.Arbitrary this.type = StreamType.Arbitrary
this.format = format this.url = dash_url
this.base_url = ''
this.stream = new PassThrough({ highWaterMark : 10 * 1000 * 1000 }) this.stream = new PassThrough({ highWaterMark : 10 * 1000 * 1000 })
this.segments_urls = [] this.segments_urls = []
this.packet_count = 0 this.packet_count = 0
@ -153,31 +101,37 @@ export class LiveEnded{
this.start() this.start()
} }
async manifest_getter(){ private async dash_getter(){
let response = await got(this.format.url) let response = await got(this.url)
this.segments_urls = response.body.split('\n').filter((x) => x.startsWith('https')) let audioFormat = response.body.split('<AdaptationSet id="0"')[1].split('</AdaptationSet>')[0].split('</Representation>')
if(audioFormat[audioFormat.length - 1] === '') audioFormat.pop()
this.base_url = audioFormat[audioFormat.length - 1].split('<BaseURL>')[1].split('</BaseURL>')[0]
let list = audioFormat[audioFormat.length - 1].split('<SegmentList>')[1].split('</SegmentList>')[0]
this.segments_urls = list.replace(new RegExp('<SegmentURL media="', 'g'), '').split('"/>')
if(this.segments_urls[this.segments_urls.length - 1] === '') this.segments_urls.pop()
} }
private cleanup(){ private cleanup(){
this.url = ''
this.base_url = ''
this.segments_urls = [] this.segments_urls = []
this.packet_count = 0 this.packet_count = 0
} }
async start(){ private async start(){
if(this.stream.destroyed){ if(this.stream.destroyed){
this.cleanup() this.cleanup()
return return
} }
await this.manifest_getter() await this.dash_getter()
if(this.packet_count === 0) this.packet_count = Number(this.segments_urls[0].split('index.m3u8/sq/')[1].split('/')[0]) if(this.packet_count === 0) this.packet_count = Number(this.segments_urls[0].split('sq/')[1].split('/')[0])
for await (let url of this.segments_urls){ for await (let segment of this.segments_urls){
if(Number(segment.split('sq/')[1].split('/')[0]) !== this.packet_count){
continue
}
await (async () => { await (async () => {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
if(Number(url.split('index.m3u8/sq/')[1].split('/')[0]) !== this.packet_count){ let stream = got.stream(this.base_url + segment)
resolve('')
return
}
let stream = this.got_stream(url)
stream.on('data', (chunk) => this.stream.write(chunk)) stream.on('data', (chunk) => this.stream.write(chunk))
stream.on('end', () => { stream.on('end', () => {
this.packet_count++ this.packet_count++
@ -187,10 +141,6 @@ export class LiveEnded{
})() })()
} }
} }
private got_stream(url: string){
return got.stream(url)
}
} }
export class Stream { export class Stream {
@ -285,6 +235,6 @@ export class Stream {
this.timer = setTimeout(() => { this.timer = setTimeout(() => {
this.loop() this.loop()
}, 290 * 1000) }, 300 * 1000)
} }
} }

View File

@ -9,10 +9,6 @@ export enum StreamType{
Opus = 'opus', Opus = 'opus',
} }
interface StreamOptions {
low_latency : boolean;
preferred_quality : "144p" | "240p" | "360p" | "480p" | "720p" | "1080p"
}
interface InfoData{ interface InfoData{
LiveStreamData : { LiveStreamData : {
@ -38,14 +34,12 @@ function parseAudioFormats(formats : any[]){
return result return result
} }
export async function stream(url : string, options : StreamOptions = { low_latency : false, preferred_quality : "144p" }): Promise<Stream | LiveStreaming | LiveEnded>{ export async function stream(url : string): Promise<Stream | LiveStreaming | LiveEnded>{
let info = await video_info(url) let info = await video_info(url)
let final: any[] = []; let final: any[] = [];
let type : StreamType; let type : StreamType;
if(!options.low_latency) options.low_latency = false
if(!options.preferred_quality) options.preferred_quality = "144p"
if(info.LiveStreamData.isLive === true && info.LiveStreamData.hlsManifestUrl !== null) { if(info.LiveStreamData.isLive === true && info.LiveStreamData.hlsManifestUrl !== null) {
return await live_stream(info as InfoData, options) return await live_stream(info as InfoData)
} }
let audioFormat = parseAudioFormats(info.format) let audioFormat = parseAudioFormats(info.format)
@ -68,13 +62,11 @@ export async function stream(url : string, options : StreamOptions = { low_laten
return new Stream(final[0].url, type, info.video_details.durationInSec) return new Stream(final[0].url, type, info.video_details.durationInSec)
} }
export async function stream_from_info(info : InfoData, options : StreamOptions = { low_latency : false, preferred_quality : "144p" }): Promise<Stream | LiveStreaming | LiveEnded>{ export async function stream_from_info(info : InfoData): Promise<Stream | LiveStreaming | LiveEnded>{
let final: any[] = []; let final: any[] = [];
let type : StreamType; let type : StreamType;
if(!options.low_latency) options.low_latency = false
if(!options.preferred_quality) options.preferred_quality = "144p"
if(info.LiveStreamData.isLive === true && info.LiveStreamData.hlsManifestUrl !== null) { if(info.LiveStreamData.isLive === true && info.LiveStreamData.hlsManifestUrl !== null) {
return await live_stream(info as InfoData, options) return await live_stream(info as InfoData)
} }
let audioFormat = parseAudioFormats(info.format) let audioFormat = parseAudioFormats(info.format)
@ -105,22 +97,18 @@ function filterFormat(formats : any[], codec : string){
return result return result
} }
async function live_stream(info : InfoData, options : StreamOptions): Promise<LiveStreaming | LiveEnded>{ async function live_stream(info : InfoData): Promise<LiveStreaming | LiveEnded>{
let res_144 : FormatInterface = { let res_144 : FormatInterface = {
url : '', url : '',
targetDurationSec : 0, targetDurationSec : 0,
maxDvrDurationSec : 0 maxDvrDurationSec : 0
} }
info.format.forEach((format) => {
if(format.qualityLabel === options.preferred_quality) res_144 = format
else return
})
let stream : LiveStreaming | LiveEnded let stream : LiveStreaming | LiveEnded
if(info.video_details.duration === '0') { if(info.video_details.durationInSec === '0') {
stream = new LiveStreaming((res_144.url.length !== 0) ? res_144 : info.format[info.format.length - 2], options.low_latency) stream = new LiveStreaming(info.LiveStreamData.dashManifestUrl, info.format[info.format.length - 1].targetDurationSec)
} }
else { else {
stream = new LiveEnded((res_144.url.length !== 0) ? res_144 : info.format[info.format.length - 2]) stream = new LiveEnded(info.format[info.format.length - 2])
} }
return stream return stream
} }

View File

@ -75,8 +75,6 @@ function parseSeconds(seconds : number): string {
export async function video_info(url : string) { export async function video_info(url : string) {
let data = await video_basic_info(url) let data = await video_basic_info(url)
if(data.LiveStreamData.isLive === true && data.LiveStreamData.hlsManifestUrl !== null){ if(data.LiveStreamData.isLive === true && data.LiveStreamData.hlsManifestUrl !== null){
let m3u8 = await url_get(data.LiveStreamData.hlsManifestUrl)
data.format = await parseM3U8(m3u8, data.format)
return data return data
} }
else if(data.format[0].signatureCipher || data.format[0].cipher){ else if(data.format[0].signatureCipher || data.format[0].cipher){
@ -88,25 +86,6 @@ export async function video_info(url : string) {
} }
} }
async function parseM3U8(m3u8_data : string, formats : any[]): Promise<any[]>{
let lines = m3u8_data.split('\n')
formats.forEach((format) => {
if(!format.qualityLabel) return
let reso = format.width + 'x' + format.height
let index = -1;
let line_count = 0
lines.forEach((line) => {
index = line.search(reso)
if(index !== -1) {
format.url = lines[line_count+1]
}
line_count++
index = -1
})
})
return formats
}
export async function playlist_info(url : string, parseIncomplete : boolean = false) { export async function playlist_info(url : string, parseIncomplete : boolean = false) {
if (!url || typeof url !== "string") throw new Error(`Expected playlist url, received ${typeof url}!`); if (!url || typeof url !== "string") throw new Error(`Expected playlist url, received ${typeof url}!`);
if(url.search('(\\?|\\&)list\\=') === -1) throw new Error('This is not a PlayList URL') if(url.search('(\\?|\\&)list\\=') === -1) throw new Error('This is not a PlayList URL')