Skip to main content

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

  • May 31, 2023
  • 1 reply
  • 258 views

rbarbosa Box

Got an interesting questions from a developer:



I’m looking to get video durations for a list of box.com 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 reply

rbarbosa Box
  • Author
  • Developer Advocate
  • 553 replies
  • May 31, 2023

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')

    auth.authenticate_instance()

    client = Client(auth)



    user = client.user(user_rb_id).get()

    print(f"User: {user.id}:{user.name}")



    user_client = client.as_user(user)





    folder = user_client.folder(video_folder_id).get()

    print(f"Folder: {folder.id}:{folder.name}")



    items = folder.get_items()

    for item in items:

        print(f"Item: {item.id}:{item.name}:{item.type}")

        if item.type != 'file':

            continue



        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_'+item.name, 'wb') as tmp_file:

            item.download_to(tmp_file)



        media_info = MediaInfo.parse('./tmp/tpm_'+item.name)

        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

Done


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


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings