LiveStream Support

This commit is contained in:
killer069
2021-08-20 12:06:12 +05:30
parent 5783275bf6
commit ad1ad2b918
8 changed files with 269 additions and 60 deletions

View File

@@ -2,10 +2,29 @@ import got from "got"
import { video_info } from "."
import { PassThrough } from 'stream'
import https from 'https'
import { FormatInterface, LiveEnded, LiveStreaming } from "./classes/LiveStream"
enum StreamType{
Arbitrary = 'arbitrary',
Raw = 'raw',
OggOpus = 'ogg/opus',
WebmOpus = 'webm/opus',
Opus = 'opus',
}
interface StreamOptions {
filter : "bestaudio" | "bestvideo" | "live"
smooth : boolean
}
interface InfoData{
LiveStreamData : {
isLive : boolean
dashManifestUrl : string
hlsManifestUrl : string
}
html5player : string
format : any[]
video_details : any
}
function parseAudioFormats(formats : any[]){
@@ -21,55 +40,61 @@ function parseAudioFormats(formats : any[]){
return result
}
function parseVideoFormats(formats : any[]){
let result: any[] = []
formats.forEach((format) => {
let type = format.mimeType as string
if(type.startsWith('audio')){
format.codec = type.split('codecs="')[1].split('"')[0]
format.container = type.split('audio/')[1].split(';')[0]
result.push(format)
}
})
return result
}
export async function stream(url : string, options? : StreamOptions): Promise<PassThrough>{
export async function stream(url : string, options : StreamOptions = { smooth : false }): Promise<PassThrough>{
let info = await video_info(url)
let final: any[] = [];
if(info.video_details.live === true && options) {
options.filter = "live"
if(info.LiveStreamData.isLive === true && info.LiveStreamData.hlsManifestUrl !== null) {
return await live_stream(info as InfoData, options.smooth)
}
if(options?.filter){
switch(options.filter){
case "bestaudio":
let audioFormat = parseAudioFormats(info.format)
if(audioFormat.length === 0) await stream(url, { filter : "bestvideo" })
let opusFormats = filterFormat(audioFormat, "opus")
if(opusFormats.length === 0){
final.push(audioFormat[audioFormat.length - 1])
}
else{
final.push(opusFormats[opusFormats.length - 1])
}
break
case "bestvideo" :
let videoFormat = parseVideoFormats(info.format)
if(videoFormat.length === 0) throw new Error('Can\'t Find Video Formats ')
let qual_1080 = filterVideo(videoFormat, "1080p")
if(qual_1080.length === 0) {
let qual_720 = filterVideo(videoFormat, "720p")
if(qual_720.length === 0) final.push(videoFormat[0])
else final.push(qual_720)
break
}
else final.push(qual_1080)
break
}
let audioFormat = parseAudioFormats(info.format)
let opusFormats = filterFormat(audioFormat, "opus")
if(opusFormats.length === 0){
final.push(audioFormat[audioFormat.length - 1])
}
else{
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
}
export async function stream_from_info(info : InfoData, options : StreamOptions){
let final: any[] = [];
if(info.LiveStreamData.isLive === true) {
return await live_stream(info as InfoData, options.smooth)
}
let audioFormat = parseAudioFormats(info.format)
let opusFormats = filterFormat(audioFormat, "opus")
if(opusFormats.length === 0){
final.push(audioFormat[audioFormat.length - 1])
}
else{
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,
@@ -98,10 +123,27 @@ function filterFormat(formats : any[], codec : string){
return result
}
function filterVideo(formats : any[], quality : string) {
let result: any[] = []
formats.forEach((format) => {
if(format.qualityLabel === quality) result.push(format)
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>{
let res_144 : FormatInterface = {
url : '',
targetDurationSec : 0,
maxDvrDurationSec : 0
}
info.format.forEach((format) => {
if(format.qualityLabel === '144p') res_144 = format
else return
})
return result
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)
}
else {
stream = new LiveEnded((res_144.url.length !== 0) ? res_144 : info.format[info.format.length - 1])
}
return stream.stream
}