Live Stream Fixes

This commit is contained in:
killer069 2021-08-21 15:03:43 +05:30
parent f8d72f85d3
commit 849a0f7100
4 changed files with 17 additions and 16 deletions

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{ {
"name": "play-dl", "name": "play-dl",
"version": "0.2.4", "version": "0.2.6",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "play-dl", "name": "play-dl",
"version": "0.2.4", "version": "0.2.6",
"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.2.4", "version": "0.2.6",
"description": "YouTube, SoundCloud, Spotify downloader", "description": "YouTube, SoundCloud, Spotify downloader",
"main": "dist/index.js", "main": "dist/index.js",
"typings": "dist/index.d.ts", "typings": "dist/index.d.ts",

View File

@ -11,8 +11,8 @@ export interface FormatInterface{
export class LiveStreaming{ export class LiveStreaming{
type : StreamType type : StreamType
actual_live : boolean;
stream : PassThrough stream : PassThrough
private actual_live : boolean;
private format : FormatInterface private format : FormatInterface
private interval : number private interval : number
private packet_count : number private packet_count : number
@ -54,7 +54,7 @@ export class LiveStreaming{
}) })
})() })()
} }
this.interval = 1 this.interval = 1 * 1000
this.timer = setTimeout(async () => { this.timer = setTimeout(async () => {
await this.looping() await this.looping()
}, this.interval) }, this.interval)
@ -79,7 +79,7 @@ export class LiveStreaming{
}) })
})() })()
} }
this.interval = 1 this.interval = 1 * 1000
this.timer = setTimeout(async () => { this.timer = setTimeout(async () => {
await this.looping() await this.looping()
}, this.interval) }, this.interval)
@ -91,7 +91,7 @@ export class LiveStreaming{
} }
private cleanup(){ private cleanup(){
clearInterval(this.timer as NodeJS.Timer) clearTimeout(this.timer as NodeJS.Timer)
this.segments_urls = [] this.segments_urls = []
this.packet_count = 0 this.packet_count = 0
} }

View File

@ -10,7 +10,8 @@ export enum StreamType{
} }
interface StreamOptions { interface StreamOptions {
actual_live : boolean actual_live : boolean;
preferred_quality : "144p" | "240p" | "360p" | "480p" | "720p" | "1080p"
} }
interface InfoData{ interface InfoData{
@ -37,12 +38,12 @@ function parseAudioFormats(formats : any[]){
return result return result
} }
export async function stream(url : string, options : StreamOptions = { actual_live : false }): Promise<Stream | LiveStreaming | LiveEnded>{ export async function stream(url : string, options : StreamOptions = { actual_live : 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(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.actual_live) return await live_stream(info as InfoData, options)
} }
let audioFormat = parseAudioFormats(info.format) let audioFormat = parseAudioFormats(info.format)
@ -65,11 +66,11 @@ 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 }): Promise<Stream | LiveStreaming | LiveEnded>{ export async function stream_from_info(info : InfoData, options : StreamOptions = { actual_live : false, preferred_quality : "144p" }): Promise<Stream | LiveStreaming | LiveEnded>{
let final: any[] = []; let final: any[] = [];
let type : StreamType; let type : StreamType;
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.actual_live) return await live_stream(info as InfoData, options)
} }
let audioFormat = parseAudioFormats(info.format) let audioFormat = parseAudioFormats(info.format)
@ -100,19 +101,19 @@ function filterFormat(formats : any[], codec : string){
return result return result
} }
async function live_stream(info : InfoData, actual_live : boolean): Promise<LiveStreaming | LiveEnded>{ async function live_stream(info : InfoData, options : StreamOptions): 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) => { info.format.forEach((format) => {
if(format.qualityLabel === '144p') res_144 = format if(format.qualityLabel === options.preferred_quality) res_144 = format
else return else return
}) })
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 - 1], actual_live) stream = new LiveStreaming((res_144.url.length !== 0) ? res_144 : info.format[info.format.length - 1], options.actual_live)
} }
else { else {
stream = new LiveEnded((res_144.url.length !== 0) ? res_144 : info.format[info.format.length - 1]) stream = new LiveEnded((res_144.url.length !== 0) ? res_144 : info.format[info.format.length - 1])