通过七牛云 jdk 批量修改视频格式

接上一篇:解决小程序中视频因为编码格式不能播放的问题

上一篇只说了如何修改单个视频的编码格式,下面是批量处理的方法。所有代码放在了 GitHub 上。

主函数

分三步:

  • 第一步:批量修改 bucket 中所有视频编码格式,重命名新格式视频。
  • 第二步:因为需要批量下载 bucket 中所有视频,所以批量删除原视频。
  • 第三步:批量下载所有新格式视频。
    public static void main(String[] args) {
// 构造一个带指定 Zone 对象的配置类
Configuration cfg = new Configuration(Zone.zone0());
//... 其他参数参考类注释
String accessKey = "access key";
String secretKey = "secret key";
// 待处理文件所在空间
String bucket = "bucket name";
Auth auth = Auth.create(accessKey, secretKey);
BucketManager bucketManager = new BucketManager(auth, cfg);
// 文件名前缀
String prefix = "";
// 每次迭代的长度限制,最大 1000,推荐值 1000
int limit = 1000;
// 指定目录分隔符,列出所有公共前缀(模拟列出目录效果)。缺省值为空字符串
String delimiter = "";
// 列举空间文件列表
BucketManager.FileListIterator fileListIterator = bucketManager.createFileListIterator(bucket, prefix, limit, delimiter);

List<String> deleteKeyList = new ArrayList<>();
while (fileListIterator.hasNext()) {
// 处理获取的 file list 结果
FileInfo[] items = fileListIterator.next();
for (FileInfo item : items) {
String key = item.key;

changVcodec(auth, key, bucket);// 第一步:改变视频编码格式
// downLoadFromUrl(key);// 第三步,根据 url 下载视频
if (!key.contains("_")) {
deleteKeyList.add(key);
}
}
}

// deleteFiles(bucketManager, deleteKeyList, bucket);// 第二步,批量删除文件

}

改变视频编码格式

public static void changVcodec(Auth auth, String key, String bucket) {
System.out.println(key);
String name = key.substring(0, 4);

String targetName = "% s:" + name + "_target.mp4";

// 数据处理指令,支持多个指令
String saveMp4Entry = String.format(targetName, bucket);//avthumb_test_target.mp4 是转换后的文件名
String avthumbMp4Fop = String.format("avthumb/mp4/vcodec/libx264|saveas/% s", UrlSafeBase64.encodeToString(saveMp4Entry));//libx264 为编码格式

// 数据处理队列名称,为空时代表使用公有队列
String persistentPipeline = "";
// 数据处理完成结果通知地址
String persistentNotifyUrl = "http://pov1yx2ze.bkt.clouddn.com/qiniu/pfop/notify";
// 构造一个带指定 Zone 对象的配置类
Configuration cfg = new Configuration(Zone.zone0());
//... 其他参数参考类注释
// 构建持久化数据处理对象
OperationManager operationManager = new OperationManager(auth, cfg);
try {
String persistentId = operationManager.pfop(bucket, key, avthumbMp4Fop, persistentPipeline, persistentNotifyUrl, true);
// 可以根据该 persistentId 查询任务处理进度
System.out.println(persistentId + " " + targetName);
OperationStatus operationStatus = operationManager.prefop(persistentId);
System.out.println(operationStatus + " " + targetName);
// 解析 operationStatus 的结果
} catch (QiniuException e) {
System.err.println(e.response.toString());
}
}

批量删除视频

private static void deleteFiles(BucketManager bucketManager, List<String> deleteKeyList, String bucket) {
try {

String[] deleteKeys = new String[deleteKeyList.size()];
deleteKeys = deleteKeyList.toArray(deleteKeys);
BucketManager.BatchOperations batchOperations = new BucketManager.BatchOperations();
batchOperations.addDeleteOp(bucket, deleteKeys);
Response response = bucketManager.batch(batchOperations);
BatchStatus[] batchStatusList = response.jsonToObject(BatchStatus[].class);
for (int i = 0; i < deleteKeys.length; i++) {
BatchStatus status = batchStatusList[i];
String key = deleteKeys[i];
System.out.print(key + "\t");
if (status.code == 200) {
System.out.println("delete success");
} else {
System.out.println(status.data.error);
}
}
} catch (QiniuException ex) {
System.err.println(ex.response.toString());
}
}

根据 url 下载视频

private static void downLoadFromUrl(String fileName) {
try {

String domainOfBucket = "http://pqdazcbn4.bkt.clouddn.com/";
String urlStr = String.format("% s/% s", domainOfBucket, fileName);

URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 设置超时间为 3 秒
conn.setConnectTimeout(3 * 1000);
// 防止屏蔽程序抓取而返回 403 错误
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");

// 得到输入流
InputStream inputStream = conn.getInputStream();
// 获取自己数组
byte[] getData = readInputStream(inputStream);

// 文件保存位置
File saveDir = new File("/Users/yanjie/Movies/miniprogram3");
if (!saveDir.exists()) {
saveDir.mkdir();
}
File file = new File(saveDir + File.separator + fileName);
FileOutputStream fos = new FileOutputStream(file);
fos.write(getData);
if (fos != null) {
fos.close();
}
if (inputStream != null) {
inputStream.close();
}
System.out.println("info:" + url + " download success");
} catch (Exception e) {
e.printStackTrace();
}

}

public static byte[] readInputStream(InputStream inputStream) throws IOException {
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ((len = inputStream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.close();
return bos.toByteArray();
}

源码

评论默认使用 ,你也可以切换到 来留言。