使用 YouTube 获取时长 API

Get duration using YouTube API

我需要使用 Youtube API V3 获取视频时长。我的应用程序在 API V3 上运行良好,但现在无法运行。

我找到了一个工作示例并且它有效:

$dur = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=contentDetails&id=[VIDOID]&key=[API KEY]");


$duration =json_decode($dur, true);

foreach ($duration['items'] as $vidTime) {
    $vTime= $vidTime['contentDetails']['duration'];
}

致谢:Youtube API v3 , how to get video durations?

这将 return 时间格式类似这样。

PT24M30S 

如何将其转换为可读时间。喜欢 24:30?

video resource 包含一个 duration 字段,它是以下格式的 string。开发人员可以根据您的应用程序和需要重新格式化它。

contentDetails.duration

The length of the video. The tag value is an ISO 8601 duration. For example, for a video that is at least one minute long and less than one hour long, the duration is in the format PT#M#S, in which the letters PT indicate that the value specifies a period of time, and the letters M and S refer to length in minutes and seconds, respectively. The # characters preceding the M and S letters are both integers that specify the number of minutes (or seconds) of the video. For example, a value of PT15M33S indicates that the video is 15 minutes and 33 seconds long.

If the video is at least one hour long, the duration is in the format PT#H#M#S, in which the # preceding the letter H specifies the length of the video in hours and all of the other details are the same as described above. If the video is at least one day long, the letters P and T are separated, and the value's format is P#DT#H#M#S. Please refer to the ISO 8601 specification for complete details.

至于如何做到这一点,我不得不说删除 PT 和 S 并将 M 替换为 :。根据值为空时发生的情况,您可能需要进行一些测试。我会对 ISO-8061

做一些研究
 $time_format = "PT24M30S ";

preg_match_all('/(\d+)/',$time_format,$parts);

$hours = floor($parts[0][0]/60);
$minutes = $parts[0][0]%60;
$seconds = $parts[0][1];

echo $hours . ": ". $minutes .":". $seconds; 

一种可能的方法是使用函数 str_replace();

$stamp = "PT24M30S";
$formated_stamp = str_replace(array("PT","M","S"), array("",":",""),$stamp);

echo $formated_stamp; //should give "24:30"

奖金内容 - 前导零

为了添加前导零,必须首先使用 explode() 拆分字符串,然后使用 sprintf() 单独格式化数字;最后再把它们加在一起。

$exploded_string = explode(":",$formated_stamp);
$new_formated_stamp = sprintf("%02d", $exploded_string[0]).":".sprintf("%02d", $exploded_string[1]);

难以置信,有人用过DateInterval,为什么?就是这样:

$duration = new DateInterval('PT24M30S');

print $duration->format('%H:%i:%s'); // outputs: 00:24:30

有用的链接:

我使用以下函数来转换 YouTube 时长。只需将 $yt 替换为 YouTube 提供给您的格式,它就会打印出漂亮且可读的格式。

function ConvertYoutubeVideoDuration($yt){
$yt=str_replace(['P','T'],'',$yt);
foreach(['D','H','M','S'] as $a){
    $pos=strpos($yt,$a);
    if($pos!==false) ${$a}=substr($yt,0,$pos); else { ${$a}=0; continue; }
    $yt=substr($yt,$pos+1);
}
if($D>0){
    $M=str_pad($M,2,'0',STR_PAD_LEFT);
    $S=str_pad($S,2,'0',STR_PAD_LEFT);
    return ($H+(24*$D)).":$M:$S"; // add days to hours
} elseif($H>0){
    $M=str_pad($M,2,'0',STR_PAD_LEFT);
    $S=str_pad($S,2,'0',STR_PAD_LEFT);
    return "$H:$M:$S";
} else {
    $S=str_pad($S,2,'0',STR_PAD_LEFT);
    return "$M:$S";
}
}

解决方案PHP

function covtime($youtube_time){
            preg_match_all('/(\d+)/',$youtube_time,$parts);
            $hours = $parts[0][0];
            $minutes = $parts[0][1];
            $seconds = $parts[0][2];
            if($seconds != 0)
                return $hours.':'.$minutes.':'.$seconds;
            else
                return $hours.':'.$minutes;
        }