Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.qiniu.process.qdora.PfopCommand Maven / Gradle / Ivy
package com.qiniu.process.qdora;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.qiniu.config.JsonFile;
import com.qiniu.model.qdora.Avinfo;
import com.qiniu.model.qdora.VideoStream;
import com.qiniu.process.Base;
import com.qiniu.storage.Configuration;
import com.qiniu.util.JsonUtils;
import com.qiniu.util.PfopUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class PfopCommand extends Base> {
private boolean hasDuration;
private boolean hasSize;
private String avinfoIndex;
private List pfopConfigs;
private Configuration configuration;
private MediaManager mediaManager;
public PfopCommand(Configuration configuration, String avinfoIndex, boolean hasDuration, boolean hasSize,
String pfopJsonPath, List pfopConfigs) throws IOException {
super("pfopcmd", "", "", null);
set(configuration, avinfoIndex, hasDuration, hasSize, pfopJsonPath, pfopConfigs);
this.mediaManager = new MediaManager(configuration.clone());
}
public PfopCommand(Configuration configuration, String avinfoIndex, boolean hasDuration, boolean hasSize,
String pfopJsonPath, List pfopConfigs, String savePath, int saveIndex) throws IOException {
super("pfopcmd", "", "", null, savePath, saveIndex);
set(configuration, avinfoIndex, hasDuration, hasSize, pfopJsonPath, pfopConfigs);
this.mediaManager = new MediaManager(configuration.clone());
}
public PfopCommand(Configuration configuration, String avinfoIndex, boolean hasDuration, boolean hasSize,
String pfopJsonPath, List pfopConfigs, String savePath) throws IOException {
this(configuration, avinfoIndex, hasDuration, hasSize, pfopJsonPath, pfopConfigs, savePath, 0);
}
private void set(Configuration configuration, String avinfoIndex, boolean hasDuration, boolean hasSize, String pfopJsonPath,
List pfopConfigs) throws IOException {
this.configuration = configuration;
if (avinfoIndex == null || "".equals(avinfoIndex)) throw new IOException("please set the avinfo-index.");
else this.avinfoIndex = avinfoIndex;
this.hasDuration = hasDuration;
this.hasSize = hasSize;
if (pfopConfigs != null && pfopConfigs.size() > 0) {
this.pfopConfigs = pfopConfigs;
} else if (pfopJsonPath != null && !"".equals(pfopJsonPath)) {
this.pfopConfigs = new ArrayList<>();
JsonFile jsonFile = new JsonFile(pfopJsonPath);
JsonArray array = jsonFile.getElement("pfopcmd").getAsJsonArray();
for (JsonElement jsonElement : array) {
JsonObject jsonObject = PfopUtils.checkPfopJson(jsonElement.getAsJsonObject(), true);
this.pfopConfigs.add(jsonObject);
}
} else {
throw new IOException("please set valid pfop-configs.");
}
}
@SuppressWarnings("unchecked")
public PfopCommand clone() throws CloneNotSupportedException {
PfopCommand pfopCommand = (PfopCommand)super.clone();
pfopCommand.mediaManager = new MediaManager(configuration.clone());
return pfopCommand;
}
@Override
protected String resultInfo(Map line) {
return String.join("\t", line.get("key"), line.get(avinfoIndex));
}
@Override
protected String singleResult(Map line) throws Exception {
String key;
String info;
Avinfo avinfo;
StringBuilder other = new StringBuilder();
VideoStream videoStream;
List scale;
List resultList = new ArrayList<>();
for (JsonObject pfopConfig : pfopConfigs) {
scale = JsonUtils.fromJsonArray(pfopConfig.get("scale").getAsJsonArray(), new TypeToken>(){});
key = line.get("key");
if (key == null) throw new IOException("key is not exists or empty in " + line);
info = line.get(avinfoIndex);
if (info == null || "".equals(info)) throw new IOException("avinfo is empty.");
avinfo = mediaManager.getAvinfoByJson(JsonUtils.fromJson(info, JsonObject.class));
if (hasDuration) other.append("\t").append(Double.valueOf(avinfo.getFormat().duration));
if (hasSize) other.append("\t").append(Long.valueOf(avinfo.getFormat().size));
videoStream = avinfo.getVideoStream();
if (videoStream == null) throw new IOException("videoStream is null.");
if (scale.get(0) < videoStream.width && videoStream.width <= scale.get(1)) {
resultList.add(String.join("\t", key,
PfopUtils.generateFopCmd(key, pfopConfig), other.toString()));
}
}
return String.join("\n", resultList);
}
@Override
public void closeResource() {
super.closeResource();
avinfoIndex = null;
pfopConfigs = null;
configuration = null;
mediaManager = null;
}
}