Made segment merger more robust
This commit is contained in:
parent
5efee697b2
commit
ebe717f0e8
|
@ -6,6 +6,9 @@ import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
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;
|
||||||
|
@ -17,6 +20,8 @@ import com.iheartradio.m3u8.data.TrackData;
|
||||||
|
|
||||||
|
|
||||||
public class SegmentMerger {
|
public class SegmentMerger {
|
||||||
|
private static final transient Logger LOG = LoggerFactory.getLogger(SegmentMerger.class);
|
||||||
|
|
||||||
private int lastPercentage;
|
private int lastPercentage;
|
||||||
|
|
||||||
public void merge(File recDir, File targetFile) throws IOException, ParseException, PlaylistException {
|
public void merge(File recDir, File targetFile) throws IOException, ParseException, PlaylistException {
|
||||||
|
@ -25,6 +30,11 @@ public class SegmentMerger {
|
||||||
}
|
}
|
||||||
|
|
||||||
File playlistFile = new File(recDir, "playlist.m3u8");
|
File playlistFile = new File(recDir, "playlist.m3u8");
|
||||||
|
if(!playlistFile.exists()) {
|
||||||
|
LOG.warn("Couldn't merge segments. Playlist {} does not exist", playlistFile);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try (FileInputStream fin = new FileInputStream(playlistFile); FileOutputStream fos = new FileOutputStream(targetFile)) {
|
try (FileInputStream fin = new FileInputStream(playlistFile); FileOutputStream fos = new FileOutputStream(targetFile)) {
|
||||||
PlaylistParser parser = new PlaylistParser(fin, Format.EXT_M3U, Encoding.UTF_8);
|
PlaylistParser parser = new PlaylistParser(fin, Format.EXT_M3U, Encoding.UTF_8);
|
||||||
Playlist playlist = parser.parse();
|
Playlist playlist = parser.parse();
|
||||||
|
@ -33,6 +43,7 @@ public class SegmentMerger {
|
||||||
for (int i = 0; i < tracks.size(); i++) {
|
for (int i = 0; i < tracks.size(); i++) {
|
||||||
TrackData trackData = tracks.get(i);
|
TrackData trackData = tracks.get(i);
|
||||||
File segment = new File(recDir, trackData.getUri());
|
File segment = new File(recDir, trackData.getUri());
|
||||||
|
if(segment.exists()) {
|
||||||
try (FileInputStream segmentStream = new FileInputStream(segment)) {
|
try (FileInputStream segmentStream = new FileInputStream(segment)) {
|
||||||
int length = -1;
|
int length = -1;
|
||||||
byte[] b = new byte[1024 * 1024];
|
byte[] b = new byte[1024 * 1024];
|
||||||
|
@ -40,6 +51,9 @@ public class SegmentMerger {
|
||||||
fos.write(b, 0, length);
|
fos.write(b, 0, length);
|
||||||
}
|
}
|
||||||
lastPercentage = (int) (i * 100.0 / tracks.size());
|
lastPercentage = (int) (i * 100.0 / tracks.size());
|
||||||
|
} catch(Exception e) {
|
||||||
|
LOG.error("Couldn't append segment {} to merged file {}", segment.getName(), targetFile.getName(), e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue