Search in sources :

Example 26 with MediaPackageElementFlavor

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());
}
Also used : TrackImpl(org.opencastproject.mediapackage.track.TrackImpl) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) WorkflowOperationResult(org.opencastproject.workflow.api.WorkflowOperationResult) Track(org.opencastproject.mediapackage.Track) Test(org.junit.Test)

Example 27 with MediaPackageElementFlavor

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());
}
Also used : SmilMediaVideoImpl(org.opencastproject.smil.entity.media.element.SmilMediaVideoImpl) TrackImpl(org.opencastproject.mediapackage.track.TrackImpl) SmilObject(org.opencastproject.smil.entity.api.SmilObject) AudioStreamImpl(org.opencastproject.mediapackage.track.AudioStreamImpl) VideoStreamImpl(org.opencastproject.mediapackage.track.VideoStreamImpl) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) URI(java.net.URI) SmilResponse(org.opencastproject.smil.api.SmilResponse) SmilMediaAudioImpl(org.opencastproject.smil.entity.media.element.SmilMediaAudioImpl) SmilMediaObject(org.opencastproject.smil.entity.media.api.SmilMediaObject) SmilMediaElement(org.opencastproject.smil.entity.media.element.api.SmilMediaElement) Track(org.opencastproject.mediapackage.Track) SmilMediaContainer(org.opencastproject.smil.entity.media.container.api.SmilMediaContainer) Test(org.junit.Test)

Example 28 with MediaPackageElementFlavor

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;
}
Also used : MediaPackageElementBuilder(org.opencastproject.mediapackage.MediaPackageElementBuilder) InputStream(java.io.InputStream) JAXBException(javax.xml.bind.JAXBException) IOException(java.io.IOException) AssetManagerException(org.opencastproject.assetmanager.api.AssetManagerException) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) URI(java.net.URI) Catalog(org.opencastproject.mediapackage.Catalog) SAXException(org.xml.sax.SAXException)

Example 29 with MediaPackageElementFlavor

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);
}
Also used : InputStream(java.io.InputStream) TrackImpl(org.opencastproject.mediapackage.track.TrackImpl) IOException(java.io.IOException) URI(java.net.URI) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) AnimateServiceException(org.opencastproject.animate.api.AnimateServiceException) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) IOException(java.io.IOException) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) AnimateServiceException(org.opencastproject.animate.api.AnimateServiceException) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) Job(org.opencastproject.job.api.Job) File(java.io.File)

Example 30 with MediaPackageElementFlavor

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;
}
Also used : EName(org.opencastproject.mediapackage.EName) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) DublinCoreCatalog(org.opencastproject.metadata.dublincore.DublinCoreCatalog) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) HashMap(java.util.HashMap) Map(java.util.Map) Catalog(org.opencastproject.mediapackage.Catalog) DublinCoreCatalog(org.opencastproject.metadata.dublincore.DublinCoreCatalog)

Aggregations

MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)109 MediaPackage (org.opencastproject.mediapackage.MediaPackage)49 Track (org.opencastproject.mediapackage.Track)34 URI (java.net.URI)31 Test (org.junit.Test)31 WorkflowOperationException (org.opencastproject.workflow.api.WorkflowOperationException)28 IOException (java.io.IOException)27 NotFoundException (org.opencastproject.util.NotFoundException)27 ArrayList (java.util.ArrayList)26 WorkflowOperationResult (org.opencastproject.workflow.api.WorkflowOperationResult)26 HashMap (java.util.HashMap)23 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)22 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)22 Job (org.opencastproject.job.api.Job)21 WorkflowOperationInstance (org.opencastproject.workflow.api.WorkflowOperationInstance)19 InputStream (java.io.InputStream)18 TrackImpl (org.opencastproject.mediapackage.track.TrackImpl)17 TrackSelector (org.opencastproject.mediapackage.selector.TrackSelector)16 File (java.io.File)13 Catalog (org.opencastproject.mediapackage.Catalog)13