Search in sources :

Example 1 with Mpeg7CatalogImpl

use of org.opencastproject.metadata.mpeg7.Mpeg7CatalogImpl in project opencast by opencast.

the class Mpeg7CaptionConverter method importCaption.

/**
 * @see org.opencastproject.caption.api.CaptionConverter#importCaption(java.io.InputStream, java.lang.String)
 */
@SuppressWarnings("unchecked")
@Override
public List<Caption> importCaption(InputStream inputStream, String language) throws CaptionConverterException {
    List<Caption> captions = new ArrayList<Caption>();
    Mpeg7Catalog catalog = new Mpeg7CatalogImpl(inputStream);
    Iterator<Audio> audioContentIterator = catalog.audioContent();
    if (audioContentIterator == null)
        return captions;
    content: while (audioContentIterator.hasNext()) {
        Audio audioContent = audioContentIterator.next();
        TemporalDecomposition<AudioSegment> audioSegments = (TemporalDecomposition<AudioSegment>) audioContent.getTemporalDecomposition();
        Iterator<AudioSegment> audioSegmentIterator = audioSegments.segments();
        if (audioSegmentIterator == null)
            continue content;
        while (audioSegmentIterator.hasNext()) {
            AudioSegment segment = audioSegmentIterator.next();
            Iterator<TextAnnotation> annotationIterator = segment.textAnnotations();
            if (annotationIterator == null)
                continue content;
            while (annotationIterator.hasNext()) {
                TextAnnotation annotation = annotationIterator.next();
                if (!annotation.getLanguage().equals(language)) {
                    logger.debug("Skipping audio content '{}' because of language mismatch", audioContent.getId());
                    continue content;
                }
                List<String> captionLines = new ArrayList<String>();
                Iterator<FreeTextAnnotation> freeTextAnnotationIterator = annotation.freeTextAnnotations();
                if (freeTextAnnotationIterator == null)
                    continue;
                while (freeTextAnnotationIterator.hasNext()) {
                    FreeTextAnnotation freeTextAnnotation = freeTextAnnotationIterator.next();
                    captionLines.add(freeTextAnnotation.getText());
                }
                MediaTime segmentTime = segment.getMediaTime();
                MediaTimePoint stp = segmentTime.getMediaTimePoint();
                MediaDuration d = segmentTime.getMediaDuration();
                Calendar startCalendar = Calendar.getInstance();
                int millisAtStart = (int) (stp.getTimeInMilliseconds() - (((stp.getHour() * 60 + stp.getMinutes()) * 60 + stp.getSeconds()) * 1000));
                int millisAtEnd = (int) (d.getDurationInMilliseconds() - (((d.getHours() * 60 + d.getMinutes()) * 60 + d.getSeconds()) * 1000));
                startCalendar.set(Calendar.HOUR, stp.getHour());
                startCalendar.set(Calendar.MINUTE, stp.getMinutes());
                startCalendar.set(Calendar.SECOND, stp.getSeconds());
                startCalendar.set(Calendar.MILLISECOND, millisAtStart);
                startCalendar.add(Calendar.HOUR, d.getHours());
                startCalendar.add(Calendar.MINUTE, d.getMinutes());
                startCalendar.add(Calendar.SECOND, d.getSeconds());
                startCalendar.set(Calendar.MILLISECOND, millisAtEnd);
                try {
                    Time startTime = new TimeImpl(stp.getHour(), stp.getMinutes(), stp.getSeconds(), millisAtStart);
                    Time endTime = new TimeImpl(startCalendar.get(Calendar.HOUR), startCalendar.get(Calendar.MINUTE), startCalendar.get(Calendar.SECOND), startCalendar.get(Calendar.MILLISECOND));
                    Caption caption = new CaptionImpl(startTime, endTime, captionLines.toArray(new String[captionLines.size()]));
                    captions.add(caption);
                } catch (IllegalTimeFormatException e) {
                    logger.warn("Error setting caption time: {}", e.getMessage());
                }
            }
        }
    }
    return captions;
}
Also used : IllegalTimeFormatException(org.opencastproject.caption.api.IllegalTimeFormatException) MediaTimePoint(org.opencastproject.metadata.mpeg7.MediaTimePoint) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) MediaTime(org.opencastproject.metadata.mpeg7.MediaTime) Time(org.opencastproject.caption.api.Time) Caption(org.opencastproject.caption.api.Caption) FreeTextAnnotation(org.opencastproject.metadata.mpeg7.FreeTextAnnotation) TimeImpl(org.opencastproject.caption.impl.TimeImpl) MediaTimeImpl(org.opencastproject.metadata.mpeg7.MediaTimeImpl) Mpeg7Catalog(org.opencastproject.metadata.mpeg7.Mpeg7Catalog) CaptionImpl(org.opencastproject.caption.impl.CaptionImpl) MediaTime(org.opencastproject.metadata.mpeg7.MediaTime) Iterator(java.util.Iterator) MediaDuration(org.opencastproject.metadata.mpeg7.MediaDuration) Mpeg7CatalogImpl(org.opencastproject.metadata.mpeg7.Mpeg7CatalogImpl) ArrayList(java.util.ArrayList) List(java.util.List) TemporalDecomposition(org.opencastproject.metadata.mpeg7.TemporalDecomposition) Audio(org.opencastproject.metadata.mpeg7.Audio) TextAnnotation(org.opencastproject.metadata.mpeg7.TextAnnotation) FreeTextAnnotation(org.opencastproject.metadata.mpeg7.FreeTextAnnotation) AudioSegment(org.opencastproject.metadata.mpeg7.AudioSegment)

Example 2 with Mpeg7CatalogImpl

use of org.opencastproject.metadata.mpeg7.Mpeg7CatalogImpl in project opencast by opencast.

the class Mpeg7CaptionConverter method getLanguageList.

/**
 * @see org.opencastproject.caption.api.CaptionConverter#getLanguageList(java.io.InputStream)
 */
@SuppressWarnings("unchecked")
@Override
public String[] getLanguageList(InputStream inputStream) throws CaptionConverterException {
    Set<String> languages = new HashSet<String>();
    Mpeg7Catalog catalog = new Mpeg7CatalogImpl(inputStream);
    Iterator<Audio> audioContentIterator = catalog.audioContent();
    if (audioContentIterator == null)
        return languages.toArray(new String[languages.size()]);
    content: while (audioContentIterator.hasNext()) {
        Audio audioContent = audioContentIterator.next();
        TemporalDecomposition<AudioSegment> audioSegments = (TemporalDecomposition<AudioSegment>) audioContent.getTemporalDecomposition();
        Iterator<AudioSegment> audioSegmentIterator = audioSegments.segments();
        if (audioSegmentIterator == null)
            continue content;
        while (audioSegmentIterator.hasNext()) {
            AudioSegment segment = audioSegmentIterator.next();
            Iterator<TextAnnotation> annotationIterator = segment.textAnnotations();
            if (annotationIterator == null)
                continue content;
            while (annotationIterator.hasNext()) {
                TextAnnotation annotation = annotationIterator.next();
                String language = annotation.getLanguage();
                if (language != null)
                    languages.add(language);
            }
        }
    }
    return languages.toArray(new String[languages.size()]);
}
Also used : Mpeg7Catalog(org.opencastproject.metadata.mpeg7.Mpeg7Catalog) Iterator(java.util.Iterator) Mpeg7CatalogImpl(org.opencastproject.metadata.mpeg7.Mpeg7CatalogImpl) TemporalDecomposition(org.opencastproject.metadata.mpeg7.TemporalDecomposition) Audio(org.opencastproject.metadata.mpeg7.Audio) TextAnnotation(org.opencastproject.metadata.mpeg7.TextAnnotation) FreeTextAnnotation(org.opencastproject.metadata.mpeg7.FreeTextAnnotation) HashSet(java.util.HashSet) AudioSegment(org.opencastproject.metadata.mpeg7.AudioSegment)

Example 3 with Mpeg7CatalogImpl

use of org.opencastproject.metadata.mpeg7.Mpeg7CatalogImpl in project opencast by opencast.

the class VideoSegmenterTest method testAnalyzeOptimization.

@Test
public void testAnalyzeOptimization() throws Exception {
    Job receipt = vsegmenter1.segment(track1);
    JobBarrier jobBarrier = new JobBarrier(null, serviceRegistry1, 1000, receipt);
    jobBarrier.waitForJobs();
    Catalog catalog = (Catalog) MediaPackageElementParser.getFromXml(receipt.getPayload());
    Mpeg7Catalog mpeg7 = new Mpeg7CatalogImpl(catalog.getURI().toURL().openStream());
    // Is there multimedia content in the mpeg7?
    assertTrue("Audiovisual content was expected", mpeg7.hasVideoContent());
    assertNotNull("Audiovisual content expected", mpeg7.multimediaContent().next().elements().hasNext());
    MultimediaContentType contentType = mpeg7.multimediaContent().next().elements().next();
    // Is there at least one segment?
    TemporalDecomposition<? extends Segment> segments = contentType.getTemporalDecomposition();
    Iterator<? extends Segment> si = segments.segments();
    assertTrue(si.hasNext());
    // Is the error of optimization small enough?
    int segmentCounter = 0;
    for (; si.hasNext(); ++segmentCounter) {
        si.next();
    }
    float error = Math.abs((segmentCounter - vsegmenter1.prefNumber) / (float) vsegmenter1.prefNumber);
    assertTrue("Error of Optimization is too big", error <= vsegmenter1.maxError);
}
Also used : Mpeg7Catalog(org.opencastproject.metadata.mpeg7.Mpeg7Catalog) Mpeg7CatalogImpl(org.opencastproject.metadata.mpeg7.Mpeg7CatalogImpl) Job(org.opencastproject.job.api.Job) JobBarrier(org.opencastproject.job.api.JobBarrier) MultimediaContentType(org.opencastproject.metadata.mpeg7.MultimediaContentType) Catalog(org.opencastproject.mediapackage.Catalog) Mpeg7Catalog(org.opencastproject.metadata.mpeg7.Mpeg7Catalog) Test(org.junit.Test)

Example 4 with Mpeg7CatalogImpl

use of org.opencastproject.metadata.mpeg7.Mpeg7CatalogImpl in project opencast by opencast.

the class VideoSegmenterTest method testAnalyze.

@Test
public void testAnalyze() throws Exception {
    Job receipt = vsegmenter.segment(track);
    JobBarrier jobBarrier = new JobBarrier(null, serviceRegistry, 1000, receipt);
    jobBarrier.waitForJobs();
    Catalog catalog = (Catalog) MediaPackageElementParser.getFromXml(receipt.getPayload());
    Mpeg7Catalog mpeg7 = new Mpeg7CatalogImpl(catalog.getURI().toURL().openStream());
    // Is there multimedia content in the mpeg7?
    assertTrue("Audiovisual content was expected", mpeg7.hasVideoContent());
    assertNotNull("Audiovisual content expected", mpeg7.multimediaContent().next().elements().hasNext());
    MultimediaContentType contentType = mpeg7.multimediaContent().next().elements().next();
    // Is there at least one segment?
    TemporalDecomposition<? extends Segment> segments = contentType.getTemporalDecomposition();
    Iterator<? extends Segment> si = segments.segments();
    assertTrue(si.hasNext());
    Segment firstSegment = si.next();
    MediaTime firstSegmentMediaTime = firstSegment.getMediaTime();
    long startTime = firstSegmentMediaTime.getMediaTimePoint().getTimeInMilliseconds();
    long duration = firstSegmentMediaTime.getMediaDuration().getDurationInMilliseconds();
    assertEquals("Unexpected start time of first segment", 0, startTime);
    assertEquals("Unexpected duration of first segment", firstSegmentDuration, duration);
    // What about the second one?
    assertTrue("Video is expected to have more than one segment", si.hasNext());
    Segment secondSegment = si.next();
    MediaTime secondSegmentMediaTime = secondSegment.getMediaTime();
    startTime = secondSegmentMediaTime.getMediaTimePoint().getTimeInMilliseconds();
    duration = secondSegmentMediaTime.getMediaDuration().getDurationInMilliseconds();
    assertEquals("Unexpected start time of second segment", firstSegmentDuration, startTime);
    assertEquals("Unexpected duration of second segment", secondSegmentDuration, duration);
    // There should be no third segment
    assertFalse("Found an unexpected third video segment", si.hasNext());
}
Also used : Mpeg7Catalog(org.opencastproject.metadata.mpeg7.Mpeg7Catalog) MediaTime(org.opencastproject.metadata.mpeg7.MediaTime) Mpeg7CatalogImpl(org.opencastproject.metadata.mpeg7.Mpeg7CatalogImpl) Job(org.opencastproject.job.api.Job) JobBarrier(org.opencastproject.job.api.JobBarrier) MultimediaContentType(org.opencastproject.metadata.mpeg7.MultimediaContentType) Catalog(org.opencastproject.mediapackage.Catalog) Mpeg7Catalog(org.opencastproject.metadata.mpeg7.Mpeg7Catalog) Segment(org.opencastproject.metadata.mpeg7.Segment) Test(org.junit.Test)

Example 5 with Mpeg7CatalogImpl

use of org.opencastproject.metadata.mpeg7.Mpeg7CatalogImpl in project opencast by opencast.

the class VideoSegmenterTest method testAnalyzeOptimizedList.

@Test
public void testAnalyzeOptimizedList() throws Exception {
    Job receipt = vsegmenter.segment(track);
    JobBarrier jobBarrier = new JobBarrier(null, serviceRegistry, 1000, receipt);
    jobBarrier.waitForJobs();
    Catalog catalog = (Catalog) MediaPackageElementParser.getFromXml(receipt.getPayload());
    Mpeg7Catalog mpeg7 = new Mpeg7CatalogImpl(catalog.getURI().toURL().openStream());
    List<OptimizationStep> optimizedList = new LinkedList<OptimizationStep>();
    OptimizationStep firstStep = new OptimizationStep(10, 0.015f, 46, 41, mpeg7, null);
    OptimizationStep secondStep = new OptimizationStep(10, 0.167f, 34, 41, mpeg7, null);
    OptimizationStep thirdStep = new OptimizationStep(10, 0.011f, 44, 41, mpeg7, null);
    OptimizationStep fourthStep = new OptimizationStep(10, 0.200f, 23, 41, mpeg7, null);
    // ~  0.122
    float error1 = (46 - 41) / (float) 41;
    // ~ -0.171
    float error2 = (34 - 41) / (float) 41;
    // ~  0.073
    float error3 = (44 - 41) / (float) 41;
    // ~ -0.439
    float error4 = (23 - 41) / (float) 41;
    optimizedList.add(firstStep);
    optimizedList.add(secondStep);
    optimizedList.add(thirdStep);
    optimizedList.add(fourthStep);
    Collections.sort(optimizedList);
    // check if the errors were calculated correctly and  whether the elements are in the correct order
    assertEquals("first element of optimized list incorrect", error3, optimizedList.get(0).getError(), 0.0001f);
    assertEquals("second element of optimized list incorrect", error1, optimizedList.get(1).getError(), 0.0001f);
    assertEquals("third element of optimized list incorrect", error4, optimizedList.get(2).getError(), 0.0001f);
    assertEquals("fourth element of optimized list incorrect", error2, optimizedList.get(3).getError(), 0.0001f);
    assertTrue("first error in optimized list is not positive", optimizedList.get(0).getError() >= 0);
    assertTrue("second error in optimized list is not bigger than first", optimizedList.get(1).getError() > optimizedList.get(0).getError());
    assertTrue("third error in optimized list is not negative", optimizedList.get(2).getError() < 0);
    assertTrue("fourth error in optimized list is smaller than third", optimizedList.get(3).getError() > optimizedList.get(2).getError());
}
Also used : Mpeg7Catalog(org.opencastproject.metadata.mpeg7.Mpeg7Catalog) Mpeg7CatalogImpl(org.opencastproject.metadata.mpeg7.Mpeg7CatalogImpl) Job(org.opencastproject.job.api.Job) JobBarrier(org.opencastproject.job.api.JobBarrier) Catalog(org.opencastproject.mediapackage.Catalog) Mpeg7Catalog(org.opencastproject.metadata.mpeg7.Mpeg7Catalog) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Aggregations

Mpeg7CatalogImpl (org.opencastproject.metadata.mpeg7.Mpeg7CatalogImpl)6 Mpeg7Catalog (org.opencastproject.metadata.mpeg7.Mpeg7Catalog)5 Catalog (org.opencastproject.mediapackage.Catalog)4 Test (org.junit.Test)3 Job (org.opencastproject.job.api.Job)3 JobBarrier (org.opencastproject.job.api.JobBarrier)3 MediaTime (org.opencastproject.metadata.mpeg7.MediaTime)3 TemporalDecomposition (org.opencastproject.metadata.mpeg7.TemporalDecomposition)3 Iterator (java.util.Iterator)2 Audio (org.opencastproject.metadata.mpeg7.Audio)2 AudioSegment (org.opencastproject.metadata.mpeg7.AudioSegment)2 FreeTextAnnotation (org.opencastproject.metadata.mpeg7.FreeTextAnnotation)2 MediaTimeImpl (org.opencastproject.metadata.mpeg7.MediaTimeImpl)2 MultimediaContentType (org.opencastproject.metadata.mpeg7.MultimediaContentType)2 TextAnnotation (org.opencastproject.metadata.mpeg7.TextAnnotation)2 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1