forked from j62/ctbrec
1
0
Fork 0

Add try-with-resources blocks to free up the HLS playlist file handle

This commit is contained in:
0xboobface 2019-12-22 15:01:49 +01:00
parent 9df30b3342
commit 5b9b91d6e8
2 changed files with 29 additions and 25 deletions

View File

@ -135,23 +135,25 @@ public class PlaylistGenerator {
public void validate(File recDir) throws IOException, ParseException, PlaylistException {
File playlist = new File(recDir, "playlist.m3u8");
if(playlist.exists()) {
PlaylistParser playlistParser = new PlaylistParser(new FileInputStream(playlist), 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) {
throw new InvalidPlaylistException("No segments found. Playlist is empty");
} 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");
}
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) {
throw new InvalidPlaylistException("No segments found. Playlist is empty");
} 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");
}
}
} else {
throw new FileNotFoundException(playlist.getAbsolutePath() + " does not exist");
}

View File

@ -332,14 +332,16 @@ public class HlsDownload extends AbstractHlsDownload {
private double getPlaylistLength(File playlist) throws IOException, ParseException, PlaylistException {
if (playlist.exists()) {
PlaylistParser playlistParser = new PlaylistParser(new FileInputStream(playlist), Format.EXT_M3U, Encoding.UTF_8, ParsingMode.LENIENT);
Playlist m3u = playlistParser.parse();
MediaPlaylist mediaPlaylist = m3u.getMediaPlaylist();
double length = 0;
for (TrackData trackData : mediaPlaylist.getTracks()) {
length += trackData.getTrackInfo().duration;
}
return length;
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();
double length = 0;
for (TrackData trackData : mediaPlaylist.getTracks()) {
length += trackData.getTrackInfo().duration;
}
return length;
}
} else {
throw new FileNotFoundException(playlist.getAbsolutePath() + " does not exist");
}