How to convert YouTube API duration to seconds?

前端 未结 7 1506
星月不相逢
星月不相逢 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:25

    Python's built-in dateutil module only supports parsing ISO 8601 dates, not ISO 8601 durations. For that, you can use the "isodate" library (in pypi at https://pypi.python.org/pypi/isodate -- install through pip or easy_install). This library has full support for ISO 8601 durations, converting them to datetime.timedelta objects. So once you've imported the library, it's as simple as:

    dur=isodate.parse_duration('P1W2DT6H21M32S')
    print dur.total_seconds()
    

提交回复
热议问题