Live Stream Latency issues fixed

This commit is contained in:
killer069 2021-08-22 14:34:38 +05:30
parent a05ea18589
commit 6a380a7f54
4 changed files with 17 additions and 17 deletions

View File

@ -21,7 +21,7 @@ client.on('messageCreate', async message => {
/* /*
OR if you want to stream Live Video have less delay OR if you want to stream Live Video have less delay
let stream = await youtube.stream(yt_info[0].url, { actual_live : true }) let stream = await youtube.stream(yt_info[0].url, { low_latency : true })
OR if you want higher quality audio Live Stream OR if you want higher quality audio Live Stream
@ -31,7 +31,7 @@ client.on('messageCreate', async message => {
OR both OR both
let stream = await youtube.stream(yt_info[0].url, { actual_live : true ,preferred_quality : "480p"}) 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, {

View File

@ -20,7 +20,7 @@ client.on('messageCreate', async message => {
/* /*
OR if you want to stream Live Video have less delay OR if you want to stream Live Video have less delay
let stream = await youtube.stream(args, { actual_live : true }) let stream = await youtube.stream(args, { low_latency : true })
OR if you want higher quality audio Live Stream OR if you want higher quality audio Live Stream
@ -30,7 +30,7 @@ client.on('messageCreate', async message => {
OR both OR both
let stream = await youtube.stream(args, { actual_live : true ,preferred_quality : "480p"}) let stream = await youtube.stream(args, { low_latency : true ,preferred_quality : "480p"})
*/ */
let resource = createAudioResource(stream.stream, { let resource = createAudioResource(stream.stream, {

View File

@ -12,15 +12,15 @@ export interface FormatInterface{
export class LiveStreaming{ export class LiveStreaming{
type : StreamType type : StreamType
stream : PassThrough stream : PassThrough
private actual_live : boolean; private low_latency : boolean;
private format : FormatInterface private format : FormatInterface
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, actual_live : boolean){ constructor(format : FormatInterface, low_latency : boolean){
this.type = StreamType.Arbitrary this.type = StreamType.Arbitrary
this.actual_live = actual_live || false this.low_latency = low_latency || false
this.format = format this.format = format
this.stream = new PassThrough({ highWaterMark : 10 * 1000 * 1000 }) this.stream = new PassThrough({ highWaterMark : 10 * 1000 * 1000 })
this.segments_urls = [] this.segments_urls = []
@ -30,7 +30,7 @@ export class LiveStreaming{
this.stream.on('close', () => { this.stream.on('close', () => {
this.cleanup() this.cleanup()
}); });
(this.actual_live) ? this.live_loop() :this.start() (this.low_latency) ? this.live_loop() :this.start()
} }
private async live_loop(){ private async live_loop(){
@ -39,7 +39,7 @@ export class LiveStreaming{
return return
} }
await this.manifest_getter() await this.manifest_getter()
this.segments_urls.splice(0, (this.segments_urls.length / 2)) this.segments_urls.splice(0, this.segments_urls.length - 2)
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('index.m3u8/sq/')[1].split('/')[0])
for await (let url of this.segments_urls){ for await (let url of this.segments_urls){
await (async () => { await (async () => {
@ -57,7 +57,7 @@ export class LiveStreaming{
}) })
})() })()
} }
this.interval = 1 * 1000 this.interval = this.format.targetDurationSec
this.timer = setTimeout(async () => { this.timer = setTimeout(async () => {
await this.looping() await this.looping()
}, this.interval) }, this.interval)
@ -69,6 +69,7 @@ export class LiveStreaming{
return return
} }
await this.manifest_getter() await this.manifest_getter()
this.segments_urls.splice(0, (this.segments_urls.length / 2))
for await (let url of this.segments_urls){ for await (let url of this.segments_urls){
await (async () => { await (async () => {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
@ -85,7 +86,6 @@ export class LiveStreaming{
}) })
})() })()
} }
this.interval = 1 * 1000
this.timer = setTimeout(async () => { this.timer = setTimeout(async () => {
await this.looping() await this.looping()
}, this.interval) }, this.interval)

View File

@ -10,7 +10,7 @@ export enum StreamType{
} }
interface StreamOptions { interface StreamOptions {
actual_live : boolean; low_latency : boolean;
preferred_quality : "144p" | "240p" | "360p" | "480p" | "720p" | "1080p" preferred_quality : "144p" | "240p" | "360p" | "480p" | "720p" | "1080p"
} }
@ -38,11 +38,11 @@ function parseAudioFormats(formats : any[]){
return result return result
} }
export async function stream(url : string, options : StreamOptions = { actual_live : false, preferred_quality : "144p" }): Promise<Stream | LiveStreaming | LiveEnded>{ export async function stream(url : string, options : StreamOptions = { low_latency : false, preferred_quality : "144p" }): 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.actual_live) options.actual_live = false if(!options.low_latency) options.low_latency = false
if(!options.preferred_quality) options.preferred_quality = "144p" 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, options)
@ -68,10 +68,10 @@ export async function stream(url : string, options : StreamOptions = { actual_li
return new Stream(final[0].url, type) return new Stream(final[0].url, type)
} }
export async function stream_from_info(info : InfoData, options : StreamOptions = { actual_live : false, preferred_quality : "144p" }): Promise<Stream | LiveStreaming | LiveEnded>{ export async function stream_from_info(info : InfoData, options : StreamOptions = { low_latency : false, preferred_quality : "144p" }): Promise<Stream | LiveStreaming | LiveEnded>{
let final: any[] = []; let final: any[] = [];
let type : StreamType; let type : StreamType;
if(!options.actual_live) options.actual_live = false if(!options.low_latency) options.low_latency = false
if(!options.preferred_quality) options.preferred_quality = "144p" 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, options)
@ -117,7 +117,7 @@ async function live_stream(info : InfoData, options : StreamOptions): Promise<Li
}) })
let stream : LiveStreaming | LiveEnded let stream : LiveStreaming | LiveEnded
if(info.video_details.duration === '0') { if(info.video_details.duration === '0') {
stream = new LiveStreaming((res_144.url.length !== 0) ? res_144 : info.format[info.format.length - 2], options.actual_live) stream = new LiveStreaming((res_144.url.length !== 0) ? res_144 : info.format[info.format.length - 2], options.low_latency)
} }
else { else {
stream = new LiveEnded((res_144.url.length !== 0) ? res_144 : info.format[info.format.length - 2]) stream = new LiveEnded((res_144.url.length !== 0) ? res_144 : info.format[info.format.length - 2])