Cette rubrique présente des exemples d'utilisation des opérations API du module de gestion des médias. Ces opérations sont encapsulées dans le SDK ApsaraVideo VOD pour Java. Utilisez ces opérations pour rechercher des informations sur les ressources multimédias, modifier les métadonnées vidéo et interroger les informations sur les fichiers source. Vous pouvez également consulter et supprimer des vidéos et des images.
Notes d'utilisation
Tous les exemples de cette rubrique utilisent une paire AccessKey pour initialiser une instance de client.
Pour obtenir le détail des paramètres de requête et de réponse, consultez Alibaba Cloud OpenAPI Explorer et ouvrez l'onglet API Documentations.
Pour consulter des exemples de SDK relatifs à d'autres opérations API, rendez-vous sur Alibaba Cloud OpenAPI Explorer. Dans l'onglet Parameters, spécifiez les paramètres et lancez l'appel. Sélectionnez ensuite la version du SDK et le langage souhaité dans l'onglet SDK Sample Code pour afficher et télécharger le code.
Les exemples de cette rubrique utilisent le SDK V1.0. Pour les exemples V2.0, sélectionnez la version appropriée du SDK dans OpenAPI Explorer.
Initialisation d'un client
Avant d'utiliser le SDK, initialisez un client. Pour plus d'informations, consultez la section Initialisation.
Interrogation des informations sur les ressources multimédias
Appelez l'opération SearchMedia pour interroger les informations relatives aux ressources multimédias.
Cliquez sur SearchMedia pour en savoir plus sur cette opération API.
Exemple de code :
import com.aliyuncs.auth.AlibabaCloudCredentials;
import com.aliyuncs.auth.EnvironmentVariableCredentialsProvider;
import com.aliyuncs.vod.model.v20170321.SearchMediaRequest;
import com.aliyuncs.vod.model.v20170321.SearchMediaResponse;
/**
* Obtain the AccessKey information.
*/
public static DefaultAcsClient initVodClient() throws ClientException {
String regionId = "cn-shanghai"; // Specify the region in which ApsaraVideo VOD is activated.
// The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M.
// We recommend that you do not save your AccessKey pair (AccessKey ID and AccessKey secret) in your project code. Otherwise, the AccessKey pair may be leaked. As a result, the security of all resources in your account is compromised.
// In this example, the system reads the AccessKey pair from environment variables to implement authentication for API access. Before you run the sample code, configure the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET.
DefaultProfile profile = DefaultProfile.getProfile(regionId, System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
DefaultAcsClient client = new DefaultAcsClient(profile);
return client;
}
/**
* Search for media asset information.
* @param client The client that sends a request.
* @return SearchMediaResponse The information contained in the response.
* @throws Exception
*/
public static SearchMediaResponse searchMedia(DefaultAcsClient client) throws Exception {
SearchMediaRequest request = new SearchMediaRequest();
request.setFields("Title,CoverURL,Status");
request.setMatch("Status in ('Normal','Checking') and CreationTime = ('2018-07-01T08:00:00Z','2018-08-01T08:00:00Z')");
request.setPageNo(1);
request.setPageSize(10);
request.setSearchType("video");
request.setSortBy("CreationTime:Desc");
return client.getAcsResponse(request);
}
// Sample request
public static void main(String[] argv) {
DefaultAcsClient client = initVodClient();
SearchMediaResponse response = new SearchMediaResponse();
try {
response = searchMedia(client);
if (response.getMediaList() != null && response.getMediaList().size() > 0) {
System.out.print("Total = " + response.getTotal() + "\n");
System.out.print("ScrollToken = " + response.getScrollToken() + "\n");
for (SearchMediaResponse.Media media : response.getMediaList()) {
System.out.print("MediaId = " + media.getMediaId() + "\n");
System.out.print("MediaType = " + media.getMediaType() + "\n");
System.out.print("CreationTime = " + media.getCreationTime() + "\n");
System.out.print("Title = " + media.getVideo().getTitle() + "\n");
System.out.print("CoverURL = " + media.getVideo().getCoverURL() + "\n");
System.out.print("Status = " + media.getVideo().getStatus() + "\n");
}
}
} catch (Exception e) {
System.out.print("ErrorMessage = " + e.getLocalizedMessage());
}
System.out.print("RequestId = " + response.getRequestId() + "\n");
}
Interrogation des informations sur les fichiers audio ou vidéo
-
Appelez l'opération GetVideoInfo pour interroger les informations relatives à un fichier audio ou vidéo.
Cliquez sur GetVideoInfo pour en savoir plus sur cette opération API.
-
Utilisez l'opération GetVideoInfos pour interroger simultanément les informations relatives à plusieurs fichiers audio ou vidéo.
Cliquez sur GetVideoInfos pour en savoir plus sur cette opération API.
Modification des informations d'un fichier audio ou vidéo
Appelez l'opération UpdateVideoInfo pour modifier les informations d'un fichier audio ou vidéo.
Cliquez sur UpdateVideoInfo pour en savoir plus sur cette opération API.
Modification des informations de plusieurs fichiers audio ou vidéo
L'opération UpdateVideoInfos permet de modifier simultanément les informations de plusieurs fichiers audio ou vidéo.
Cliquez sur UpdateVideoInfos pour en savoir plus sur cette opération API.
Exemple de code :
import com.aliyuncs.auth.AlibabaCloudCredentials;
import com.aliyuncs.auth.EnvironmentVariableCredentialsProvider;
import com.aliyuncs.vod.model.v20170321.UpdateVideoInfosRequest;
import com.aliyuncs.vod.model.v20170321.UpdateVideoInfosResponse;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/**
* Obtain the AccessKey information.
*/
public static DefaultAcsClient initVodClient() throws ClientException {
String regionId = "cn-shanghai"; // Specify the region in which ApsaraVideo VOD is activated.
// The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M.
// We recommend that you do not save your AccessKey pair (AccessKey ID and AccessKey secret) in your project code. Otherwise, the AccessKey pair may be leaked. As a result, the security of all resources in your account is compromised.
// In this example, the system reads the AccessKey pair from environment variables to implement authentication for API access. Before you run the sample code, configure the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET.
DefaultProfile profile = DefaultProfile.getProfile(regionId, System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
DefaultAcsClient client = new DefaultAcsClient(profile);
return client;
}
/**
* Modify information about multiple video files at a time.
* @param client The client that sends a request.
* @return UpdateVideoInfosResponse The information contained in the response.
* @throws Exception
*/
public static UpdateVideoInfosResponse updateVideoInfos(DefaultAcsClient client) throws Exception {
UpdateVideoInfosRequest request = new UpdateVideoInfosRequest();
JSONArray updateContentArray = new JSONArray();
JSONObject updateContent1 = new JSONObject();
updateContent1.put("VideoId", "VideoId1");
// updateContent1.put("Title", "new Title");
// updateContent1.put("Tags", "new Tag1,new Tag2");
updateContentArray.add((updateContent1));
JSONObject updateContent2 = new JSONObject();
updateContent2.put("VideoId", "VideoId2");
// updateContent2.put("Title", "new Title");
// updateContent2.put("Tags", "new Tag1,new Tag2");
updateContentArray.add((updateContent2));
request.setUpdateContent(updateContentArray.toJSONString());
return client.getAcsResponse(request);
}
// Sample request
public static void main(String[] argv) {
DefaultAcsClient client = initVodClient();
UpdateVideoInfosResponse response = new UpdateVideoInfosResponse();
try {
response = updateVideoInfos(client);
if (response.getNonExistVideoIds() != null && response.getNonExistVideoIds().size() > 0) {
System.out.print("======nonexistent VideoIds : ======\n");
for (String videoId : response.getNonExistVideoIds()) {
System.out.print(videoId + "\n");
}
}
} catch (Exception e) {
System.out.print("ErrorMessage = " + e.getLocalizedMessage());
}
System.out.print("RequestId = " + response.getRequestId() + "\n");
}
Interrogation des informations sur les fichiers source (incluant l'URL de téléchargement)
Appelez l'opération GetMezzanineInfo pour interroger les informations relatives aux fichiers source.
Cliquez sur GetMezzanineInfo pour en savoir plus sur cette opération API.
Interrogation des fichiers audio et vidéo
L'opération GetVideoList permet d'interroger les fichiers audio et vidéo.
Cliquez sur GetVideoList pour en savoir plus sur cette opération API.
Suppression de fichiers audio et vidéo
Appelez l'opération DeleteVideo pour supprimer des fichiers audio et vidéo.
Cliquez sur DeleteVideo pour en savoir plus sur cette opération API.
Suppression de flux multimédias
L'opération DeleteStream permet de supprimer des flux multimédias.
Cliquez sur DeleteStream pour en savoir plus sur cette opération API.
Suppression de plusieurs fichiers source
Appelez l'opération DeleteMezzanines pour supprimer simultanément plusieurs fichiers source.
Cliquez sur DeleteMezzanines pour en savoir plus sur cette opération API.
Modification des informations de plusieurs images
L'opération UpdateImageInfos permet de modifier simultanément les informations de plusieurs images.
Cliquez sur UpdateImageInfos pour en savoir plus sur cette opération API.
Exemple de code :
import com.aliyuncs.auth.AlibabaCloudCredentials;
import com.aliyuncs.auth.EnvironmentVariableCredentialsProvider;
import com.aliyuncs.vod.model.v20170321.UpdateImageInfosRequest;
import com.aliyuncs.vod.model.v20170321.UpdateImageInfosResponse;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/**
* Obtain the AccessKey information.
*/
public static DefaultAcsClient initVodClient() throws ClientException {
String regionId = "cn-shanghai"; // Specify the region in which ApsaraVideo VOD is activated.
// The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M.
// We recommend that you do not save your AccessKey pair (AccessKey ID and AccessKey secret) in your project code. Otherwise, the AccessKey pair may be leaked. As a result, the security of all resources in your account is compromised.
// In this example, the system reads the AccessKey pair from environment variables to implement authentication for API access. Before you run the sample code, configure the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET.
DefaultProfile profile = DefaultProfile.getProfile(regionId, System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
DefaultAcsClient client = new DefaultAcsClient(profile);
return client;
}
/**
* Modify information about multiple images at a time.
* @param client The client that sends a request.
* @return UpdateImageInfosResponse The information contained in the response.
* @throws Exception
*/
public static UpdateImageInfosResponse updateImageInfos(DefaultAcsClient client) throws Exception{
UpdateImageInfosRequest request = new UpdateImageInfosRequest();
JSONArray updateContentArray = new JSONArray();
JSONObject updateContent1 = new JSONObject();
updateContent1.put("ImageId", "ImageId1");
// updateContent1.put("Title", "new Title");
// updateContent1.put("Tags", "new Tag1,new Tag2");
updateContentArray.add((updateContent1));
JSONObject updateContent2 = new JSONObject();
updateContent2.put("ImageId", "ImageId2");
// updateContent2.put("Title", "new Title");
// updateContent2.put("Tags", "new Tag1,new Tag2");
updateContentArray.add((updateContent2));
request.setUpdateContent(updateContentArray.toJSONString());
return client.getAcsResponse(request);
}
// Sample request
public static void main(String[] argv) throws Exception {
DefaultAcsClient client = initVodClient();
UpdateImageInfosResponse response = new UpdateImageInfosResponse();
try {
response = updateImageInfos(client);
if (response.getNonExistImageIds() != null && response.getNonExistImageIds().size() > 0) {
System.out.print("======nonexistent ImageIds : ======\n");
for (String imageId : response.getNonExistImageIds()) {
System.out.print(imageId + "\n");
}
}
} catch (Exception e) {
System.out.print("ErrorMessage = " + e.getLocalizedMessage());
}
System.out.print("RequestId = " + response.getRequestId() + "\n");
}
Interrogation des informations d'une image
Appelez l'opération GetImageInfo pour interroger les informations relatives à une image.
Cliquez sur GetImageInfo pour en savoir plus sur cette opération API.
Suppression d'images
L'opération DeleteImage permet de supprimer des images.
Cliquez sur DeleteImage pour en savoir plus sur cette opération API.
Exemple de code :
import com.aliyuncs.auth.AlibabaCloudCredentials;
import com.aliyuncs.auth.EnvironmentVariableCredentialsProvider;
import com.aliyuncs.vod.model.v20170321.DeleteImageRequest;
import com.aliyuncs.vod.model.v20170321.DeleteImageResponse;
/**
* Obtain the AccessKey information.
*/
public static DefaultAcsClient initVodClient() throws ClientException {
String regionId = "cn-shanghai"; // Specify the region in which ApsaraVideo VOD is activated.
// The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M.
// We recommend that you do not save your AccessKey pair (AccessKey ID and AccessKey secret) in your project code. Otherwise, the AccessKey pair may be leaked. As a result, the security of all resources in your account is compromised.
// In this example, the system reads the AccessKey pair from environment variables to implement authentication for API access. Before you run the sample code, configure the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET.
DefaultProfile profile = DefaultProfile.getProfile(regionId, System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
DefaultAcsClient client = new DefaultAcsClient(profile);
return client;
}
/**
* Delete images.
*
* @param client The client that sends a request.
* @return DeleteImageResponse The information contained in the response.
* @throws Exception
*/
public static DeleteImageResponse deleteImage(DefaultAcsClient client) throws Exception {
DeleteImageRequest request = new DeleteImageRequest();
// Delete image files based on ImageURL.
request.setDeleteImageType("ImageURL");
// Example of ImageURL: http://example.aliyundoc.com/cover-****.jpg.
String url = "<your image URL>";
String encodeUrl = URLEncoder.encode(url, "UTF-8");
request.setImageURLs(encodeUrl);
// Delete image files based on ImageId.
//request.setDeleteImageType("ImageId");
//request.setImageIds("ImageId1,ImageId2");
// Delete image files of a specified type based on VideoId.
//request.setDeleteImageType("VideoId");
//request.setVideoId("VideoId");
//request.setImageType("SpriteSnapshot");
return client.getAcsResponse(request);
}
// Sample request
public static void main(String[] argv) throws ClientException {
DefaultAcsClient client = initVodClient();
DeleteImageResponse response = new DeleteImageResponse();
try {
response = deleteImage(client);
} catch (Exception e) {
System.out.print("ErrorMessage = " + e.getLocalizedMessage());
}
System.out.print("RequestId = " + response.getRequestId() + "\n");
}