Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Youtube video title and duration without api key

How can get the youtube video title and duration without using api key ?

I have checked Youtube Video title with API v3 without API key? link but this only give the title and not the duration.

So how can I get duration also without api key ?

like image 209
XamDev Avatar asked Dec 23 '16 05:12

XamDev


People also ask

How do I disable YouTube API?

Disable an APIGo to the API Console. From the projects list, select a project or create a new one. If the API Manager page isn't already open, open the console left side menu and select API Manager. Next to the API you want to disable, click Disable.


2 Answers

To people looking for an answer to this in 2020. You can do this using the Youtube Iframe API. You don't need a API key! First you insert the api link into the JS like so:

  var tag = document.createElement('script');
  tag.src = "https://www.youtube.com/iframe_api";
  var firstScriptTag = document.getElementsByTagName('script')[0];
  firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

After that you create the player with the desired videoID like this:

  function onYouTubeIframeAPIReady() {
    player = new YT.Player('player', {
      height: '390',
      width: '640',
      videoId: 'SIWbjgPYcJY',
      playerVars: {
        'autoplay': 0,
        'controls': 1
      },
      events: {
        'onReady': onPlayerReady
      }
    });

  }

Then you need to get the title and duration by assigning these in a variable to your HTML. We place this in the onPlayerReady.

  function onPlayerReady(event) {
    time = player.getDuration();
    $(".dur").val(time);
    title = player.getVideoData().title
    $(".title").val(title);
  }

Last thing is to put the player, duration and title in the HTML:

<div id="player"></div>
<br>
<span>Titel:</span><input class="title">
<br>
<span>duration:</span><input class="dur">
<br>

JSFIDDLE

like image 111
meekz89 Avatar answered Jan 02 '23 20:01

meekz89


You can use youtube-dl:

$ youtube-dl -e "https://www.youtube.com/watch?v=2i2khp_npdE"
Alan Walker - Sing Me To Sleep
$ youtube-dl --get-duration "https://www.youtube.com/watch?v=2i2khp_npdE"
3:12

Alternatively:

$ curl -s "https://www.youtube.com/watch?v=2i2khp_npdE" | tr '<' '\n' | awk -F'"' '/name="title"/ { print $4 }'
Alan Walker - Sing Me To Sleep
$ curl -s "https://www.youtube.com/watch?v=2i2khp_npdE" | awk -F'"' '/itemprop="duration"/ { print $4 }'
PT3M12S

You can use this bash function to convert from the Youtube format to seconds:

duration_calculation ()
{
    local total_in_second=0
    local hours=0
    local minutes=0
    local seconds=0
    local duration="$1"
    
    if [[ $duration =~ ^PT([0-9]{1,})H([0-9]{1,})M([0-9]{1,})S$ ]]; then
        hours=$((hours + BASH_REMATCH[1]))
        minutes=$((minutes + BASH_REMATCH[2]))
        seconds=$((seconds + BASH_REMATCH[3]))
    # PT1H4M    H:M:00
    elif [[ $duration =~ ^PT([0-9]{1,})H([0-9]{1,})M$ ]];then
        hours=$((hours + BASH_REMATCH[1]))
        minutes=$((minutes + BASH_REMATCH[2]))
    # PT1H29S   H:00:S
    elif [[ $duration =~ ^PT([0-9]{1,})H([0-9]{1,})S$ ]]; then
        hours=$((hours + BASH_REMATCH[1]))
        seconds=$((seconds + BASH_REMATCH[2]))
    # PT4M29S   M:S
    elif [[ $duration =~ ^PT([0-9]{1,})M([0-9]{1,})S$ ]]; then
        minutes=$((minutes + BASH_REMATCH[1]))
        seconds=$((seconds + BASH_REMATCH[2]))
    # PT1H      H:00:00
    elif [[ $duration =~ ^PT([0-9]{1,})H$ ]]; then
        hours=$((hours + BASH_REMATCH[1]))
    # PT4M      00:M:00
    elif [[ $duration =~ ^PT([0-9]{1,})M$ ]]; then
        minutes=$((minutes + BASH_REMATCH[1]))
    # PT29S     S
    elif [[ $duration =~ ^PT([0-9]{1,})S$ ]]; then
        seconds=$((seconds + BASH_REMATCH[1]))
    fi
    total_in_seconds=$(( (hours * 3600) + (minutes * 60) + seconds ))
    echo "$total_in_seconds"
}
like image 24
poboxy Avatar answered Jan 02 '23 20:01

poboxy