2012-02-06 18 views
5

से अवधि प्राप्त करें मैं एक ऐसे फ़ंक्शन की तलाश कर रहा हूं जो यूआरएल से वीडियो की यूट्यूब अवधि खींच लेगा। मैंने कुछ ट्यूटोरियल पढ़े लेकिन इसे नहीं मिला। मैं एक यूआरएल का उपयोग कर अपनी साइट पर वीडियो एम्बेड करता हूं और मेरे पास एक ऐसा फ़ंक्शन है जो थंबनेल खींचता है और मुझे बस कुछ ऐसा ही चाहिए जो अवधि प्राप्त करता हो।यूट्यूब यूआरएल

<?php 

    function getDuration($url){ 

     parse_str(parse_url($url,PHP_URL_QUERY),$arr); 
     $video_id=$arr['v']; 


     [email protected]_get_contents('http://gdata.youtube.com/feeds/api/videos/'.$video_id.'?v=2&alt=jsonc'); 
     if (false===$data) return false; 

     $obj=json_decode($data); 

     return $obj->data->duration; 
    } 

    echo getDuration('http://www.youtube.com/watch?v=rFQc7VRJowk'); 

?> 

उस वीडियो की अवधि को सेकंड में प्रस्तुत करती है: यहाँ कैसे मैं अंगूठे मिल

function get_youtube_screen_link($url = '', $type = 'default', $echo = true) { 
if(empty($url)) 
    return false; 

if(!isset($type)) 
    $type = ''; 

$url = esc_url($url); 

preg_match("|[\\?&]v=([^&#]*)|",$url,$vid_id); 

if(!isset($vid_id[1])) 
    return false; 

$img_server_num = 'i'. rand(1,4); 

switch($type) { 
    case 'large': 
     $img_link = "http://{$img_server_num}.ytimg.com/vi/{$vid_id[1]}/0.jpg"; 
     break; 
    case 'first': 
     // Thumbnail of the first frame 
     $img_link = "http://{$img_server_num}.ytimg.com/vi/{$vid_id[1]}/1.jpg"; 
     break; 
    case 'small': 
     // Thumbnail of a later frame(i'm not sure how they determine this) 
     $img_link = "http://{$img_server_num}.ytimg.com/vi/{$vid_id[1]}/2.jpg"; 
     break; 
    case 'default': 
    case '': 
    default: 
     $img_link = "http://{$img_server_num}.ytimg.com/vi/{$vid_id[1]}/default.jpg"; 
     break; 
} 
if($echo) 
    echo $img_link; 
else 
    return $img_link; 

}

उत्तर

11

आप कुछ इस तरह इस्तेमाल कर सकते हैं है ...।

संदर्भ: http://code.google.com/apis/youtube/2.0/developers_guide_protocol.html

आप घंटे, मिनट और सेकंड के लिए सेकंड बदलने के लिए this one की तरह एक समारोह का उपयोग कर सकते हैं।

+0

मुझे प्रत्येक पोस्ट के लिए एक कस्टम फ़ील्ड में दर्ज यूआरएल से खींचने के लिए $ video_id की आवश्यकता है। –

+0

url – stewe

+0

से वीडियो_आईडी प्राप्त करने के लिए उदाहरण अपडेट किया गया मैंने इसे फ़ंक्शन के रूप में उपयोग किया और यह काम करता है लेकिन यदि वीडियो को यूट्यूब से हटा दिया गया है तो मुझे त्रुटियां मिलती हैं जहां यह आउटपुट करने का प्रयास करती है। इसके खिलाफ सुरक्षा के लिए मैं एक कथन कैसे कर सकता हूं? –

0

आप आसानी से किसी भी प्रारूप में सेकंड को बदलने के लिए एक समय फॉर्मेटर का उपयोग कर सकते हैं। यानी जीएमडीएटी ("एच: आई: एस", $ obj-> डेटा-> अवधि लौटाएं;

0
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"); 
+1

यह एंडपॉइंट (v2) अब काम नहीं करता है। –

3

यूट्यूब API v3


उपयोग

echo youtubeVideoDuration('video_url', 'your_api_key'); 
// output: 63 (seconds) 

समारोह

/** 
* Return video duration in seconds. 
* 
* @param $video_url 
* @param $api_key 
* @return integer|null 
*/ 
function youtubeVideoDuration($video_url, $api_key) { 

    // video id from url 
    parse_str(parse_url($video_url, PHP_URL_QUERY), $get_parameters); 
    $video_id = $get_parameters['v']; 

    // video json data 
    $json_result = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=contentDetails&id=$video_id&key=$api_key"); 
    $result = json_decode($json_result, true); 

    // video duration data 
    if (!count($result['items'])) { 
     return null; 
    } 
    $duration_encoded = $result['items'][0]['contentDetails']['duration']; 

    // duration 
    $interval = new DateInterval($duration_encoded); 
    $seconds = $interval->days * 86400 + $interval->h * 3600 + $interval->i * 60 + $interval->s; 

    return $seconds; 
} 
+0

मेरे लिए काम नहीं करता है, कहते हैं file_get_content सुरक्षा कारणों से अवरुद्ध है –

1

यह मेरा कार्य है जो यूट्यूब अवधि को दूसरे में प्राप्त करने के लिए है। वह पूरी तरह से 100% काम है। आपको केवल यूट्यूब आईडी वीडियो और यूट्यूब एपीआई कुंजी की आवश्यकता है।

कैसे यूट्यूब api कुंजी शो इस वीडियो https://www.youtube.com/watch?v=4AQ9UamPN6E

public static function getYoutubeDuration($id) 
    { 



     $Youtube_KEY=''; 

     $dur = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=contentDetails&id={$id}&key={$Youtube_KEY}"); 

     $vTime=''; 
     $H=0; 
     $M=0; 
     $S=0; 

     $duration = json_decode($dur, true); 
     foreach ($duration['items'] as $vidTime) { 
     $vTime = $vidTime['contentDetails']['duration']; 
     } 

     $vTime = substr($vTime, 2); 
     $exp = explode("H",$vTime); 

     //if explode executed 
     if($exp[0] != $vTime) 
     { 
      $H = $exp[0]; 
      $vTime = $exp[1]; 
     } 

     $exp = explode("M",$vTime); 

     if($exp[0] != $vTime) 
     { 
      $M = $exp[0]; 
      $vTime = $exp[1]; 
     } 

     $exp = explode("S",$vTime); 
     $S = $exp[0]; 


     $H = ($H*3600); 
     $M = ($M*60); 
     $S = ($H+$M+$S); 


     return $S; 

    } 

यह दूसरी में यूट्यूब अवधि पाने के लिए मेरी समारोह है जोड़ने के लिए। वह पूरी तरह से 100% काम है। आपको केवल यूट्यूब आईडी वीडियो और यूट्यूब एपीआई कुंजी की आवश्यकता है।

कैसे यूट्यूब api कुंजी शो इस वीडियो https://www.youtube.com/watch?v=4AQ9UamPN6E

0

1) आप एक API कुंजी की आवश्यकता होगी जोड़ने के लिए। https://developers.google.com/ पर जाएं और यदि आवश्यक हो, तो लॉग इन करें या एक खाता बनाएं। लॉग इन करने के बाद इस लिंक पर जाएं https://console.developers.google.com/project और ब्लू क्रिएट प्रोजेक्ट बटन पर क्लिक करें। एक पल प्रतीक्षा करें क्योंकि Google आपकी प्रोजेक्ट तैयार करता है।

2) आपको एक वेब होस्ट की आवश्यकता होगी जो PHP_ file_content समर्थित PHP के साथ चल रहा है। आप केवल "echo php_info();" के साथ एक नई स्क्रिप्ट बनाकर जांच सकते हैं और यह सत्यापित करना कि allow_url_fopen चालू है। यदि ऐसा नहीं है, तो कॉन्फ़िगरेशन बदलें या अपने वेब होस्ट से बात करें।

<?php 

    $sYouTubeApiKey = "<enter the key you get>"; 
    //Replace This With The Video ID. You can get it from the URL. 
    //ie https://www.youtube.com/XWuUdJo9ubM would be 
    $sVideoId = "XWuUdJo9ubM"; 

    //Get The Video Details From The API. 
    function getVideoDetails ($sVideoId) { 
    global $sYouTubeApiKey; 
    //Generate The API URL 
    $sUrl = "https://www.googleapis.com/youtube/v3/videos?id=".$sVideoId."&key=".$sYouTubeApiKey."&part=contentDetails"; 
    //Perform The Request 
    $sData = file_get_contents($sUrl); 
    //Decode The JSON Response 
    return json_decode($sData); 
    } 

    //Get The Video Duration In Seconds. 
    function getVideoDuration ($sVideoId) { 
    $oData = getVideoDetails($sVideoId); 
    //Deal With No Videos In List 
    if (count($oData->items) < 1) return false; 
    //Get The First Video 
    $oVideo = array_shift($oData->items); 
    //Extract The Duration 
    $sDuration = $oVideo->contentDetails->duration; 
    //Use Regular Expression To Parse The Length 
    $pFields = "'PT(?:([0-9]+)H)?(?:([0-9]+)M)?(?:([0-9]+)S)?'si"; 
    if (preg_match($pFields, $sDuration, $aMatch)) { 
     //Add Up Hours, Minutes, and Seconds 
     return $aMatch[1]*3600 + $aMatch[2]*60 + $aMatch[3]; 
    } 
    } 

    header("Content-Type: text/plain"); 
    $oData = getVideoDetails($sVideoId); 
    print_r($oData); 
    echo "Length: ".getVideoDuration($sVideoId); 
?> 

आशा है कि इससे मदद मिलती है!

संबंधित मुद्दे