forked from j62/ctbrec
Change PlaylistGenerator.genertate to not return null
Instead an exception is thrown, if something goes wrong
This commit is contained in:
parent
5faf12b41b
commit
d48498e715
|
@ -13,6 +13,7 @@ import java.util.List;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import com.google.common.io.Files;
|
||||||
import com.iheartradio.m3u8.Encoding;
|
import com.iheartradio.m3u8.Encoding;
|
||||||
import com.iheartradio.m3u8.Format;
|
import com.iheartradio.m3u8.Format;
|
||||||
import com.iheartradio.m3u8.ParseException;
|
import com.iheartradio.m3u8.ParseException;
|
||||||
|
@ -30,7 +31,7 @@ import ctbrec.MpegUtil;
|
||||||
|
|
||||||
|
|
||||||
public class PlaylistGenerator {
|
public class PlaylistGenerator {
|
||||||
private static final transient Logger LOG = LoggerFactory.getLogger(PlaylistGenerator.class);
|
private static final Logger LOG = LoggerFactory.getLogger(PlaylistGenerator.class);
|
||||||
|
|
||||||
private int lastPercentage;
|
private int lastPercentage;
|
||||||
private List<ProgressListener> listeners = new ArrayList<>();
|
private List<ProgressListener> listeners = new ArrayList<>();
|
||||||
|
@ -38,10 +39,10 @@ public class PlaylistGenerator {
|
||||||
public File generate(File directory) throws IOException, ParseException, PlaylistException {
|
public File generate(File directory) throws IOException, ParseException, PlaylistException {
|
||||||
LOG.debug("Starting playlist generation for {}", directory);
|
LOG.debug("Starting playlist generation for {}", directory);
|
||||||
// get a list of all ts files and sort them by sequence
|
// get a list of all ts files and sort them by sequence
|
||||||
File[] files = directory.listFiles((f) -> f.getName().endsWith(".ts"));
|
File[] files = directory.listFiles(f -> f.getName().endsWith(".ts"));
|
||||||
if(files == null || files.length == 0) {
|
if (files == null || files.length == 0) {
|
||||||
LOG.debug("{} is empty. Not going to generate a playlist", directory);
|
LOG.debug("{} is empty. Not going to generate a playlist", directory);
|
||||||
return null;
|
throw new InvalidPlaylistException("Directory is empty");
|
||||||
}
|
}
|
||||||
|
|
||||||
Arrays.sort(files, (f1, f2) -> {
|
Arrays.sort(files, (f1, f2) -> {
|
||||||
|
@ -57,22 +58,21 @@ public class PlaylistGenerator {
|
||||||
for (File file : files) {
|
for (File file : files) {
|
||||||
try {
|
try {
|
||||||
float duration = (float) MpegUtil.getFileDuration(file);
|
float duration = (float) MpegUtil.getFileDuration(file);
|
||||||
if(duration <= 0) {
|
if (duration <= 0) {
|
||||||
throw new RuntimeException("Track has negative duration: " + file.getName());
|
throw new InvalidTrackLengthException("Track has negative duration: " + file.getName());
|
||||||
} else {
|
} else {
|
||||||
track.add(new TrackData.Builder()
|
track.add(new TrackData.Builder()
|
||||||
.withUri(file.getName())
|
.withUri(file.getName())
|
||||||
.withTrackInfo(new TrackInfo(duration, file.getName()))
|
.withTrackInfo(new TrackInfo(duration, file.getName()))
|
||||||
.build());
|
.build());
|
||||||
}
|
}
|
||||||
} catch(Exception e) {
|
} catch (Exception e) {
|
||||||
LOG.warn("Couldn't determine duration for {}. Skipping this file.", file.getName());
|
LOG.warn("Couldn't determine duration for {}. Skipping this file.", file.getName());
|
||||||
file.renameTo(new File(directory, file.getName()+".corrupt"));
|
File corruptedFile = new File(directory, file.getName() + ".corrupt");
|
||||||
} catch(Throwable t) {
|
Files.move(file, corruptedFile);
|
||||||
LOG.error("Unexpected error", t);
|
|
||||||
}
|
}
|
||||||
done++;
|
done++;
|
||||||
double percentage = (double)done / (double) total;
|
double percentage = (double) done / (double) total;
|
||||||
updateProgressListeners(percentage);
|
updateProgressListeners(percentage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -93,7 +93,7 @@ public class PlaylistGenerator {
|
||||||
|
|
||||||
// write the playlist to a file
|
// write the playlist to a file
|
||||||
File output = new File(directory, "playlist.m3u8");
|
File output = new File(directory, "playlist.m3u8");
|
||||||
try(FileOutputStream fos = new FileOutputStream(output)) {
|
try (FileOutputStream fos = new FileOutputStream(output)) {
|
||||||
PlaylistWriter writer = new PlaylistWriter.Builder()
|
PlaylistWriter writer = new PlaylistWriter.Builder()
|
||||||
.withFormat(Format.EXT_M3U)
|
.withFormat(Format.EXT_M3U)
|
||||||
.withEncoding(Encoding.UTF_8)
|
.withEncoding(Encoding.UTF_8)
|
||||||
|
@ -106,8 +106,8 @@ public class PlaylistGenerator {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateProgressListeners(double percentage) {
|
private void updateProgressListeners(double percentage) {
|
||||||
int p = (int) (percentage*100);
|
int p = (int) (percentage * 100);
|
||||||
if(p > lastPercentage) {
|
if (p > lastPercentage) {
|
||||||
for (ProgressListener progressListener : listeners) {
|
for (ProgressListener progressListener : listeners) {
|
||||||
progressListener.update(p);
|
progressListener.update(p);
|
||||||
}
|
}
|
||||||
|
@ -134,21 +134,16 @@ public class PlaylistGenerator {
|
||||||
|
|
||||||
public void validate(File recDir) throws IOException, ParseException, PlaylistException {
|
public void validate(File recDir) throws IOException, ParseException, PlaylistException {
|
||||||
File playlist = new File(recDir, "playlist.m3u8");
|
File playlist = new File(recDir, "playlist.m3u8");
|
||||||
if(playlist.exists()) {
|
if (playlist.exists()) {
|
||||||
try(FileInputStream fin = new FileInputStream(playlist)) {
|
try (FileInputStream fin = new FileInputStream(playlist)) {
|
||||||
PlaylistParser playlistParser = new PlaylistParser(fin, Format.EXT_M3U, Encoding.UTF_8, ParsingMode.LENIENT);
|
PlaylistParser playlistParser = new PlaylistParser(fin, Format.EXT_M3U, Encoding.UTF_8, ParsingMode.LENIENT);
|
||||||
Playlist m3u = playlistParser.parse();
|
Playlist m3u = playlistParser.parse();
|
||||||
MediaPlaylist mediaPlaylist = m3u.getMediaPlaylist();
|
MediaPlaylist mediaPlaylist = m3u.getMediaPlaylist();
|
||||||
int playlistSize = mediaPlaylist.getTracks().size();
|
int playlistSize = mediaPlaylist.getTracks().size();
|
||||||
File[] segments = recDir.listFiles(new FilenameFilter() {
|
File[] segments = recDir.listFiles((FilenameFilter) (dir, name) -> name.endsWith(".ts"));
|
||||||
@Override
|
if (segments.length == 0) {
|
||||||
public boolean accept(File dir, String name) {
|
|
||||||
return name.endsWith(".ts");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
if(segments.length == 0) {
|
|
||||||
throw new InvalidPlaylistException("No segments found. Playlist is empty");
|
throw new InvalidPlaylistException("No segments found. Playlist is empty");
|
||||||
} else if(segments.length != playlistSize) {
|
} else if (segments.length != playlistSize) {
|
||||||
throw new InvalidPlaylistException("Playlist size and amount of segments differ (" + segments.length + " != " + playlistSize + ")");
|
throw new InvalidPlaylistException("Playlist size and amount of segments differ (" + segments.length + " != " + playlistSize + ")");
|
||||||
} else {
|
} else {
|
||||||
LOG.debug("Generated playlist looks good");
|
LOG.debug("Generated playlist looks good");
|
||||||
|
@ -164,4 +159,10 @@ public class PlaylistGenerator {
|
||||||
super(msg);
|
super(msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class InvalidTrackLengthException extends RuntimeException {
|
||||||
|
public InvalidTrackLengthException(String msg) {
|
||||||
|
super(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue