Skip to content

Instantly share code, notes, and snippets.

@turbo
Last active September 30, 2016 18:19
Show Gist options
  • Save turbo/d34af781691df0a7876f3a514aac3be5 to your computer and use it in GitHub Desktop.
Save turbo/d34af781691df0a7876f3a514aac3be5 to your computer and use it in GitHub Desktop.
Serverless YouTube stream decoder.

Tired of pseudo-client-side YouTube downloaders? Then it's time to get right to the source. Just create a new bookmark, type

javascript:

in the "link" field and then paste this code after it:

a=function(g,h,i){return i.indexOf(g)==h};b=yt.player.Application.create('player-api',ytplayer.config);b.dispose();d=JSON.stringify(b.getVideoData()),e=d.match(/https:[^"]+videoplayback[^"]+/g),f=e.filter(z=>z.length<1000);for(c of (f.length?(f.filter(a).map((j,k)=>j+'&signature='+d.match(/[0123456789ABCDEF.]+(?=")/g).filter(z=>z.length>20).filter(a)[k])):e.filter(a)))if(~["141","251","140","171","250","249"].indexOf(new URLSearchParams(c.split('?')[1]).get('itag')))window.location=c

(I only tested this in Chrome). Now, whenever you are on a youtube video page, just click the bookmarklet and it will

  • Decrypt the trivial encryption Google applies to the media stream sources.
  • Use a well known range exploit to get the full length media instead of individual chunks.
  • Choose the best audio bitrate and format available (anywhere from 320k Vorbis to 70k Opus)
  • Redirect the current page to the best raw audio stream.

If you reach the media stream, just press CTRL-S and save the file. If the save-file dialog doesn't automatically give you a file extension, just use "m4a". Some, but not all stream formats can be played directly in the browser. But it is always a better idea just to save them.

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