How do you get Video Duration in API for a file?

Got an interesting questions from a developer:

I’m looking to get video durations for a list of files but can’t find anywhere in the API how to do it. I’m needing this for lots of videos uploaded to Box so I’m hoping to grab the video duration (you can see it on the video preview) instead of downloading the entire file. I’m working in Python.

1 Like

This question puzzled me for some time, how to somehow read the length of the video without downloading the entire file.

Doing some tests with the pymediainfo library, came up with this:

def main():
    auth = JWTAuth.from_settings_file('.jwt.config.json')
    client = Client(auth)

    user = client.user(user_rb_id).get()
    print(f"User: {}:{}")

    user_client = client.as_user(user)

    folder = user_client.folder(video_folder_id).get()
    print(f"Folder: {}:{}")

    items = folder.get_items()
    for item in items:
        print(f"Item: {}:{}:{item.type}")
        if item.type != 'file':

        item_url = item.get_download_url()
        # print(f"URL {item_url}")

        tic_download = time.perf_counter()
        media_info = MediaInfo.parse(item_url)
        print(f"MediaInfo w/ URL time: {time.perf_counter() - tic_download} seconds")

        tic_download = time.perf_counter()

        with open('./tmp/tpm_', 'wb') as tmp_file:

        media_info = MediaInfo.parse('./tmp/tpm_'
        print(f"MediaInfo w/ download time: {time.perf_counter() - tic_download} seconds")

With the following results:

Folder: 191494027812:Video Samples
Item: 1121082178302:BigBuckBunny.mp4:file
MediaInfo w/ URL time: 3.798498541000299 seconds
MediaInfo w/ download time: 21.247453375020996 seconds

Looking at the time it takes to run the MediaInfo.parse() , it seems it doesn’t need to download the entire file.