如需获取媒体ID,可以通过媒体的输入地址进行媒体信息的查询,接口为 QueryMediaListByURL。本文提供使用OSS文件地址查询媒体的示例代码。
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.mts.model.v20140618.QueryMediaListByURLRequest;
import com.aliyuncs.mts.model.v20140618.QueryMediaListByURLResponse;
import com.aliyuncs.profile.DefaultProfile;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
public class QueryMediaListByURLDemo {
//根据视频源OSS地址查询媒体信息, 如: 媒体ID, 媒体状态及其他属性
private void queryMediaListByURL() throws ClientException, UnsupportedEncodingException {
//初始化
DefaultAcsClient client = InitClient.initMpsClient();
QueryMediaListByURLRequest request = new QueryMediaListByURLRequest();
String ossHost = 'http://<input-bucket>.<region>.aliyuncs.com/';
String ossObject = "test/video.mp4";
//ossObject需要符合rfc3986标准
String rfc3986Object = encodeByRFC3986(ossObject);
request.setFileURLs(ossHost + rfc3986Object);
QueryMediaListByURLResponse response = client.getAcsResponse(request);
System.out.println(JSONObject.toJSONString(response.getMediaList()));
}
private String encodeByRFC3986(String object) throws UnsupportedEncodingException {
StringBuilder builder = new StringBuilder();
String[] segments = object.split("/");
for (int i = 0; i < segments.length; i++) {
builder.append(percentEncode(segments[i]));
if (i != segments.length - 1) {
builder.append("/");
}
}
return builder.toString();
}
private static String percentEncode(String value) throws UnsupportedEncodingException {
if (value == null)
return null;
return URLEncoder.encode(value, "UTF-8").replace("+", "%20").replace("*", "%2A").replace("%7E", "~");
}
public static void main(String[] args) {
try {
QueryMediaListByURLDemo demo = new QueryMediaListByURLDemo();
demo.queryMediaListByURL();
} catch (Exception e) {
e.printStackTrace();
}
}
}
反馈
- 本页导读 (1)
文档反馈