Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get duration from a youtube url

Tags:

youtube

meta

Im looking for a function that will pull the youtube duration of a video from the url. I read some tutorials but don't get it. I embed videos on my site using a url and I have a function that pulls the thumbnail and I just want something similar that gets the duration. Here's how I get the thumb...

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;

}

like image 590
Pollux Khafra Avatar asked Feb 06 '12 21:02

Pollux Khafra


2 Answers

youtube api v3


usage

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

function

/**
* 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;
}
like image 125
Mantas D Avatar answered Nov 04 '22 23:11

Mantas D


This is my function to get youtube duration in second. he is fully 100% work. you need just youtube id video and youtube api key.

how to add youtube api key show this video 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;

    }

This is my function to get youtube duration in second. he is fully 100% work. you need just youtube id video and youtube api key.

how to add youtube api key show this video https://www.youtube.com/watch?v=4AQ9UamPN6E

like image 28
around throutle Avatar answered Nov 04 '22 21:11

around throutle