Search in sources :

Example 6 with Time

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

the class Mpeg7CaptionConverter method exportCaption.

@Override
public void exportCaption(OutputStream outputStream, List<Caption> captions, String language) throws IOException {
    Mpeg7Catalog mpeg7 = Mpeg7CatalogImpl.newInstance();
    MediaTime mediaTime = new MediaTimeImpl(0, 0);
    Audio audioContent = mpeg7.addAudioContent("captions", mediaTime, null);
    @SuppressWarnings("unchecked") TemporalDecomposition<AudioSegment> captionDecomposition = (TemporalDecomposition<AudioSegment>) audioContent.getTemporalDecomposition();
    int segmentCount = 0;
    for (Caption caption : captions) {
        // Get all the words/parts for the transcript
        String[] words = caption.getCaption();
        if (words.length == 0)
            continue;
        // Create a new segment
        AudioSegment segment = captionDecomposition.createSegment("segment-" + segmentCount++);
        Time captionST = caption.getStartTime();
        Time captionET = caption.getStopTime();
        // Calculate start time
        Calendar startTime = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
        startTime.setTimeInMillis(0);
        startTime.add(Calendar.HOUR_OF_DAY, captionST.getHours());
        startTime.add(Calendar.MINUTE, captionST.getMinutes());
        startTime.add(Calendar.SECOND, captionST.getSeconds());
        startTime.add(Calendar.MILLISECOND, captionST.getMilliseconds());
        // Calculate end time
        Calendar endTime = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
        endTime.setTimeInMillis(0);
        endTime.add(Calendar.HOUR_OF_DAY, captionET.getHours());
        endTime.add(Calendar.MINUTE, captionET.getMinutes());
        endTime.add(Calendar.SECOND, captionET.getSeconds());
        endTime.add(Calendar.MILLISECOND, captionET.getMilliseconds());
        long startTimeInMillis = startTime.getTimeInMillis();
        long endTimeInMillis = endTime.getTimeInMillis();
        long duration = endTimeInMillis - startTimeInMillis;
        segment.setMediaTime(new MediaTimeImpl(startTimeInMillis, duration));
        TextAnnotation textAnnotation = segment.createTextAnnotation(0, 0, language);
        // Collect all the words in the segment
        StringBuffer captionLine = new StringBuffer();
        // Add each words/parts as segment to the catalog
        for (String word : words) {
            if (captionLine.length() > 0)
                captionLine.append(' ');
            captionLine.append(word);
        }
        // Append the text to the annotation
        textAnnotation.addFreeTextAnnotation(new FreeTextAnnotationImpl(captionLine.toString()));
    }
    Transformer tf = null;
    try {
        tf = TransformerFactory.newInstance().newTransformer();
        DOMSource xmlSource = new DOMSource(mpeg7.toXml());
        tf.transform(xmlSource, new StreamResult(outputStream));
    } catch (TransformerConfigurationException e) {
        logger.warn("Error serializing mpeg7 captions catalog: {}", e.getMessage());
        throw new IOException(e);
    } catch (TransformerFactoryConfigurationError e) {
        logger.warn("Error serializing mpeg7 captions catalog: {}", e.getMessage());
        throw new IOException(e);
    } catch (TransformerException e) {
        logger.warn("Error serializing mpeg7 captions catalog: {}", e.getMessage());
        throw new IOException(e);
    } catch (ParserConfigurationException e) {
        logger.warn("Error serializing mpeg7 captions catalog: {}", e.getMessage());
        throw new IOException(e);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) MediaTime(org.opencastproject.metadata.mpeg7.MediaTime) Time(org.opencastproject.caption.api.Time) MediaTimeImpl(org.opencastproject.metadata.mpeg7.MediaTimeImpl) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TextAnnotation(org.opencastproject.metadata.mpeg7.TextAnnotation) FreeTextAnnotation(org.opencastproject.metadata.mpeg7.FreeTextAnnotation) TransformerException(javax.xml.transform.TransformerException) TransformerFactoryConfigurationError(javax.xml.transform.TransformerFactoryConfigurationError) StreamResult(javax.xml.transform.stream.StreamResult) Calendar(java.util.Calendar) IOException(java.io.IOException) MediaTimePoint(org.opencastproject.metadata.mpeg7.MediaTimePoint) Caption(org.opencastproject.caption.api.Caption) Mpeg7Catalog(org.opencastproject.metadata.mpeg7.Mpeg7Catalog) MediaTime(org.opencastproject.metadata.mpeg7.MediaTime) FreeTextAnnotationImpl(org.opencastproject.metadata.mpeg7.FreeTextAnnotationImpl) TemporalDecomposition(org.opencastproject.metadata.mpeg7.TemporalDecomposition) Audio(org.opencastproject.metadata.mpeg7.Audio) AudioSegment(org.opencastproject.metadata.mpeg7.AudioSegment)

Aggregations

Time (org.opencastproject.caption.api.Time)6 Caption (org.opencastproject.caption.api.Caption)5 ArrayList (java.util.ArrayList)3 IllegalTimeFormatException (org.opencastproject.caption.api.IllegalTimeFormatException)3 CaptionImpl (org.opencastproject.caption.impl.CaptionImpl)3 TimeImpl (org.opencastproject.caption.impl.TimeImpl)3 IOException (java.io.IOException)2 Calendar (java.util.Calendar)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 CaptionConverterException (org.opencastproject.caption.api.CaptionConverterException)2 Audio (org.opencastproject.metadata.mpeg7.Audio)2 AudioSegment (org.opencastproject.metadata.mpeg7.AudioSegment)2 FreeTextAnnotation (org.opencastproject.metadata.mpeg7.FreeTextAnnotation)2 MediaTime (org.opencastproject.metadata.mpeg7.MediaTime)2 MediaTimeImpl (org.opencastproject.metadata.mpeg7.MediaTimeImpl)2 MediaTimePoint (org.opencastproject.metadata.mpeg7.MediaTimePoint)2 Mpeg7Catalog (org.opencastproject.metadata.mpeg7.Mpeg7Catalog)2 TemporalDecomposition (org.opencastproject.metadata.mpeg7.TemporalDecomposition)2 TextAnnotation (org.opencastproject.metadata.mpeg7.TextAnnotation)2 Iterator (java.util.Iterator)1