10

YouTube の動画の長さを URL から取得する関数を探しています。いくつかのチュートリアルを読みましたが、わかりません。URL を使用して自分のサイトに動画を埋め込みます。サムネイルを取得する機能があり、持続時間を取得する同様の機能が必要です。親指の取り方はこちら…

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;

}

4

6 に答える 6

13

次のようなものを使用できます。

<?php

    function getDuration($url){

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


        $data=@file_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');

?>

ビデオの長さを秒単位で返します。

参照: http://code.google.com/apis/youtube/2.0/developers_guide_protocol.html

このような関数を使用して、秒を時、分、秒に変更できます。

于 2012-02-06T21:47:31.673 に答える
5

YouTube 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;
}
于 2015-06-04T07:01:00.260 に答える
0

時刻フォーマッタを使用して、秒を好きな形式に変更するだけです。つまり、return gmdate("H:i:s", $obj->data->duration);

于 2013-06-11T04:58:48.110 に答える
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");
于 2014-07-19T21:37:12.677 に答える