From a3dee454a52475dc73afb3a4941765db7c5ff90a Mon Sep 17 00:00:00 2001 From: 0xboobface <0xboobface@gmail.com> Date: Sat, 28 Dec 2019 16:57:55 +0100 Subject: [PATCH] Write the output of FFmpeg to a log file in the segment directory --- .../recorder/download/dash/FfmpegMuxer.java | 12 ++++++++---- .../recorder/download/hls/MergedHlsDownload.java | 15 +++++++++++---- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/common/src/main/java/ctbrec/recorder/download/dash/FfmpegMuxer.java b/common/src/main/java/ctbrec/recorder/download/dash/FfmpegMuxer.java index 1b6a495c..0be33244 100644 --- a/common/src/main/java/ctbrec/recorder/download/dash/FfmpegMuxer.java +++ b/common/src/main/java/ctbrec/recorder/download/dash/FfmpegMuxer.java @@ -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(); diff --git a/common/src/main/java/ctbrec/recorder/download/hls/MergedHlsDownload.java b/common/src/main/java/ctbrec/recorder/download/hls/MergedHlsDownload.java index 14cb2445..5ddfcf8a 100644 --- a/common/src/main/java/ctbrec/recorder/download/hls/MergedHlsDownload.java +++ b/common/src/main/java/ctbrec/recorder/download/hls/MergedHlsDownload.java @@ -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());