如何更改 URL 格式?

问题描述

我正在使用 ytdl-core,它提供了这个 URL(音频):

https://r2---sn-gwpa-w5py.googlevideo.com/videoplayback?expire=1612552132&ei=ZEMdYNnJDumPz7sPyrSLmAw&ip=49.36.246.217&id=o-AFQLS1cSUJ6_bXBjMOIiWk1NuIYTMUTVu4rmliPZmJCS&itag=251&source=youtube&requiressl=yes&mh=VD&mm=31%2C29&mn=sn-gwpa-w5py%2Csn-gwpa-qxaz&ms=au%2Crdu&mv=m&mvi=2&pl=21&initcwndbps=225000&vprv=1&mime=audio%2Fwebm&ns=nxhNYqBZK92ToigY08JRZjEF&gir=yes&clen=230633&dur=18.961&lmt=1524503096692151&mt=1612530050&fvip=2&keepalive=yes&c=WEB&n=UIc0qqVvrMQOjPVkP&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cvprv%2Cmime%2Cns%2Cgir%2cclen%2Cdur%2Clmt&sig=AOq0QJ8wRQIhAObqQA5vFYy6rJ3O3J4zswofNj2NFZOrwMF05p_PgWg9AiAmnde3njBRfvPmW_2VVA0YO1aLWQl0V3STGayuqgBRKA%3D%3D&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps&lsig=AG3C_xAwRAIgFTqy7Ng9zxhQY4GM4vP1BooPeHOf50PTikb59C67voACIGDybTTJBdLkp9lUx_-ZLdCulG_y3TH6iMhBbAn1AZR_&ratebypass=yes

但我想要像这样结尾的 .mp3:

https://r2---sn-gwpa-w5py.googlevideo.com/13%20Beethoven%20-%20Fur%20Elise.mp3

我想在最后加上 .mp3

这可能吗?我正在使用 JavaScript 框架(后端为 NodeJS,前端为 React-Native)。

解决方法

不,这不可能。 Google 决定向其提供视频和音频资源的网址。

,

这不可能。您无法更改 Google 提供给您的网址。您唯一能做的就是使用他们给您的网址。

,

没有.mp3,对于音频它使用.m4a.weba,您需要ffmpeg将其转换为.mp3

,

通过视频 ID,您可以使用 YouTube API 查找视频的标题。然后,您可以获取 URL 的来源并附加曲目标题。

如果你只想要视频的名字作为文件名,下载后只需将文件名设置为getVideoTitle的结果即可。

const url = new URL('https://r2---sn-gwpa-w5py.googlevideo.com/videoplayback?expire=1612552132&ei=ZEMdYNnJDumPz7sPyrSLmAw&ip=49.36.246.217&id=o-AFQLS1cSUJ6_bXBjMOIiWk1NuIYTMUTVu4rmliPZmJCS&itag=251&source=youtube&requiressl=yes&mh=VD&mm=31%2C29&mn=sn-gwpa-w5py%2Csn-gwpa-qxaz&ms=au%2Crdu&mv=m&mvi=2&pl=21&initcwndbps=225000&vprv=1&mime=audio%2Fwebm&ns=nxhNYqBZK92ToigY08JRZjEF&gir=yes&clen=230633&dur=18.961&lmt=1524503096692151&mt=1612530050&fvip=2&keepalive=yes&c=WEB&n=UIc0qqVvrMQOjPVkP&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cvprv%2Cmime%2Cns%2Cgir%2Cclen%2Cdur%2Clmt&sig=AOq0QJ8wRQIhAObqQA5vFYy6rJ3O3J4zswofNj2NFZOrwMF05p_PgWg9AiAmnde3njBRfvPmW_2VVA0YO1aLWQl0V3STGayuqgBRKA%3D%3D&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps&lsig=AG3C_xAwRAIgFTqy7Ng9zxhQY4GM4vP1BooPeHOf50PTikb59C67voACIGDybTTJBdLkp9lUx_-ZLdCulG_y3TH6iMhBbAn1AZR_&ratebypass=yes');

const api = 'https://www.googleapis.com/youtube/v3';
const apiKey = '<API_KEY>';

const getVideoPath = videoId => `${api}/videos?id=${videoId}&key=${apiKey}&part=snippet`;
const getVideoInfo = async (videoId) => fetch(getVideoPath(videoId));

const getVideoTitle = async (videoId) => {
  const response = await getVideoInfo(videoId);
  const info = await response.json();
  const { items: [ { snippet: { title } } ] } = info;
  return title;
};

const main = async () => {
  let title = null;
  try {
    title = await getVideoTitle('dQw4w9WgXcQ');
  } catch (err) {
    console.log('Please supply a valid API key!');
  }
  
  // Fallback: If API key is valid,title should not be null
  title = title || 'Rick Astley - Never Gonna Give You Up (Video)';
  const newUrl = `${url.origin}/${encodeURIComponent(title)}.mp3`;

  console.log(newUrl);

  const query = Object.fromEntries(url.search.substring(1).split('&')
    .map(pair => pair.split('=').map(v => decodeURIComponent(v))));

  console.log(query); // Nothing of interest...
};

main();
.as-console-wrapper { top: 0; max-height: 100% !important; }

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...