Created
May 6, 2021 20:50
-
-
Save nestoralonso/fc593072fd9de12ece41a1b98336c0b3 to your computer and use it in GitHub Desktop.
List Durations of Video Files, requires java >=11 and ffprobe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env java --source=11 | |
import java.util.List; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.time.Duration; | |
import java.io.BufferedReader; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
class ListDurations { | |
public static Collection<FileInfo> listDurations(String[] filePaths) { | |
var res = new ArrayList<FileInfo>(); | |
for (var f : filePaths) { | |
ProcessBuilder pBuilder = new ProcessBuilder(List.of("ffprobe", "-v", "error", "-show_entries", | |
"format=duration", "-of", "default=noprint_wrappers=1:nokey=1", f)); | |
String durationFmt = ""; | |
try { | |
Process process = pBuilder.start(); | |
var bis = new BufferedReader(new InputStreamReader(process.getInputStream())); | |
String cmdOut = bis.readLine(); | |
int exitCode = process.waitFor(); | |
if (exitCode == 0) { | |
long secs = (long) Double.parseDouble(cmdOut); | |
durationFmt = String.format("%d:%02d:%02d", secs / 3600, (secs % 3600) / 60, (secs % 60)); | |
} | |
} catch (InterruptedException | IOException e) { | |
e.printStackTrace(); | |
} | |
var info = new FileInfo(f, durationFmt); | |
res.add(info); | |
} | |
return res; | |
} | |
public static void main(String[] args) { | |
for (var entry : listDurations(args)) { | |
System.out.format("%s %s\n", entry.name, entry.duration); | |
} | |
} | |
} | |
class FileInfo { | |
public final String name; | |
public final String duration; | |
public FileInfo(String name, String duration) { | |
this.name = name; | |
this.duration = duration; | |
} | |
public String toString() { | |
return String.format("<%s, %s>", this.name, this.duration); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment