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;
}
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()]);
}
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);
}
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());
}
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());
}
Aggregations