forked from kokorin/Jaffree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShowStreams.java
92 lines (73 loc) · 2.67 KB
/
ShowStreams.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
package examples.ffprobe;
import com.github.kokorin.jaffree.ffmpeg.*;
import com.github.kokorin.jaffree.ffprobe.FFprobe;
import com.github.kokorin.jaffree.ffprobe.FFprobeResult;
import com.github.kokorin.jaffree.ffprobe.Stream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
public class ShowStreams {
private final String ffmpegBin;
private final String video;
private static final Logger LOGGER = LoggerFactory.getLogger(ShowStreams.class);
public ShowStreams(String ffmpegBin, String video) {
this.ffmpegBin = ffmpegBin;
this.video = video;
}
public void execute() {
FFprobe ffprobe;
if (ffmpegBin != null) {
ffprobe = FFprobe.atPath(Paths.get(ffmpegBin));
} else {
ffprobe = FFprobe.atPath();
}
FFprobeResult result = ffprobe
.setShowStreams(true)
.setInput(video)
.execute();
for (Stream stream : result.getStreams()) {
System.out.println("Stream " + stream.getIndex() + " type " + stream.getCodecType() + " duration " + stream.getDuration(TimeUnit.SECONDS));
}
FFmpeg ffmpeg;
if (ffmpegBin != null) {
ffmpeg = FFmpeg.atPath(Paths.get(ffmpegBin));
} else {
ffmpeg = FFmpeg.atPath();
}
final AtomicLong durationMillis = new AtomicLong();
FFmpegResult fFmpegResult = ffmpeg
.addInput(
UrlInput.fromUrl(video)
)
.addOutput(new NullOutput())
.setProgressListener(new ProgressListener() {
@Override
public void onProgress(FFmpegProgress progress) {
durationMillis.set(progress.getTimeMillis());
}
})
.execute();
System.out.println("Exact duration: " + durationMillis.get() + " milliseconds");
}
public static void main(String[] args) {
Iterator<String> argIter = Arrays.asList(args).iterator();
String ffmpegBin = null;
String video = null;
while (argIter.hasNext()) {
String argName = argIter.next();
if (!argIter.hasNext()) {
video = argName;
break;
}
String argValue = argIter.next();
if ("-ffmpeg_bin".equals(argName)) {
ffmpegBin = argValue;
}
}
new ShowStreams(ffmpegBin, video).execute();
}
}