快速入門:使用 Bing 影片搜尋用戶端程式庫

警告

2020 年 10 月 30 日,Bing 搜尋 API 已從 Azure AI 服務移至Bing 搜尋服務。 本文件僅供參考之用。 如需更新的文件,請參閱 Bing 搜尋 API 文件。 如需針對 Bing 搜尋建立新 Azure 資源的指示,請參閱透過 Azure Marketplace 建立 Bing 搜尋資源

使用本快速入門,透過適用於 C# 的 Bing 影片搜尋用戶端程式庫開始搜尋新聞。 雖然 Bing 影片搜尋具有與大部分程式設計語言相容的 REST API,但此用戶端程式庫可提供簡單的方法,將服務整合到您的應用程式。 在 Github 上可找到此範例的完整原始程式碼,以及其他註釋和功能。

Prerequisites

若要將 Bing 影片搜尋用戶端程式庫新增至您的專案,請在 Visual Studio 中選取 [方案總管] 中的 [管理 NuGet 套件] 。 新增 Microsoft.Azure.CognitiveServices.Search.VideoSearch 套件。

安裝 [NuGet 影片搜尋 SDK 套件] 也會安裝下列相依性:

  • Microsoft.Rest.ClientRuntime
  • Microsoft.Rest.ClientRuntime.Azure
  • Newtonsoft.Json

建立 Azure 資源

藉由建立下列其中一項 Azure 資源,開始使用 Bing 影片搜尋 API:

Bing 搜尋 v7 資源

  • 您可以透過 Azure 入口網站取得該資源,直到將其刪除為止。
  • 使用免費定價層來試用服務,之後可升級至付費層以用於實際執行環境。

多服務資源

  • 您可以透過 Azure 入口網站取得該資源,直到將其刪除為止。
  • 針對您的應用程式,跨多個 Azure AI 服務使用相同的金鑰和端點。

建立專案並將其初始化

  1. 在 Visual Studio 中建立新的 C# 主控台解決方案。 然後將下列內容新增至主要程式碼檔案。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Microsoft.Azure.CognitiveServices.Search.VideoSearch;
    using Microsoft.Azure.CognitiveServices.Search.VideoSearch.Models;
    
  2. 藉由使用訂用帳戶金鑰建立新的 ApiKeyServiceClientCredentials 物件,並呼叫建構函式,來將用戶端具現化。

    var client = new VideoSearchAPI(new ApiKeyServiceClientCredentials("YOUR-ACCESS-KEY"));
    

傳送搜尋要求並處理結果

  1. 使用用戶端傳送搜尋要求。 將 "SwiftKey" 用於搜尋查詢。

    var videoResults = client.Videos.SearchAsync(query: "SwiftKey").Result;
    
  2. 如果傳回任何結果,取得第一個具有 videoResults.Value[0] 的結果。 然後列印影片的識別碼、標題和 URL。

    if (videoResults.Value.Count > 0)
    {
        var firstVideoResult = videoResults.Value[0];
    
        Console.WriteLine($"\r\nVideo result count: {videoResults.Value.Count}");
        Console.WriteLine($"First video id: {firstVideoResult.VideoId}");
        Console.WriteLine($"First video name: {firstVideoResult.Name}");
        Console.WriteLine($"First video url: {firstVideoResult.ContentUrl}");
    }
    else
    {
        Console.WriteLine("Couldn't find video results!");
    }
    

後續步驟

另請參閱

使用本快速入門,透過適用於 Java 的 Bing 影片搜尋用戶端程式庫開始搜尋新聞。 雖然 Bing 影片搜尋具有與大部分程式設計語言相容的 REST API,但此用戶端程式庫可提供簡單的方法,將服務整合到您的應用程式。 在 Github 上可找到此範例的完整原始程式碼,以及其他註釋和功能。

Prerequisites

建立 Azure 資源

藉由建立下列其中一項 Azure 資源,開始使用 Bing 影片搜尋 API:

Bing 搜尋 v7 資源

  • 您可以透過 Azure 入口網站取得該資源,直到將其刪除為止。
  • 使用免費定價層來試用服務,之後可升級至付費層以用於實際執行環境。

多服務資源

  • 您可以透過 Azure 入口網站取得該資源,直到將其刪除為止。
  • 針對您的應用程式,跨多個 Azure AI 服務使用相同的金鑰和端點。

使用 Maven、Gradle 或另一個相依性管理系統,安裝 Bing 影片搜尋用戶端程式庫相依性。 Maven POM 檔案需要下列宣告:

  <dependencies>
    <dependency>
      <groupId>com.microsoft.azure.cognitiveservices</groupId>
      <artifactId>azure-cognitiveservices-videosearch</artifactId>
      <version>0.0.1-beta-SNAPSHOT</version>
    </dependency>
  </dependencies> 

建立專案並將其初始化

在您最愛的 IDE 或編輯器中建立新的 Java 專案,並匯入下列程式庫。

    import com.microsoft.azure.cognitiveservices.videosearch.*;
    import com.microsoft.azure.cognitiveservices.videosearch.VideoObject;
    import com.microsoft.rest.credentials.ServiceClientCredentials;
    import okhttp3.Interceptor;
    import okhttp3.OkHttpClient;
    import okhttp3.Request;
    import okhttp3.Response;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List; 

建立搜尋用戶端

  1. 實作 VideoSearchAPIImpl 用戶端,這需要 API 端點及 ServiceClientCredentials 類別的執行個體。

    public static VideoSearchAPIImpl getClient(final String subscriptionKey) {
        return new VideoSearchAPIImpl("https://api.cognitive.microsoft.com/bing/v7.0/",
                new ServiceClientCredentials() {
                //...
                }
    )};
    

    若要實作 ServiceClientCredentials,請遵循下列步驟:

    1. 以作為參數的 OkHttpClient.Builder 物件覆寫 applyCredentialsFilter() 函式。

      //...
      new ServiceClientCredentials() {
              @Override
              public void applyCredentialsFilter(OkHttpClient.Builder builder) {
              //...
              }
      //...
      
    2. applyCredentialsFilter() 內呼叫 builder.addNetworkInterceptor()。 建立新的 Interceptor 物件,並覆寫其 intercept() 方法,以取用 Chain 攔截器物件。

      //...
      builder.addNetworkInterceptor(
          new Interceptor() {
              @Override
              public Response intercept(Chain chain) throws IOException {
              //...    
              }
          });
      ///...
      
    3. intercept 函式內,為您的要求建立變數。 使用 Request.Builder() 來建置您的要求。 將您的訂用帳戶金鑰新增至 Ocp-Apim-Subscription-Key 標頭,然後傳回要求物件上的 chain.proceed()

      //...
      public Response intercept(Chain chain) throws IOException {
          Request request = null;
          Request original = chain.request();
          Request.Builder requestBuilder = original.newBuilder()
                  .addHeader("Ocp-Apim-Subscription-Key", subscriptionKey);
          request = requestBuilder.build();
          return chain.proceed(request);
      }
      //...
      

傳送搜尋要求並接收回應

  1. 建立名為 VideoSearch() 的函式,並將您的訂用帳戶金鑰當作字串。 具現化稍早建立的搜尋用戶端。

    public static void VideoSearch(String subscriptionKey){
        VideoSearchAPIImpl client = VideoSDK.getClient(subscriptionKey);
        //...
    }
    
  2. VideoSearch() 內,使用用戶端傳送影片搜尋,並以 SwiftKey 作為搜尋字詞。 如果影片搜尋 API 傳回結果,取得第一個結果,並列印其識別碼、名稱和 URL,以及傳回的影片總數。

    VideosInner videoResults = client.searchs().list("SwiftKey");
    
    if (videoResults == null){
        System.out.println("Didn't see any video result data..");
    }
    else{
        if (videoResults.value().size() > 0){
            VideoObject firstVideoResult = videoResults.value().get(0);
    
            System.out.println(String.format("Video result count: %d", videoResults.value().size()));
            System.out.println(String.format("First video id: %s", firstVideoResult.videoId()));
            System.out.println(String.format("First video name: %s", firstVideoResult.name()));
            System.out.println(String.format("First video url: %s", firstVideoResult.contentUrl()));
        }
        else{
            System.out.println("Couldn't find video results!");
        }
    }
    
  3. 從您的 main 方法呼叫 search 方法。

    public static void main(String[] args) {
        VideoSDK.VideoSearch("YOUR-SUBSCRIPTION-KEY");
    }
    

後續步驟

另請參閱

使用本快速入門,透過適用於 JavaScript 的 Bing 影片搜尋用戶端程式庫開始搜尋新聞。 雖然 Bing 影片搜尋具有與大部分程式設計語言相容的 REST API,但此用戶端程式庫可提供簡單的方法,將服務整合到您的應用程式。 此範例的原始程式碼可以在 GitHub 上找到。 其中包含更多的註解和功能。

Prerequisites

  • 最新版的 Node.js
  • 適用於 JavaScript 的 Bing 影片搜尋 SDK
    • 若要安裝,請執行 npm install @azure/cognitiveservices-videosearch
  • 來自 @azure/ms-rest-azure-js 套件中的 CognitiveServicesCredentials 類別,用來驗證用戶端。
    • 若要安裝,請執行 npm install @azure/ms-rest-azure-js

建立 Azure 資源

藉由建立下列其中一項 Azure 資源,開始使用 Bing 影片搜尋 API:

Bing 搜尋 v7 資源

  • 您可以透過 Azure 入口網站取得該資源,直到將其刪除為止。
  • 使用免費定價層來試用服務,之後可升級至付費層以用於實際執行環境。

多服務資源

  • 您可以透過 Azure 入口網站取得該資源,直到將其刪除為止。
  • 針對您的應用程式,跨多個 Azure AI 服務使用相同的金鑰和端點。

建立應用程式並將其初始化

  1. 在您慣用的 IDE 或編輯器中建立新的 JavaScript 檔案,然後為 Bing 影片搜尋用戶端程式庫新增 require() 陳述式,以及新增 CognitiveServicesCredentials 模組。 建立訂用帳戶金鑰的變數。

    const CognitiveServicesCredentials = require('@azure/ms-rest-azure-js').CognitiveServicesCredentials;
    const VideoSearchAPIClient = require('@azure/cognitiveservices-videosearch');
    
  2. 以您的金鑰建立 CognitiveServicesCredentials 的執行個體。 然後用其建立影片搜尋用戶端的執行個體。

    let credentials = new CognitiveServicesCredentials('YOUR-ACCESS-KEY');
    let client = new VideoSearchAPIClient(credentials);
    

傳送搜尋要求

  1. 使用 client.videosOperations.search() 將搜尋要求傳送到 Bing 影片搜尋 API。 當搜尋結果傳回時,使用 .then() 來記錄結果。

    client.videosOperations.search('Interstellar Trailer').then((result) => {
        console.log(result.value);
    }).catch((err) => {
        throw err;
    });
    

後續步驟

另請參閱

使用本快速入門,透過適用於 Python 的 Bing 影片搜尋用戶端程式庫開始搜尋新聞。 雖然 Bing 影片搜尋具有與大部分程式設計語言相容的 REST API,但此用戶端程式庫可提供簡單的方法,將服務整合到您的應用程式。 在 Github 上可找到此範例的完整原始程式碼,以及其他註釋和功能。

建立 Azure 資源

藉由建立下列其中一項 Azure 資源,開始使用 Bing 影片搜尋 API:

Bing 搜尋 v7 資源

  • 您可以透過 Azure 入口網站取得該資源,直到將其刪除為止。
  • 使用免費定價層來試用服務,之後可升級至付費層以用於實際執行環境。

多服務資源

  • 您可以透過 Azure 入口網站取得該資源,直到將其刪除為止。
  • 針對您的應用程式,跨多個 Azure AI 服務使用相同的金鑰和端點。

Prerequisites

  • Python 2.x 或 3.x
  • 適用於 Python 的 Bing 影片搜尋用戶端程式庫

建議您使用 Python 虛擬環境。 您可以使用 venv 模組來安裝和初始化虛擬環境。 透過下列方式安裝適用於 Python 2.7 的 virtualenv:

python -m venv mytestenv

透過下列方式安裝 Bing 影片搜尋用戶端程式庫:

cd mytestenv
python -m pip install azure-cognitiveservices-search-videosearch

建立應用程式並將其初始化

  1. 在您慣用的 IDE 或編輯器中建立新的 Python 專案,以及新增下列匯入陳述式。

    from azure.cognitiveservices.search.videosearch import VideoSearchClient
    from azure.cognitiveservices.search.videosearch.models import VideoPricing, VideoLength, VideoResolution, VideoInsightModule
    from msrest.authentication import CognitiveServicesCredentials
    
  2. 建立訂用帳戶金鑰的變數。

    subscription_key = "YOUR-SUBSCRIPTION-KEY"
    endpoint = "YOUR-ENDPOINT"
    

建立搜尋用戶端

建立 CognitiveServicesCredentials 的執行個體,並具現化用戶端:

client = VideoSearchAPI(endpoint, CognitiveServicesCredentials(subscription_key))

傳送搜尋要求並取得回應

  1. 使用 client.videos.search() 與您的搜尋查詢,將要求傳送至 Bing 影片搜尋 API 並取得回應。

    video_result = client.videos.search(query="SwiftKey")
    
  2. 如果回應包含搜尋結果,則取得第一個結果,並列印其識別碼、名稱和 URL。

    if video_result.value:
        first_video_result = video_result.value[0]
        print("Video result count: {}".format(len(video_result.value)))
        print("First video id: {}".format(first_video_result.video_id))
        print("First video name: {}".format(first_video_result.name))
        print("First video url: {}".format(first_video_result.content_url))
    else:
        print("Didn't see any video result data..")
    

後續步驟

另請參閱