views:

221

answers:

3

Hi,

On my website, people are able to drop their YouTube code link Like faXwJ9TfX9g

How do I get the name of the artist and name of the song, with a simple Javascript.

Input  :  faXwJ9TfX9g   by User
Output :  Name of artist
Output :  Name of the Song
Output :  Length of the Song

Best regards,

Simon Buijs Zwaag Netherlands

A: 

There are many API available for Youtube, you'll find the documentation you want there

Valentin Rocher
Hi, Thank you for reading my question, but i espected a specific script or a good referantial.I am able to handle all the questions on this site by refferring look at the Internet.......Sorry Simon
Simon
+3  A: 

Youtube videos don't have tags for artist and song title (like mp3 files do, for example). So, AFAIK the best you can do is get the video title. You could go further and parse it to get the artist and song name, but I don't think this is something you'd want to do. Using the Data API here's what you can do:

<html>
<head>
<script type="text/javascript">
  function processData(data) {
    var title = data.entry.title.$t;
    var duration = data.entry.media$group.media$content[0].duration;
  }
</script>
</head>
  <body>
    <script 
      type="text/javascript" 
      src="http://gdata.youtube.com/feeds/api/videos/faXwJ9TfX9g?alt=json-in-script&amp;callback=processData"&gt;
    </script> 
  </body>
</html>
slack3r
+1  A: 

I've been trying to use the above example in a chrome extension I'm working on. It's an extension for scrobbling songs played on youtube to my last.fm account. I'm mostly interested in retrieving information about the duration of the song. The following code examples are taking from my extension content script (http://code.google.com/chrome/extensions/content_scripts.html)

Simple code to extract the video ID:

var videoID = document.URL.replace(/^[^v]+v.(.{11}).*/,"$1"); // get video id from URL

Then different approaches to use the code above:

var script = document.createElement("script");
script.setAttribute("type", "text/javascript");    
script.setAttribute("src", "http://gdata.youtube.com/feeds/api/videos/"+ videoID +"?alt=json-in-script&callback=processData");

document.getElementsByTagName('head')[0].appendChild(script);

function processData(data) {
    var title = data.entry.title.$t;
    var duration = data.entry.media$group.media$content[0].duration;
}

And alternately, using a helper function:

function loadJSON(url) {
  var headID = document.getElementsByTagName("head")[0];         
  var newScript = document.createElement('script');
      newScript.type = 'text/javascript';
      newScript.src = url;
  headID.appendChild(newScript);
}

loadJSON("http://gdata.youtube.com/feeds/api/videos/"+ videoID +"?alt=json-in-script&callback=processData");

Any help would be much appreciated, thanks.

mortenfriis