Change PlaylistGenerator.genertate to not return null

Instead an exception is thrown, if something goes wrong
This commit is contained in:
0xboobface 2019-12-26 12:25:46 +01:00
parent 5faf12b41b
commit d48498e715
1 changed files with 35 additions and 34 deletions

View File

@ -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) -> {
@ -58,7 +59,7 @@ public class PlaylistGenerator {
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())
@ -67,9 +68,8 @@ public class PlaylistGenerator {
} }
} 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;
@ -140,12 +140,7 @@ public class PlaylistGenerator {
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
public boolean accept(File dir, String name) {
return name.endsWith(".ts");
}
});
if (segments.length == 0) { 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) {
@ -164,4 +159,10 @@ public class PlaylistGenerator {
super(msg); super(msg);
} }
} }
public static class InvalidTrackLengthException extends RuntimeException {
public InvalidTrackLengthException(String msg) {
super(msg);
}
}
} }