[Python] Get "Media created" timestamp with python for .mp4 and .m4a video, audio files (no...

Discussão em 'Python' iniciado por Stack, Maio 9, 2025.

  1. Stack

    Stack Membro Participativo

    Trying to get "Media created" timestamp and insert as the "Last modified date" with python for .mp4 and .m4a video, audio files (no EXIF). The "Media created" timestamp shows up and correctly in Windows with right click file inspection, but I can not get it with python. What am I doing wrong?

    [​IMG]

    enter from mutagen.mp4 import MP4, M4A
    from datetime import datetime
    import os

    def get_mp4_media_created_date(filepath):
    """
    Extracts the "Media Created" date from an MP4 or M4A file.
    Args:
    filepath (str): The path to the MP4 or M4A file.

    Returns:
    datetime or None: The creation date as a datetime object, or None if not found.
    """
    file_lower = filepath.lower()
    try:
    if file_lower.endswith(".mp4"):
    media = MP4(filepath)
    elif file_lower.endswith(".m4a"):
    media = M4A(filepath)
    else:
    return None # Not an MP4 or M4A file

    found_date = None
    date_tags_to_check = ['creation_time', 'com.apple.quicktime.creationdate']

    for tag in date_tags_to_check:
    if tag in media:
    values = media[tag]
    if not isinstance(values, list):
    values = [values]
    for value in values:
    if isinstance(value, datetime):
    found_date = value
    break
    elif isinstance(value, str):
    try:
    found_date = datetime.fromisoformat(value.replace('Z', '+00:00'))
    break
    except ValueError:
    pass
    if found_date:
    break
    return found_date

    except Exception as e:
    print(f"Error processing {filepath}: {e}")
    return None

    if __name__ == "__main__":
    filepath = input("Enter the path to the MP4/M4A file: ")
    if os.path.exists(filepath):
    creation_date = get_mp4_media_created_date(filepath)
    if creation_date:
    print(f"Media Created Date: {creation_date}")
    else:
    print("Could not find Media Created Date.")
    else:
    print("File not found.")
    here

    Continue reading...

Compartilhe esta Página