How to convert YouTube API duration to seconds?

前端 未结 7 1507
星月不相逢
星月不相逢 2021-02-07 04:52

For the sake of interest I want to convert video durations from YouTubes ISO 8601 to seconds. To future proof my solution, I picked a really long video to test it a

7条回答
  •  你的背包
    2021-02-07 05:36

    Here's my answer which takes 9000's regex solution (thank you - amazing mastery of regex!) and finishes the job for the original poster's YouTube use case i.e. converting hours, minutes, and seconds to seconds. I used .groups() instead of .groupdict(), followed by a couple of lovingly constructed list comprehensions.

    import re
    
    def yt_time(duration="P1W2DT6H21M32S"):
        """
        Converts YouTube duration (ISO 8061)
        into Seconds
    
        see http://en.wikipedia.org/wiki/ISO_8601#Durations
        """
        ISO_8601 = re.compile(
            'P'   # designates a period
            '(?:(?P\d+)Y)?'   # years
            '(?:(?P\d+)M)?'  # months
            '(?:(?P\d+)W)?'   # weeks
            '(?:(?P\d+)D)?'    # days
            '(?:T' # time part must begin with a T
            '(?:(?P\d+)H)?'   # hours
            '(?:(?P\d+)M)?' # minutes
            '(?:(?P\d+)S)?' # seconds
            ')?')   # end of time part
        # Convert regex matches into a short list of time units
        units = list(ISO_8601.match(duration).groups()[-3:])
        # Put list in ascending order & remove 'None' types
        units = list(reversed([int(x) if x != None else 0 for x in units]))
        # Do the maths
        return sum([x*60**units.index(x) for x in units])
    

    Sorry for not posting higher up - still new here and not enough reputation points to add comments.

提交回复
热议问题