javascript 获取youtube视频文件的时间长度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7772801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 01:15:52  来源:igfitidea点击:

Getting time length of youtube video file

phpjavascriptyoutubeyoutube-api

提问by theGame

I am having a problem. how to get the time duration of youtube video? Here is the scenario.

我有问题。如何获取youtube视频的持续时间?这是场景。

I have a input field in this field say i enter youtube url now i want to put a validation that video should only be of 1 min, if yes then i store this in database else i show an error message.

我在这个字段中有一个输入字段,说我现在输入 youtube url,我想验证视频应该只有 1 分钟,如果是,那么我将其存储在数据库中,否则我会显示一条错误消息。

is it possible to do this thing?

有可能做这件事吗?

回答by Daren Chandisingh

You can use the data API to get the information for a video. You might need to extract the video's identifier from the URL.

您可以使用数据 API 来获取视频的信息。您可能需要从 URL 中提取视频的标识符。

http://code.google.com/apis/youtube/2.0/developers_guide_php.html#Retrieving_Video_Entry

http://code.google.com/apis/youtube/2.0/developers_guide_php.html#Retrieving_Video_Entry

If you're using Zend, there's already a class to do the heavy lifting:

如果您正在使用 Zend,那么已经有一个类可以完成繁重的工作:

<?php
require_once 'Zend/Loader.php'; // the Zend dir must be in your include_path
Zend_Loader::loadClass('Zend_Gdata_YouTube');
$yt = new Zend_Gdata_YouTube();
$videoEntry = $yt->getVideoEntry('the0KZLEacs');
$duration = $videoEntry->getVideoDuration();

If not, you can go to http://gdata.youtube.com/feeds/api/videos/{$videoId}and get an XML document to process yourself.

如果没有,您可以去http://gdata.youtube.com/feeds/api/videos/{$videoId}获取一个 XML 文档来自己处理。

For example, http://gdata.youtube.com/feeds/api/videos/KURI9EQV3dYreturns an XML document for a video which contains information including the duration: <yt:duration seconds='153'/>

例如,http://gdata.youtube.com/feeds/api/videos/KURI9EQV3dY返回视频的 XML 文档,其中包含包括持续时间在内的信息:<yt:duration seconds='153'/>

回答by AlienWebguy

Per their docs, In a video feed entry, the <yt:duration> tag specifies a video's length.

根据他们的文档, In a video feed entry, the <yt:duration> tag specifies a video's length.

回答by Kaszoni Ferencz

The code below will get you the video thumbnail, title, and duration, FROM THE URL. Just change the youtube link from the end.

下面的代码将从 URL 中获取视频缩略图、标题和持续时间。只需从最后更改youtube链接即可。

Demo: http://100ro.ro/wp-includes/ajaxdemo/test.php?y=www.youtube.com/watch?v=V80jm1rs2UQ:

演示:http: //100ro.ro/wp-includes/ajaxdemo/test.php?y=www.youtube.com/watch?v=V80jm1rs2UQ

(note: use the youtube url in y=youtubeurl without http://)

注意:在 y=youtubeurl 中使用 youtube url 而不使用 http://

<?php

      getYoutubeImage($_GET["y"]);

            function getYoutubeImage($e){
            //GET THE URL
            $url = $e;

            $queryString = parse_url($url, PHP_URL_QUERY);

            parse_str($queryString, $params);

            $v = $params['v'];  




        // function to parse a video <entry>
        function parseVideoEntry($entry) {      
          $obj= new stdClass;

          // get nodes in media: namespace for media information
          $media = $entry->children('http://search.yahoo.com/mrss/');
          $obj->title = $media->group->title;
          $obj->description = $media->group->description;



          // get <yt:duration> node for video length
          $yt = $media->children('http://gdata.youtube.com/schemas/2007');
          $attrs = $yt->duration->attributes();
          $obj->length = $attrs['seconds']; 


          // return object to caller  
          return $obj;      
        }   

        // get video ID from $_GET 
        if (!isset($v)) {
          die ('ERROR: Missing video ID');  
        } else {
          $vid = $v;
        }

        // set video data feed URL
        $feedURL = 'http://gdata.youtube.com/feeds/api/videos/' . $v;

        // read feed into SimpleXML object
        $entry = simplexml_load_file($feedURL);

        // parse video entry
        $video = parseVideoEntry($entry);

        // display video image, title and duration
        echo "<img src='http://i3.ytimg.com/vi/$v/default.jpg' width='150' />";
        echo "<p>{$video->title}</p>";
        echo "<p>".sprintf("%0.2f", $video->length/60) . " min. </p>";


        } 

        ?>

回答by Russell Durham

The URL of the video will contain the video ID. You can use this ID when requesting video information using YouTube's API. In the video feed response you get back there should be a <yt:duration>tag that you can use to get the duration of the video. Just take a bit to go over the API and you should be able to find what you need.

视频的 URL 将包含视频 ID。您可以在使用YouTube 的 API请求视频信息时使用此 ID 。在您返回的视频源响应中,应该有一个<yt:duration>标签,您可以使用它来获取视频的持续时间。只需稍微浏览一下 API,您就应该能够找到所需的内容。