You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

38 lines
1.1 KiB

import datetime
import logging
from tinytag import TinyTag
logger = logging.getLogger(__name__)
def get_media_duration_seconds(file_path):
"""
Given a local file path, reads the duration using tinytag.
Returns duration in seconds (float) or None if error occurs.
"""
try:
tag = TinyTag.get(file_path)
return tag.duration
except Exception as e:
logger.error(f"Error reading media duration for {file_path}: {e}")
return None
def get_media_duration_time(file_path):
"""
Given a local file path, reads the duration and returns a datetime.time object.
Caps hours at 23 to comply with Django TimeField limits.
"""
duration_seconds = get_media_duration_seconds(file_path)
if duration_seconds is None:
return None
seconds = int(duration_seconds)
hours = seconds // 3600
if hours > 23:
hours = 23
minutes = 59
secs = 59
else:
minutes = (seconds % 3600) // 60
secs = seconds % 60
return datetime.time(hour=hours, minute=minutes, second=secs)