找回密码
 注册免广告
搜索
人人必备的 Wise 💳华侨银行送 15 新加坡币 🎁个人 IBAN 出金,注册送 $25 比特币 ฿数字货币银行卡,注册送 7 美元💲
查看: 300|回复: 2

nodejs 获取油管下载链接的方式

[复制链接]

465

主题

5019

回帖

1万

积分

版主

积分
12108
HelloWorld 发表于 2024-1-12 20:36:43 | 显示全部楼层 |阅读模式

只能在 node 环境运行,react-native 没 node 环境不行。有兴趣的人可以去改编成更通用的版本,以适配非 node 环境

  1. const ytdl = require('ytdl-core')
  2. const url = process.argv[2]
  3. if (!url) {
  4.   console.log('usage: ', 'npx ts-node youtube/yt.ts "https://www.youtube.com/watch?v=jNQXAC9IVRw"')
  5.   process.exit()
  6. }
  7. ytdl.getBasicInfo(url).then(res=>{
  8.   const formats = res.player_response.streamingData.formats
  9.   let format = formats[0]
  10.   for (let i=1; i<formats.length; i++) {
  11.     if (formats[i].width > format.width) {
  12.       format = formats[i]
  13.     }
  14.   }
  15.   const videoUrl = format?.url
  16.   const sizeMB = format.bitrate*Number(format.approxDurationMs)/8/1000/1024/1024
  17.   const durationSecond = Number(format.approxDurationMs)/1000
  18.   const thumbnails = res.player_response.videoDetails.thumbnail.thumbnails
  19.   let thumbnail = thumbnails[0]
  20.   for (let i=1; i<thumbnails.length; i++) {
  21.     if (thumbnail.width < thumbnails[i].width) {
  22.       thumbnail = thumbnails[i]
  23.     }
  24.   }
  25.   const coverUrl = thumbnail?.url
  26.   console.log({videoUrl, coverUrl, sizeMB, durationSecond})
  27. })
复制代码

7

主题

58

回帖

304

积分

中级会员

积分
304
How 发表于 2024-1-13 05:12:44 | 显示全部楼层
我刚刚还在弄自己的博客,用的hexo,node版本问题折腾了好久。有些modules 新版node没有。我从18换到14才成功跑起来。

465

主题

5019

回帖

1万

积分

版主

积分
12108
 楼主| HelloWorld 发表于 2024-1-13 09:07:44 | 显示全部楼层
How 发表于 2024-1-13 05:12
我刚刚还在弄自己的博客,用的hexo,node版本问题折腾了好久。有些modules 新版node没有。我从18换到14才成 ...

nvm 切换一下很方便
您需要登录后才可以回帖 登录 | 注册免广告

本版积分规则

排行榜|意见建议|数字居民论坛

GMT+8, 2024-5-20 12:38

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表