0

I want to reproduce all youtube files embedded on a site using SoundManager2 audio player. How can I get access to Youtube file URL from video ID?

Note: Soundcloud offers the possibility to get the direct streaming URL from the song URL. Here is a working example with Soundmanager2 and Angular. Now I want to do the same for Youtube.

geraldo
  • 562
  • 11
  • 33

2 Answers2

2

One slightly hacky way - You could use Youtube's OEmbed API, however you need to provide a URL, so you'd need to add the ID onto the end. https://www.youtube.com/oembed?url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DiwGFalTRHDA

This will consistently return an embed iframe, which you could regex the URL out of.

var regex = /<iframe.*?src='(.*?)'/;
var src = regex.exec(str)[1];
Yack
  • 51
  • 5
2

Your title said using API. It's not possible to get access to YouTube file URL from video ID by that. By mean, with file URL you can also download it, distribute content etc while it's against YouTube Terms. So there is no such API for that.

Since you're tagged with Javascript, there is possibility to get direct access to the file URL. This probably is not as you wanted exactly but it worked flawlessly from the developer tools console available in any browser. For details Here you go

const videoUrls = ytplayer.config.args.url_encoded_fmt_stream_map
  .split(',')
  .map(item => item
    .split('&')
    .reduce((prev, curr) => (curr = curr.split('='),
      Object.assign(prev, {[curr[0]]: decodeURIComponent(curr[1])})
    ), {})
  )
  .reduce((prev, curr) => Object.assign(prev, {
    [curr.quality + ':' + curr.type.split(';')[0]]: curr
  }), {});
console.log(videoUrls);
Community
  • 1
  • 1
Ling Loeng
  • 128
  • 2
  • 9
  • This actually does not work for embedded youtube files: "Uncaught ReferenceError: ytplayer is not defined". ytplayer is a Javascript object, do you know how to find out how it is called on embedded iframe players? – geraldo Feb 20 '17 at 13:04
  • Seems like there was a misunderstanding about FILES term as yours. AFAIK, YouTube FILES pointed out to any YouTube videos FILE (mkv,mp4 etc) which were hosted on YouTube site. While embedded, only lets you embed a YouTube video player - not YouTube video files. – Ling Loeng Feb 20 '17 at 13:43
  • So could there be a solution by 1. loading YouTube page in the background (curl?), 2. get the YouTube videos FILE and 3. play it in a custom player like SoundManager2? – geraldo Feb 20 '17 at 17:54
  • Actually it's impossible (and illegal) to retrieve the Youtube file URL. The only way to control YouTube videos seems to be using Javascript [as described here](http://stackoverflow.com/questions/11283871/pause-youtube-video-youtube-api#11284045). – geraldo Mar 02 '17 at 10:27