forked from j62/ctbrec
1
0
Fork 0

Write the output of FFmpeg to a log file in the segment directory

This commit is contained in:
0xboobface 2019-12-28 16:57:55 +01:00
parent 2da3d64ac8
commit a3dee454a5
2 changed files with 19 additions and 8 deletions

View File

@ -37,6 +37,7 @@ public class FfmpegMuxer {
LOG.debug("Deleting merged video and audio tracks");
Files.delete(mp4VideoTrack.toPath());
Files.delete(mp4AudioTrack.toPath());
Files.deleteIfExists(new File(segmentDir, "merge.log").toPath());
LOG.debug("Deleting segments");
deleteFiles(segmentDir, videoSegments);
deleteFiles(segmentDir, audioSegments);
@ -74,7 +75,9 @@ public class FfmpegMuxer {
}
private int mergeTracks(File mp4VideoTrack, File mp4AudioTrack, File output) throws IOException {
try {
File dir = mp4VideoTrack.getParentFile();
File mergeLog = new File(dir, "merge.log");
try (FileOutputStream muxLogStream = new FileOutputStream(mergeLog)) {
LOG.debug("Merging:\n{}\n{}\n{}", mp4VideoTrack, mp4AudioTrack, output);
// @formatter:off
String[] cmdline = OS.getFFmpegCommand(
@ -82,14 +85,15 @@ public class FfmpegMuxer {
"-i", mp4AudioTrack.getCanonicalPath(),
"-c:v", "copy",
"-c:a", "copy",
"-movflags", "faststart",
"-movflags", "faststart", // for streaming
"-y", // overwrite existing files
output.getCanonicalPath()
);
// @formatter:on
LOG.debug("Command line: {}", Arrays.toString(cmdline));
Process ffmpeg = Runtime.getRuntime().exec(cmdline);
new Thread(new StreamRedirectThread(ffmpeg.getInputStream(), System.out)).start(); // NOSONAR
new Thread(new StreamRedirectThread(ffmpeg.getErrorStream(), System.err)).start(); // NOSONAR
new Thread(new StreamRedirectThread(ffmpeg.getInputStream(), muxLogStream)).start();
new Thread(new StreamRedirectThread(ffmpeg.getErrorStream(), muxLogStream)).start();
return ffmpeg.waitFor();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();

View File

@ -74,6 +74,7 @@ public class MergedHlsDownload extends HlsDownload {
}
private void postprocess(File playlist, File target) {
File mergeLog = new File(playlist.getParentFile(), "merge.log");
try {
File dir = playlist.getParentFile();
// @formatter:off
@ -81,18 +82,24 @@ public class MergedHlsDownload extends HlsDownload {
"-i", playlist.getAbsolutePath(),
"-c:v", "copy",
"-c:a", "copy",
"-movflags", "faststart",
"-movflags", "faststart", // for streaming
"-y", // overwrite existing files
"-f", "mp4",
target.getAbsolutePath()
);
// @formatter:on
LOG.debug("Command line: {}", Arrays.toString(cmdline));
Process ffmpeg = Runtime.getRuntime().exec(cmdline, new String[0], playlist.getParentFile());
new Thread(new StreamRedirectThread(ffmpeg.getInputStream(), System.out)).start(); // NOSONAR
new Thread(new StreamRedirectThread(ffmpeg.getErrorStream(), System.err)).start(); // NOSONAR
int exitCode = ffmpeg.waitFor();
int exitCode = 1;
try (FileOutputStream mergeLogStream = new FileOutputStream(mergeLog)) {
new Thread(new StreamRedirectThread(ffmpeg.getInputStream(), mergeLogStream)).start();
new Thread(new StreamRedirectThread(ffmpeg.getErrorStream(), mergeLogStream)).start();
exitCode = ffmpeg.waitFor();
mergeLogStream.flush();
}
if (exitCode == 0) {
Files.delete(playlist.toPath());
Files.deleteIfExists(mergeLog.toPath());
File[] segments = dir.listFiles((directory, filename) -> filename.endsWith(".ts") || filename.endsWith(".corrupt"));
for (File segment : segments) {
Files.delete(segment.toPath());