Quickstart: Search for videos using the Bing Video Search REST API and Python
Use this quickstart to make your first call to the Bing Video Search API. This simple Python application sends an HTTP video search query to the API, and displays the JSON response. Although this application is written in Python, the API is a RESTful Web service compatible with most programming languages.
The source code for this sample is available on GitHub with additional error handling, and code annotations.
You can run this example as a Jupyter notebook on MyBinder by selecting the launch binder badge:
Prerequisites
- Python 2.x or 3.x
Initialize the application
Create a new Python file in your favorite IDE or editor, and import the following libraries:
import requests from IPython.display import HTML
Create variables for your subscription key, search endpoint, and search term.
subscription_key = None assert subscription_key search_url = "https://api.bing.microsoft.com/bing/v7.0/videos/search" search_term = "kittens"
Add your subscription key to a
Ocp-Apim-Subscription-Key
header by creating a new dictionary to associate the header string to your key.headers = {"Ocp-Apim-Subscription-Key" : subscription_key}
Send your request
Add the parameters to your request by creating a dictionary named
params
. Add your search terms to theq
parameter: a video count of 5,free
for the pricing of returned videos, andshort
for the video length.params = {"q": search_term, "count":5, "pricing": "free", "videoLength":"short"}
Use the
requests
library in Python to call the Bing Video Search API. Pass the API key and search parameters by using theheaders
andparams
dictionary.response = requests.get(search_url, headers=headers, params=params) response.raise_for_status() search_results = response.json()
To view one of the returned videos, get a search result from the
search_results
object. Insert the result'sembedHtml
property into anIFrame
.HTML(search_results["value"][0]["embedHtml"].replace("autoplay=1","autoplay=0"))
JSON response
A successful response is returned in JSON, as shown in the following example:
{
"_type": "Videos",
"instrumentation": {},
"readLink": "https://api.cognitive.microsoft.com/api/v7/videos/search?q=kittens",
"webSearchUrl": "https://www.bing.com/videos/search?q=kittens",
"totalEstimatedMatches": 1000,
"value": [
{
"webSearchUrl": "https://www.bing.com/videos/search?q=kittens&view=...",
"name": "Top 10 cute kitten videos compilation",
"description": "HELP HOMELESS ANIMALS AND WIN A PRIZE BY CHOOSING...",
"thumbnailUrl": "https://tse4.mm.bing.net/th?id=OVP.n1aE_Oikl4MtzBb...",
"datePublished": "2014-11-12T22:47:36.0000000",
"publisher": [
{
"name": "Fabrikam"
}
],
"creator": {
"name": "Marcus Appel"
},
"isAccessibleForFree": true,
"contentUrl": "https://www.fabrikam.com/watch?v=8HVWitAW-Qg",
"hostPageUrl": "https://www.fabrikam.com/watch?v=8HVWitAW-Qg",
"encodingFormat": "h264",
"hostPageDisplayUrl": "https://www.fabrikam.com/watch?v=8HVWitAW-Qg",
"width": 480,
"height": 360,
"duration": "PT3M52S",
"motionThumbnailUrl": "https://tse4.mm.bing.net/th?id=OM.j4QyJAENJphdZQ_1501386166&pid=Api",
"embedHtml": "<iframe width=\"1280\" height=\"720\" src=\"https://www.fabrikam.com/embed/8HVWitAW-Qg?autoplay=1\" frameborder=\"0\" allowfullscreen></iframe>",
"allowHttpsEmbed": true,
"viewCount": 7513633,
"thumbnail": {
"width": 300,
"height": 168
},
"videoId": "655D98260D012432848F6558260D012432848F",
"allowMobileEmbed": true,
"isSuperfresh": false
},
. . .
],
"nextOffset": 36,
"queryExpansions": [
{
"text": "Kittens Meowing",
"displayText": "Meowing",
"webSearchUrl": "https://www.bing.com/videos/search?q=Kittens+Meowing...",
"searchLink": "https://api.cognitive.microsoft.com/api/v7/videos/search...",
"thumbnail": {
"thumbnailUrl": "https://tse3.mm.bing.net/th?q=Kittens+Meowing&pid..."
}
},
{
"text": "Funny Kittens",
"displayText": "Funny",
"webSearchUrl": "https://www.bing.com/videos/search?q=Funny+Kittens...",
"searchLink": "https://api.cognitive.microsoft.com/api/v7/videos/search...",
"thumbnail": {
"thumbnailUrl": "https://tse3.mm.bing.net/th?q=Funny+Kittens&..."
}
},
. . .
],
"pivotSuggestions": [
{
"pivot": "kittens",
"suggestions": [
{
"text": "Cat",
"displayText": "Cat",
"webSearchUrl": "https://www.bing.com/videos/search?q=Cat...",
"searchLink": "https://api.cognitive.microsoft.com/api/v7/videos/search?...",
"thumbnail": {
"thumbnailUrl": "https://tse3.mm.bing.net/th?q=Cat&pid=Api..."
}
},
{
"text": "Feral Cat",
"displayText": "Feral Cat",
"webSearchUrl": "https://www.bing.com/videos/search?q=Feral+Cat...",
"searchLink": "https://api.cognitive.microsoft.com/api/v7/videos/search...",
"thumbnail": {
"thumbnailUrl": "https://tse3.mm.bing.net/th?q=Feral+Cat&pid=Api&..."
}
}
]
}
],
"relatedSearches": [
{
"text": "Kittens Being Born",
"displayText": "Kittens Being Born",
"webSearchUrl": "https://www.bing.com/videos/search?q=Kittens+Being+Born...",
"searchLink": "https://api.cognitive.microsoft.com/api/v7/videos/search?...",
"thumbnail": {
"thumbnailUrl": "https://tse1.mm.bing.net/th?q=Kittens+Being+Born&pid=..."
}
},
. . .
]
}