Get duration from a youtube url

前端 未结 6 1060
感情败类
感情败类 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 11:15

    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");
    

提交回复
热议问题