Skip to content

Commit

Permalink
feat: tool 生成失败提示
Browse files Browse the repository at this point in the history
  • Loading branch information
hexleo committed Dec 23, 2020
1 parent c2ecdf7 commit 98ab801
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,14 @@ public void setToolListener(IToolListener toolListener) {
*/
public void create(final CommonArg commonArg, final boolean needVideo) throws Exception{
TLog.i(TAG, "start create");
createAllFrameImage(commonArg, new Runnable() {
createAllFrameImage(commonArg, new IRunResult() {
@Override
public void run() {
public boolean run() {
if (finalCheck(commonArg) && needVideo) {
// 最终生成视频文件
createVideo(commonArg);
return createVideo(commonArg);
}
return false;
}
});
}
Expand Down Expand Up @@ -93,7 +94,7 @@ private boolean finalCheck(CommonArg commonArg) {
return true;
}

private void createAllFrameImage(final CommonArg commonArg, final Runnable finishRunnable) throws Exception{
private void createAllFrameImage(final CommonArg commonArg, final IRunResult finishRunnable) throws Exception{
if (!checkCommonArg(commonArg)) {
if (toolListener != null) toolListener.onError();
return;
Expand Down Expand Up @@ -147,13 +148,18 @@ public void run() {
synchronized (AnimTool.class) {
finishThreadCount++;
if (finishThreadCount == threadNum) {
boolean result = false;
if (finishRunnable != null) {
finishRunnable.run();
result = finishRunnable.run();
}
long cost = System.currentTimeMillis() - time;
TLog.i(TAG,"Finish cost=" + cost);
if (toolListener != null) {
toolListener.onComplete();
if (result) {
toolListener.onComplete();
} else {
toolListener.onError();
}
}
}
}
Expand Down Expand Up @@ -195,22 +201,24 @@ private void checkDir(String path) {
* 创建最终的视频
* @param commonArg
*/
private void createVideo(CommonArg commonArg){
private boolean createVideo(CommonArg commonArg) {
try {
// 创建配置json文件
createVapcJson(commonArg);
// 创建mp4文件
boolean result = createMp4(commonArg, commonArg.outputPath, commonArg.frameOutputPath);
if (!result) {
TLog.i(TAG, "createMp4 fail");
return;
deleteFile(commonArg);
return false;
}
String tempVideoName = TEMP_VIDEO_FILE;
if (commonArg.needAudio) {
result = mergeAudio2Mp4(commonArg, tempVideoName);
if (!result) {
TLog.i(TAG, "mergeAudio2Mp4 fail");
return;
deleteFile(commonArg);
return false;
}
tempVideoName = TEMP_VIDEO_AUDIO_FILE;
}
Expand All @@ -222,20 +230,31 @@ private void createVideo(CommonArg commonArg){
result = mergeBin2Mp4(commonArg, vapcBinPath, tempVideoName, commonArg.outputPath);
if (!result) {
TLog.i(TAG, "mergeBin2Mp4 fail");
return;
}
// 删除临时视频文件
new File(commonArg.outputPath + TEMP_VIDEO_FILE).delete();
if (commonArg.needAudio) {
new File(commonArg.outputPath + TEMP_VIDEO_AUDIO_FILE).delete();
deleteFile(commonArg);
return false;
}
new File(commonArg.outputPath + VAPC_BIN_FILE).delete();
deleteFile(commonArg);
// 计算文件md5
String md5 = new Md5Util().getFileMD5(new File(commonArg.outputPath + VIDEO_FILE), commonArg.outputPath);
TLog.i(TAG, "md5="+md5);
} catch (Exception e) {
e.printStackTrace();
TLog.e(TAG, "createVideo error:" + e.getMessage());
return false;
}
return true;
}

private void deleteFile(CommonArg commonArg) {
// 删除临时视频文件
File file;
file = new File(commonArg.outputPath + TEMP_VIDEO_FILE);
if (file.exists()) file.delete();
if (commonArg.needAudio) {
file = new File(commonArg.outputPath + TEMP_VIDEO_AUDIO_FILE);
if (file.exists()) file.delete();
}
file = new File(commonArg.outputPath + VAPC_BIN_FILE);
if (file.exists()) file.delete();
}

/**
Expand Down Expand Up @@ -366,4 +385,8 @@ public interface IToolListener {
void onComplete();
}

private interface IRunResult {
boolean run();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.security.MessageDigest;

public class Md5Util {
public static final String MD5_FILE = "md5.txt";
private char[] hexDigits = new char[] {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};


Expand All @@ -48,7 +49,7 @@ public String getFileMD5(File file, String outputPath) {
md5txt = bufferToHex(digest);
}
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(outputPath + "/md5.txt"));
BufferedWriter writer = new BufferedWriter(new FileWriter(outputPath + MD5_FILE));
writer.write(md5txt);
writer.flush();
writer.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void run() {
runAnimTool();
} catch (Exception e) {
TLog.e(TAG, e.getMessage());
btnCreate.setEnabled(true);
setOutput(false, "");
}
}
}).start();
Expand Down Expand Up @@ -187,13 +187,12 @@ public void onWarning(String msg) {

@Override
public void onError() {
btnCreate.setEnabled(true);
setOutput(false, "");
}

@Override
public void onComplete() {
btnCreate.setEnabled(true);
setOutput(commonArg.outputPath);
setOutput(true, commonArg.outputPath);
try {
setProperties(commonArg);
Desktop.getDesktop().open(new File(commonArg.outputPath));
Expand Down Expand Up @@ -390,18 +389,23 @@ public void actionPerformed(ActionEvent actionEvent) {
return panel;
}

private void setOutput(final String path) {
labelOutInfo.setText("<html><font color='blue'>open output</font></html>");
labelOutInfo.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent mouseEvent) {
try {
Desktop.getDesktop().open(new File(path));
} catch (IOException e) {
TLog.e(TAG, e.getMessage());
private void setOutput(boolean success, final String path) {
btnCreate.setEnabled(true);
if (success) {
labelOutInfo.setText("<html><font color='blue'>open output</font></html>");
labelOutInfo.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent mouseEvent) {
try {
Desktop.getDesktop().open(new File(path));
} catch (IOException e) {
TLog.e(TAG, e.getMessage());
}
}
}
});
});
} else {
labelOutInfo.setText("<html><font color='red'>create error!</font></html>");
}
}


Expand Down

0 comments on commit 98ab801

Please sign in to comment.