Files
play-dl-test/play-dl/YouTube/stream.ts

132 lines
3.8 KiB
TypeScript
Raw Normal View History

2021-08-30 18:33:47 +05:30
import got from "got/dist/source"
2021-08-16 15:19:31 +05:30
import { video_info } from "."
2021-08-27 16:02:17 +05:30
import { LiveEnded, LiveStreaming, Stream } from "./classes/LiveStream"
2021-08-16 15:19:31 +05:30
export enum StreamType{
2021-08-20 12:06:12 +05:30
Arbitrary = 'arbitrary',
Raw = 'raw',
OggOpus = 'ogg/opus',
WebmOpus = 'webm/opus',
Opus = 'opus',
}
2021-08-16 15:19:31 +05:30
2021-08-20 12:06:12 +05:30
interface InfoData{
LiveStreamData : {
isLive : boolean
dashManifestUrl : string
hlsManifestUrl : string
}
html5player : string
format : any[]
video_details : any
2021-08-16 15:19:31 +05:30
}
2021-08-20 12:06:12 +05:30
function parseAudioFormats(formats : any[]){
2021-08-18 17:11:37 +05:30
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
2021-08-16 15:19:31 +05:30
}
2021-08-30 18:33:47 +05:30
export async function stream(url : string, error_check : boolean = false): Promise<Stream | LiveStreaming | LiveEnded>{
2021-08-16 15:19:31 +05:30
let info = await video_info(url)
let final: any[] = [];
let type : StreamType;
2021-08-20 12:06:12 +05:30
if(info.LiveStreamData.isLive === true && info.LiveStreamData.hlsManifestUrl !== null) {
2021-08-29 18:55:33 +05:30
return live_stream(info as InfoData)
2021-08-20 12:06:12 +05:30
}
2021-08-30 18:33:47 +05:30
if(error_check){
2021-08-30 23:54:34 +05:30
let response = await got(info.format[info.format.length - 1].url, {
headers : {
"range" : `bytes=0-1`
}
})
2021-08-30 18:33:47 +05:30
if(response.statusCode >= 400){
return await stream(info.video_details.url, true)
}
}
2021-08-16 15:19:31 +05:30
2021-08-20 12:06:12 +05:30
let audioFormat = parseAudioFormats(info.format)
let opusFormats = filterFormat(audioFormat, "opus")
if(opusFormats.length === 0){
type = StreamType.Arbitrary
2021-08-20 12:06:12 +05:30
final.push(audioFormat[audioFormat.length - 1])
}
else{
type = StreamType.WebmOpus
2021-08-20 12:06:12 +05:30
final.push(opusFormats[opusFormats.length - 1])
2021-08-18 17:11:37 +05:30
}
2021-08-16 15:19:31 +05:30
if(final.length === 0) {
type = StreamType.Arbitrary
final.push(info.format[info.format.length - 1])
}
2021-08-24 12:50:06 +05:30
return new Stream(final[0].url, type, info.video_details.durationInSec)
2021-08-20 12:06:12 +05:30
}
2021-08-30 18:33:47 +05:30
export async function stream_from_info(info : InfoData, error_check : boolean = false): Promise<Stream | LiveStreaming | LiveEnded>{
2021-08-20 12:06:12 +05:30
let final: any[] = [];
let type : StreamType;
if(info.LiveStreamData.isLive === true && info.LiveStreamData.hlsManifestUrl !== null) {
2021-08-29 18:55:33 +05:30
return live_stream(info as InfoData)
2021-08-16 15:19:31 +05:30
}
2021-08-20 12:06:12 +05:30
2021-08-30 18:33:47 +05:30
if(error_check){
2021-08-30 23:54:34 +05:30
let response = await got(info.format[info.format.length - 1].url, {
headers : {
"range" : `bytes=0-1`
}
})
2021-08-30 18:33:47 +05:30
if(response.statusCode >= 400){
return await stream(info.video_details.url, true)
}
}
2021-08-20 12:06:12 +05:30
let audioFormat = parseAudioFormats(info.format)
let opusFormats = filterFormat(audioFormat, "opus")
if(opusFormats.length === 0){
type = StreamType.Arbitrary
2021-08-20 12:06:12 +05:30
final.push(audioFormat[audioFormat.length - 1])
}
else{
type = StreamType.WebmOpus
2021-08-20 12:06:12 +05:30
final.push(opusFormats[opusFormats.length - 1])
}
if(final.length === 0) {
type = StreamType.Arbitrary
final.push(info.format[info.format.length - 1])
}
2021-08-24 12:50:06 +05:30
return new Stream(final[0].url, type, info.video_details.durationInSec)
2021-08-18 17:11:37 +05:30
}
function filterFormat(formats : any[], codec : string){
let result: any[] = []
formats.forEach((format) => {
if(format.codec === codec) result.push(format)
})
return result
}
2021-08-29 18:55:33 +05:30
function live_stream(info : InfoData): LiveStreaming | LiveEnded{
2021-08-20 12:06:12 +05:30
let stream : LiveStreaming | LiveEnded
2021-08-27 15:08:23 +05:30
if(info.video_details.durationInSec === '0') {
stream = new LiveStreaming(info.LiveStreamData.dashManifestUrl, info.format[info.format.length - 1].targetDurationSec)
2021-08-20 12:06:12 +05:30
}
else {
2021-08-27 16:03:38 +05:30
stream = new LiveEnded(info.LiveStreamData.dashManifestUrl)
2021-08-20 12:06:12 +05:30
}
return stream
2021-08-16 15:19:31 +05:30
}