GoogleSpeechCaptionConverter implements CaptionConverter { @Override public List<Caption> importCaption(InputStream inputStream, String languageLineSize) throws CaptionConverterException { List<Caption> captionList = new ArrayList<Caption>(); JSONParser jsonParser = new JSONParser(); int transcriptionLineSize = 0; try { transcriptionLineSize = Integer.parseInt(languageLineSize.trim()); logger.info("Transcripts line size {} used", transcriptionLineSize); } catch (NumberFormatException nfe) { transcriptionLineSize = LINE_SIZE; logger.info("Default transcripts line size {} used", transcriptionLineSize); } try { JSONObject outputObj = (JSONObject) jsonParser.parse(new InputStreamReader(inputStream)); String jobId = "Unknown"; if (outputObj.get("name") != null) { jobId = (String) outputObj.get("name"); } JSONObject responseObj = (JSONObject) outputObj.get("response"); JSONArray resultsArray = (JSONArray) responseObj.get("results"); resultsLoop: for (int i = 0; i < resultsArray.size(); i++) { JSONObject resultElement = (JSONObject) resultsArray.get(i); JSONArray alternativesArray = (JSONArray) resultElement.get("alternatives"); if (alternativesArray != null && alternativesArray.size() > 0) { JSONObject alternativeElement = (JSONObject) alternativesArray.get(0); String transcript = ((String) alternativeElement.get("transcript")).trim(); if (transcript != null) { JSONArray timestampsArray = (JSONArray) alternativeElement.get("words"); if (timestampsArray == null || timestampsArray.isEmpty()) { logger.warn("Could not build caption object for job {}, result index {}: timestamp data not found", jobId, i); continue; } String[] words = transcript.split("\\s+"); StringBuffer line = new StringBuffer(); int indexFirst = -1; int indexLast = -1; for (int j = 0; j < words.length; j++) { if (indexFirst == -1) { indexFirst = j; } line.append(words[j]); line.append(" "); if (line.length() >= transcriptionLineSize || j == words.length - 1) { indexLast = j; double start = -1; double end = -1; if (indexLast < timestampsArray.size()) { JSONObject wordTSList = (JSONObject) timestampsArray.get(indexFirst); if (wordTSList.size() == 3) { Number startNumber = NumberFormat.getInstance(Locale.US).parse(removeEndCharacter((wordTSList.get("startTime").toString()), "s")); start = startNumber.doubleValue(); } wordTSList = (JSONObject) timestampsArray.get(indexLast); if (wordTSList.size() == 3) { Number endNumber = NumberFormat.getInstance(Locale.US).parse(removeEndCharacter((wordTSList.get("endTime").toString()), "s")); end = endNumber.doubleValue(); } } if (start == -1 || end == -1) { logger.warn("Could not build caption object for job {}, result index {}: start/end times not found", jobId, i); continue resultsLoop; } String[] captionLines = new String[1]; captionLines[0] = line.toString().replace("%HESITATION", "..."); captionList.add(new CaptionImpl(buildTime((long) (start * 1000)), buildTime((long) (end * 1000)), captionLines)); indexFirst = -1; indexLast = -1; line.setLength(0); } } } } } } catch (Exception e) { logger.warn("Error when parsing Google transcriptions result: {}" + e.getMessage()); throw new CaptionConverterException(e); } return captionList; }  @Override List<Caption> importCaption(InputStream inputStream, String languageLineSize); @Override void exportCaption(OutputStream outputStream, List<Caption> captions, String language); @Override String[] getLanguageList(InputStream inputStream); @Override String getExtension(); @Override Type getElementType();  }
@Test public void testImportCaption() throws Exception { inputStream = GoogleSpeechCaptionConverterTest.class.getResourceAsStream("/pulled_google_transcription.json"); importCaption(); }