Backblaze B2 - Media management application

Summary: The Media Asset Management example application demonstrates how you can add video understanding capabilities to a typical media asset management application using TwelveLabs for video understanding and Backblaze B2 for cloud storage.

Description: The application allows you to upload videos and perform deep semantic searches across multiple modalities such as visual, conversation, text-in-video, and logo. The matching video segments in the response are grouped by video, and you can view details about each segment.

Step-by-step guide: The b2-twelvelabs-example GitHub repository provides detailed instructions on setting up the example application on your computer and using it.

GitHub: Backblaze B2 + TwelveLabs Media Asset Management Example

Integration with TwelveLabs

The integration with TwelveLabs video enhances the functionalities of a typical media management application by adding video understanding capabilities. The process is segmented into two main steps:

  • Upload and index videos
  • Search videos

Upload and index videos

A Huey task automates the process of uploading and indexing videos. For each video, the application invokes the create method of the task object with the following parameters and values:

  • index_id: A string representing unique identifier of the index to which the video will be updated.
  • url: A string representing the URL of the video to be uploaded.
  • disable_video_stream: A boolean indicating that the platform shouldn’t store the video for streaming.
Python
1@huey.db_task()
2def do_video_indexing(video_tasks):
3 print(f'Creating tasks: {video_tasks}')
4
5 # Create a task for each video we want to index
6 for video_task in video_tasks:
7 task = TWELVE_LABS_CLIENT.task.create(
8 TWELVE_LABS_INDEX_ID,
9 url=default_storage.url(video_task['video']),
10 disable_video_stream=True
11 )
12 print(f'Created task: {task}')
13 video_task['task_id'] = task.id
14
15 print(f'Created {len(video_tasks)} tasks')

Then, the application monitors the status of the upload process by invoking the retrieve method of the task object with the unique identifier of a task as a parameter:

Python
1 print(f'Polling TwelveLabs for {video_tasks}')
2
3 # Do a single database query for all the videos we're interested in
4 video_ids = [video_task['id'] for video_task in video_tasks]
5 videos = Video.objects.filter(id__in=video_ids)
6
7 while True:
8 done = True
9 videos_to_save = []
10
11 # Retrieve status for each task we created
12 for video_task in video_tasks:
13 # What's our current state for this video?
14 video = videos.get(video__exact=video_task['video'])
15
16 # Do we still need to retrieve status for this task?
17 if video.status != 'Ready':
18 task = TWELVE_LABS_CLIENT.task.retrieve(video_task['task_id'])
19 if task.status != 'ready':
20 # We'll need to go round the loop again
21 done = False
22
23 # Do we need to write a new status to the DB?
24 if video.status.lower() != task.status:
25 # We store the status in the DB in title case, so it's ready to render on the page
26 new_status = task.status.title()
27 print(f'Updating status for {video_task["video"]} from {video.status} to {new_status}')
28 video.status = new_status
29 if task.status == 'ready':
30 video.video_id = task.video_id
31 get_all_video_data(video)
32 videos_to_save.append(video)
33
34 if len(videos_to_save) > 0:
35 Video.objects.bulk_update(videos_to_save, ['status', 'video_id', THUMBNAILS_PATH, TRANSCRIPTS_PATH, TEXT_PATH, LOGOS_PATH])
36
37 if done:
38 break
39
40 sleep(TWELVE_LABS_POLL_INTERVAL)
41
42 print(f'Done polling {video_tasks}')

Search videos


The application invokes the query method of the search object with the following parameters:

  • index_id: A string representing the unique identifier of the index containing the videos to be searched.
  • query: A string representing the query the user has provided.
  • options: An array of strings representing the sources of information the TwelveLabs video understanding platform should consider when performing the search.
  • group_by: A string specifying that the matching video clips in the response must be grouped by video.
  • threshold: A string specifying the sstrictness of the thresholds for assigning the high, medium, or low confidence levels to search results. See the Filter on the level of confidence section for details.
Python
1def get_queryset(self):
2 """
3 Search TwelveLabs for videos matching the query
4 """
5 query = self.request.GET.get("query", None)
6
7 result = TWELVE_LABS_CLIENT.search.query(
8 TWELVE_LABS_INDEX_ID,
9 query,
10 ["visual", "conversation", "text_in_video", "logo"],
11 group_by="video",
12 threshold="medium"
13 )
14
15 # Search results may be in multiple pages, so we need to loop until we're done retrieving them
16 search_data = result.data
17 print(f"First page's data: {search_data}")
18
19 search_results = []
20 while True:
21 # Do a database query to get the videos for each page of results
22 video_ids = [group.id for group in search_data]
23 videos = Video.objects.filter(video_id__in=video_ids)
24 for group in search_data:
25 try:
26 search_results.append(SearchResult(video=videos.get(video_id__exact=group.id),
27 clip_count=len(group.clips),
28 clips=group.clips.model_dump_json()))
29 except self.model.DoesNotExist:
30 # There is a video in TwelveLabs, but no corresponding row in the database.
31 # Just report it and carry on.
32 print(f'Can\'t find match for video_id {group.id}')
33
34 # Is there another page?
35 try:
36 search_data = next(result)
37 print(f"Next page's data: {search_data}")
38 except StopIteration:
39 print("There is no next page in search result")
40 break
41
42 return search_results

Next steps

After reading this page, you have several options:

  • Customize and use the example application: Explore the Media Asset Management example application on GitHub to understand its features and implementation. You can make changes to the application and add more functionalities to suit your specific use case.
  • Explore further: Try the applications built by the community or our sample applications to get more insights into the TwelveLabs Video Understanding Platform’s diverse capabilities and learn more about integrating the platform into your applications.
Built with