Skip to main content


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.

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