2018-09-23 15:12:23 -03:00
|
|
|
package us.shandian.giga.postprocessing;
|
|
|
|
|
2018-11-15 20:17:22 -03:00
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import org.schabi.newpipe.streams.io.SharpStream;
|
|
|
|
import org.schabi.newpipe.streams.SubtitleConverter;
|
|
|
|
import org.xml.sax.SAXException;
|
2018-09-23 15:12:23 -03:00
|
|
|
|
|
|
|
import java.io.IOException;
|
2018-11-15 20:17:22 -03:00
|
|
|
import java.text.ParseException;
|
|
|
|
|
|
|
|
import javax.xml.parsers.ParserConfigurationException;
|
|
|
|
import javax.xml.xpath.XPathExpressionException;
|
2018-09-23 15:12:23 -03:00
|
|
|
|
|
|
|
import us.shandian.giga.get.DownloadMission;
|
|
|
|
import us.shandian.giga.postprocessing.io.SharpInputStream;
|
2018-11-15 20:17:22 -03:00
|
|
|
|
2018-09-23 15:12:23 -03:00
|
|
|
/**
|
|
|
|
* @author kapodamy
|
|
|
|
*/
|
2019-01-21 01:30:03 -03:00
|
|
|
class TtmlConverter extends Postprocessing {
|
|
|
|
private static final String TAG = "TtmlConverter";
|
2018-09-23 15:12:23 -03:00
|
|
|
|
2019-01-21 01:30:03 -03:00
|
|
|
TtmlConverter(DownloadMission mission) {
|
|
|
|
// due how XmlPullParser works, the xml is fully loaded on the ram
|
|
|
|
super(mission, 0, true);
|
2018-09-23 15:12:23 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
int process(SharpStream out, SharpStream... sources) throws IOException {
|
|
|
|
// check if the subtitle is already in srt and copy, this should never happen
|
|
|
|
String format = getArgumentAt(0, null);
|
|
|
|
|
|
|
|
if (format == null || format.equals("ttml")) {
|
|
|
|
SubtitleConverter ttmlDumper = new SubtitleConverter();
|
|
|
|
|
2018-11-15 20:17:22 -03:00
|
|
|
try {
|
|
|
|
ttmlDumper.dumpTTML(
|
|
|
|
sources[0],
|
|
|
|
out,
|
|
|
|
getArgumentAt(1, "true").equals("true"),
|
|
|
|
getArgumentAt(2, "true").equals("true")
|
2019-01-21 01:30:03 -03:00
|
|
|
);
|
2018-11-15 20:17:22 -03:00
|
|
|
} catch (Exception err) {
|
|
|
|
Log.e(TAG, "subtitle parse failed", err);
|
2018-09-23 15:12:23 -03:00
|
|
|
|
2018-11-15 20:17:22 -03:00
|
|
|
if (err instanceof IOException) {
|
|
|
|
return 1;
|
|
|
|
} else if (err instanceof ParseException) {
|
|
|
|
return 2;
|
|
|
|
} else if (err instanceof SAXException) {
|
|
|
|
return 3;
|
|
|
|
} else if (err instanceof ParserConfigurationException) {
|
|
|
|
return 4;
|
|
|
|
} else if (err instanceof XPathExpressionException) {
|
|
|
|
return 7;
|
|
|
|
}
|
2019-01-21 01:30:03 -03:00
|
|
|
|
2018-11-15 20:17:22 -03:00
|
|
|
return 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK_RESULT;
|
2018-09-23 15:12:23 -03:00
|
|
|
} else if (format.equals("srt")) {
|
|
|
|
byte[] buffer = new byte[8 * 1024];
|
|
|
|
int read;
|
|
|
|
while ((read = sources[0].read(buffer)) > 0) {
|
|
|
|
out.write(buffer, 0, read);
|
|
|
|
}
|
|
|
|
return OK_RESULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new UnsupportedOperationException("Can't convert this subtitle, unimplemented format: " + format);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|