Skip to content
This repository has been archived by the owner on Jul 17, 2024. It is now read-only.

Video read examples

peter-majeed edited this page Dec 27, 2012 · 6 revisions

#Video Read calls All calls that are part of the Brightcove Video Read API are wrapped and available within the .NET-MAPI-Wrapper. You will find examples of some of the more common use cases below.

Important Note

API calls are subject to the whims of internet connectivity, and may throw an exception for any number of reasons:

  • The Brightcove API may be down or not functioning correctly.
  • Network connectivity may be lost between your application and Brightcove.
  • Invalid parameters passed to the API may result in an error message in the response JSON. The error message will be wrapped in a BrightcoveApiException and thrown so that it may be handled via normal .NET error handling mechanisms.

Although the examples shown here do not include try/catch blocks, in order to ensure that your app is as robust as possible make sure to try/catch all API calls.

See Also

See how to work with the BrightcoveApi object on the Getting Started page.

Examples

Advanced

## Find all videos in your account, 100 at a time ```c# // Instantiate an API object by using the provided factory BrightcoveApi api = BrightcoveApiFactory.CreateApi("my API read token");
// Perform the API call   
BrightcoveItemCollection<BrightcoveVideo> videos = api.FindAllVideos();
foreach (BrightcoveVideo video in videos)
{
    // process each video				
}
<a name="find-top-10" />
## Find the top 10 most played videos

```c#
    // Instantiate an API object by using the provided factory
    BrightcoveApi api = BrightcoveApiFactory.CreateApi("my API read token");

    // Perform the API call (10 results per page, page 0, sorted by total number of plays, descending)
    BrightcoveItemCollection<BrightcoveVideo> videos = api.FindAllVideos(10, 0, SortBy.PlaysTotal, SortOrder.Descending);
## Find a video by ID ```c# // Instantiate an API object by using the provided factory BrightcoveApi api = BrightcoveApiFactory.CreateApi("my API read token");
// Perform the API call   
BrightcoveVideo video = api.FindVideoById(600000000000); // substitute your own video ID here

<a name="find-by-ref-id" />
## Find a video by its reference ID
```c#
    // Instantiate an API object by using the provided factory
    BrightcoveApi api = BrightcoveApiFactory.CreateApi("my API read token");

    // Perform the API call   
    BrightcoveVideo video = api.FindVideoByReferenceId("my-ref-id"); // substitute your own reference ID here
## Find a video's HTTP download links ```c# // Instantiate an API object by using the provided factory BrightcoveApi api = BrightcoveApiFactory.CreateApi("my API read token");
// Configure the API calls to specify an "http" media delivery
api.Configuration.MediaDelivery = "http";

// Perform the API call   
BrightcoveVideo video = api.FindVideoByReferenceId("my-ref-id");

// Retrieve the URL from the "FlvUrl" property
string url = video.FlvUrl; // will be an "http" URL

// Alternatively, examine one or more of the available BrightcoveRenditions for its specific URL
string mainRenditionUrl = video.VideoFullLength.Url;
foreach (BrightcoveRendition rendition in video.Renditions)
{
    string renditionUrl = rendition.Url;
}

<a name="find-by-id-custom-field" />
## Find a video by ID, and retrieve custom field values
```c#
    // Instantiate an API object by using the provided factory
    BrightcoveApi api = BrightcoveApiFactory.CreateApi("my API read token");

    // Perform the API call   
    BrightcoveVideo video = api.FindVideoById(600000000000,                // Substitute your own video ID here
                                              null,                        // No extra standard fields
                                              new [] { "myCustomField" }); // Retrieve the custom field named "myCustomField"

    // Read the custom field value
    string customFieldValue = video.CustomFields["myCustomField"];
## Search for videos by a combination of any/all/none parameters ```c# // Instantiate an API object by using the provided factory BrightcoveApi api = BrightcoveApiFactory.CreateApi("my API read token");
// The FieldValuePair type allows for simple construction of string/string
// key/value pairs which can be easily formatted for a query string.
// If the field name is set to null, the API will search among each of the
// name, shortDescription, and longDescription fields, as in the below.
FieldValuePair allPair = new FieldValuePair(null, "sea");

// The search must match all terms in this list.
var all = new List<FieldValuePair>
{
	allPair
};

// The search must match at least one of the terms in this list.
var any = new List<FieldValuePair>
{
	new FieldValuePair(null, "lion"),
	new FieldValuePair(null, "clown"),
	new FieldValuePair(null, "crab"),
	new FieldValuePair(null, "turtle"),
	new FieldValuePair(null, "seagulls")
};

// The search cannot match any of the terms in this list.
var none = new List<FieldValuePair>
{
	new FieldValuePair(null, "fish")
};

// The first 100 videos that match the above criteria are returned.
// Note: If the all/any/none parameters are empty collections, this
// returns the first 100 videos sorted by CreationDate ascending.
BrightcoveItemCollection<BrightcoveVideo> videos = Api.SearchVideos
	(
		all: all,
		any: any,
		none: none
	);

foreach (BrightcoveVideo video in videos)
{
	// Perform your logic here.
}

<a name="search-by-any-all-none-parameters-with-one-sort" />
## Search for videos by a combination of any/all/none parameters with one sort
```c#
    // Instantiate an API object by using the provided factory
    BrightcoveApi api = BrightcoveApiFactory.CreateApi("my API read token");

    // The FieldValuePair type allows for simple construction of string/string
    // key/value pairs which can be easily formatted for a query string.
    // If the field name is set to null, the API will search among each of the
    // name, shortDescription, and longDescription fields, as in the below.
    FieldValuePair allPair = new FieldValuePair(null, "sea");
	
    // The search must match all terms in this list.
    var all = new List<FieldValuePair>
    {
    	allPair
    };
    
    // The search must match at least one of the terms in this list.
    var any = new List<FieldValuePair>
    {
    	new FieldValuePair(null, "lion"),
    	new FieldValuePair(null, "clown"),
    	new FieldValuePair(null, "crab"),
    	new FieldValuePair(null, "turtle"),
    	new FieldValuePair(null, "seagulls")
    };
    
    // The search cannot match any of the terms in this list.
    var none = new List<FieldValuePair>
    {
    	new FieldValuePair(null, "fish")
    };

    // In this case, we use the overload of SearchVideos that sorts
    // by a single field.
    BrightcoveItemCollection<BrightcoveVideo> videos = Api.SearchVideos
    	(
    		all: all,
    		any: any,
    		none: none,
    		pageSize: 100,
    		pageNumber: 0,
    		exact: false,
    		sortBy: SortBy.DisplayName,
    		sortOrder: SortOrder.Ascending
    	);

    // The videos variable now contains the first 100 videos
    // in your account sorted by DisplayName ascending.    
    foreach (BrightcoveVideo video in videos)
    {
    	// Perform your logic here.
    }
## Search for videos by a combination of any/all/none parameters with multiple sorts (verbose) ```c# // Instantiate an API object by using the provided factory BrightcoveApi api = BrightcoveApiFactory.CreateApi("my API read token");
// The FieldValuePair type allows for simple construction of string/string
// key/value pairs which can be easily formatted for a query string.
// If the field name is set to null, the API will search among each of the
// name, shortDescription, and longDescription fields, as in the below.
FieldValuePair allPair = new FieldValuePair(null, "sea");

// The search must match all terms in this list.
var all = new List<FieldValuePair>
{
	allPair
};

// The search must match at least one of the terms in this list.
var any = new List<FieldValuePair>
{
	new FieldValuePair(null, "lion"),
	new FieldValuePair(null, "clown"),
	new FieldValuePair(null, "crab"),
	new FieldValuePair(null, "turtle"),
	new FieldValuePair(null, "seagulls")
};

// The search cannot match any of the terms in this list.
var none = new List<FieldValuePair>
{
	new FieldValuePair(null, "fish")
};

// The concept of an ordered dictionary is useful in the sorting
// mechanism in the new implementation of the SearchVideos
// call. In a search query,the following must be true about sort
// order:
// 1) Only certain fields are sortable (hence the SortBy/SortOrder
// enum combination)
// 2) Any given field can be sorted on only once in a given
// query (hence the IDictionary)
// 3) The order by which multiple fields are sorted matters
// (hence the IOrderedDictionary, found in the System.Collections.Specialized namespace).
// Note: The key and value of each KeyValuePair of OrderedDictionary
// are both of type object, and so they can easily be misrepresented as
// something other than a SortBy/SortOrder pair. The .NET wrapper
// handles this by ignoring any KeyValuePair in the OrderedDictionary
// that is not a SortBy/SortOrder pair.
IOrderedDictionary orderedDictionary = new OrderedDictionary
{
    { SortBy.DisplayName, SortOrder.Ascending },
    { SortBy.CreationDate, SortOrder.Descending }
};
    
// SortedFieldDictionary is a class that helps facilitate the way
// various fields are sorted. In this first example, the constructor 
// takes the IOrderedDictionary constructed above as a parameter.
var sortedFieldDictionary = new SortedFieldDictionary(orderedDictionary);
        
// In this case, we use the overload of SearchVideos that takes the new
// instance of SortedFieldDictionary as a parameter.
BrightcoveItemCollection<BrightcoveVideo> videos = Api.SearchVideos
	(
		all: all,
		any: any,
		none: none,
		pageSize: 100,
		pageNumber: 0,
		exact: false,
		sortFields: sortedFieldDictionary
	);
    
// The videos variable now contains the first 100 videos in your account
// sorted by DisplayName ascending, then by CreationDate descending.
foreach (BrightcoveVideo video in videos)
{
    // Perform your logic here.
}

<a name="search-by-any-all-none-parameters-with-multiple-sorts-method-two" />
## Search for videos by a combination of any/all/none parameters with multiple sorts (convenient)
```c#
    // Instantiate an API object by using the provided factory
    BrightcoveApi api = BrightcoveApiFactory.CreateApi("my API read token");

    // The FieldValuePair type allows for simple construction of string/string
    // key/value pairs which can be easily formatted for a query string.
    // If the field name is set to null, the API will search among each of the
    // name, shortDescription, and longDescription fields, as in the below.
    FieldValuePair allPair = new FieldValuePair(null, "sea");
	
    // The search must match all terms in this list.
    var all = new List<FieldValuePair>
    {
    	allPair
    };
    
    // The search must match at least one of the terms in this list.
    var any = new List<FieldValuePair>
    {
    	new FieldValuePair(null, "lion"),
    	new FieldValuePair(null, "clown"),
    	new FieldValuePair(null, "crab"),
    	new FieldValuePair(null, "turtle"),
    	new FieldValuePair(null, "seagulls")
    };
    
    // The search cannot match any of the terms in this list.
    var none = new List<FieldValuePair>
    {
    	new FieldValuePair(null, "fish")
    };
        
    // SortedFieldDictionary is a class that helps facilitate the way
    // various fields are sorted. In this second example, the constructor 
    // takes a params object[] parameter, where you can instead of first
    // constructing an IOrderedDictionary, pass in your sort fields with
    // direction in sequence as in the below example.
    // Note: To prevent against accidental misuse, this constructor will
    // throw an ArgumentException if each pair of params do not constitute a
    // valid SortBy/SortOrder KeyValuePair.
    var sortedFieldDictionary = new SortedFieldDictionary(SortBy.DisplayName, SortOrder.Ascending, SortBy.CreationDate, SortOrder.Descending);
            
    // In this case, we use the overload of SearchVideos that takes the
    // instance of SortedFieldDictionary as a parameter.
    BrightcoveItemCollection<BrightcoveVideo> videos = Api.SearchVideos
    	(
    		all: all,
    		any: any,
    		none: none,
    		pageSize: 100,
    		pageNumber: 0,
    		exact: false,
    		sortFields: sortedFieldDictionary
    	);
        
    // The videos variable now contains the first 100 videos in your account
    // sorted by DisplayName ascending, then by CreationDate descending.
    foreach (BrightcoveVideo video in videos)
    {
        // Perform your logic here.
    }

A slightly more advanced example

## Enumerate *all* the videos in your account Sometimes we have a need to process every single video in an account. If that's the case, you might find it useful to create a utility method that will wrap the FindAllVideos() call so that you don't have to worry about manually handling multiple pages of results within the portion of code that is processing each video. In order to accomplish this, we can leverage C#'s "yield return" syntax to create an IEnumerable that will defer loading each page of results until necessary:
    public static IEnumerable<BrightcoveVideo> AllVideosInAccount()
    {
        BrightcoveApi api = BrightcoveApiFactory.CreateApi("my API read token");

        // start on the first page
        int page = 0;
        BrightcoveItemCollection<BrightcoveVideo> videos = api.FindAllVideos(100, page);
        while (videos.Count > 0)
        {
            foreach (BrightcoveVideo video in videos)
            {
                // The "yield return" statement allows us defer execution of the next page of 
                // results until after all the videos on the current page have been processed.
                yield return video;
            }

            // next page
            page++;
            videos = api.FindAllVideos(100, page);
        }
    }

Then use the method like so:

    foreach (BrightcoveVideo video in AllVideosInAccount())
    {
        // process each video				
    }

See also

More Examples