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

98 lines
3.1 KiB
TypeScript
Raw Normal View History

2021-08-16 15:19:31 +05:30
import { video_info } from "."
2021-09-03 17:55:21 +05:30
import { 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',
2021-09-13 14:13:36 +09:00
Raw = 'raw',
OggOpus = 'ogg/opus',
WebmOpus = 'webm/opus',
Opus = 'opus',
2021-08-20 12:06:12 +05:30
}
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-09-06 16:18:42 +05:30
export async function stream(url : string, cookie? : string): Promise<Stream | LiveStreaming>{
let info = await video_info(url, cookie)
2021-08-16 15:19:31 +05:30
let final: any[] = [];
let type : StreamType;
2021-09-03 17:55:21 +05:30
if(info.LiveStreamData.isLive === true && info.LiveStreamData.hlsManifestUrl !== null && info.video_details.durationInSec === '0') {
2021-09-05 18:50:57 +05:30
return new LiveStreaming(info.LiveStreamData.dashManifestUrl, info.format[info.format.length - 1].targetDurationSec, info.video_details.url)
2021-08-20 12:06:12 +05:30
}
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
if(audioFormat.length === 0){
final.push(info.format[info.format.length - 1])
}
else{
final.push(audioFormat[audioFormat.length - 1])
}
2021-08-20 12:06:12 +05:30
}
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-09-13 10:23:18 +05:30
return new Stream(final[0].url, type, info.video_details.durationInSec, Number(final[0].contentLength), info.video_details.url, cookie as string)
2021-08-20 12:06:12 +05:30
}
export async function stream_from_info(info : InfoData, cookie? : string): Promise<Stream | LiveStreaming>{
2021-08-20 12:06:12 +05:30
let final: any[] = [];
let type : StreamType;
2021-09-03 17:55:21 +05:30
if(info.LiveStreamData.isLive === true && info.LiveStreamData.hlsManifestUrl !== null && info.video_details.durationInSec === '0') {
2021-09-05 18:50:57 +05:30
return new LiveStreaming(info.LiveStreamData.dashManifestUrl, info.format[info.format.length - 1].targetDurationSec, info.video_details.url)
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
if(audioFormat.length === 0){
final.push(info.format[info.format.length - 1])
}
else{
final.push(audioFormat[audioFormat.length - 1])
}
2021-08-20 12:06:12 +05:30
}
else{
type = StreamType.WebmOpus
2021-08-20 12:06:12 +05:30
final.push(opusFormats[opusFormats.length - 1])
}
2021-09-13 10:23:18 +05:30
return new Stream(final[0].url, type, info.video_details.durationInSec, Number(final[0].contentLength), info.video_details.url, cookie as string)
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
}