Skip to content

Instantly share code, notes, and snippets.

@geuis
Last active January 10, 2024 16:43
Show Gist options
  • Save geuis/8b1b2ea57d7f9a9ae22f80d4fbf5b97f to your computer and use it in GitHub Desktop.
Save geuis/8b1b2ea57d7f9a9ae22f80d4fbf5b97f to your computer and use it in GitHub Desktop.
Get Youtube video urls
// Run from the dev tools console of any Youtube video
// Accurate as of July 2, 2020.
//
// Copy and paste this into the dev console in a browser with the desired video loaded.
//
// NOTE: Some Youtube videos do not directly expose the video url in the response.
// This script doesn't currently attempt to handle those. It will work for most other general video types though.
(async () => {
const html = await fetch(window.location.href).then((resp) => resp.text()).then((text) => text);
const startStr = 'ytplayer.config = {';
const start = html.indexOf(startStr) + startStr.length - 1;
const end = html.indexOf('};', start) + 1;
const playerObj = JSON.parse(html.slice(start, end));
playerObj.args.player_response = JSON.parse(playerObj.args.player_response);
const videoUrls = playerObj.args.player_response.streamingData.adaptiveFormats.reduce((acc, item) => {
if (!acc[item.quality]) {
acc[item.quality] = {};
}
const mimeType = item.mimeType.split(';')[0];
acc[item.quality][mimeType] = item;
return acc;
}, {});
console.log('!!', videoUrls);
})();
@3Samourai
Copy link

For Download video with audio

(async () => {
  html = await fetch(window.location.href).then(resp => resp.text()).then(text => text);
  startStr = 'ytplayer.config = {';
  start = html.indexOf(startStr) + startStr.length - 1;
  end = html.indexOf('};', start) + 1;
  playerObj = JSON.parse(html.slice(start, end));
  playerObj.args.player_response = JSON.parse(playerObj.args.player_response);
  videoUrls = playerObj.args.player_response.streamingData.formats[1].url;
  videoUrls = videoUrls.replace('"', 'r');
  return console.log(videoUrls);
})();

@Christopher-CO
Copy link

For Download video with audio

(async () => {
  html = await fetch(window.location.href).then(resp => resp.text()).then(text => text);
  startStr = 'ytplayer.config = {';
  start = html.indexOf(startStr) + startStr.length - 1;
  end = html.indexOf('};', start) + 1;
  playerObj = JSON.parse(html.slice(start, end));
  playerObj.args.player_response = JSON.parse(playerObj.args.player_response);
  videoUrls = playerObj.args.player_response.streamingData.formats[1].url;
  videoUrls = videoUrls.replace('"', 'r');
  return console.log(videoUrls);
})();

Thank you, this works great!

@realrecordzLab
Copy link

realrecordzLab commented Nov 17, 2020

For Download video with audio

(async () => {
  html = await fetch(window.location.href).then(resp => resp.text()).then(text => text);
  startStr = 'ytplayer.config = {';
  start = html.indexOf(startStr) + startStr.length - 1;
  end = html.indexOf('};', start) + 1;
  playerObj = JSON.parse(html.slice(start, end));
  playerObj.args.player_response = JSON.parse(playerObj.args.player_response);
  videoUrls = playerObj.args.player_response.streamingData.formats[1].url;
  videoUrls = videoUrls.replace('"', 'r');
  return console.log(videoUrls);
})();

Thank you, this works great!

I've tried the script but I get a reject in promise. Seems not working. TypeError: Cannot read property 'url' of undefined↵ at <anonymous>:8:71

@tcarroll2
Copy link

in what language is it written in? I need to rewrite it to swift

@nysander - did you ever get this converted to Swift?

@nysander
Copy link

in what language is it written in? I need to rewrite it to swift

@nysander - did you ever get this converted to Swift?

Nope because Apple is very strict with playback rights and I have dropped idea of this script in my app

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment