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

View File

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