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 ?
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.
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
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"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With