【问题标题】:How to convert YouTube API duration (ISO 8601 duration in the format PT#M#S) to seconds如何将 YouTube API 持续时间(采用 PT#M#S 格式的 ISO 8601 持续时间)转换为秒
【发布时间】:2013-10-04 09:02:24
【问题描述】:

如何使用 JavaScript 以 PT#M#S 格式操作日期时间?

例如:PT5M33S

我想输出为hh:mm:ss

【问题讨论】:

标签: javascript youtube-javascript-api


【解决方案1】:

这是获取总秒数和其他部分的基本代码。 这样做我感到不安,因为规则说,任何时候你都不应该对逻辑进行约会:) 但无论如何,就这样 - 谷歌让它变得不容易,在 getduration 播放器 API 中提供总秒数,并提供完全不同的gdata api 中的格式。

          var reptms = /^PT(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?$/;
          var hours = 0, minutes = 0, seconds = 0, totalseconds;

          if (reptms.test(input)) {
            var matches = reptms.exec(input);
            if (matches[1]) hours = Number(matches[1]);
            if (matches[2]) minutes = Number(matches[2]);
            if (matches[3]) seconds = Number(matches[3]);
            totalseconds = hours * 3600  + minutes * 60 + seconds;
          }

【讨论】:

  • 谢谢,通过一些调整,我让它按我的需要工作。
  • duration: "PT26M"怎么样
  • 输入格式也可以包含小数。例如PT36.53S。这是和更新的正则表达式:^PT(?:(\d+\.*\d*)H)?(?:(\d+\.*\d*)M)?(?:(\d+\.*\d*)S)?$
【解决方案2】:

您可以通过 Youtube API (v3) 以简单的方式获取 youtube 视频数据并将视频时长 (ISO 8601) 转换为秒。不要忘记将 URL 中的 { YOUR VIDEO ID }{ YOUR KEY } 属性更改为您的 video id公开的谷歌密钥。您可以创建一个访问 google 开发者控制台的公钥。

  $.ajax({
       url: "https://www.googleapis.com/youtube/v3/videos?id={ YOUR VIDEO ID }&part=contentDetails&key={ YOUR KEY }",
       dataType: "jsonp",
       success: function (data) { youtubeCallback (data); }
  });

    function youtubeCallback(data) {

        var duration = data.items[0].contentDetails.duration;
        alert ( convertISO8601ToSeconds (duration) );
    }        

    function convertISO8601ToSeconds(input) {

        var reptms = /^PT(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?$/;
        var hours = 0, minutes = 0, seconds = 0, totalseconds;

        if (reptms.test(input)) {
            var matches = reptms.exec(input);
            if (matches[1]) hours = Number(matches[1]);
            if (matches[2]) minutes = Number(matches[2]);
            if (matches[3]) seconds = Number(matches[3]);
            totalseconds = hours * 3600  + minutes * 60 + seconds;
        }

        return (totalseconds);
    }

【讨论】:

  • duration: "PT26M"怎么样
【解决方案3】:

虽然这些答案在技术上是正确的;如果您打算在时间和持续时间上做很多事情,您应该查看momentjs。另请查看moment-duration-formats,它使格式化持续时间与常规 momentjs 时间一样简单

这两个模块的简单示例

moment.duration('PT5M33S').format('hh:mm:ss')

这将输出 05:33。还有很多其他用途。

尽管 youtube 使用 ISO8601 格式是有原因的,因为它是一种标准,所以请记住这一点。

【讨论】:

  • TypeError: moment.duration(...).format is not a function 我用 Node 8.10 得到这个,时刻 2.22.2
  • @Abhi 你安装了 moment-duration-format 包并需要它吗?
  • 我是这样使用的:moment().day("Monday").year(year_val).week(week_val).format('YYYY-MM-DD'),它正在工作。
  • @Abhi 所以您没有阅读软件包的文档?好的。
【解决方案4】:

我通过检查单位左侧的两个索引字符 (H、M、S) 并检查它是否是数字来实现这一点,如果不是,则单位是单个数字,我在前面加上一个额外的“0”。否则我返回两位数。

   function formatTimeUnit(input, unit){
     var index = input.indexOf(unit);
     var output = "00"
    if(index < 0){
      return output; // unit isn't in the input
    }

    if(isNaN(input.charAt(index-2))){
      return '0' + input.charAt(index-1);
    }else{
      return input.charAt(index-2) + input.charAt(index-1);
    }
  }

我这样做了几小时、几分钟和几秒钟。如果输入中没有任何时间,我也会放弃时间,这当然是可选的。

    function ISO8601toDuration(input){
     var H = formatTimeUnit(input, 'H');
     var M = formatTimeUnit(input, 'M');
     var S = formatTimeUnit(input, 'S');

    if(H == "00"){
      H = "";
    }else{
      H += ":"
    }

    return H  + M + ':' + S ;
  }

那就这样称呼吧

  duration = ISO8601toDuration(item.duration);

我使用它来格式化 youtube 数据 API 视频时长。 希望这可以帮助某人

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-04
    • 2021-01-11
    相关资源
    最近更新 更多