Search in sources :

Example 11 with TrackSelector

use of org.opencastproject.mediapackage.selector.TrackSelector in project opencast by opencast.

the class VideoEditorWorkflowOperationHandler method resume.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.workflow.api.ResumableWorkflowOperationHandler#resume(org.opencastproject.workflow.api.WorkflowInstance,
 *      JobContext, java.util.Map)
 */
@Override
public WorkflowOperationResult resume(WorkflowInstance workflowInstance, JobContext context, Map<String, String> properties) throws WorkflowOperationException {
    MediaPackage mp = workflowInstance.getMediaPackage();
    logger.info("Resume video editor operation for mediapackage {}", mp.getIdentifier().compact());
    // Get configuration
    WorkflowOperationInstance worflowOperationInstance = workflowInstance.getCurrentOperation();
    String sourceTrackFlavorsProperty = StringUtils.trimToNull(worflowOperationInstance.getConfiguration(SOURCE_FLAVORS_PROPERTY));
    if (sourceTrackFlavorsProperty == null) {
        throw new WorkflowOperationException(format("Required configuration property %s not set.", SOURCE_FLAVORS_PROPERTY));
    }
    String targetSmilFlavorProperty = StringUtils.trimToNull(worflowOperationInstance.getConfiguration(TARGET_SMIL_FLAVOR_PROPERTY));
    if (targetSmilFlavorProperty == null) {
        throw new WorkflowOperationException(format("Required configuration property %s not set.", TARGET_SMIL_FLAVOR_PROPERTY));
    }
    String targetFlavorSybTypeProperty = StringUtils.trimToNull(worflowOperationInstance.getConfiguration(TARGET_FLAVOR_SUBTYPE_PROPERTY));
    if (targetFlavorSybTypeProperty == null) {
        throw new WorkflowOperationException(format("Required configuration property %s not set.", TARGET_FLAVOR_SUBTYPE_PROPERTY));
    }
    boolean skipIfNoTrim = BooleanUtils.toBoolean(worflowOperationInstance.getConfiguration(SKIP_NOT_TRIMMED_PROPERTY));
    // Get source tracks
    TrackSelector trackSelector = new TrackSelector();
    for (String flavor : asList(sourceTrackFlavorsProperty)) {
        trackSelector.addFlavor(flavor);
    }
    Collection<Track> sourceTracks = trackSelector.select(mp, false);
    if (sourceTracks.isEmpty()) {
        throw new WorkflowOperationException(format("No source tracks found in mediapacksge %s with flavors %s.", mp.getIdentifier().compact(), sourceTrackFlavorsProperty));
    }
    // Get SMIL file
    MediaPackageElementFlavor smilTargetFlavor = MediaPackageElementFlavor.parseFlavor(targetSmilFlavorProperty);
    Catalog[] smilCatalogs = mp.getCatalogs(smilTargetFlavor);
    if (smilCatalogs == null || smilCatalogs.length == 0) {
        throw new WorkflowOperationException(format("No SMIL catalog found in mediapackage %s with flavor %s.", mp.getIdentifier().compact(), targetSmilFlavorProperty));
    }
    File smilFile = null;
    Smil smil = null;
    try {
        smilFile = workspace.get(smilCatalogs[0].getURI());
        smil = smilService.fromXml(smilFile).getSmil();
        smil = replaceAllTracksWith(smil, sourceTracks.toArray(new Track[sourceTracks.size()]));
        InputStream is = null;
        try {
            is = IOUtils.toInputStream(smil.toXML(), "UTF-8");
            // Remove old SMIL
            workspace.delete(mp.getIdentifier().compact(), smilCatalogs[0].getIdentifier());
            mp.remove(smilCatalogs[0]);
            // put modified SMIL into workspace
            URI newSmilUri = workspace.put(mp.getIdentifier().compact(), smil.getId(), SMIL_FILE_NAME, is);
            Catalog catalog = (Catalog) MediaPackageElementBuilderFactory.newInstance().newElementBuilder().elementFromURI(newSmilUri, MediaPackageElement.Type.Catalog, smilCatalogs[0].getFlavor());
            catalog.setIdentifier(smil.getId());
            mp.add(catalog);
        } catch (Exception ex) {
            throw new WorkflowOperationException(ex);
        } finally {
            IOUtils.closeQuietly(is);
        }
    } catch (NotFoundException ex) {
        throw new WorkflowOperationException(format("Failed to get SMIL catalog %s from mediapackage %s.", smilCatalogs[0].getIdentifier(), mp.getIdentifier().compact()), ex);
    } catch (IOException ex) {
        throw new WorkflowOperationException(format("Can't open SMIL catalog %s from mediapackage %s.", smilCatalogs[0].getIdentifier(), mp.getIdentifier().compact()), ex);
    } catch (SmilException ex) {
        throw new WorkflowOperationException(ex);
    }
    if (skipIfNoTrim) {
        // We should not modify the SMIL file as we traverse through its elements, so we make a copy and modify it instead
        try {
            Smil filteredSmil = smilService.fromXml(smil.toXML()).getSmil();
            for (SmilMediaObject element : smil.getBody().getMediaElements()) {
                // body should contain par elements
                if (element.isContainer()) {
                    SmilMediaContainer container = (SmilMediaContainer) element;
                    if (SmilMediaContainer.ContainerType.PAR == container.getContainerType()) {
                        continue;
                    }
                }
                filteredSmil = smilService.removeSmilElement(filteredSmil, element.getId()).getSmil();
            }
            // one that takes the whole video size
            switch(filteredSmil.getBody().getMediaElements().size()) {
                case 0:
                    logger.info("Skipping SMIL job generation for mediapackage '{}', " + "because the SMIL does not define any trimming points", mp.getIdentifier());
                    return skip(workflowInstance, context);
                case 1:
                    // component represents the whole duration or not, therefore we don't bother to try
                    if (mp.getDuration() < 0)
                        break;
                    SmilMediaContainer parElement = (SmilMediaContainer) filteredSmil.getBody().getMediaElements().get(0);
                    boolean skip = true;
                    for (SmilMediaObject elementChild : parElement.getElements()) {
                        if (!elementChild.isContainer()) {
                            SmilMediaElement media = (SmilMediaElement) elementChild;
                            // If they don't represent the whole length, then we break --we have a trimming point
                            if ((media.getClipBeginMS() != 0) || (media.getClipEndMS() != mp.getDuration())) {
                                skip = false;
                                break;
                            }
                        }
                    }
                    if (skip) {
                        logger.info("Skipping SMIL job generation for mediapackage '{}', " + "because the trimming points in the SMIL correspond " + "to the beginning and the end of the video", mp.getIdentifier());
                        return skip(workflowInstance, context);
                    }
                    break;
                default:
                    break;
            }
        } catch (MalformedURLException | SmilException | JAXBException | SAXException e) {
            logger.warn("Error parsing input SMIL to determine if it has trimpoints. " + "We will assume it does and go on creating jobs.");
        }
    }
    // Create video edit jobs and run them
    List<Job> jobs = null;
    try {
        logger.info("Create processing jobs for SMIL file: {}", smilCatalogs[0].getIdentifier());
        jobs = videoEditorService.processSmil(smil);
        if (!waitForStatus(jobs.toArray(new Job[jobs.size()])).isSuccess()) {
            throw new WorkflowOperationException(format("Processing SMIL file failed: %s", smilCatalogs[0].getIdentifier()));
        }
        logger.info("Finished processing of SMIL file: {}", smilCatalogs[0].getIdentifier());
    } catch (ProcessFailedException ex) {
        throw new WorkflowOperationException(format("Finished processing of SMIL file: %s", smilCatalogs[0].getIdentifier()), ex);
    }
    // Move edited tracks to work location and set target flavor
    Track editedTrack = null;
    boolean mpAdded = false;
    for (Job job : jobs) {
        try {
            editedTrack = (Track) MediaPackageElementParser.getFromXml(job.getPayload());
            MediaPackageElementFlavor editedTrackFlavor = editedTrack.getFlavor();
            editedTrack.setFlavor(new MediaPackageElementFlavor(editedTrackFlavor.getType(), targetFlavorSybTypeProperty));
            URI editedTrackNewUri = workspace.moveTo(editedTrack.getURI(), mp.getIdentifier().compact(), editedTrack.getIdentifier(), FilenameUtils.getName(editedTrack.getURI().toString()));
            editedTrack.setURI(editedTrackNewUri);
            for (Track track : sourceTracks) {
                if (track.getFlavor().getType().equals(editedTrackFlavor.getType())) {
                    mp.addDerived(editedTrack, track);
                    mpAdded = true;
                    break;
                }
            }
            if (!mpAdded) {
                mp.add(editedTrack);
            }
        } catch (MediaPackageException ex) {
            throw new WorkflowOperationException("Failed to get information about the edited track(s)", ex);
        } catch (NotFoundException | IOException | IllegalArgumentException ex) {
            throw new WorkflowOperationException("Moving edited track to work location failed.", ex);
        } catch (Exception ex) {
            throw new WorkflowOperationException(ex);
        }
    }
    logger.info("VideoEdit workflow {} finished", workflowInstance.getId());
    return createResult(mp, Action.CONTINUE);
}
Also used : MalformedURLException(java.net.MalformedURLException) TrackSelector(org.opencastproject.mediapackage.selector.TrackSelector) NotFoundException(org.opencastproject.util.NotFoundException) URI(java.net.URI) SAXException(org.xml.sax.SAXException) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) SmilException(org.opencastproject.smil.api.SmilException) SmilMediaObject(org.opencastproject.smil.entity.media.api.SmilMediaObject) Job(org.opencastproject.job.api.Job) SmilMediaContainer(org.opencastproject.smil.entity.media.container.api.SmilMediaContainer) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) InputStream(java.io.InputStream) JAXBException(javax.xml.bind.JAXBException) IOException(java.io.IOException) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) Catalog(org.opencastproject.mediapackage.Catalog) SmilException(org.opencastproject.smil.api.SmilException) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) JAXBException(javax.xml.bind.JAXBException) SAXException(org.xml.sax.SAXException) ProcessFailedException(org.opencastproject.videoeditor.api.ProcessFailedException) NotFoundException(org.opencastproject.util.NotFoundException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) MediaPackage(org.opencastproject.mediapackage.MediaPackage) Smil(org.opencastproject.smil.entity.api.Smil) SmilMediaElement(org.opencastproject.smil.entity.media.element.api.SmilMediaElement) ProcessFailedException(org.opencastproject.videoeditor.api.ProcessFailedException) File(java.io.File) Track(org.opencastproject.mediapackage.Track)

Example 12 with TrackSelector

use of org.opencastproject.mediapackage.selector.TrackSelector in project opencast by opencast.

the class TrackSelectorTest method testOnlyTagsAndSelect.

@Test
public void testOnlyTagsAndSelect() {
    TrackSelector tagsSelector = new TrackSelector();
    tagsSelector.addTag(tag);
    assertEquals(1, tagsSelector.select(mediaPackage, true).size());
}
Also used : TrackSelector(org.opencastproject.mediapackage.selector.TrackSelector) Test(org.junit.Test)

Example 13 with TrackSelector

use of org.opencastproject.mediapackage.selector.TrackSelector in project opencast by opencast.

the class TrackSelectorTest method testTagsAndFlavorSelect.

@Test
public void testTagsAndFlavorSelect() {
    TrackSelector tagsSelector = new TrackSelector();
    tagsSelector.addTag(tag);
    tagsSelector.addFlavor(PRESENTATION_SOURCE);
    assertEquals(1, tagsSelector.select(mediaPackage, true).size());
}
Also used : TrackSelector(org.opencastproject.mediapackage.selector.TrackSelector) Test(org.junit.Test)

Example 14 with TrackSelector

use of org.opencastproject.mediapackage.selector.TrackSelector in project opencast by opencast.

the class TrackSelectorTest method testTagsOrFlavorSelect2.

@Test
public void testTagsOrFlavorSelect2() {
    TrackSelector tagsSelector = new TrackSelector();
    tagsSelector.addTag(tag);
    tagsSelector.addFlavor(AUDIENCE_SOURCE);
    assertEquals(2, tagsSelector.select(mediaPackage, false).size());
}
Also used : TrackSelector(org.opencastproject.mediapackage.selector.TrackSelector) Test(org.junit.Test)

Example 15 with TrackSelector

use of org.opencastproject.mediapackage.selector.TrackSelector in project opencast by opencast.

the class TrackSelectorTest method testTagsOrFlavorSelect.

@Test
public void testTagsOrFlavorSelect() {
    TrackSelector tagsSelector = new TrackSelector();
    tagsSelector.addTag(tag);
    tagsSelector.addFlavor(PRESENTATION_SOURCE);
    assertEquals(2, tagsSelector.select(mediaPackage, false).size());
}
Also used : TrackSelector(org.opencastproject.mediapackage.selector.TrackSelector) Test(org.junit.Test)

Aggregations

TrackSelector (org.opencastproject.mediapackage.selector.TrackSelector)25 Track (org.opencastproject.mediapackage.Track)17 MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)16 WorkflowOperationException (org.opencastproject.workflow.api.WorkflowOperationException)15 MediaPackage (org.opencastproject.mediapackage.MediaPackage)12 Job (org.opencastproject.job.api.Job)11 Test (org.junit.Test)9 WorkflowOperationResult (org.opencastproject.workflow.api.WorkflowOperationResult)9 URI (java.net.URI)8 WorkflowOperationInstance (org.opencastproject.workflow.api.WorkflowOperationInstance)8 ArrayList (java.util.ArrayList)7 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)7 HashMap (java.util.HashMap)6 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)5 IOException (java.io.IOException)4 Map (java.util.Map)4 SortedMap (java.util.SortedMap)4 TreeMap (java.util.TreeMap)4 EncodingProfile (org.opencastproject.composer.api.EncodingProfile)4 NotFoundException (org.opencastproject.util.NotFoundException)4