Search in sources :

Example 1 with CaptionConverter

use of org.opencastproject.caption.api.CaptionConverter 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;
}
Also used : CaptionConverterException(org.opencastproject.caption.api.CaptionConverterException) UnsupportedCaptionFormatException(org.opencastproject.caption.api.UnsupportedCaptionFormatException) FileNotFoundException(java.io.FileNotFoundException) NotFoundException(org.opencastproject.util.NotFoundException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) CaptionConverter(org.opencastproject.caption.api.CaptionConverter) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 2 with CaptionConverter

use of org.opencastproject.caption.api.CaptionConverter 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);
        }
    }
}
Also used : CaptionConverterException(org.opencastproject.caption.api.CaptionConverterException) UnsupportedCaptionFormatException(org.opencastproject.caption.api.UnsupportedCaptionFormatException) NotFoundException(org.opencastproject.util.NotFoundException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) URI(java.net.URI) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) Caption(org.opencastproject.caption.api.Caption) CaptionConverterException(org.opencastproject.caption.api.CaptionConverterException) ConfigurationException(org.osgi.service.cm.ConfigurationException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedCaptionFormatException(org.opencastproject.caption.api.UnsupportedCaptionFormatException) MediaPackageElementBuilder(org.opencastproject.mediapackage.MediaPackageElementBuilder) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) CaptionConverter(org.opencastproject.caption.api.CaptionConverter) File(java.io.File)

Example 3 with CaptionConverter

use of org.opencastproject.caption.api.CaptionConverter 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);
}
Also used : UnsupportedCaptionFormatException(org.opencastproject.caption.api.UnsupportedCaptionFormatException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) CaptionConverter(org.opencastproject.caption.api.CaptionConverter)

Example 4 with CaptionConverter

use of org.opencastproject.caption.api.CaptionConverter in project opencast by opencast.

the class CaptionServiceImpl method getCaptionConverter.

/**
 * Returns specific {@link CaptionConverter}. Registry is searched based on formatName, so in order for
 * {@link CaptionConverter} to be found, it has to have <code>caption.format</code> property set with
 * {@link CaptionConverter} format. If none is found, null is returned, if more than one is found then the first
 * reference is returned.
 *
 * @param formatName
 *          name of the caption format
 * @return {@link CaptionConverter} or null if none is found
 */
protected CaptionConverter getCaptionConverter(String formatName) {
    ServiceReference[] ref = null;
    try {
        ref = componentContext.getBundleContext().getServiceReferences(CaptionConverter.class.getName(), "(caption.format=" + formatName + ")");
    } catch (InvalidSyntaxException e) {
        throw new RuntimeException(e);
    }
    if (ref == null) {
        logger.warn("No caption format available for {}.", formatName);
        return null;
    }
    if (ref.length > 1)
        logger.warn("Multiple references for caption format {}! Returning first service reference.", formatName);
    CaptionConverter converter = (CaptionConverter) componentContext.getBundleContext().getService(ref[0]);
    return converter;
}
Also used : InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) CaptionConverter(org.opencastproject.caption.api.CaptionConverter) ServiceReference(org.osgi.framework.ServiceReference)

Example 5 with CaptionConverter

use of org.opencastproject.caption.api.CaptionConverter in project opencast by opencast.

the class CaptionServiceImpl method getAvailableCaptionConverters.

/**
 * Returns all registered CaptionFormats.
 */
protected HashMap<String, CaptionConverter> getAvailableCaptionConverters() {
    HashMap<String, CaptionConverter> captionConverters = new HashMap<String, CaptionConverter>();
    ServiceReference[] refs = null;
    try {
        refs = componentContext.getBundleContext().getServiceReferences(CaptionConverter.class.getName(), null);
    } catch (InvalidSyntaxException e) {
    // should not happen since it is called with null argument
    }
    if (refs != null) {
        for (ServiceReference ref : refs) {
            CaptionConverter converter = (CaptionConverter) componentContext.getBundleContext().getService(ref);
            String format = (String) ref.getProperty("caption.format");
            if (captionConverters.containsKey(format)) {
                logger.warn("Caption converter with format {} has already been registered. Ignoring second definition.", format);
            } else {
                captionConverters.put((String) ref.getProperty("caption.format"), converter);
            }
        }
    }
    return captionConverters;
}
Also used : HashMap(java.util.HashMap) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) CaptionConverter(org.opencastproject.caption.api.CaptionConverter) ServiceReference(org.osgi.framework.ServiceReference)

Aggregations

CaptionConverter (org.opencastproject.caption.api.CaptionConverter)6 UnsupportedCaptionFormatException (org.opencastproject.caption.api.UnsupportedCaptionFormatException)4 FileNotFoundException (java.io.FileNotFoundException)3 IOException (java.io.IOException)3 CaptionConverterException (org.opencastproject.caption.api.CaptionConverterException)3 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)3 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 Caption (org.opencastproject.caption.api.Caption)2 NotFoundException (org.opencastproject.util.NotFoundException)2 ServiceReference (org.osgi.framework.ServiceReference)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 URI (java.net.URI)1 HashMap (java.util.HashMap)1 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)1 MediaPackageElementBuilder (org.opencastproject.mediapackage.MediaPackageElementBuilder)1 MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)1 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)1 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)1