Add post-processing support to the DashDownload
This commit is contained in:
parent
a018b15384
commit
8dfc4c775f
|
@ -0,0 +1,59 @@
|
||||||
|
package ctbrec.recorder.download;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import ctbrec.Config;
|
||||||
|
import ctbrec.OS;
|
||||||
|
import ctbrec.Recording;
|
||||||
|
import ctbrec.io.StreamRedirectThread;
|
||||||
|
import ctbrec.recorder.download.hls.AbstractHlsDownload;
|
||||||
|
|
||||||
|
public abstract class AbstractDownload implements Download {
|
||||||
|
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(AbstractHlsDownload.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void postprocess(Recording recording) {
|
||||||
|
runPostProcessingScript(recording);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void runPostProcessingScript(Recording recording) {
|
||||||
|
String postProcessing = Config.getInstance().getSettings().postProcessing;
|
||||||
|
if (postProcessing != null && !postProcessing.isEmpty()) {
|
||||||
|
File target = recording.getAbsoluteFile();
|
||||||
|
Runtime rt = Runtime.getRuntime();
|
||||||
|
try {
|
||||||
|
String[] args = new String[] {
|
||||||
|
postProcessing,
|
||||||
|
target.getParentFile().getAbsolutePath(),
|
||||||
|
target.getAbsolutePath(),
|
||||||
|
getModel().getName(),
|
||||||
|
getModel().getSite().getName(),
|
||||||
|
Long.toString(recording.getStartDate().getEpochSecond())
|
||||||
|
};
|
||||||
|
if(LOG.isDebugEnabled()) {
|
||||||
|
LOG.debug("Running {}", Arrays.toString(args));
|
||||||
|
}
|
||||||
|
Process process = rt.exec(args, OS.getEnvironment());
|
||||||
|
// TODO maybe write these to a separate log file, e.g. recname.ts.pp.log
|
||||||
|
Thread std = new Thread(new StreamRedirectThread(process.getInputStream(), System.out));
|
||||||
|
std.setName("Process stdout pipe");
|
||||||
|
std.setDaemon(true);
|
||||||
|
std.start();
|
||||||
|
Thread err = new Thread(new StreamRedirectThread(process.getErrorStream(), System.err));
|
||||||
|
err.setName("Process stderr pipe");
|
||||||
|
err.setDaemon(true);
|
||||||
|
err.start();
|
||||||
|
|
||||||
|
process.waitFor();
|
||||||
|
LOG.debug("Process finished.");
|
||||||
|
} catch (Exception e) {
|
||||||
|
LOG.error("Error in process thread", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,7 @@
|
||||||
package ctbrec.recorder.download.dash;
|
package ctbrec.recorder.download.dash;
|
||||||
|
|
||||||
|
import static ctbrec.Recording.State.POST_PROCESSING;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
|
@ -32,12 +34,12 @@ import ctbrec.Model;
|
||||||
import ctbrec.Recording;
|
import ctbrec.Recording;
|
||||||
import ctbrec.Recording.State;
|
import ctbrec.Recording.State;
|
||||||
import ctbrec.io.HttpClient;
|
import ctbrec.io.HttpClient;
|
||||||
import ctbrec.recorder.download.Download;
|
import ctbrec.recorder.download.AbstractDownload;
|
||||||
import ctbrec.recorder.download.dash.SegmentTimelineType.S;
|
import ctbrec.recorder.download.dash.SegmentTimelineType.S;
|
||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
import okhttp3.Response;
|
import okhttp3.Response;
|
||||||
|
|
||||||
public class DashDownload implements Download {
|
public class DashDownload extends AbstractDownload {
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(DashDownload.class);
|
private static final Logger LOG = LoggerFactory.getLogger(DashDownload.class);
|
||||||
|
|
||||||
private int audioCounter = 0;
|
private int audioCounter = 0;
|
||||||
|
@ -342,12 +344,14 @@ public class DashDownload implements Download {
|
||||||
@Override
|
@Override
|
||||||
public void postprocess(Recording recording) {
|
public void postprocess(Recording recording) {
|
||||||
try {
|
try {
|
||||||
|
recording.setStatus(POST_PROCESSING);
|
||||||
String path = recording.getPath();
|
String path = recording.getPath();
|
||||||
File dir = new File(Config.getInstance().getSettings().recordingsDir, path);
|
File dir = new File(Config.getInstance().getSettings().recordingsDir, path);
|
||||||
File file = new File(dir.getParentFile(), dir.getName().substring(0, dir.getName().length() - 5));
|
File file = new File(dir.getParentFile(), dir.getName().substring(0, dir.getName().length() - 5));
|
||||||
new FfmpegMuxer(dir, file);
|
new FfmpegMuxer(dir, file);
|
||||||
targetFile = file;
|
targetFile = file;
|
||||||
recording.setPath(path.substring(0, path.length() - 5));
|
recording.setPath(path.substring(0, path.length() - 5));
|
||||||
|
super.postprocess(recording);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
LOG.error("Error while merging dash segments", e);
|
LOG.error("Error while merging dash segments", e);
|
||||||
recording.setStatus(State.FAILED);
|
recording.setStatus(State.FAILED);
|
||||||
|
|
|
@ -1,13 +1,11 @@
|
||||||
package ctbrec.recorder.download.hls;
|
package ctbrec.recorder.download.hls;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -37,19 +35,16 @@ import com.iheartradio.m3u8.data.TrackData;
|
||||||
|
|
||||||
import ctbrec.Config;
|
import ctbrec.Config;
|
||||||
import ctbrec.Model;
|
import ctbrec.Model;
|
||||||
import ctbrec.OS;
|
|
||||||
import ctbrec.Recording;
|
|
||||||
import ctbrec.Recording.State;
|
import ctbrec.Recording.State;
|
||||||
import ctbrec.UnknownModel;
|
import ctbrec.UnknownModel;
|
||||||
import ctbrec.io.HttpClient;
|
import ctbrec.io.HttpClient;
|
||||||
import ctbrec.io.HttpException;
|
import ctbrec.io.HttpException;
|
||||||
import ctbrec.io.StreamRedirectThread;
|
import ctbrec.recorder.download.AbstractDownload;
|
||||||
import ctbrec.recorder.download.Download;
|
|
||||||
import ctbrec.recorder.download.StreamSource;
|
import ctbrec.recorder.download.StreamSource;
|
||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
import okhttp3.Response;
|
import okhttp3.Response;
|
||||||
|
|
||||||
public abstract class AbstractHlsDownload implements Download {
|
public abstract class AbstractHlsDownload extends AbstractDownload {
|
||||||
|
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(AbstractHlsDownload.class);
|
private static final Logger LOG = LoggerFactory.getLogger(AbstractHlsDownload.class);
|
||||||
private static int threadCounter = 0;
|
private static int threadCounter = 0;
|
||||||
|
@ -195,46 +190,7 @@ public abstract class AbstractHlsDownload implements Download {
|
||||||
return model;
|
return model;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void postprocess(Recording recording) {
|
|
||||||
runPostProcessingScript(recording);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void runPostProcessingScript(Recording recording) {
|
|
||||||
String postProcessing = Config.getInstance().getSettings().postProcessing;
|
|
||||||
if (postProcessing != null && !postProcessing.isEmpty()) {
|
|
||||||
File target = recording.getAbsoluteFile();
|
|
||||||
Runtime rt = Runtime.getRuntime();
|
|
||||||
try {
|
|
||||||
String[] args = new String[] {
|
|
||||||
postProcessing,
|
|
||||||
target.getParentFile().getAbsolutePath(),
|
|
||||||
target.getAbsolutePath(),
|
|
||||||
getModel().getName(),
|
|
||||||
getModel().getSite().getName(),
|
|
||||||
Long.toString(recording.getStartDate().getEpochSecond())
|
|
||||||
};
|
|
||||||
if(LOG.isDebugEnabled()) {
|
|
||||||
LOG.debug("Running {}", Arrays.toString(args));
|
|
||||||
}
|
|
||||||
Process process = rt.exec(args, OS.getEnvironment());
|
|
||||||
// TODO maybe write these to a separate log file, e.g. recname.ts.pp.log
|
|
||||||
Thread std = new Thread(new StreamRedirectThread(process.getInputStream(), System.out));
|
|
||||||
std.setName("Process stdout pipe");
|
|
||||||
std.setDaemon(true);
|
|
||||||
std.start();
|
|
||||||
Thread err = new Thread(new StreamRedirectThread(process.getErrorStream(), System.err));
|
|
||||||
err.setName("Process stderr pipe");
|
|
||||||
err.setDaemon(true);
|
|
||||||
err.start();
|
|
||||||
|
|
||||||
process.waitFor();
|
|
||||||
LOG.debug("Process finished.");
|
|
||||||
} catch (Exception e) {
|
|
||||||
LOG.error("Error in process thread", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class SegmentPlaylist {
|
public static class SegmentPlaylist {
|
||||||
public String url;
|
public String url;
|
||||||
|
|
Loading…
Reference in New Issue