-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathMediaHandlerDemo.java
More file actions
170 lines (128 loc) · 5.16 KB
/
Copy pathMediaHandlerDemo.java
File metadata and controls
170 lines (128 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package demo;
import com.upyun.MediaHandler;
import com.upyun.Result;
import com.upyun.UpException;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class MediaHandlerDemo {
// 运行前先设置好以下三个参数
private static final String BUCKET_NAME = "空间名称";
private static final String OPERATOR_NAME = "操作员名称";
private static final String OPERATOR_PWD = "操作员密码";
public static void main(String[] args) {
String[] ids = testMediaProcess();
if (ids != null) {
testMediaStatus(ids);
testMediaResult(ids);
}
}
/**
* 发起异步影视频处理请求DEMO
*
* @return TaskId 数组
*/
private static String[] testMediaProcess() {
//初始化 MediaHandler
MediaHandler handler = new MediaHandler(BUCKET_NAME, OPERATOR_NAME, OPERATOR_PWD);
//初始化参数组 Map
Map<String, Object> paramsMap = new HashMap<String, Object>();
//添加必选参数 bucket_name, notify_url, source, tasks, accept
//空间名
paramsMap.put(MediaHandler.Params.BUCKET_NAME, BUCKET_NAME);
//回调地址
paramsMap.put(MediaHandler.Params.NOTIFY_URL, "http://httpbin.org/post");
//必须指定为 json
paramsMap.put(MediaHandler.Params.ACCEPT, "json");
//需要处理视频路径
paramsMap.put(MediaHandler.Params.SOURCE, "/test.mp4");
//已json格式生成任务信息
JSONArray array = new JSONArray();
JSONObject json = new JSONObject();
//添加处理参数
//不同的处理任务对应不同的 type
json.put(MediaHandler.Params.TYPE, "video");
//影视频处理参数 包括视频转码,HLS 切片,视频水印,视频截图,视频拼接,音频剪辑,元数据获取 请见API文档
json.put(MediaHandler.Params.AVOPTS, "/s/240p(4:3)/as/1/r/30");
//是否返回 JSON 格式元数据,默认 false。支持 type 值为 video 功能
json.put(MediaHandler.Params.RETURN_INFO, "true");
//输出文件保存路径(同一个空间下),如果没有指定,系统自动生成在同空间同目录下
json.put(MediaHandler.Params.SAVE_AS, "testProcess.mp4");
JSONObject json2 = new JSONObject();
json2.put(MediaHandler.Params.TYPE, "video");
json2.put(MediaHandler.Params.AVOPTS, "/s/240p(4:3)/as/1/r/30");
json2.put(MediaHandler.Params.RETURN_INFO, "true");
json2.put(MediaHandler.Params.SAVE_AS, "testProcess2.mp4");
array.put(json2);
array.put(json);
//添加任务信息
paramsMap.put(MediaHandler.Params.TASKS, array);
try {
Result result = handler.process(paramsMap);
System.out.println(result);
if (result.isSucceed()) {
String[] ids = handler.getTaskId(result.getMsg());
System.out.println(Arrays.toString(ids));
return ids;
}
} catch (IOException e) {
e.printStackTrace();
} catch (UpException e) {
e.printStackTrace();
}
return null;
}
/**
* 查询处理进度DEMO
*
* @param ids 需要查询的任务ID
*/
private static void testMediaStatus(String[] ids) {
MediaHandler handle = new MediaHandler(BUCKET_NAME, OPERATOR_NAME, OPERATOR_PWD);
//初始化参数组 Map
Map<String, Object> paramsMap = new HashMap<String, Object>();
paramsMap.put(MediaHandler.Params.BUCKET_NAME, BUCKET_NAME);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < ids.length; i++) {
sb.append(ids[i] + ",");
}
String task_ids = sb.toString().substring(0, sb.length() - 1);
paramsMap.put(MediaHandler.Params.TASK_IDS, task_ids);
try {
Result result = handle.getStatus(paramsMap);
System.out.println("status:" + result);
} catch (IOException e) {
e.printStackTrace();
} catch (UpException e) {
e.printStackTrace();
}
}
/**
* 查询处理结果DEMO
*
* @param ids 需要查询的任务ID
*/
private static void testMediaResult(String[] ids) {
MediaHandler handle = new MediaHandler(BUCKET_NAME, OPERATOR_NAME, OPERATOR_PWD);
//初始化参数组 Map
Map<String, Object> paramsMap = new HashMap<String, Object>();
paramsMap.put(MediaHandler.Params.BUCKET_NAME, BUCKET_NAME);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < ids.length; i++) {
sb.append(ids[i] + ",");
}
String task_ids = sb.toString().substring(0, sb.length() - 1);
paramsMap.put(MediaHandler.Params.TASK_IDS, task_ids);
try {
Result result = handle.getResult(paramsMap);
System.out.println("result:" + result);
} catch (IOException e) {
e.printStackTrace();
} catch (UpException e) {
e.printStackTrace();
}
}
}