11

Youtube動画の長さを取得したい。私が試したコードは次のとおりです。

$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;

XML ファイルを確認してください。この動画のタイトルがわかりました。から期間を取得したい<yt:duration seconds='29'/>

このタグのseconds属性を取得するには?<yt>

4

6 に答える 6

17

YouTube v2 API の今後の廃止に伴い、更新されたソリューションがここにあります。

  1. ここでGoogle API PHP クライアントのクローンを作成することから始めます
  2. 次に、新しいプロジェクトを作成し、「APIs and auth → API」セクションで YouTube Data API を有効にして、 Google にアプリケーションを登録します。
  3. 「APIs and auth → Credentials」の下に新しい OAuth クライアント ID を作成し、リダイレクト URI リストにフルパスを追加します。(つまりhttp://example.com/get_youtube_duration.php )
  4. $ OAUTH2_CLIENT_ID変数と $OAUTH2_CLIENT_SECRET 変数にステップ 3 の独自の値を入力するようにしながら、このコード (このサンプル コードを改変したもの) を使用します。
  5. $videoId 変数の値をハードコーディングするか、video_id get パラメータを使用して設定します (例: http://example.com/get_youtube_duration.php?video_id=XXXXXXX ) 。
  6. スクリプトにアクセスし、リンクをクリックしてアプリを承認し、Google アカウントを使用してトークンを取得します。これにより、コードにリダイレクトされ、期間が表示されます。

出力は次のとおりです。

Video Duration

0 mins

29 secs

いくつかの追加リソース: https://developers.google.com/youtube/v3/docs/videos#contentDetails.duration https://developers.google.com/youtube/v3/docs/videos/list

以下は、V2 API のみで機能します。

これは、フィードに期間要素が 1 つしかないという前提に基づいて機能しました。

<?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 />";

出力:

TITLE: Introducing iTime
Duration: 29
于 2013-09-20T16:28:15.407 に答える
10

これは YouTube API V3 で、動画の長さを取得する例を示していますが、 https://console.developers.google.com/projectで API キーを作成することを忘れないでください。

     $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'];
}
于 2015-04-21T05:53:12.740 に答える
3

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);
}
于 2013-09-20T15:47:10.550 に答える
2

前の回答はすべて、期間をテキスト形式で提供します。

このソリューションは、時間/分/秒を提供して、希望する形式に変換します。

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;
}
于 2015-08-07T10:56:04.800 に答える