use of org.opencastproject.mediapackage.MediaPackageElementFlavor in project opencast by opencast.
the class NormalizeAudioWorkflowOperationHandlerTest method testAudioContainer.
@Test
public void testAudioContainer() throws Exception {
operationInstance.setConfiguration("source-tags", "");
operationInstance.setConfiguration("source-flavor", "*/container-audio");
operationInstance.setConfiguration("source-flavors", "");
operationInstance.setConfiguration("target-flavor", "*/normalized");
operationInstance.setConfiguration("target-tags", "norm");
operationInstance.setConfiguration("force-transcode", "true");
operationInstance.setConfiguration("target-decibel", "-30");
WorkflowOperationResult result = operationHandler.start(instance, null);
Assert.assertEquals(Action.CONTINUE, result.getAction());
Assert.assertEquals("Resulting mediapackage has the wrong number of tracks", 4, result.getMediaPackage().getElements().length);
Track[] tracks = result.getMediaPackage().getTracks(new MediaPackageElementFlavor("presentation", "normalized"));
Assert.assertEquals("Resulting mediapackage has the wrong number of tracks", 1, tracks.length);
Assert.assertTrue(tracks[0].containsTag("norm"));
TrackImpl audioVideo = (TrackImpl) tracks[0];
Assert.assertEquals(-30f, audioVideo.getAudio().get(0).getRmsLevDb().floatValue(), 0.001d);
tracks = result.getMediaPackage().getTracks(new MediaPackageElementFlavor("presentation", "container-audio"));
Assert.assertEquals("Resulting mediapackage has the wrong number of tracks", 1, tracks.length);
audioVideo = (TrackImpl) tracks[0];
Assert.assertNull(audioVideo.getAudio().get(0).getRmsLevDb());
}
use of org.opencastproject.mediapackage.MediaPackageElementFlavor in project opencast by opencast.
the class SmilServiceImplTest method testAddClip.
/**
* Test of addClip(s) methods, of class SmilServiceImpl.
*/
@Test
public void testAddClip() throws Exception {
TrackImpl videoTrack = new TrackImpl();
videoTrack.setIdentifier("track-1");
videoTrack.setFlavor(new MediaPackageElementFlavor("source", "presentation"));
videoTrack.setURI(new URI("http://hostname/video.mp4"));
videoTrack.addStream(new VideoStreamImpl());
videoTrack.setDuration(1000000000000L);
SmilResponse smilResponse = smilService.createNewSmil();
smilResponse = smilService.addParallel(smilResponse.getSmil());
SmilMediaContainer par = (SmilMediaContainer) smilResponse.getEntity();
// add video track into parallel element
smilResponse = smilService.addClip(smilResponse.getSmil(), par.getId(), videoTrack, 1000L, 1000000L);
// logger.info(smilResponse.getSmil().toXML());
SmilMediaObject media = null;
for (SmilObject entity : smilResponse.getEntities()) {
if (entity instanceof SmilMediaObject) {
media = (SmilMediaObject) entity;
break;
}
}
assertNotNull(media);
assertEquals(media.getId(), ((SmilMediaContainer) smilResponse.getSmil().getBody().getMediaElements().get(0)).getElements().get(0).getId());
assertTrue(media instanceof SmilMediaVideoImpl);
assertSame(((SmilMediaElement) media).getMediaType(), SmilMediaElement.MediaType.VIDEO);
// 1000 milliseconds = 1 second
assertEquals(1000L, ((SmilMediaElement) media).getClipBeginMS());
// duration is 1000000 milliseconds = 1000 soconds
// start + duration = 1s + 1000s = 1001s
assertEquals(1001000L, ((SmilMediaElement) media).getClipEndMS());
TrackImpl audioTrack = new TrackImpl();
audioTrack.setIdentifier("track-2");
audioTrack.setFlavor(new MediaPackageElementFlavor("source", "presenter"));
audioTrack.setURI(new URI("http://hostname/audio.mp3"));
audioTrack.addStream(new AudioStreamImpl());
audioTrack.setDuration(1000000000000L);
// add audio track into parallel element
smilResponse = smilService.addClip(smilResponse.getSmil(), par.getId(), audioTrack, 1000L, 1000000L);
// logger.info(smilResponse.getSmil().toXML());
media = null;
for (SmilObject entity : smilResponse.getEntities()) {
if (entity instanceof SmilMediaObject) {
media = (SmilMediaObject) entity;
break;
}
}
assertNotNull(media);
assertEquals(media.getId(), ((SmilMediaContainer) smilResponse.getSmil().getBody().getMediaElements().get(0)).getElements().get(1).getId());
assertTrue(media instanceof SmilMediaAudioImpl);
assertSame(((SmilMediaElement) media).getMediaType(), SmilMediaElement.MediaType.AUDIO);
// 1000 milliseconds = 1 second
assertEquals(1000L, ((SmilMediaElement) media).getClipBeginMS());
// duration is 1000000 milliseconds = 1000 soconds
// start + duration = 1s + 1000s = 1001s
assertEquals(1001000L, ((SmilMediaElement) media).getClipEndMS());
// add new par
smilResponse = smilService.addParallel(smilResponse.getSmil());
par = (SmilMediaContainer) smilResponse.getEntity();
// add tracks (as array) to par
smilResponse = smilService.addClips(smilResponse.getSmil(), par.getId(), new Track[] { audioTrack, videoTrack }, 15000L, 1000L);
// logger.info(smilResponse.getSmil().toXML());
assertSame(2, smilResponse.getEntitiesCount());
assertTrue(smilResponse.getEntities()[0] instanceof SmilMediaElement);
// get audio element
SmilMediaElement mediaElement = (SmilMediaElement) smilResponse.getEntities()[0];
assertTrue(mediaElement.getMediaType() == SmilMediaElement.MediaType.AUDIO);
// 15000ms = 15s
assertEquals(15000L, mediaElement.getClipBeginMS());
// start + duration = 15s + 1s = 16s
assertEquals(16000L, mediaElement.getClipEndMS());
// get video element
mediaElement = (SmilMediaElement) smilResponse.getEntities()[1];
assertTrue(mediaElement.getMediaType() == SmilMediaElement.MediaType.VIDEO);
// 15000ms = 15s
assertEquals(15000L, mediaElement.getClipBeginMS());
// start + duration = 15s + 1s = 16s
assertEquals(16000L, mediaElement.getClipEndMS());
}
use of org.opencastproject.mediapackage.MediaPackageElementFlavor in project opencast by opencast.
the class ToolsEndpoint method addSmilToArchive.
/**
* Adds the SMIL file as {@link Catalog} to the media package and sends the updated media package to the archive.
*
* @param mediaPackage
* the media package to at the SMIL catalog
* @param smil
* the SMIL catalog
* @return the updated media package
* @throws IOException
* if the SMIL catalog cannot be read or not be written to the archive
*/
MediaPackage addSmilToArchive(MediaPackage mediaPackage, final Smil smil) throws IOException {
MediaPackageElementFlavor mediaPackageElementFlavor = adminUIConfiguration.getSmilCatalogFlavor();
// set default catalog Id if there is none existing
String catalogId = smil.getId();
Catalog[] catalogs = mediaPackage.getCatalogs();
// get the first smil/cutting catalog-ID to overwrite it with new smil info
for (Catalog p : catalogs) {
if (p.getFlavor().matches(mediaPackageElementFlavor)) {
logger.debug("Set Idendifier for Smil-Catalog to: " + p.getIdentifier());
catalogId = p.getIdentifier();
break;
}
}
Catalog catalog = mediaPackage.getCatalog(catalogId);
URI smilURI;
try (InputStream is = IOUtils.toInputStream(smil.toXML(), "UTF-8")) {
smilURI = workspace.put(mediaPackage.getIdentifier().compact(), catalogId, TARGET_FILE_NAME, is);
} catch (SAXException e) {
logger.error("Error while serializing the SMIL catalog to XML: {}", e.getMessage());
throw new IOException(e);
} catch (JAXBException e) {
logger.error("Error while serializing the SMIL catalog to XML: {}", e.getMessage());
throw new IOException(e);
}
if (catalog == null) {
MediaPackageElementBuilder mpeBuilder = MediaPackageElementBuilderFactory.newInstance().newElementBuilder();
catalog = (Catalog) mpeBuilder.elementFromURI(smilURI, MediaPackageElement.Type.Catalog, adminUIConfiguration.getSmilCatalogFlavor());
mediaPackage.add(catalog);
}
catalog.setURI(smilURI);
catalog.setIdentifier(catalogId);
catalog.setMimeType(MimeTypes.XML);
for (String tag : adminUIConfiguration.getSmilCatalogTags()) {
catalog.addTag(tag);
}
// setting the URI to a new source so the checksum will most like be invalid
catalog.setChecksum(null);
try {
// FIXME SWITCHP-333: Start in new thread
assetManager.takeSnapshot(DEFAULT_OWNER, mediaPackage);
} catch (AssetManagerException e) {
logger.error("Error while adding the updated media package ({}) to the archive: {}", mediaPackage.getIdentifier(), e.getMessage());
throw new IOException(e);
}
return mediaPackage;
}
use of org.opencastproject.mediapackage.MediaPackageElementFlavor in project opencast by opencast.
the class AnimateWorkflowOperationHandler method start.
/**
* {@inheritDoc}
*
* @see
* org.opencastproject.workflow.api.WorkflowOperationHandler#start(org.opencastproject.workflow.api.WorkflowInstance,
* org.opencastproject.job.api.JobContext)
*/
@Override
public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
MediaPackage mediaPackage = workflowInstance.getMediaPackage();
logger.info("Start animate workflow operation for media package {}", mediaPackage);
WorkflowOperationInstance operation = workflowInstance.getCurrentOperation();
List<String> arguments;
// Check required options
final File animationFile = new File(StringUtils.trimToEmpty(operation.getConfiguration(ANIMATION_FILE_PROPERTY)));
if (!animationFile.isFile()) {
throw new WorkflowOperationException(String.format("Animation file `%s` does not exist", animationFile));
}
URI animation = animationFile.toURI();
final MediaPackageElementFlavor targetFlavor;
try {
targetFlavor = MediaPackageElementFlavor.parseFlavor(StringUtils.trimToNull(operation.getConfiguration(TARGET_FLAVOR_PROPERTY)));
} catch (IllegalArgumentException e) {
throw new WorkflowOperationException("Invalid target flavor", e);
}
// Get optional options
String targetTagsProperty = StringUtils.trimToNull(operation.getConfiguration(TARGET_TAGS_PROPERTY));
// Check if we have custom command line options
String cmd = operation.getConfiguration(COMMANDLINE_ARGUMENTS_PROPERTY);
if (StringUtils.isNotEmpty(cmd)) {
arguments = Arrays.asList(StringUtils.split(cmd));
} else {
// set default encoding
arguments = new ArrayList<>();
arguments.add("-t");
arguments.add("ffmpeg");
arguments.add("--video-codec");
arguments.add("libx264-lossless");
arguments.add("--video-bitrate");
arguments.add("10000");
addArgumentIfExists(operation, arguments, WIDTH_PROPERTY, "-w");
addArgumentIfExists(operation, arguments, HEIGHT_PROPERTY, "-h");
addArgumentIfExists(operation, arguments, FPS_PROPERTY, "--fps");
}
final Map<String, String> metadata = getMetadata(mediaPackage);
Job job;
try {
job = animateService.animate(animation, metadata, arguments);
} catch (AnimateServiceException e) {
throw new WorkflowOperationException(String.format("Rendering animation from '%s' in media package '%s' failed", animation, mediaPackage), e);
}
if (!waitForStatus(job).isSuccess()) {
throw new WorkflowOperationException(String.format("Animate job for media package '%s' failed", mediaPackage));
}
// put animated clip into media package
try {
URI output = new URI(job.getPayload());
String id = UUID.randomUUID().toString();
InputStream in = workspace.read(output);
URI uri = workspace.put(mediaPackage.getIdentifier().toString(), id, FilenameUtils.getName(output.getPath()), in);
TrackImpl track = new TrackImpl();
track.setIdentifier(id);
track.setFlavor(targetFlavor);
track.setURI(uri);
Job inspection = mediaInspectionService.enrich(track, true);
if (!waitForStatus(inspection).isSuccess()) {
throw new AnimateServiceException(String.format("Animating %s failed", animation));
}
track = (TrackImpl) MediaPackageElementParser.getFromXml(inspection.getPayload());
// add track to media package
for (String tag : asList(targetTagsProperty)) {
track.addTag(tag);
}
mediaPackage.add(track);
workspace.delete(output);
} catch (Exception e) {
throw new WorkflowOperationException("Error handling animation service output", e);
}
try {
workspace.cleanup(mediaPackage.getIdentifier());
} catch (IOException e) {
throw new WorkflowOperationException(e);
}
logger.info("Animate workflow operation for media package {} completed", mediaPackage);
return createResult(mediaPackage, WorkflowOperationResult.Action.CONTINUE);
}
use of org.opencastproject.mediapackage.MediaPackageElementFlavor in project opencast by opencast.
the class AnimateWorkflowOperationHandler method getMetadata.
private Map<String, String> getMetadata(MediaPackage mediaPackage) {
Map<String, String> metadata = new HashMap<>();
// get episode metadata
MediaPackageElementFlavor[] flavors = { MediaPackageElements.EPISODE, MediaPackageElements.SERIES };
for (MediaPackageElementFlavor flavor : flavors) {
// Get metadata catalogs
for (Catalog catalog : mediaPackage.getCatalogs(flavor)) {
DublinCoreCatalog dc = DublinCoreUtil.loadDublinCore(workspace, catalog);
for (Map.Entry<EName, List<DublinCoreValue>> entry : dc.getValues().entrySet()) {
String key = String.format("%s.%s", flavor.getSubtype(), entry.getKey().getLocalName());
String value = entry.getValue().get(0).getValue();
metadata.put(key, value);
logger.debug("metadata: {} -> {}", key, value);
}
}
}
return metadata;
}
Aggregations