forked from j62/ctbrec
1
0
Fork 0

Implement custom command line parameters and clean shutdown

This commit is contained in:
0xboobface 2020-02-22 12:19:01 +01:00
parent d2f490f8f6
commit f98d442096
3 changed files with 26 additions and 16 deletions

View File

@ -233,8 +233,6 @@ public abstract class AbstractModel implements Model {
if (Config.isServerMode()) { if (Config.isServerMode()) {
return new HlsDownload(getSite().getHttpClient()); return new HlsDownload(getSite().getHttpClient());
} else { } else {
// return new MergedHlsDownload(getSite().getHttpClient());
//return new MergedFfmpegHlsDownload(getSite().getHttpClient());
return new FFmpegDownload(getSite().getHttpClient()); return new FFmpegDownload(getSite().getHttpClient());
} }
} }

View File

@ -50,7 +50,8 @@ public class Settings {
public List<EventHandlerConfiguration> eventHandlers = new ArrayList<>(); public List<EventHandlerConfiguration> eventHandlers = new ArrayList<>();
public String fc2livePassword = ""; public String fc2livePassword = "";
public String fc2liveUsername = ""; public String fc2liveUsername = "";
public String ffmpegMergedDownloadArgs = "-i - -c:v copy -c:a copy -movflags faststart -y -f mp4"; public String ffmpegMergedDownloadArgs = "-c:v copy -c:a copy -movflags faststart -y -f mpegts";
public String ffmpegFileSuffix = "ts";
public String flirt4freePassword; public String flirt4freePassword;
public String flirt4freeUsername; public String flirt4freeUsername;
public boolean generatePlaylist = true; public boolean generatePlaylist = true;

View File

@ -3,9 +3,12 @@ package ctbrec.recorder.download.hls;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.time.Duration; import java.time.Duration;
import java.time.Instant; import java.time.Instant;
import java.util.Arrays;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -19,6 +22,7 @@ import com.iheartradio.m3u8.PlaylistException;
import ctbrec.Config; import ctbrec.Config;
import ctbrec.Model; import ctbrec.Model;
import ctbrec.OS;
import ctbrec.Recording; import ctbrec.Recording;
import ctbrec.io.HttpClient; import ctbrec.io.HttpClient;
import ctbrec.io.StreamRedirectThread; import ctbrec.io.StreamRedirectThread;
@ -40,7 +44,8 @@ public class FFmpegDownload extends AbstractHlsDownload {
this.config = config; this.config = config;
this.model = model; this.model = model;
this.startTime = startTime; this.startTime = startTime;
targetFile = config.getFileForRecording(model, "mp4", startTime); String suffix = config.getSettings().ffmpegFileSuffix;
targetFile = config.getFileForRecording(model, suffix, startTime);
} }
@Override @Override
@ -49,17 +54,15 @@ public class FFmpegDownload extends AbstractHlsDownload {
Files.createDirectories(targetFile.getParentFile().toPath()); Files.createDirectories(targetFile.getParentFile().toPath());
String chunkPlaylist = getSegmentPlaylistUrl(model); String chunkPlaylist = getSegmentPlaylistUrl(model);
// @formatter:off String[] args = config.getSettings().ffmpegMergedDownloadArgs.split(" ");
ffmpeg = Runtime.getRuntime().exec(new String[] { String[] argsPlusFile = new String[args.length + 3];
"/usr/bin/ffmpeg", System.arraycopy(args, 0, argsPlusFile, 2, args.length);
"-y", // overwrite output files without asking argsPlusFile[0] = "-i";
"-headers", "User-Agent: " + config.getSettings().httpUserAgent, argsPlusFile[1] = chunkPlaylist;
"-i", chunkPlaylist, argsPlusFile[argsPlusFile.length-1] = targetFile.getAbsolutePath();
"-c", "copy", String[] cmdline = OS.getFFmpegCommand(argsPlusFile);
"-f", "mp4", LOG.debug("Command line: {}", Arrays.toString(cmdline));
targetFile.getCanonicalPath() ffmpeg = Runtime.getRuntime().exec(cmdline, new String[0], targetFile.getParentFile());
});
// @formatter:on
int exitCode = 1; int exitCode = 1;
File ffmpegLog = File.createTempFile(targetFile.getName(), ".log"); File ffmpegLog = File.createTempFile(targetFile.getName(), ".log");
try (FileOutputStream mergeLogStream = new FileOutputStream(ffmpegLog)) { try (FileOutputStream mergeLogStream = new FileOutputStream(ffmpegLog)) {
@ -91,7 +94,15 @@ public class FFmpegDownload extends AbstractHlsDownload {
@Override @Override
public void stop() { public void stop() {
if (ffmpeg != null && ffmpeg.isAlive()) { if (ffmpeg != null && ffmpeg.isAlive()) {
ffmpeg.destroy(); OutputStream ffmpegStdIn = ffmpeg.getOutputStream();
try {
ffmpegStdIn.write("q".getBytes(StandardCharsets.UTF_8));
ffmpegStdIn.flush();
ffmpegStdIn.close();
} catch (IOException e) {
LOG.error("Couldn't terminate FFmpeg by sending 'q'", e);
ffmpeg.destroy();
}
} }
} }