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"));
|
||||
if(files == null || files.length == 0) {
|
||||
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) -> {
|
||||
|
@ -57,22 +58,21 @@ public class PlaylistGenerator {
|
|||
for (File file : files) {
|
||||
try {
|
||||
float duration = (float) MpegUtil.getFileDuration(file);
|
||||
if(duration <= 0) {
|
||||
throw new RuntimeException("Track has negative duration: " + file.getName());
|
||||
if (duration <= 0) {
|
||||
throw new InvalidTrackLengthException("Track has negative duration: " + file.getName());
|
||||
} else {
|
||||
track.add(new TrackData.Builder()
|
||||
.withUri(file.getName())
|
||||
.withTrackInfo(new TrackInfo(duration, file.getName()))
|
||||
.build());
|
||||
}
|
||||
} catch(Exception e) {
|
||||
} 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;
|
||||
double percentage = (double) done / (double) total;
|
||||
updateProgressListeners(percentage);
|
||||
}
|
||||
|
||||
|
@ -93,7 +93,7 @@ public class PlaylistGenerator {
|
|||
|
||||
// write the playlist to a file
|
||||
File output = new File(directory, "playlist.m3u8");
|
||||
try(FileOutputStream fos = new FileOutputStream(output)) {
|
||||
try (FileOutputStream fos = new FileOutputStream(output)) {
|
||||
PlaylistWriter writer = new PlaylistWriter.Builder()
|
||||
.withFormat(Format.EXT_M3U)
|
||||
.withEncoding(Encoding.UTF_8)
|
||||
|
@ -106,8 +106,8 @@ public class PlaylistGenerator {
|
|||
}
|
||||
|
||||
private void updateProgressListeners(double percentage) {
|
||||
int p = (int) (percentage*100);
|
||||
if(p > lastPercentage) {
|
||||
int p = (int) (percentage * 100);
|
||||
if (p > lastPercentage) {
|
||||
for (ProgressListener progressListener : listeners) {
|
||||
progressListener.update(p);
|
||||
}
|
||||
|
@ -134,21 +134,16 @@ public class PlaylistGenerator {
|
|||
|
||||
public void validate(File recDir) throws IOException, ParseException, PlaylistException {
|
||||
File playlist = new File(recDir, "playlist.m3u8");
|
||||
if(playlist.exists()) {
|
||||
try(FileInputStream fin = new FileInputStream(playlist)) {
|
||||
if (playlist.exists()) {
|
||||
try (FileInputStream fin = new FileInputStream(playlist)) {
|
||||
PlaylistParser playlistParser = new PlaylistParser(fin, Format.EXT_M3U, Encoding.UTF_8, ParsingMode.LENIENT);
|
||||
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");
|
||||
}
|
||||
});
|
||||
if(segments.length == 0) {
|
||||
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) {
|
||||
} else if (segments.length != playlistSize) {
|
||||
throw new InvalidPlaylistException("Playlist size and amount of segments differ (" + segments.length + " != " + playlistSize + ")");
|
||||
} else {
|
||||
LOG.debug("Generated playlist looks good");
|
||||
|
@ -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