-
Notifications
You must be signed in to change notification settings - Fork 826
Expand file tree
/
Copy pathFFmpegExecuteAsyncTask.java
More file actions
111 lines (96 loc) · 3.64 KB
/
FFmpegExecuteAsyncTask.java
File metadata and controls
111 lines (96 loc) · 3.64 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
package com.github.hiteshsondhi88.libffmpeg;
import android.os.AsyncTask;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.concurrent.TimeoutException;
class FFmpegExecuteAsyncTask extends AsyncTask<Void, String, CommandResult> {
private final String[] cmd;
private final FFmpegExecuteResponseHandler ffmpegExecuteResponseHandler;
private final ShellCommand shellCommand;
private final long timeout;
private long startTime;
private Process process;
private String output = "";
FFmpegExecuteAsyncTask(String[] cmd, long timeout, FFmpegExecuteResponseHandler ffmpegExecuteResponseHandler) {
this.cmd = cmd;
this.timeout = timeout;
this.ffmpegExecuteResponseHandler = ffmpegExecuteResponseHandler;
this.shellCommand = new ShellCommand();
}
@Override
protected void onPreExecute() {
startTime = System.currentTimeMillis();
if (ffmpegExecuteResponseHandler != null) {
ffmpegExecuteResponseHandler.onStart();
}
}
@Override
protected CommandResult doInBackground(Void... params) {
try {
process = shellCommand.run(cmd);
if (process == null) {
return CommandResult.getDummyFailureResponse();
}
Log.d("Running publishing updates method");
checkAndUpdateProcess();
return CommandResult.getOutputFromProcess(process);
} catch (TimeoutException e) {
Log.e("FFmpeg timed out", e);
return new CommandResult(false, e.getMessage());
} catch (Exception e) {
Log.e("Error running FFmpeg", e);
} finally {
Util.destroyProcess(process);
}
return CommandResult.getDummyFailureResponse();
}
@Override
protected void onProgressUpdate(String... values) {
if (values != null && values[0] != null && ffmpegExecuteResponseHandler != null) {
ffmpegExecuteResponseHandler.onProgress(values[0]);
}
}
@Override
protected void onPostExecute(CommandResult commandResult) {
if (ffmpegExecuteResponseHandler != null) {
output += commandResult.output;
if (commandResult.success) {
ffmpegExecuteResponseHandler.onSuccess(output);
} else {
ffmpegExecuteResponseHandler.onFailure(output);
}
ffmpegExecuteResponseHandler.onFinish();
}
}
private void checkAndUpdateProcess() throws TimeoutException, InterruptedException {
while (!Util.isProcessCompleted(process)) {
// checking if process is completed
if (Util.isProcessCompleted(process)) {
return;
}
// Handling timeout
if (timeout != Long.MAX_VALUE && System.currentTimeMillis() > startTime + timeout) {
throw new TimeoutException("FFmpeg timed out");
}
try {
String line;
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
while ((line = reader.readLine()) != null) {
if (isCancelled()) {
process.destroy();
process.waitFor();
return;
}
output += line+"\n";
publishProgress(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
boolean isProcessCompleted() {
return Util.isProcessCompleted(process);
}
}