Python Forum
Python convert duration in seconds to hh:mm:ss:ms
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python convert duration in seconds to hh:mm:ss:ms
#1
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
Reply
#2
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
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020