How to convert YouTube API duration (ISO 8601 duration in the format PT#M#S) to seconds

百般思念 提交于 2019-12-10 01:56:58

问题


How can I manipulate a date time in the format PT#M#S with JavaScript?

for example: PT5M33S

I'd like to output as hh:mm:ss.


回答1:


Here's the basic code to get total number of seconds and other parts. I feel uneasy doing this, as the rule says that any time you want to date logic you shouldn't :) But anyways, here it goes - Google made it not easy, providing total seconds in the getduration player API, and providing totally different format in the 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;
          }



回答2:


Here is how you can get a youtube video data via Youtube API (v3) in a simple way and convert the video duration (ISO 8601) to seconds. Don't forget to change the { YOUR VIDEO ID } and { YOUR KEY } attribute in URL to your video id and your public google key. You can create a public key accessing the google developer console.

  $.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);
    }



回答3:


While these answers are technically correct; you should check out momentjs if you plan to do a lot of with time and durations. Also check out moment-duration-formats which makes formatting durations just as simple as regular momentjs time

An example of how easy these 2 modules make this

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

that will output 05:33. And there's plenty of other uses.

Though there's a reason youtube uses ISO8601 format since it's a standard, so keep that in mind.




回答4:


I approuched this by checking the character two indices to the left of the unit (H,M,S) and checking if it is a number, if not the unit is single digit and I prepend an extra '0'. Otherwise I return the two digit number.

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

I do this for hours, minutes and seconds. I also drop hours if there aren't any in the input, this is ofcourse optional.

    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 ;
  }

then just call it like this

  duration = ISO8601toDuration(item.duration);

I use this to format youtube data API video durations. Hope this helps somebody



来源:https://stackoverflow.com/questions/19061360/how-to-convert-youtube-api-duration-iso-8601-duration-in-the-format-ptms-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!