Code cleanup

This commit is contained in:
0xb00bface 2023-04-08 13:06:32 +02:00
parent 5137e2819e
commit 2ceec9ce94
1 changed files with 12 additions and 10 deletions

View File

@ -1,5 +1,11 @@
package ctbrec.recorder.download;
import ctbrec.OS;
import ctbrec.io.DevNull;
import ctbrec.io.StreamRedirector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
@ -8,20 +14,16 @@ import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ctbrec.OS;
import ctbrec.io.DevNull;
import ctbrec.io.StreamRedirector;
public class VideoLengthDetector {
private static final Logger LOG = LoggerFactory.getLogger(VideoLengthDetector.class);
private VideoLengthDetector() {
}
/**
* Tries to determine the length of a video file by calling FFmpeg and parsing it's output
*
* @param videoFile
* @param videoFile the video file to analyze
* @return the length as Duration object. The duration is negative, if the length couldn't be determined
*/
public static Duration getLength(File videoFile) {
@ -37,7 +39,7 @@ public class VideoLengthDetector {
String[] cmdline = OS.getFFmpegCommand(args);
LOG.debug("Command line: {}", Arrays.toString(cmdline));
Process ffmpeg = Runtime.getRuntime().exec(cmdline, new String[0], videoFile.getParentFile());
int exitCode = 1;
int exitCode;
ByteArrayOutputStream stdErrBuffer = new ByteArrayOutputStream();
Thread stdout = new Thread(new StreamRedirector(ffmpeg.getInputStream(), new DevNull()));
Thread stderr = new Thread(new StreamRedirector(ffmpeg.getErrorStream(), stdErrBuffer));
@ -47,7 +49,7 @@ public class VideoLengthDetector {
LOG.debug("FFmpeg exited with code {}", exitCode);
stdout.join();
stderr.join();
String ffmpegStderr = new String(stdErrBuffer.toByteArray());
String ffmpegStderr = stdErrBuffer.toString();
return parseDuration(ffmpegStderr);
} catch (IOException | ProcessExitedUncleanException e) {
LOG.error("Error in FFMpeg thread", e);