|
注册免广告
您需要 登录 才可以下载或查看,没有账号?注册
×
只能在 node 环境运行,react-native 没 node 环境不行。有兴趣的人可以去改编成更通用的版本,以适配非 node 环境
- const ytdl = require('ytdl-core')
- const url = process.argv[2]
- if (!url) {
- console.log('usage: ', 'npx ts-node youtube/yt.ts "https://www.youtube.com/watch?v=jNQXAC9IVRw"')
- process.exit()
- }
- ytdl.getBasicInfo(url).then(res=>{
- const formats = res.player_response.streamingData.formats
- let format = formats[0]
- for (let i=1; i<formats.length; i++) {
- if (formats[i].width > format.width) {
- format = formats[i]
- }
- }
- const videoUrl = format?.url
- const sizeMB = format.bitrate*Number(format.approxDurationMs)/8/1000/1024/1024
- const durationSecond = Number(format.approxDurationMs)/1000
- const thumbnails = res.player_response.videoDetails.thumbnail.thumbnails
- let thumbnail = thumbnails[0]
- for (let i=1; i<thumbnails.length; i++) {
- if (thumbnail.width < thumbnails[i].width) {
- thumbnail = thumbnails[i]
- }
- }
- const coverUrl = thumbnail?.url
- console.log({videoUrl, coverUrl, sizeMB, durationSecond})
- })
复制代码 |
|