【问题标题】:Youtube playlist all videos duration show in phpYoutube 播放列表所有视频时长显示在 php
【发布时间】:2014-06-14 16:18:50
【问题描述】:

我想通过此链接http://gdata.youtube.com/feeds/api/playlists/PLCK7NnIZXn7gGU5wDy9iKOK6T2fwtGL6l 将所有视频时间等同于 youtube 播放列表。这里有像这样的时间代码time='00:05:11.500' ..我想从 php 获取所有视频时间,然后从 php 中获取这样的显示

show it like this : 2:10:50 (2=hours,10=minutes,50=seconds)

我想从 php 中获得像这样的变量。请帮助这篇文章谢谢。我试图这样做.. 但我可以这样做.. 如果有人可以帮助我.. 如果有 4 个视频,想要等于所有视频的时间,然后只想显示来自 php 的所有持续时间

【问题讨论】:

  • 你“想要”很多。你试过什么?
  • 我想在我的视频图像上显示 youtube 播放列表的持续时间.. 就像 youtube.. 但我想获得播放列表的持续时间
  • ILI 询问您已经编码了什么?然后我们可以检查问题

标签: php youtube


【解决方案1】:

好的,这是一个解决问题的答案,假设您没有任何代码,并且无意 尝试自己做实验。

除了描述的 exact 问题之外,您可能无法将其用于其他任何事情: 将此提要的所有持续时间相加并显示为小时:分钟:秒

<?php

$total_seconds = 0;

$dom = new DOMDocument();
$dom->loadXML(file_get_contents('http://gdata.youtube.com/feeds/api/playlists/PLCK7NnIZXn7gGU5wDy9iKOK6T2fwtGL6l'));

$xpath = new DOMXPath($dom);
foreach ($xpath->query('//yt:duration/@seconds') as $duration) {
    $total_seconds += (int) $duration->value;
}

然后以您的格式显示 $total_seconds。这里有两个选项:

assuming that hours will never be larger than 24

echo gmdate("H:i:s", $total_seconds);

allowing total time to be larger than 24 hours

echo (int) ($total_seconds / 3600) . ':' . (int) ($total_seconds / 60) % 60 . ':' . $total_seconds % 60;

请记住:此代码执行完全零错误检查。可能出错的地方:

  • PHP 配置可能不允许 http stream wrapper
  • PHP 构建可能没有Dom enabled
  • XML 供稿可能不可用
  • XML 供稿可能不包含任何条目

【讨论】:

    【解决方案2】:

    编辑: 我仔细查看了提要,似乎“时间”条目只是缩略图的指针。视频的实际 duration 以秒为单位设置&lt;yt:duration seconds='667'/&gt;,因此您可以将它们作为整数相加,然后使用 DateTime 类转换为您的格式。示例here

    结束编辑

    首先,要获得所有时间,您可能需要 PHP 中的 atom 提要阅读器。有plenty out there。不要试图解析 XML,ATOM 是一个众所周知的标准,应该很容易使用(如果你真的只想要时间,你可以使用xpath 查询)。

    既然您可以随时使用,您需要一种方法来轻松地将它们相加,最好不要弄乱嵌套循环和 if 语句。

    这是一个代表单个视频的单个时间条目的类:

    final class Duration
    {
        private $hours;
        private $minutes;
        private $seconds;
        private $centis;
    
        /* we don't want any Durations not created with a create function */
        private function __construct() {}
    
        public static function fromString($input = '00:00:00.000') {
            $values = self::valuesFromString($input);
    
            return self::fromValues($values['hours'], $values['minutes'], $values['seconds'], $values['centis']);
        }
    
        public function addString($string) {
            $duration = self::fromString($string);
            return $this->addDuration($duration);
        }
    
        public function addDuration(Duration $duration) {
            // add the durations, and return a new duration;
            $values = self::valuesFromString((string) $duration);
    
            // adding logic here
            $centis  = $values['centis'] + $this->centis;
            $this->fixValue($centis, 1000, $values['seconds']);
    
            $seconds = $values['seconds'] + $this->seconds;
            $this->fixValue($seconds, 60, $values['minutes']);
    
            $minutes = $values['minutes'] + $this->minutes;
            $this->fixValue($minutes, 60, $values['hours']);
    
            $hours   = $values['hours'] + $this->hours;
    
            return self::fromValues($hours, $minutes, $seconds, $centis);
        }
    
        public function __toString() {
            return str_pad($this->hours,2,'0',STR_PAD_LEFT) . ':'
                   . str_pad($this->minutes,2,'0',STR_PAD_LEFT) . ':'
                   . str_pad($this->seconds,2,'0',STR_PAD_LEFT) . '.'
                   . str_pad($this->centis,3,'0',STR_PAD_LEFT);
        }
    
        public function toValues() {
            return self::valuesFromString($this);
        }
    
        private static function valuesFromString($input) {
            if (1 !== preg_match('/(?<hours>[0-9]{2}):(?<minutes>([0-5]{1}[0-9]{1})):(?<seconds>[0-5]{1}[0-9]{1}).(?<centis>[0-9]{3})/', $input, $matches)) {
                throw new InvalidArgumentException('Invalid input string (should be 01:00:00.000): ' . $input);
            }
    
            return array(
                    'hours' => (int) $matches['hours'],
                    'minutes' => (int) $matches['minutes'],
                    'seconds' => (int) $matches['seconds'],
                    'centis' => (int) $matches['centis']
                );
        }
    
        private static function fromValues($hours = 0, $minutes = 0, $seconds = 0, $centis = 0) {
            $duration = new Duration();
            $duration->hours = $hours;
            $duration->minutes = $minutes;
            $duration->seconds = $seconds;
            $duration->centis = $centis;
    
            return $duration;
        }
    
        private function fixValue(&$input, $max, &$nextUp) {
            if ($input >= $max) {
                $input -= $max;
                $nextUp += 1;
            }
        }
    }
    

    您只能通过调用静态工厂 fromString() 来创建新的 Duration,它只接受“00:00:00.000”(小时:分钟:秒.毫秒)形式的字符串:

    $duration = Duration::fromString('00:04:16.250');
    

    接下来,您可以添加另一个字符串或实际的持续时间对象,以创建新的持续时间:

    $newDuration = $duration->addString('00:04:16.250');
    $newDuration = $duration->addDuration($duration);
    

    Duration 对象将以 '00:00:00.000' 格式输出它自己的持续时间字符串:

    echo $duration;
    
    // Gives
    00:04:16.250
    

    或者,如果您对单独的值感兴趣,可以这样获取它们:

    print_r($duration->toValues());
    
    // Gives
    Array
    (
        [hours] => 0
        [minutes] => 4
        [seconds] => 16
        [milliseconds] => 250
    )
    

    在循环中使用它来获取总视频时间的最终示例:

    $allTimes = array(
        '00:30:05:250',
        '01:24:38:250',
        '00:07:01:750'
    );
    
    $d = Duration::fromString();
    foreach ($allTimes as $time) {
        $d = $d->addString($time);
    }
    
    echo $d . "\n";
    print_r($d->toValues());
    
    // Gives
    02:01:45.250
    Array
    (
        [hours] => 2
        [minutes] => 1
        [seconds] => 45
        [milliseconds] => 250
    )
    

    关于为什么我使用了一个带有私有构造函数的最终类:

    我写了这篇文章作为自己的练习,遵循 Mathias Veraes 在 "named constructors" 上的博文。

    另外,我也忍不住加了他的"TestFrameworkInATweet"

    function it($m,$p){echo ($p?'✔︎':'✘')." It $m\n"; if(!$p){$GLOBALS['f']=1;}}function done(){if(@$GLOBALS['f'])die(1);}
    function throws($exp,Closure $cb){try{$cb();}catch(Exception $e){return $e instanceof $exp;}return false;}
    
    it('should be an empty duration from string', Duration::fromString() == '00:00:00.000');
    it('should throw an exception with invalid input string', throws("InvalidArgumentException", function () { Duration::fromString('invalid'); }));
    it('should throw an exception with invalid seconds input string', throws("InvalidArgumentException", function () { Duration::fromString('00:00:61:000'); }));
    it('should throw an exception with invalid minutes input string', throws("InvalidArgumentException", function () { Duration::fromString('00:61:00:000'); }));
    it('should add milliseconds to seconds', Duration::fromString('00:00:00.999')->addString('00:00:00.002') == Duration::fromString('00:00:01.001'));
    it('should add seconds to minutes', Duration::fromString('00:00:59.000')->addString('00:00:02.000') == Duration::fromString('00:01:01.000'));
    it('should add minutes to hours', Duration::fromString('00:59:00.000')->addString('00:02:00.000') == Duration::fromString('01:01:00.000'));
    it('should add all levels up', Duration::fromString('00:59:59.999')->addString('00:01:01.002') == Duration::fromString('01:01:01.001'));
    $duration = Duration::fromString('00:00:01.500');
    it('should add a Duration', $duration->addDuration($duration) == '00:00:03.000');
    

    【讨论】:

    • 那么如何汇总所有视频时间.. 如何连接 youtube 提要?请帮我解决这个问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-01
    • 2014-01-19
    • 1970-01-01
    • 2011-04-05
    • 1970-01-01
    • 2018-09-05
    • 1970-01-01
    相关资源
    最近更新 更多