When your media library stores a vast collection of audio, video, and image files, quickly locating a specific asset becomes a challenge. The media asset search feature in ApsaraVideo VOD provides powerful multi-dimensional search, filtering, and sorting capabilities, allowing you to efficiently retrieve the media assets you need from massive datasets by using the console or APIs/SDKs.
Scenario 1: Search in the console
Follow these steps to search for audio or video assets:
-
Log in to the ApsaraVideo VOD console.
-
In the left-side navigation pane, choose Media Files > Audio/Video.
-
Use the filter and search features at the top of the page to find media assets.
-
Search: Supports fuzzy search by Video Title and exact search by Media ID or Tag Name.
-
Filter: Supports filtering audio/video files by Address, Type, Category, Status, Source, and storage type. You can also sort the results by Created At in ascending or descending order.
-
Scenario 2: Search using APIs and SDKs
We recommend using a server-side SDK to search for media assets. For more information, see VOD SDK usage instructions.
The SearchMedia API operation lets you integrate search capabilities into your business systems. Filterable fields include VideoId, CateId, StorageLocation, Title, Tags, and more. For details, see protocol for media asset search. The following code provides examples:
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.SearchMediaRequest;
import com.aliyuncs.vod.model.v20170321.SearchMediaResponse;
public class SearchMediaExample {
public static void main(String[] args) {
// Initialize the client.
DefaultProfile profile = DefaultProfile.getProfile(
"cn-shanghai", // The region ID.
"YOUR_ACCESS_KEY_ID", // Your AccessKey ID.
"YOUR_ACCESS_KEY_SECRET"// Your AccessKey secret.
);
IAcsClient client = new DefaultAcsClient(profile);
// Build the request.
SearchMediaRequest request = new SearchMediaRequest();
request.setMatch("Title='VOD' AND Status='Normal'");
request.setFields("Title,CoverURL,Duration,Size");
request.setSortBy("CreationTime:Desc");
request.setPageSize(20);
request.setPageNo(1);
try {
SearchMediaResponse response = client.getAcsResponse(request);
System.out.println("Total Records: " + response.getTotal());
for (SearchMediaResponse.Media media : response.getMediaList()) {
System.out.println("VideoId: " + media.getMediaId() + ", Title: " + media.getVideo().getTitle());
}
} catch (Exception e) {
System.err.println("An error occurred: " + e.getMessage());
e.printStackTrace();
}
}
}from aliyunsdkcore.client import AcsClient
from aliyunsdkvod.request.v20170321 import SearchMediaRequest
import json
# Initialize the client.
client = AcsClient(
'YOUR_ACCESS_KEY_ID',
'YOUR_ACCESS_KEY_SECRET',
'cn-shanghai'
)
# Build the request.
request = SearchMediaRequest.SearchMediaRequest()
request.set_Match("Title='VOD' AND Status='Normal'")
request.set_Fields("Title,CoverURL,Duration,Size")
request.set_SortBy("CreationTime:Desc")
request.set_PageSize(20)
request.set_PageNo(1)
try:
response_str = client.do_action_with_exception(request)
response_data = json.loads(response_str)
print(f"Total Records: {response_data.get('Total')}")
for media in response_data.get('MediaList', {}).get('Media', []):
print(f"VideoId: {media.get('MediaId')}, Title: {media.get('Video', {}).get('Title')}")
except Exception as e:
print(f"An error occurred: {e}")Pagination and data traversal
To ensure query performance, media asset search provides two pagination modes.
-
Standard pagination (for UI display)
Use the
PageNoandPageSizepagination parameters to retrieve data page by page. This method can access only the first 5,000 results. -
Scrolling (for data export)
Use the
PageNoandPageSizepagination parameters and theScrollTokenscroll token to traverse all results. This method retrieves the results in segments of up to 1,200 records each. The following code provides an example:// Scrolling example String scrollToken = null; // The SessionId must remain unchanged throughout a complete traversal. String sessionId = java.util.UUID.randomUUID().toString(); List<SearchMediaResponse.Media> allMedia = new ArrayList<>(); do { SearchMediaRequest request = new SearchMediaRequest(); request.setPageSize(100); // Retrieve up to 100 records at a time. request.setSessionId(sessionId); if (scrollToken != null) { request.setScrollToken(scrollToken); } SearchMediaResponse response = client.getAcsResponse(request); if (response.getMediaList() != null) { allMedia.addAll(response.getMediaList()); } scrollToken = response.getScrollToken(); // If the returned scrollToken is null or empty, all data has been traversed. } while (scrollToken != null && !scrollToken.isEmpty());
API call examples
The sample code in the following section describes the query statements used for querying video information.
Before you send a request, you must perform URL encoding on the request parameters.
The equal signs (=), double quotation marks ("), single quotation marks ('), and parentheses used in the statement must be single-byte characters.
Item | Description |
Return field |
By default, the SearchMedia API operation returns basic information about a media asset. To include additional information, specify the
Sample request:
|
Exact match |
To search for media assets where the
Sample request:
|
Fuzzy match |
If
or
Sample request:
|
Multi-value query |
Note: If the fields used for the query only support fuzzy match, the query results are also returned based on fuzzy match. To search for media assets where the
Sample request:
|
Range query |
To search for media assets with a
Sample request:
If you specify only a left or right boundary, leave the value for the other boundary empty. For example, specify that
Sample request:
|
Sort field | Sort media asset information in reverse chronological order based on the creation time: Sample request: |