Get duration from a youtube url

前端 未结 6 1062
感情败类
感情败类 2021-02-08 10:51

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

6条回答
  •  无人共我
    2021-02-08 10:59

    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

提交回复
热议问题