javascript 我怎样才能得到每个分辨率的文件大小而不下载它第一ytdl-core

igetnqfo  于 2023-02-07  发布在  Java
关注(0)|答案(2)|浏览(133)

所以我用YTDL内核做了一个项目
我有一个问题,我怎么能得到每个分辨率的文件大小而不下载它
我的代码到目前为止

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) 
})

我想做的像

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

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

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

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

我怎么能这么做呢?

4szc88ey

4szc88ey1#

使用formats.contentLengthformats[]是一个填充了所找到的每种格式的详细信息的数组,例如通过调用ytdl.getBasicInfo()获得
以下是典型JSON响应的摘录:

`"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"
    }
  ],`
64jmpszr

64jmpszr2#

要获取文件大小,您可以将contentLength除以1024,得到Kb。请尝试以下操作

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

相关问题