use of org.opencastproject.caption.api.UnsupportedCaptionFormatException in project opencast by opencast.
the class CaptionServiceImpl method getLanguageList.
/**
* {@inheritDoc}
*/
@Override
public String[] getLanguageList(MediaPackageElement input, String format) throws UnsupportedCaptionFormatException, CaptionConverterException {
if (format == null) {
throw new UnsupportedCaptionFormatException("<null>");
}
CaptionConverter converter = getCaptionConverter(format);
if (converter == null) {
throw new UnsupportedCaptionFormatException(format);
}
File captions;
try {
captions = workspace.get(input.getURI());
} catch (NotFoundException e) {
throw new CaptionConverterException("Requested media package element " + input + " could not be found.");
} catch (IOException e) {
throw new CaptionConverterException("Requested media package element " + input + "could not be accessed.");
}
FileInputStream stream = null;
String[] languageList;
try {
stream = new FileInputStream(captions);
languageList = converter.getLanguageList(stream);
} catch (FileNotFoundException e) {
throw new CaptionConverterException("Requested file " + captions + "could not be found.");
} finally {
IoSupport.closeQuietly(stream);
}
return languageList == null ? new String[0] : languageList;
}
use of org.opencastproject.caption.api.UnsupportedCaptionFormatException in project opencast by opencast.
the class CaptionServiceImpl method convert.
/**
* Converts the captions and returns them in a new catalog.
*
* @return the converted catalog
*/
protected MediaPackageElement convert(Job job, MediaPackageElement input, String inputFormat, String outputFormat, String language) throws UnsupportedCaptionFormatException, CaptionConverterException, MediaPackageException {
try {
// check parameters
if (input == null)
throw new IllegalArgumentException("Input element can't be null");
if (StringUtils.isBlank(inputFormat))
throw new IllegalArgumentException("Input format is null");
if (StringUtils.isBlank(outputFormat))
throw new IllegalArgumentException("Output format is null");
// get input file
File captionsFile;
try {
captionsFile = workspace.get(input.getURI());
} catch (NotFoundException e) {
throw new CaptionConverterException("Requested media package element " + input + " could not be found.");
} catch (IOException e) {
throw new CaptionConverterException("Requested media package element " + input + "could not be accessed.");
}
logger.debug("Atempting to convert from {} to {}...", inputFormat, outputFormat);
List<Caption> collection = null;
try {
collection = importCaptions(captionsFile, inputFormat, language);
logger.debug("Parsing to collection succeeded.");
} catch (UnsupportedCaptionFormatException e) {
throw new UnsupportedCaptionFormatException(inputFormat);
} catch (CaptionConverterException e) {
throw e;
}
URI exported;
try {
exported = exportCaptions(collection, job.getId() + "." + FilenameUtils.getExtension(captionsFile.getAbsolutePath()), outputFormat, language);
logger.debug("Exporting captions succeeding.");
} catch (UnsupportedCaptionFormatException e) {
throw new UnsupportedCaptionFormatException(outputFormat);
} catch (IOException e) {
throw new CaptionConverterException("Could not export caption collection.", e);
}
// create catalog and set properties
CaptionConverter converter = getCaptionConverter(outputFormat);
MediaPackageElementBuilder elementBuilder = MediaPackageElementBuilderFactory.newInstance().newElementBuilder();
MediaPackageElement mpe = elementBuilder.elementFromURI(exported, converter.getElementType(), new MediaPackageElementFlavor("captions", outputFormat + (language == null ? "" : "+" + language)));
if (mpe.getMimeType() == null) {
String[] mimetype = FileTypeMap.getDefaultFileTypeMap().getContentType(exported.getPath()).split("/");
mpe.setMimeType(mimeType(mimetype[0], mimetype[1]));
}
if (language != null)
mpe.addTag("lang:" + language);
return mpe;
} catch (Exception e) {
logger.warn("Error converting captions in " + input, e);
if (e instanceof CaptionConverterException) {
throw (CaptionConverterException) e;
} else if (e instanceof UnsupportedCaptionFormatException) {
throw (UnsupportedCaptionFormatException) e;
} else {
throw new CaptionConverterException(e);
}
}
}
use of org.opencastproject.caption.api.UnsupportedCaptionFormatException in project opencast by opencast.
the class CaptionServiceImpl method exportCaptions.
/**
* Exports captions {@link List} to specified format. Extension is added to exported file name. Throws
* {@link UnsupportedCaptionFormatException} if format is not supported.
*
* @param captions
* {@link {@link List} to be exported
* @param outputName
* name under which exported captions will be stored
* @param outputFormat
* format of exported collection
* @param language
* (optional) captions' language
* @throws UnsupportedCaptionFormatException
* if there is no registered engine for given format
* @return location of converted captions
* @throws IOException
* if exception occurs while writing to output stream
*/
private URI exportCaptions(List<Caption> captions, String outputName, String outputFormat, String language) throws UnsupportedCaptionFormatException, IOException {
CaptionConverter converter = getCaptionConverter(outputFormat);
if (converter == null) {
logger.error("No available caption format found for {}.", outputFormat);
throw new UnsupportedCaptionFormatException(outputFormat);
}
// TODO instead of first writing it all in memory, write it directly to disk
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
converter.exportCaption(outputStream, captions, language);
} catch (IOException e) {
// since we're writing to memory, this should not happen
}
ByteArrayInputStream in = new ByteArrayInputStream(outputStream.toByteArray());
return workspace.putInCollection(COLLECTION, outputName + "." + converter.getExtension(), in);
}
use of org.opencastproject.caption.api.UnsupportedCaptionFormatException in project opencast by opencast.
the class CaptionServiceRemoteImpl method convert.
/**
* @see org.opencastproject.caption.api.CaptionService#convert(MediaPackageElement, String, String, String)
*/
@Override
public Job convert(MediaPackageElement input, String inputFormat, String outputFormat, String language) throws UnsupportedCaptionFormatException, CaptionConverterException, MediaPackageException {
HttpPost post = new HttpPost("/convert");
try {
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
params.add(new BasicNameValuePair("captions", MediaPackageElementParser.getAsXml(input)));
params.add(new BasicNameValuePair("input", inputFormat));
params.add(new BasicNameValuePair("output", outputFormat));
if (StringUtils.isNotBlank(language))
params.add(new BasicNameValuePair("language", language));
post.setEntity(new UrlEncodedFormEntity(params));
} catch (Exception e) {
throw new CaptionConverterException(e);
}
HttpResponse response = null;
try {
response = getResponse(post);
if (response != null) {
String content = EntityUtils.toString(response.getEntity());
Job r = JobParser.parseJob(content);
logger.info("Converting job {} started on a remote caption service", r.getId());
return r;
}
} catch (Exception e) {
throw new CaptionConverterException("Unable to convert catalog " + input + " using a remote caption service", e);
} finally {
closeConnection(response);
}
throw new CaptionConverterException("Unable to convert catalog " + input + " using a remote caption service");
}
use of org.opencastproject.caption.api.UnsupportedCaptionFormatException in project opencast by opencast.
the class CaptionServiceImpl method importCaptions.
/**
* Imports captions using registered converter engine and specified language.
*
* @param input
* file containing captions
* @param inputFormat
* format of imported captions
* @param language
* (optional) captions' language
* @return {@link List} of parsed captions
* @throws UnsupportedCaptionFormatException
* if there is no registered engine for given format
* @throws IllegalCaptionFormatException
* if parser encounters exception
*/
private List<Caption> importCaptions(File input, String inputFormat, String language) throws UnsupportedCaptionFormatException, CaptionConverterException {
// get input format
CaptionConverter converter = getCaptionConverter(inputFormat);
if (converter == null) {
logger.error("No available caption format found for {}.", inputFormat);
throw new UnsupportedCaptionFormatException(inputFormat);
}
FileInputStream fileStream = null;
try {
fileStream = new FileInputStream(input);
List<Caption> collection = converter.importCaption(fileStream, language);
return collection;
} catch (FileNotFoundException e) {
throw new CaptionConverterException("Could not locate file " + input);
} finally {
IOUtils.closeQuietly(fileStream);
}
}
Aggregations