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.LoggerFactory;
|
||||
|
||||
import com.google.common.io.Files;
|
||||
import com.iheartradio.m3u8.Encoding;
|
||||
import com.iheartradio.m3u8.Format;
|
||||
import com.iheartradio.m3u8.ParseException;
|
||||
|
@ -30,7 +31,7 @@ import ctbrec.MpegUtil;
|
|||
|
||||
|
||||
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 List<ProgressListener> listeners = new ArrayList<>();
|
||||
|
@ -38,10 +39,10 @@ public class PlaylistGenerator {
|
|||
public File generate(File directory) throws IOException, ParseException, PlaylistException {
|
||||
LOG.debug("Starting playlist generation for {}", directory);
|
||||
// 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) {
|
||||
LOG.debug("{} is empty. Not going to generate a playlist", directory);
|
||||
return null;
|
||||
throw new InvalidPlaylistException("Directory is empty");
|
||||
}
|
||||
|
||||
Arrays.sort(files, (f1, f2) -> {
|
||||
|
@ -58,7 +59,7 @@ public class PlaylistGenerator {
|
|||
try {
|
||||
float duration = (float) MpegUtil.getFileDuration(file);
|
||||
if (duration <= 0) {
|
||||
throw new RuntimeException("Track has negative duration: " + file.getName());
|
||||
throw new InvalidTrackLengthException("Track has negative duration: " + file.getName());
|
||||
} else {
|
||||
track.add(new TrackData.Builder()
|
||||
.withUri(file.getName())
|
||||
|
@ -67,9 +68,8 @@ public class PlaylistGenerator {
|
|||
}
|
||||
} catch (Exception e) {
|
||||
LOG.warn("Couldn't determine duration for {}. Skipping this file.", file.getName());
|
||||
file.renameTo(new File(directory, file.getName()+".corrupt"));
|
||||
} catch(Throwable t) {
|
||||
LOG.error("Unexpected error", t);
|
||||
File corruptedFile = new File(directory, file.getName() + ".corrupt");
|
||||
Files.move(file, corruptedFile);
|
||||
}
|
||||
done++;
|
||||
double percentage = (double) done / (double) total;
|
||||
|
@ -140,12 +140,7 @@ public class PlaylistGenerator {
|
|||
Playlist m3u = playlistParser.parse();
|
||||
MediaPlaylist mediaPlaylist = m3u.getMediaPlaylist();
|
||||
int playlistSize = mediaPlaylist.getTracks().size();
|
||||
File[] segments = recDir.listFiles(new FilenameFilter() {
|
||||
@Override
|
||||
public boolean accept(File dir, String name) {
|
||||
return name.endsWith(".ts");
|
||||
}
|
||||
});
|
||||
File[] segments = recDir.listFiles((FilenameFilter) (dir, name) -> name.endsWith(".ts"));
|
||||
if (segments.length == 0) {
|
||||
throw new InvalidPlaylistException("No segments found. Playlist is empty");
|
||||
} else if (segments.length != playlistSize) {
|
||||
|
@ -164,4 +159,10 @@ public class PlaylistGenerator {
|
|||
super(msg);
|
||||
}
|
||||
}
|
||||
|
||||
public static class InvalidTrackLengthException extends RuntimeException {
|
||||
public InvalidTrackLengthException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue