Add video search functionality to your apps and services
Blog|by Jamie Maguire|27 June 2019
In this blog post we look at another Search API from Microsoft Cognitive Services, the Bing Video Search API.
We’ll look at what the Bing Video Search API lets you do, the features it ships, and run through some code examples that show you how you can consume the API using just a few lines of code.
We’ll see how by just writing a few lines of code, the Bing Video Search API lets you crawl the web for videos and related metadata!
What is the Bing Video Search API?
The Bing Video Search API lets you add video searching functionality to your existing software applications and services. By submitting search queries to the API you can fetch and display videos like that of the Bing.com Video feed.
The API ships with the following features straight out of the box:
Filter and restrict video results: Filter the videos returned by editing query parameters.
Crop, resize, and display thumbnails: Edit and display thumbnail previews for the videos returned by Bing Video Search API.
Get trending videos: Search for trending videos from around the world.
Get video insights: Customise a search for trending videos from around the world.
You can also augment the Video Search experience by integrating the Bing Auto Suggest API to display suggested search terms as you type.
Now we’ve looked at the main features, it’s worth mentioning the endpoints that are available. At the time of writing there are three API endpoints available, each offers discrete functionality.
Trending: Get videos currently trending.
Video Details: Get insights about a video, such as related videos.
Video Search: Get videos relevant for a given query.
You can find out more information about these endpoints here
Consuming the Bing Video Search API
Now that we’ve introduced the Bing Video Search API, its key features and the endpoints that are currently available, let’s look at how you can consume the API.
Like other APIs in the Cognitive Services ecosystem, you can consume the Bing Video Search API using a language of your choice. You also have a few options at your disposal.
The Bing Video Search API has a dedicated SDK in the form of a NuGet package which makes it easy to install and reduces the lines of code you need to write. You can download the SDK here.
Alternatively, if you like a little more control over the code that you’re writing, you can construct REST requests and send these directly to one of the endpoints that we mentioned earlier. I generally prefer to do this. It involves creating your own custom classes to map the messages you send and receive but you have more flexibility in terms of data parsing, object mapping and so on.
We’ve covered all the main points now so let’s look at some examples that show you how to construct and send requests to the Bing Video Search API. We’ll see how easy it is to do using Postman and then look at an example in C#.
Invoking the Bing Video Search using Postman
The first thing we’ll do is make a simple request using Postman. This request will search for videos that contain “cats” and we’ll return one search result to aid readability. Using Postman lets you quickly see what data you can expect to get back from APIs and can help you isolate any issues that you encounter, i.e., code!
In this screenshot below we’re setting up GET request. We set the parameter “q” to the search term we need.
For conciseness we also set the “count” parameter to only return one search result.
After we click Send the following JSON below is returned. The key fields to pick out from the response are:
- Date Published
- Content URL
You can find a complete definition of data included in the JSON response and parameters that you can send to the API here.
If you take the contentURL value and paste it into your browser, you can watch a collection of cats being looked after and fed in a sanctuary!
Invoking the Bing Video Search using C#
Next, let’s see what’s involved from a C# perspective when you want to consume the Bing Video Search API. Like the other APIs you can find within Azure Cognitive Services, you don’t need to write much code to harness the power of video search and integrate this with your existing applications.
We’ll use a console application as an example.
Consuming most of the Cognitive Services APIs involves the following workflow:
- Getting the relevant Cognitive Services key from your Azure Portal (you can create an account for free).
- Constructing a valid request.
- Processing the JSON message by parsing out the data returned by the Bing Video Search API.
What follows is the code that’s needed by our console application to consume the Bing Video Search API.
We have one method BingVideoSearch which is responsible for constructing the REST request, then in our Main method we simply execute this and output the JSON response to the console.
You can run this application and view the response in the JSON Visualizer (it’s easier to read):
Here you can see a result titled “BIG CATS Getting Snacks”. You can copy and paste the contentURL into your browser and watch the video if you want!
You’ll see the response also contains additional metadata in the response that can be useful.
For example, expand the queryExpansions node. This contains a list of expanded queries that narrows the original query, i.e., our original query is “cats” – in this node, however, you’ll find values like “Cat vs Snake” or “Cartoon Cat”.
Full source code for this console application can be found on my GitHub profile here.
Get Video Insights using Video ID
You can go beyond the general video search to get further insights related to each video by using its unique VideoID. This can be sent to the video/details endpoint that belongs to the Bing Video Search API.
For example, if we take the video id from the JSON in our earlier example (CF7188639F73C06ADB19CF7188639F73C06ADB19) and set the modules parameter to “RelatedVideos”, the Bing Video Search API will return a collection of videos that are like the video specified by original video id!
You can see this in action in the screenshot below in Postman:
If you’d like to know more about further insights and modules the Bing Video Search API can supply, you can check out the online documentation here.
We’ve looked at a few ways that you can consume the Bing Video Search API. If you’d rather not manually build the REST request or create your own POCOS and map these to the JSON response you can always use the dedicated SDK which will save you time and effort.
By now you probably have your own ideas brewing about what’s possible with the Bing Video Search API. I have a few thoughts of some use cases the API could be implemented in.
Do you need to surface videos that are trending online? Use the Trending Videos endpoint and integrate this data with information from other social media channels to give you a 360-degree view of what videos are popular. Take this a step further and leverage the Market parameter to ensure the API only retrieves results from specific countries.
Are you building chatbots that need to search the web for videos? You can easily integrate the Bing Video Search API and surface these results to users in conversations.
Video Filtering and Child Friendly
Do you need to ensure that video search results are child friendly and free from adult content? Use the Bing Video Search Safe Search feature to make sure adult content is removed from search results.
Check out the comprehensive documentation and support online which will give you even more ideas!
In this blog post we’ve introduced the Bing Video Search API. We’ve seen how you can harness the power of Bing Video Search in just a few lines of code. We’ve looked at how you can use Postman to quickly test the API and we’ve run through a code example using C#.
We’ve also explored some out of the box features and data the Bing Video Search API returns. One feature that we didn’t cover is “Trending Videos”, so be sure to experiment with that!
If you need to integrate Video Search into your existing application the Bing Video Search API ships with endpoints and features that make this seamless.
Do you need to integrate video search, or considering introducing Azure Cognitive Services to your existing software applications?
The members of our Managed Services team are experts at managing Azure related projects. If you need their technical advice or wish to discuss Azure and Visual Studio options and costs, please call us on 01364 654100.
Contact Grey Matter
If you have any questions or want some extra information, complete the form below and one of the team will be in touch ASAP. If you have a specific use case, please let us know and we'll help you find the right solution faster.
Software Architect, Consultant, Developer, and Microsoft AI MVP. 15+ years’ experience architecting and building solutions using the .NET stack. Into tech, web, code, AI, machine learning, business and start-ups.
Mon 20 February 2023 2:00 pm - 3:30 pm GMT
Intel has launched its fourth-generation Intel Xeon Scalable processor, codenamed Sapphire Rapids, the successor to Ice Lake. Find out more!
Mon 20 February 2023 10:00 am - 11:30 am GMT
Grey Matter has passed the Government-backed Cyber Essentials assessment once again. This certification recognises our data protection efforts.