-
Notifications
You must be signed in to change notification settings - Fork 80
/
CompletionExample.java
81 lines (67 loc) · 2.75 KB
/
CompletionExample.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
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 CompletionExample {
public static void completionWithException(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("Stopped with exception!");
}
}
}
);
final AtomicReference<FFmpegResult> result = new AtomicReference<>();
ffmpeg.executeAsync().toCompletableFuture().thenAccept(result::set).exceptionally(ex -> {
System.out.println("Completion exception: " + ex);
return null;
});
Thread.sleep(5_000);
stopped.set(true);
Thread.sleep(1_000);
System.out.println(result.get());
}
public static void completionWithGracefulStop(final FFmpeg ffmpeg) throws Exception {
final AtomicReference<FFmpegResult> result = new AtomicReference<>();
FFmpegResultFuture future = ffmpeg.executeAsync();
future.toCompletableFuture().thenAccept(result::set);
Thread.sleep(5_000);
future.graceStop();
Thread.sleep(1_000);
System.out.println(result.get());
}
public static void main(String[] args) throws Exception {
FFmpeg ffmpeg;
ffmpeg = createTestFFmpeg();
completionWithException(ffmpeg);
ffmpeg = createTestFFmpeg();
completionWithGracefulStop(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()
);
}
}