Skip to content

Instantly share code, notes, and snippets.

@jemygraw
Created November 19, 2015 01:17
Show Gist options
  • Save jemygraw/217214543f3500ab2991 to your computer and use it in GitHub Desktop.
Save jemygraw/217214543f3500ab2991 to your computer and use it in GitHub Desktop.
create image composition and generate the webvtt file
package com.qiniulab.ufop;
import java.util.ArrayList;
import java.util.List;
import com.qiniu.common.QiniuException;
import com.qiniu.processing.OperationManager;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import com.qiniu.util.UrlSafeBase64;
public class ImageComp {
// ufop的名称
private String ufopName;
// 为了保障处理效率,请使用私有队列
private String pipeline;
// 持久化结果自动通知接收地址,可以不填
private String notifyURL;
// 图片处理保存的目标空间
private String destSaveBucket;
// 图片处理保存的目标文件名
private String destImageKey;
// 授权信息
private Auth auth;
public ImageComp(String ufopName, String accessKey, String secretKey) {
this.ufopName = ufopName;
this.auth = Auth.create(accessKey, secretKey);
}
/**
* 设置图片合成的保存结果
*/
public void setDestTarget(String destBucket, String destKey) {
this.destSaveBucket = destBucket;
this.destImageKey = destKey;
}
/**
* 设置图片处理私有队列
*/
public void setPipeline(String pipeline) {
this.pipeline = pipeline;
}
/**
* 设置图片处理结果通知地址
*/
public void setNotifyURL(String notifyURL) {
this.notifyURL = notifyURL;
}
/**
* @param bucket
* @param dummyKey
* @param interval
* @param webvtt
* @param destFormat
* @param rows
* @param cols
* @param halign
* @param vlign
* @param margin
* @param alpha
* @param order
* @param bgColor
* @param urls
* @throws QiniuException
*/
public String fireUfop(String bucket, String dummyKey, int interval, String webvttSaveKey, String destImageFormat,
int rows, int cols, String halign, String valign, int margin, int alpha, int order, String bgColor,
List<String> urls) throws QiniuException {
String webvttSaveEntry = String.format("%s:%s", this.destSaveBucket, webvttSaveKey);
StringBuilder fopBuilder = new StringBuilder();
fopBuilder.append(this.ufopName);
fopBuilder.append("/bucket/").append(UrlSafeBase64.encodeToString(bucket));
fopBuilder.append("/interval/").append(interval);
fopBuilder.append("/webvtt/").append(UrlSafeBase64.encodeToString(webvttSaveEntry));
fopBuilder.append("/dest/").append(UrlSafeBase64.encodeToString(this.destImageKey));
if (!isNullOrEmpty(destImageFormat)) {
fopBuilder.append("/format/").append(destImageFormat);
}
if (rows != 0) {
fopBuilder.append("/rows/").append(rows);
}
if (cols != 0) {
fopBuilder.append("/cols/").append(cols);
}
if (!isNullOrEmpty(halign)) {
fopBuilder.append("/halign/").append(halign);
}
if (!isNullOrEmpty(valign)) {
fopBuilder.append("/valign/").append(valign);
}
if (margin >= 0) {
fopBuilder.append("/margin/").append(margin);
}
if (alpha >= 0 && alpha <= 255) {
fopBuilder.append("/alpha/").append(alpha);
}
if (order == 0 || order == 1) {
fopBuilder.append("/order/").append(order);
}
if (!isNullOrEmpty(bgColor)) {
fopBuilder.append("/bgcolor/").append(UrlSafeBase64.encodeToString(bgColor));
}
for (String url : urls) {
fopBuilder.append("/url/").append(UrlSafeBase64.encodeToString(url));
}
// set saveas
String destImageSaveEntry = String.format("%s:%s", this.destSaveBucket, this.destImageKey);
fopBuilder.append(String.format("|saveas/%s", UrlSafeBase64.encodeToString(destImageSaveEntry)));
String fopCmd = fopBuilder.toString();
OperationManager m = new OperationManager(this.auth);
StringMap extraParams = new StringMap();
if (!isNullOrEmpty(this.notifyURL)) {
extraParams.put("notifyURL", this.notifyURL);
}
if (!isNullOrEmpty(this.pipeline)) {
extraParams.put("pipeline", this.pipeline);
}
String persistentId = m.pfop(bucket, dummyKey, fopCmd, extraParams);
return persistentId;
}
private boolean isNullOrEmpty(String str) {
return str == null || str.trim().isEmpty();
}
public static void main(String[] args) {
List<String> urls = new ArrayList<String>();
urls.add("http://7xliow.com2.z0.glb.qiniucdn.com/1441174854604_000001.jpg");
urls.add("http://7xliow.com2.z0.glb.qiniucdn.com/1441174854604_000002.jpg");
urls.add("http://7xliow.com2.z0.glb.qiniucdn.com/1441174854604_000003.jpg");
urls.add("http://7xliow.com2.z0.glb.qiniucdn.com/1441174854604_000004.jpg");
urls.add("http://7xliow.com2.z0.glb.qiniucdn.com/1441174854604_000005.jpg");
urls.add("http://7xliow.com2.z0.glb.qiniucdn.com/1441174854604_000006.jpg");
// 合成后的图片保存空间
String destSaveBucket = "video";
// 合成后的图片保存的名字
String destImageKey = "1441174854604_comp.jpg";
// 截图的时间间隔,单位秒
int interval = 5;
// 截图所在的空间名称
String bucket = "video";
// 视频文件的名称,这个文件名可以是空间中任意的文件名,并无实际作用
String dummyKey = "1441174854604";
// webvtt文件的名称
String webvttSaveKey = "1441174854604.webvtt";
// 目标图片格式,支持png和jpeg,如果需要其他格式,可通过七牛管道处理使用imageMogr2命令生成其他格式图片
String destImageFormat = "jpeg";
// 目标图片的行数
int rows = 3;
// 目标图片的列数
int cols = 2;
// 目标图片中的每个图片的周边空白大小
int margin = 10;
// 目标图片的背景透明度
int alpha = 255;
// 目标图片中的每个图片的排列顺序
int order = 1;
// 目标图片的背景颜色
String bgColor = "#000000";
// ufop 名称,根据账号下部署的ufop设置
String ufopName = "hxc-imagecomp";
// access key & secret key
String accessKey = "<AccessKey>";
String secretKey = "<SecretKey>";
String pipeline = "hksjdl001";
String notifyURL = null;
// 创建对象
ImageComp composer = new ImageComp(ufopName, accessKey, secretKey);
// 设置目标图片保存
composer.setDestTarget(destSaveBucket, destImageKey);
composer.setPipeline(pipeline);
composer.setNotifyURL(notifyURL);
try {
String persistentId = composer.fireUfop(bucket, dummyKey, interval, webvttSaveKey, destImageFormat, rows,
cols, null, null, margin, alpha, order, bgColor, urls);
System.out.println(String.format("http://api.qiniu.com/status/get/prefop?id=%s", persistentId));
} catch (QiniuException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment