LiveStream update, added type making it easy for playback

This commit is contained in:
killer069
2021-08-20 23:37:30 +05:30
parent 98ccbb9671
commit f8d72f85d3
4 changed files with 123 additions and 94 deletions

View File

@@ -1,10 +1,7 @@
import got from "got"
import { video_info } from "."
import { PassThrough } from 'stream'
import https from 'https'
import { FormatInterface, LiveEnded, LiveStreaming } from "./classes/LiveStream"
import { FormatInterface, LiveEnded, LiveStreaming, Stream } from "./classes/LiveStream"
enum StreamType{
export enum StreamType{
Arbitrary = 'arbitrary',
Raw = 'raw',
OggOpus = 'ogg/opus',
@@ -13,7 +10,7 @@ enum StreamType{
}
interface StreamOptions {
smooth : boolean
actual_live : boolean
}
interface InfoData{
@@ -40,79 +37,59 @@ function parseAudioFormats(formats : any[]){
return result
}
export async function stream(url : string, options : StreamOptions = { smooth : false }): Promise<PassThrough>{
export async function stream(url : string, options : StreamOptions = { actual_live : false }): Promise<Stream | LiveStreaming | LiveEnded>{
let info = await video_info(url)
let final: any[] = [];
let type : StreamType;
if(info.LiveStreamData.isLive === true && info.LiveStreamData.hlsManifestUrl !== null) {
return await live_stream(info as InfoData, options.smooth)
return await live_stream(info as InfoData, options.actual_live)
}
let audioFormat = parseAudioFormats(info.format)
let opusFormats = filterFormat(audioFormat, "opus")
if(opusFormats.length === 0){
type = StreamType.Arbitrary
final.push(audioFormat[audioFormat.length - 1])
}
else{
type = StreamType.WebmOpus
final.push(opusFormats[opusFormats.length - 1])
}
if(final.length === 0) final.push(info.format[info.format.length - 1])
let piping_stream = got.stream(final[0].url, {
retry : 5,
headers: {
'Connection': 'keep-alive',
'Accept-Encoding': '',
'Accept-Language': 'en-US,en;q=0.8',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36'
},
agent : {
https : new https.Agent({ keepAlive : true })
},
http2 : true
})
let playing_stream = new PassThrough({ highWaterMark: 10 * 1000 * 1000 })
piping_stream.pipe(playing_stream)
return playing_stream
if(final.length === 0) {
type = StreamType.Arbitrary
final.push(info.format[info.format.length - 1])
}
return new Stream(final[0].url, type)
}
export async function stream_from_info(info : InfoData, options : StreamOptions){
export async function stream_from_info(info : InfoData, options : StreamOptions = { actual_live : false }): Promise<Stream | LiveStreaming | LiveEnded>{
let final: any[] = [];
if(info.LiveStreamData.isLive === true) {
return await live_stream(info as InfoData, options.smooth)
let type : StreamType;
if(info.LiveStreamData.isLive === true && info.LiveStreamData.hlsManifestUrl !== null) {
return await live_stream(info as InfoData, options.actual_live)
}
let audioFormat = parseAudioFormats(info.format)
let opusFormats = filterFormat(audioFormat, "opus")
if(opusFormats.length === 0){
type = StreamType.Arbitrary
final.push(audioFormat[audioFormat.length - 1])
}
else{
type = StreamType.WebmOpus
final.push(opusFormats[opusFormats.length - 1])
}
if(final.length === 0) final.push(info.format[info.format.length - 1])
let piping_stream = got.stream(final[0].url, {
retry : 5,
headers: {
'Connection': 'keep-alive',
'Accept-Encoding': '',
'Accept-Language': 'en-US,en;q=0.8',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36'
},
agent : {
https : new https.Agent({ keepAlive : true })
},
http2 : true
})
let playing_stream = new PassThrough({ highWaterMark: 10 * 1000 * 1000 })
piping_stream.pipe(playing_stream)
return playing_stream
if(final.length === 0) {
type = StreamType.Arbitrary
final.push(info.format[info.format.length - 1])
}
return new Stream(final[0].url, type)
}
function filterFormat(formats : any[], codec : string){
@@ -123,12 +100,7 @@ function filterFormat(formats : any[], codec : string){
return result
}
export function stream_type(info:InfoData): StreamType{
if(info.LiveStreamData.isLive === true && info.LiveStreamData.hlsManifestUrl !== null) return StreamType.Arbitrary
else return StreamType.WebmOpus
}
async function live_stream(info : InfoData, smooth : boolean): Promise<PassThrough>{
async function live_stream(info : InfoData, actual_live : boolean): Promise<LiveStreaming | LiveEnded>{
let res_144 : FormatInterface = {
url : '',
targetDurationSec : 0,
@@ -140,10 +112,10 @@ async function live_stream(info : InfoData, smooth : boolean): Promise<PassThrou
})
let stream : LiveStreaming | LiveEnded
if(info.video_details.duration === '0') {
stream = new LiveStreaming((res_144.url.length !== 0) ? res_144 : info.format[info.format.length - 1], smooth)
stream = new LiveStreaming((res_144.url.length !== 0) ? res_144 : info.format[info.format.length - 1], actual_live)
}
else {
stream = new LiveEnded((res_144.url.length !== 0) ? res_144 : info.format[info.format.length - 1])
}
return stream.stream
return stream
}