3 changed files with 104 additions and 0 deletions
@ -0,0 +1,38 @@ |
|||
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) |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue