Converting Youtube Data API V3 video duration format to seconds in JavaScript/Node.js

后端 未结 15 1987
北海茫月
北海茫月 2020-12-05 07:09

I\'m trying to convert ISO 8601 string to seconds in JS/Node. The best I could come up with was:

function convert_time(duration) {
    var a = duration.match         


        
相关标签:
15条回答
  • 2020-12-05 07:51

    You can find a very simple PHP solution here - How To Convert Youtube API Time (ISO 8601 String Video Duration) to Seconds In PHP - Code

    This function convert_time() takes one parameter as input - the Youtube API Time (Video Duration) which is in ISO 8601 string format and returns its duration in seconds.

    function convert_time($str) 
    {
        $n = strlen($str);
        $ans = 0;
        $curr = 0;
        for($i=0; $i<$n; $i++)
        {
            if($str[$i] == 'P' || $str[$i] == 'T')
            {
    
            }
            else if($str[$i] == 'H')
            {
                $ans = $ans + 3600*$curr;
                $curr = 0;
            }
            else if($str[$i] == 'M')
            {
                $ans = $ans + 60*$curr;
                $curr = 0;
            }
            else if($str[$i] == 'S')
            {
                $ans = $ans + $curr;
                $curr = 0;
            }
            else
            {
                $curr = 10*$curr + $str[$i];
            }
        }
        return($ans);
    }
    

    Testing Some Inputs:

    "PT2M23S" => 143
    "PT2M" => 120
    "PT28S" => 28
    "PT5H22M31S" => 19351
    "PT3H" => 10800
    "PT1H6M" => 3660
    "PT1H6S" => 3606
    
    0 讨论(0)
  • 2020-12-05 07:52

    Here's my solution:

    function parseDuration(duration) {
        var matches = duration.match(/[0-9]+[HMS]/g);
    
        var seconds = 0;
    
        matches.forEach(function (part) {
            var unit = part.charAt(part.length-1);
            var amount = parseInt(part.slice(0,-1));
    
            switch (unit) {
                case 'H':
                    seconds += amount*60*60;
                    break;
                case 'M':
                    seconds += amount*60;
                    break;
                case 'S':
                    seconds += amount;
                    break;
                default:
                    // noop
            }
        });
    
        return seconds;
    }
    
    0 讨论(0)
  • 2020-12-05 07:58

    I suggest this little hack to prevent your problematic case:

    function convert_time(duration) {
        var a = duration.match(/\d+/g);
    
        if (duration.indexOf('M') >= 0 && duration.indexOf('H') == -1 && duration.indexOf('S') == -1) {
            a = [0, a[0], 0];
        }
    
        if (duration.indexOf('H') >= 0 && duration.indexOf('M') == -1) {
            a = [a[0], 0, a[1]];
        }
        if (duration.indexOf('H') >= 0 && duration.indexOf('M') == -1 && duration.indexOf('S') == -1) {
            a = [a[0], 0, 0];
        }
    
        duration = 0;
    
        if (a.length == 3) {
            duration = duration + parseInt(a[0]) * 3600;
            duration = duration + parseInt(a[1]) * 60;
            duration = duration + parseInt(a[2]);
        }
    
        if (a.length == 2) {
            duration = duration + parseInt(a[0]) * 60;
            duration = duration + parseInt(a[1]);
        }
    
        if (a.length == 1) {
            duration = duration + parseInt(a[0]);
        }
        return duration
    }
    

    Fiddle

    0 讨论(0)
提交回复
热议问题