Ich möchte die Dauer eines Youtube-Videos erhalten. Hier ist der Code, den ich ausprobiert habe:
$vidID="voNEBqRZmBc";
//http://www.youtube.com/watch?v=voNEBqRZmBc
$url = "http://gdata.youtube.com/feeds/api/videos/". $vidID;
$doc = new DOMDocument;
$doc->load($url);
$title = $doc->getElementsByTagName("title")->item(0)->nodeValue;
Bitte überprüfen Sie die XML-Datei. Ich habe den Titel dieses Videos erhalten. Ich möchte die Dauer von <yt:duration seconds='29'/>
erhalten.
Wie erhält man das seconds
-Attribut dieses <yt>
-Tags?
Hier ist Ihre Ausgabe:
Video Duration
0 mins
29 secs
Einige zusätzliche Ressourcen: https://developers.google.com/youtube/v3/docs/videos#contentDetails.durationhttps: // Entwickler. google.com/youtube/v3/docs/videos/list
Dies funktionierte für mich auf der Annahme, dass nur ein Element für die Dauer im Feed enthalten ist.
<?php
$vidID="voNEBqRZmBc";
//http://www.youtube.com/watch?v=voNEBqRZmBc
$url = "http://gdata.youtube.com/feeds/api/videos/". $vidID;
$doc = new DOMDocument;
$doc->load($url);
$title = $doc->getElementsByTagName("title")->item(0)->nodeValue;
$duration = $doc->getElementsByTagName('duration')->item(0)->getAttribute('seconds');
print "TITLE: ".$title."<br />";
print "Duration: ".$duration ."<br />";
Ausgabe:
TITLE: Introducing iTime
Duration: 29
Hier ist YouTube API V3 mit Beispiel für das Abrufen der Videodauer. Vergessen Sie jedoch nicht, Ihren API-Schlüssel unter https://console.developers.google.com/project zu erstellen.
$vidkey = "cHPMH2sa2Q" ; video key for example
$apikey = "xxxxxxxxxxxxxxxxxxxxx" ;
$dur = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=contentDetails&id=$vidkey&key=$apikey");
$VidDuration =json_decode($dur, true);
foreach ($VidDuration['items'] as $vidTime)
{
$VidDuration= $vidTime['contentDetails']['duration'];
}
Eine kurze und einfache Lösung mit SimpleXML:
function getYouTubeVideoDuration($id) {
$xml = simplexml_load_file('http://gdata.youtube.com/feeds/api/videos/'.$id);
return strval($xml->xpath('//yt:duration[@seconds]')[0]->attributes()->seconds);
}
Alle vorherigen Antworten geben die Dauer in einem Textformat an:
Diese Lösung gibt Ihnen die Stunden/Minuten/Sekunden für die Umwandlung in das gewünschte Format:
function getDurationInSeconds($talkId){
$vidkey = $talkId;
$apikey = "xxxxx";
$dur = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=contentDetails&id=$vidkey&key=$apikey");
$VidDuration =json_decode($dur, true);
foreach ($VidDuration['items'] as $vidTime)
{
$VidDuration= $vidTime['contentDetails']['duration'];
}
preg_match_all('/(\d+)/', $VidDuration, $parts);
$hours = intval(floor($parts[0][0]/60) * 60 * 60);
$minutes = intval($parts[0][0]%60 * 60);
$seconds = intval($parts[0][1]);
$totalSec = $hours + $minutes + $seconds; // This is the example in seconds
return $totalSec;
}
Sehr leicht
function getYoutubeDuration($videoid) {
$xml = simplexml_load_file('https://gdata.youtube.com/feeds/api/videos/' . $videoid . '?v=2');
$result = $xml->xpath('//yt:duration[@seconds]');
$total_seconds = (int) $result[0]->attributes()->seconds;
return $total_seconds;
}
//now call this pretty function. As parameter I gave a video id to my function and bam!
echo getYoutubeDuration("y5nKxHn4yVA");