-
Notifications
You must be signed in to change notification settings - Fork 83
/
StopExample.java
119 lines (96 loc) · 3.6 KB
/
StopExample.java
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
112
113
114
115
116
117
118
119
package examples;
import com.github.kokorin.jaffree.ffmpeg.FFmpeg;
import com.github.kokorin.jaffree.ffmpeg.FFmpegProgress;
import com.github.kokorin.jaffree.ffmpeg.FFmpegResult;
import com.github.kokorin.jaffree.ffmpeg.FFmpegResultFuture;
import com.github.kokorin.jaffree.ffmpeg.NullOutput;
import com.github.kokorin.jaffree.ffmpeg.ProgressListener;
import com.github.kokorin.jaffree.ffmpeg.UrlInput;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
public class StopExample {
public static void stopWithException(final FFmpeg ffmpeg) throws Exception {
final AtomicBoolean stopped = new AtomicBoolean();
ffmpeg.setProgressListener(
new ProgressListener() {
@Override
public void onProgress(FFmpegProgress progress) {
if (stopped.get()) {
throw new RuntimeException("Stooped with exception!");
}
}
}
);
final AtomicReference<FFmpegResult> result = new AtomicReference<>();
new Thread() {
@Override
public void run() {
FFmpegResult r = ffmpeg.execute();
result.set(r);
}
}.start();
Thread.sleep(5_000);
stopped.set(true);
Thread.sleep(1_000);
System.out.println(result.get());
}
public static void stopWithInterruption(final FFmpeg ffmpeg) throws Exception {
final AtomicReference<FFmpegResult> result = new AtomicReference<>();
Thread thread = new Thread() {
@Override
public void run() {
FFmpegResult r = ffmpeg.execute();
result.set(r);
}
};
thread.start();
Thread.sleep(5_000);
thread.interrupt();
Thread.sleep(1_000);
System.out.println(result.get());
}
public static void stopForcefully(final FFmpeg ffmpeg) throws Exception {
FFmpegResultFuture future = ffmpeg.executeAsync();
Thread.sleep(5_000);
future.forceStop();
Thread.sleep(1_000);
System.out.println(future.get());
// Uncaught exception in executeAsync thread:
// Process execution has ended with non-zero status: 1
}
public static void stopGracefully(final FFmpeg ffmpeg) throws Exception {
FFmpegResultFuture future = ffmpeg.executeAsync();
Thread.sleep(5_000);
future.graceStop();
Thread.sleep(1_000);
System.out.println(future.get());
}
public static void main(String[] args) throws Exception {
FFmpeg ffmpeg;
ffmpeg = createTestFFmpeg();
stopWithException(ffmpeg);
ffmpeg = createTestFFmpeg();
stopWithInterruption(ffmpeg);
ffmpeg = createTestFFmpeg();
stopForcefully(ffmpeg);
ffmpeg = createTestFFmpeg();
stopGracefully(ffmpeg);
}
public static FFmpeg createTestFFmpeg() {
return FFmpeg.atPath()
.addInput(
UrlInput
.fromUrl("testsrc=duration=3600:size=1280x720:rate=30")
.setFormat("lavfi")
)
.setProgressListener(new ProgressListener() {
@Override
public void onProgress(FFmpegProgress progress) {
//System.out.println(progress);
}
})
.addOutput(
new NullOutput()
);
}
}