how can i get the filesize of each resolution without downloading it first ytdl-core

1k Views Asked by At

so I'm making a project using YTDL-core

I had a question, how can I get the file size of each resolution without downloading it

my code so far

const { getInfo } = require("ytdl-core");
const ytdl = require("ytdl-core")
const url = 'https://www.youtube.com/watch?v=FOwXlANwerI'

const vid = ytdl(url)
ytdl.getInfo(url).then(info =>{
    console.log("Video Title : "+ info.videoDetails.title) 
}) 

Iwant to make like

console.log("144p" + 144 p size)

console.log("240p" + 240 p size)

console.log("360p" + 360 p size)

console.log("480p" + 480 p size)

how can i do that ?

2

There are 2 best solutions below

0
On

Use formats.contentLength . formats[] is an array populated with details for every format found, obtained for example calling ytdl.getBasicInfo()

This is an extract of a typical JSON response:

`"formats": [
    {
      "itag": 18,
      "mimeType": "video/mp4; codecs=\"avc1.42001E, mp4a.40.2\"",
      "bitrate": 730250,
      "width": 640,
      "height": 360,
      "lastModified": "1540940877707739",
      "contentLength": "17340341",
      "quality": "medium",
      "fps": 30,
      "qualityLabel": "360p",
      "projectionType": "RECTANGULAR",
      "averageBitrate": 729908,
      "audioQuality": "AUDIO_QUALITY_LOW",
      "approxDurationMs": "190055",
      "audioSampleRate": "44100",
      "audioChannels": 2,
      "s": "QOq0AJ8wRQIgCbPrsAEt3PIyCCEET_Fcoa0B_hJSRDVqLQdD0=uH8ZkCIQCcf5m4i52WhllLYCTKzFPb97os3BCyYtJazCSyoEpHrQ=C",
      "sp": "sig",
      "url": "https://r2---sn-hp57kn7z.googlevideo.com/videoplayback?expire=1608282564&ei=ZB3cX9DUDsPR4QSQq76oBw&ip=0.0.0.0&id=o-AFfB7IbdqRRBTM2a3_IS5lxOxi2aCPz9i1wuj7TqTMgs&itag=18&source=youtube&requiressl=yes&mh=bz&mm=31%2C26&mn=sn-hp57kn7z%2Csn-q4fl6ns6&ms=au%2Conr&mv=m&mvi=2&pl=24&initcwndbps=278750&vprv=1&mime=video%2Fmp4&ns=Ut07FDufHwnP4p0Bs9qdDDgF&gir=yes&clen=17340341&ratebypass=yes&dur=190.055&lmt=1540940877707739&mt=1608260683&fvip=2&c=WEB&txp=5431432&n=5kzbgSygQqixfwo9g&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cvprv%2Cmime%2Cns%2Cgir%2Cclen%2Cratebypass%2Cdur%2Clmt&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps&lsig=AG3C_xAwRgIhANdVqyHVbFSicpCXRz0qj5OXLolFEC5sup_vXuzXIkUAAiEA_RMc27U0fdbTOpNRwz9Kjaax8kRNKs1D04UCZhcPEhk%3D&sig=AOq0QJ8wRQIgCbPrsAEt3PIyCCEET_Fcoa0B_hJSRDVqLQdD0CuH8ZkCIQCcf5m4i52WhllLYCTKzFPb97os3BCyYtJazCSyoEpHrQ%3D%3D"
    }
  ],`
0
On

For getting the file size, you can divide contentLength by 1024 for Kb format. Try this:

const formats = info?.formats?.map((item) => ({
  ...item,
  size: (+item?.contentLength / 1024 / 1024).toFixed(2),
}));