Python Forum

Full Version: Python convert duration in seconds to hh:mm:ss:ms
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi. I want read audio file duration in hh:mm:ss:ms, so i use mutagen and i obtain duration time in seconds. Next i try convert it to final format but i have error ValueError: Invalid format string.


import mutagen
import random
import time
import sys
import mad
from datetime import datetime
from mutagen.mp3 import MP3
import glob,os
audio = MP3("example.mp3")
len = time.strftime('%H:%M:%S.%f', time.gmtime(audio.info.length))
print (len)enter code here
time.gmtime() dos not have milliseconds.
The term is seconds since the epoch: January 1, 1970, 00:00:00 (UTC).
>>> time.gmtime(audio.info.length)
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=3, tm_sec=53, tm_wday=3, tm_yday=1, tm_isdst=0)

>>> time.strftime('%H:%M:%S', time.gmtime(audio.info.length))
'00:03:53'
Don't use len as variable name.
Name it's used bye Python.
>>> len('hello')
5