mirror of
https://github.com/YuzuZensai/play-dl-test.git
synced 2026-01-06 04:32:40 +00:00
LiveStream update, added type making it easy for playback
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import { PassThrough } from 'stream'
|
||||
import got from 'got'
|
||||
import Request from 'got/dist/source/core';
|
||||
import { StreamType } from '../stream';
|
||||
|
||||
export interface FormatInterface{
|
||||
url : string;
|
||||
@@ -8,35 +10,84 @@ export interface FormatInterface{
|
||||
}
|
||||
|
||||
export class LiveStreaming{
|
||||
smooth : boolean;
|
||||
private __stream : PassThrough
|
||||
type : StreamType
|
||||
actual_live : boolean;
|
||||
stream : PassThrough
|
||||
private format : FormatInterface
|
||||
private interval : number
|
||||
private packet_count : number
|
||||
private timer : NodeJS.Timer | null
|
||||
private segments_urls : string[]
|
||||
constructor(format : FormatInterface, smooth : boolean){
|
||||
this.smooth = smooth || false
|
||||
constructor(format : FormatInterface, actual_live : boolean){
|
||||
this.type = StreamType.Arbitrary
|
||||
this.actual_live = actual_live || false
|
||||
this.format = format
|
||||
this.__stream = new PassThrough({ highWaterMark : 10 * 1000 * 1000 })
|
||||
this.stream = new PassThrough({ highWaterMark : 10 * 1000 * 1000 })
|
||||
this.segments_urls = []
|
||||
this.packet_count = 0
|
||||
this.interval = 0
|
||||
this.timer = null
|
||||
this.__stream.on('close', () => {
|
||||
this.stream.on('close', () => {
|
||||
this.cleanup()
|
||||
})
|
||||
if(this.smooth === true) this.__stream.pause()
|
||||
this.start()
|
||||
});
|
||||
(this.actual_live) ? this.live_loop() :this.start()
|
||||
}
|
||||
|
||||
async manifest_getter(){
|
||||
let response = await got(this.format.url)
|
||||
this.segments_urls = response.body.split('\n').filter((x) => x.startsWith('https'))
|
||||
private async live_loop(){
|
||||
if(this.stream.destroyed) this.cleanup()
|
||||
await this.manifest_getter()
|
||||
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])
|
||||
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.interval = 1
|
||||
this.timer = setTimeout(async () => {
|
||||
await this.looping()
|
||||
}, this.interval)
|
||||
}
|
||||
|
||||
get stream(){
|
||||
return this.__stream
|
||||
private async looping(){
|
||||
if(this.stream.destroyed) this.cleanup()
|
||||
await this.manifest_getter()
|
||||
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.interval = 1
|
||||
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(){
|
||||
@@ -45,19 +96,19 @@ export class LiveStreaming{
|
||||
this.packet_count = 0
|
||||
}
|
||||
|
||||
async start(){
|
||||
if(this.__stream.destroyed) this.cleanup()
|
||||
private async start(){
|
||||
if(this.stream.destroyed) this.cleanup()
|
||||
await this.manifest_getter()
|
||||
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){
|
||||
await (async () => {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
if(Number(url.split('index.m3u8/sq/')[1].split('/')[0]) !== this.packet_count){
|
||||
resolve('')
|
||||
return
|
||||
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', () => {
|
||||
this.packet_count++
|
||||
resolve('')
|
||||
@@ -67,10 +118,6 @@ export class LiveStreaming{
|
||||
}
|
||||
this.interval = (this.segments_urls.length / 2) * 1000
|
||||
this.timer = setTimeout(async () => {
|
||||
if(this.smooth === true){
|
||||
this.__stream.resume()
|
||||
this.smooth = false
|
||||
}
|
||||
await this.start()
|
||||
}, this.interval)
|
||||
}
|
||||
@@ -81,16 +128,18 @@ export class LiveStreaming{
|
||||
}
|
||||
|
||||
export class LiveEnded{
|
||||
private __stream : PassThrough
|
||||
type : StreamType
|
||||
stream : PassThrough
|
||||
private format : FormatInterface
|
||||
private packet_count : number
|
||||
private segments_urls : string[]
|
||||
constructor(format : FormatInterface){
|
||||
this.type = StreamType.Arbitrary
|
||||
this.format = format
|
||||
this.__stream = new PassThrough({ highWaterMark : 10 * 1000 * 1000 })
|
||||
this.stream = new PassThrough({ highWaterMark : 10 * 1000 * 1000 })
|
||||
this.segments_urls = []
|
||||
this.packet_count = 0
|
||||
this.__stream.on('close', () => {
|
||||
this.stream.on('close', () => {
|
||||
this.cleanup()
|
||||
})
|
||||
this.start()
|
||||
@@ -101,17 +150,13 @@ export class LiveEnded{
|
||||
this.segments_urls = response.body.split('\n').filter((x) => x.startsWith('https'))
|
||||
}
|
||||
|
||||
get stream(){
|
||||
return this.__stream
|
||||
}
|
||||
|
||||
private cleanup(){
|
||||
this.segments_urls = []
|
||||
this.packet_count = 0
|
||||
}
|
||||
|
||||
async start(){
|
||||
if(this.__stream.destroyed) this.cleanup()
|
||||
if(this.stream.destroyed) this.cleanup()
|
||||
await this.manifest_getter()
|
||||
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){
|
||||
@@ -122,7 +167,7 @@ export class LiveEnded{
|
||||
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', () => {
|
||||
this.packet_count++
|
||||
resolve('')
|
||||
@@ -137,3 +182,18 @@ export class LiveEnded{
|
||||
}
|
||||
}
|
||||
|
||||
export class Stream {
|
||||
type : StreamType
|
||||
private piping_stream : Request
|
||||
private playing_stream : PassThrough
|
||||
constructor(url : string, type : StreamType){
|
||||
this.type = type
|
||||
this.piping_stream = got.stream(url)
|
||||
this.playing_stream = new PassThrough({ highWaterMark : 10 * 1000 * 1000 })
|
||||
this.piping_stream.pipe(this.playing_stream)
|
||||
}
|
||||
|
||||
get stream(){
|
||||
return this.playing_stream
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user