Thumbnail Fix
This commit is contained in:
parent
a712ef732a
commit
5783275bf6
@ -3,88 +3,75 @@ import { video_info } from "."
|
|||||||
import { PassThrough } from 'stream'
|
import { PassThrough } from 'stream'
|
||||||
import https from 'https'
|
import https from 'https'
|
||||||
|
|
||||||
interface FilterOptions {
|
|
||||||
averagebitrate? : number;
|
|
||||||
videoQuality? : "144p" | "240p" | "360p" | "480p" | "720p" | "1080p";
|
|
||||||
audioQuality? : "AUDIO_QUALITY_LOW" | "AUDIO_QUALITY_MEDIUM";
|
|
||||||
audioSampleRate? : number;
|
|
||||||
audioChannels? : number;
|
|
||||||
audioCodec? : string;
|
|
||||||
audioContainer? : string;
|
|
||||||
hasAudio? : boolean;
|
|
||||||
hasVideo? : boolean;
|
|
||||||
isLive? : boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface StreamOptions {
|
interface StreamOptions {
|
||||||
filter : "bestaudio" | "bestvideo"
|
filter : "bestaudio" | "bestvideo" | "live"
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseFormats(formats : any[]): { audio: any[], video:any[] } {
|
function parseAudioFormats(formats : any[]){
|
||||||
let audio: any[] = []
|
let result: any[] = []
|
||||||
let video: any[] = []
|
|
||||||
formats.forEach((format) => {
|
formats.forEach((format) => {
|
||||||
let type = format.mimeType as string
|
let type = format.mimeType as string
|
||||||
if(type.startsWith('audio')){
|
if(type.startsWith('audio')){
|
||||||
format.audioCodec = type.split('codecs="')[1].split('"')[0]
|
format.codec = type.split('codecs="')[1].split('"')[0]
|
||||||
format.audioContainer = type.split('audio/')[1].split(';')[0]
|
format.container = type.split('audio/')[1].split(';')[0]
|
||||||
format.hasAudio = true
|
result.push(format)
|
||||||
format.hasVideo = false
|
|
||||||
audio.push(format)
|
|
||||||
}
|
|
||||||
else if(type.startsWith('video')){
|
|
||||||
format.videoQuality = format.qualityLabel
|
|
||||||
format.hasAudio = false
|
|
||||||
format.hasVideo = true
|
|
||||||
video.push(format)
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return { audio, video }
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
function filter_songs(formats : any[], options : FilterOptions) {
|
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): Promise<PassThrough>{
|
||||||
let info = await video_info(url)
|
let info = await video_info(url)
|
||||||
let final: any[] = [];
|
let final: any[] = [];
|
||||||
|
|
||||||
if(options?.filter === 'bestaudio'){
|
if(info.video_details.live === true && options) {
|
||||||
info.format.forEach((format) => {
|
options.filter = "live"
|
||||||
let type = format.mimeType as string
|
}
|
||||||
if(type.startsWith('audio/webm')){
|
|
||||||
return final.push(format)
|
|
||||||
}
|
|
||||||
else return
|
|
||||||
})
|
|
||||||
|
|
||||||
if(final.length === 0){
|
if(options?.filter){
|
||||||
info.format.forEach((format) => {
|
switch(options.filter){
|
||||||
let type = format.mimeType as string
|
case "bestaudio":
|
||||||
if(type.startsWith('audio/')){
|
let audioFormat = parseAudioFormats(info.format)
|
||||||
return final.push(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 return
|
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
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if(options?.filter === 'bestvideo'){
|
if(final.length === 0) final.push(info.format[info.format.length - 1])
|
||||||
info.format.forEach((format) => {
|
let piping_stream = got.stream(final[0].url, {
|
||||||
let type = format.mimeType as string
|
|
||||||
if(type.startsWith('video/')){
|
|
||||||
if(parseInt(format.qualityLabel) > 480) final.push(format)
|
|
||||||
else return
|
|
||||||
}
|
|
||||||
else return
|
|
||||||
})
|
|
||||||
|
|
||||||
if(final.length === 0) throw new Error("Video Format > 480p is not found")
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
final.push(info.format[info.format.length - 1])
|
|
||||||
}
|
|
||||||
|
|
||||||
let stream = got.stream(final[0].url, {
|
|
||||||
retry : 5,
|
retry : 5,
|
||||||
headers: {
|
headers: {
|
||||||
'Connection': 'keep-alive',
|
'Connection': 'keep-alive',
|
||||||
@ -99,6 +86,22 @@ export async function stream(url : string, options? : StreamOptions): Promise<Pa
|
|||||||
})
|
})
|
||||||
let playing_stream = new PassThrough({ highWaterMark: 10 * 1000 * 1000 })
|
let playing_stream = new PassThrough({ highWaterMark: 10 * 1000 * 1000 })
|
||||||
|
|
||||||
stream.pipe(playing_stream)
|
piping_stream.pipe(playing_stream)
|
||||||
return playing_stream
|
return playing_stream
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function filterFormat(formats : any[], codec : string){
|
||||||
|
let result: any[] = []
|
||||||
|
formats.forEach((format) => {
|
||||||
|
if(format.codec === codec) result.push(format)
|
||||||
|
})
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
function filterVideo(formats : any[], quality : string) {
|
||||||
|
let result: any[] = []
|
||||||
|
formats.forEach((format) => {
|
||||||
|
if(format.qualityLabel === quality) result.push(format)
|
||||||
|
})
|
||||||
|
return result
|
||||||
|
}
|
||||||
@ -16,8 +16,6 @@ export async function video_basic_info(url : string){
|
|||||||
if(player_response.playabilityStatus.status === 'ERROR') throw new Error(`While getting info from url \n ${player_response.playabilityStatus.reason}`)
|
if(player_response.playabilityStatus.status === 'ERROR') throw new Error(`While getting info from url \n ${player_response.playabilityStatus.reason}`)
|
||||||
let html5player = 'https://www.youtube.com' + body.split('"jsUrl":"')[1].split('"')[0]
|
let html5player = 'https://www.youtube.com' + body.split('"jsUrl":"')[1].split('"')[0]
|
||||||
let format = []
|
let format = []
|
||||||
format.push(player_response.streamingData.formats[0])
|
|
||||||
format.push(...player_response.streamingData.adaptiveFormats)
|
|
||||||
let vid = player_response.videoDetails
|
let vid = player_response.videoDetails
|
||||||
let microformat = player_response.microformat.playerMicroformatRenderer
|
let microformat = player_response.microformat.playerMicroformatRenderer
|
||||||
let video_details = {
|
let video_details = {
|
||||||
@ -30,7 +28,7 @@ export async function video_basic_info(url : string){
|
|||||||
thumbnail : {
|
thumbnail : {
|
||||||
width : vid.thumbnail.thumbnails[vid.thumbnail.thumbnails.length - 1].width,
|
width : vid.thumbnail.thumbnails[vid.thumbnail.thumbnails.length - 1].width,
|
||||||
height : vid.thumbnail.thumbnails[vid.thumbnail.thumbnails.length - 1].height,
|
height : vid.thumbnail.thumbnails[vid.thumbnail.thumbnails.length - 1].height,
|
||||||
url : `https://i.ytimg.com/vi/${vid.videoId}/maxresdefault.jpg`
|
url : vid.thumbnail.thumbnails[vid.thumbnail.thumbnails.length - 1].url,
|
||||||
},
|
},
|
||||||
channel : {
|
channel : {
|
||||||
name : vid.author,
|
name : vid.author,
|
||||||
@ -43,6 +41,8 @@ export async function video_basic_info(url : string){
|
|||||||
live : vid.isLiveContent,
|
live : vid.isLiveContent,
|
||||||
private : vid.isPrivate
|
private : vid.isPrivate
|
||||||
}
|
}
|
||||||
|
if(!video_details.live) format.push(player_response.streamingData.formats[0])
|
||||||
|
format.push(...player_response.streamingData.adaptiveFormats)
|
||||||
return {
|
return {
|
||||||
html5player,
|
html5player,
|
||||||
format,
|
format,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user