use of org.opencastproject.mediapackage.selector.TrackSelector in project opencast by opencast.
the class VideoEditorWorkflowOperationHandlerTest method testEditorOperationSkip.
@Test
public void testEditorOperationSkip() throws WorkflowOperationException {
WorkflowInstanceImpl workflowInstance = getWorkflowInstance(mp, getDefaultConfiguration(true));
WorkflowOperationResult result = videoEditorWorkflowOperationHandler.skip(workflowInstance, null);
Assert.assertNotNull("VideoEditor workflow operation returns null but should be an instantiated WorkflowOperationResult", result);
// mediapackage should contain new derived track with flavor given by "target-flavor-subtype" configuration
WorkflowOperationInstance worflowOperationInstance = workflowInstance.getCurrentOperation();
String targetFlavorSubtypeProperty = worflowOperationInstance.getConfiguration("target-flavor-subtype");
String skippedFlavorsProperty = worflowOperationInstance.getConfiguration("skipped-flavors");
TrackSelector trackSelector = new TrackSelector();
trackSelector.addFlavor(skippedFlavorsProperty);
Collection<Track> skippedTracks = trackSelector.select(result.getMediaPackage(), false);
Assert.assertTrue("Mediapackage does not contain any tracks matching flavor " + skippedFlavorsProperty, skippedTracks != null && !skippedTracks.isEmpty());
for (Track skippedTrack : skippedTracks) {
MediaPackageElementFlavor derivedTrackFlavor = MediaPackageElementFlavor.flavor(skippedTrack.getFlavor().getType(), targetFlavorSubtypeProperty);
MediaPackageElement[] derivedElements = result.getMediaPackage().getDerived(skippedTrack, derivedTrackFlavor);
Assert.assertTrue("Media package should contain track with flavor " + derivedTrackFlavor.toString(), derivedElements != null && derivedElements.length > 0);
}
}
use of org.opencastproject.mediapackage.selector.TrackSelector in project opencast by opencast.
the class VideoEditorWorkflowOperationHandlerTest method testEditorOperationSkipWithModifiedSkippedFlavorsAndTargetFlavorProperty.
@Test
@Ignore
public void testEditorOperationSkipWithModifiedSkippedFlavorsAndTargetFlavorProperty() throws WorkflowOperationException {
Map<String, String> configuration = getDefaultConfiguration(true);
configuration.put("skipped-flavors", "*/preview");
configuration.put("target-flavor-subtype", "edited");
WorkflowInstanceImpl workflowInstance = getWorkflowInstance(mp, configuration);
WorkflowOperationResult result = videoEditorWorkflowOperationHandler.skip(workflowInstance, null);
Assert.assertNotNull("VideoEditor workflow operation returns null but should be an instantiated WorkflowOperationResult", result);
// mediapackage should contain new derived track with flavor given by "target-flavor-subtype" configuration
WorkflowOperationInstance worflowOperationInstance = workflowInstance.getCurrentOperation();
String targetFlavorSubtypeProperty = worflowOperationInstance.getConfiguration("target-flavor-subtype");
String skippedFlavorsProperty = worflowOperationInstance.getConfiguration("skipped-flavors");
TrackSelector trackSelector = new TrackSelector();
trackSelector.addFlavor(skippedFlavorsProperty);
Collection<Track> skippedTracks = trackSelector.select(result.getMediaPackage(), false);
Assert.assertTrue("Mediapackage does not contain any tracks matching flavor " + skippedFlavorsProperty, skippedTracks != null && !skippedTracks.isEmpty());
for (Track skippedTrack : skippedTracks) {
MediaPackageElementFlavor derivedTrackFlavor = MediaPackageElementFlavor.flavor(skippedTrack.getFlavor().getType(), targetFlavorSubtypeProperty);
MediaPackageElement[] derivedElements = result.getMediaPackage().getDerived(skippedTrack, derivedTrackFlavor);
Assert.assertTrue("Media package should contain track with flavor " + derivedTrackFlavor.toString(), derivedElements != null && derivedElements.length > 0);
Assert.assertTrue("Mediapackage schould contain a derived track with flavor subtype " + targetFlavorSubtypeProperty, derivedElements[0].getFlavor().getSubtype().equals(targetFlavorSubtypeProperty));
}
}
use of org.opencastproject.mediapackage.selector.TrackSelector in project opencast by opencast.
the class VideoEditorWorkflowOperationHandler method start.
/**
* {@inheritDoc}
*
* @see org.opencastproject.workflow.api.WorkflowOperationHandler#start(org.opencastproject.workflow.api.WorkflowInstance,
* JobContext)
*/
@Override
public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
MediaPackage mp = workflowInstance.getMediaPackage();
logger.info("Start editor workflow for mediapackage {}", mp.getIdentifier().compact());
// Get configuration
WorkflowOperationInstance worflowOperationInstance = workflowInstance.getCurrentOperation();
String smilFlavorsProperty = StringUtils.trimToNull(worflowOperationInstance.getConfiguration(SMIL_FLAVORS_PROPERTY));
if (smilFlavorsProperty == null) {
throw new WorkflowOperationException(format("Required configuration property %s not set", SMIL_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 previewTrackFlavorsProperty = StringUtils.trimToNull(worflowOperationInstance.getConfiguration(PREVIEW_FLAVORS_PROPERTY));
if (previewTrackFlavorsProperty == null) {
logger.info("Configuration property '{}' not set, use preview tracks from SMIL catalog", PREVIEW_FLAVORS_PROPERTY);
}
if (StringUtils.trimToNull(worflowOperationInstance.getConfiguration(TARGET_FLAVOR_SUBTYPE_PROPERTY)) == null) {
throw new WorkflowOperationException(format("Required configuration property %s not set", TARGET_FLAVOR_SUBTYPE_PROPERTY));
}
final boolean interactive = BooleanUtils.toBoolean(worflowOperationInstance.getConfiguration(INTERACTIVE_PROPERTY));
// Check at least one SMIL catalog exists
SimpleElementSelector elementSelector = new SimpleElementSelector();
for (String flavor : asList(smilFlavorsProperty)) {
elementSelector.addFlavor(flavor);
}
Collection<MediaPackageElement> smilCatalogs = elementSelector.select(mp, false);
MediaPackageElementBuilder mpeBuilder = MediaPackageElementBuilderFactory.newInstance().newElementBuilder();
if (smilCatalogs.isEmpty()) {
// There is nothing to do, skip the operation
if (!interactive) {
logger.info("Skipping cutting opertion since no edit decision list is available");
return skip(workflowInstance, context);
}
// Without SMIL catalogs and without preview tracks, there is nothing we can do
if (previewTrackFlavorsProperty == null) {
throw new WorkflowOperationException(format("No SMIL catalogs with flavor %s nor preview files with flavor %s found in mediapackage %s", smilFlavorsProperty, previewTrackFlavorsProperty, mp.getIdentifier().compact()));
}
// Based on the preview tracks, create new and empty SMIL catalog
TrackSelector trackSelector = new TrackSelector();
for (String flavor : asList(previewTrackFlavorsProperty)) {
trackSelector.addFlavor(flavor);
}
Collection<Track> previewTracks = trackSelector.select(mp, false);
if (previewTracks.isEmpty()) {
throw new WorkflowOperationException(format("No preview tracks found in mediapackage %s with flavor %s", mp.getIdentifier().compact(), previewTrackFlavorsProperty));
}
Track[] previewTracksArr = previewTracks.toArray(new Track[previewTracks.size()]);
MediaPackageElementFlavor smilFlavor = MediaPackageElementFlavor.parseFlavor(smilFlavorsProperty);
for (Track previewTrack : previewTracks) {
try {
SmilResponse smilResponse = smilService.createNewSmil(mp);
smilResponse = smilService.addParallel(smilResponse.getSmil());
smilResponse = smilService.addClips(smilResponse.getSmil(), smilResponse.getEntity().getId(), previewTracksArr, 0L, previewTracksArr[0].getDuration());
Smil smil = smilResponse.getSmil();
InputStream is = null;
try {
// Put new SMIL into workspace
is = IOUtils.toInputStream(smil.toXML(), "UTF-8");
URI smilURI = workspace.put(mp.getIdentifier().compact(), smil.getId(), SMIL_FILE_NAME, is);
MediaPackageElementFlavor trackSmilFlavor = previewTrack.getFlavor();
if (!"*".equals(smilFlavor.getType())) {
trackSmilFlavor = new MediaPackageElementFlavor(smilFlavor.getType(), trackSmilFlavor.getSubtype());
}
if (!"*".equals(smilFlavor.getSubtype())) {
trackSmilFlavor = new MediaPackageElementFlavor(trackSmilFlavor.getType(), smilFlavor.getSubtype());
}
Catalog catalog = (Catalog) mpeBuilder.elementFromURI(smilURI, MediaPackageElement.Type.Catalog, trackSmilFlavor);
catalog.setIdentifier(smil.getId());
mp.add(catalog);
} finally {
IOUtils.closeQuietly(is);
}
} catch (Exception ex) {
throw new WorkflowOperationException(format("Failed to create SMIL catalog for mediapackage %s", mp.getIdentifier().compact()), ex);
}
}
}
// Check target SMIL catalog exists
MediaPackageElementFlavor targetSmilFlavor = MediaPackageElementFlavor.parseFlavor(targetSmilFlavorProperty);
Catalog[] targetSmilCatalogs = mp.getCatalogs(targetSmilFlavor);
if (targetSmilCatalogs == null || targetSmilCatalogs.length == 0) {
if (!interactive)
return skip(workflowInstance, context);
// Create new empty SMIL to fill it from editor UI
try {
SmilResponse smilResponse = smilService.createNewSmil(mp);
Smil smil = smilResponse.getSmil();
InputStream is = null;
try {
// Put new SMIL into workspace
is = IOUtils.toInputStream(smil.toXML(), "UTF-8");
URI smilURI = workspace.put(mp.getIdentifier().compact(), smil.getId(), SMIL_FILE_NAME, is);
Catalog catalog = (Catalog) mpeBuilder.elementFromURI(smilURI, MediaPackageElement.Type.Catalog, targetSmilFlavor);
catalog.setIdentifier(smil.getId());
mp.add(catalog);
} finally {
IOUtils.closeQuietly(is);
}
} catch (Exception ex) {
throw new WorkflowOperationException(format("Failed to create an initial empty SMIL catalog for mediapackage %s", mp.getIdentifier().compact()), ex);
}
logger.info("Holding for video edit...");
return createResult(mp, Action.PAUSE);
} else {
logger.debug("Move on, SMIL catalog ({}) already exists for media package '{}'", targetSmilFlavor, mp);
return resume(workflowInstance, context, Collections.<String, String>emptyMap());
}
}
use of org.opencastproject.mediapackage.selector.TrackSelector in project opencast by opencast.
the class VideoEditorWorkflowOperationHandler method skip.
/**
* {@inheritDoc}
*
* @see org.opencastproject.workflow.api.AbstractWorkflowOperationHandler#skip(org.opencastproject.workflow.api.WorkflowInstance,
* JobContext)
*/
@Override
public WorkflowOperationResult skip(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
// If we do not hold for trim, we still need to put tracks in the mediapackage with the target flavor
MediaPackage mp = workflowInstance.getMediaPackage();
logger.info("Skip video editor operation for mediapackage {}", mp.getIdentifier().compact());
// Get configuration
WorkflowOperationInstance worflowOperationInstance = workflowInstance.getCurrentOperation();
String sourceTrackFlavorsProperty = StringUtils.trimToNull(worflowOperationInstance.getConfiguration(SKIPPED_FLAVORS_PROPERTY));
if (sourceTrackFlavorsProperty == null || sourceTrackFlavorsProperty.isEmpty()) {
logger.info("\"{}\" option not set, use value of \"{}\"", SKIPPED_FLAVORS_PROPERTY, SOURCE_FLAVORS_PROPERTY);
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 targetFlavorSubTypeProperty = StringUtils.trimToNull(worflowOperationInstance.getConfiguration(TARGET_FLAVOR_SUBTYPE_PROPERTY));
if (targetFlavorSubTypeProperty == null) {
throw new WorkflowOperationException(format("Required configuration property %s not set.", TARGET_FLAVOR_SUBTYPE_PROPERTY));
}
// Get source tracks
TrackSelector trackSelector = new TrackSelector();
for (String flavor : asList(sourceTrackFlavorsProperty)) {
trackSelector.addFlavor(flavor);
}
Collection<Track> sourceTracks = trackSelector.select(mp, false);
for (Track sourceTrack : sourceTracks) {
// Set target track flavor
Track clonedTrack = (Track) sourceTrack.clone();
clonedTrack.setIdentifier(null);
// Use the same URI as the original
clonedTrack.setURI(sourceTrack.getURI());
clonedTrack.setFlavor(new MediaPackageElementFlavor(sourceTrack.getFlavor().getType(), targetFlavorSubTypeProperty));
mp.addDerived(clonedTrack, sourceTrack);
}
return createResult(mp, Action.SKIP);
}
use of org.opencastproject.mediapackage.selector.TrackSelector in project opencast by opencast.
the class WaveformWorkflowOperationHandler 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 waveform workflow operation for mediapackage {}", mediaPackage);
String sourceFlavorProperty = StringUtils.trimToNull(workflowInstance.getCurrentOperation().getConfiguration(SOURCE_FLAVOR_PROPERTY));
String sourceTagsProperty = StringUtils.trimToNull(workflowInstance.getCurrentOperation().getConfiguration(SOURCE_TAGS_PROPERTY));
if (StringUtils.isEmpty(sourceFlavorProperty) && StringUtils.isEmpty(sourceTagsProperty)) {
throw new WorkflowOperationException(String.format("Required property %s or %s not set", SOURCE_FLAVOR_PROPERTY, SOURCE_TAGS_PROPERTY));
}
String targetFlavorProperty = StringUtils.trimToNull(workflowInstance.getCurrentOperation().getConfiguration(TARGET_FLAVOR_PROPERTY));
if (targetFlavorProperty == null) {
throw new WorkflowOperationException(String.format("Required property %s not set", TARGET_FLAVOR_PROPERTY));
}
String targetTagsProperty = StringUtils.trimToNull(workflowInstance.getCurrentOperation().getConfiguration(TARGET_TAGS_PROPERTY));
TrackSelector trackSelector = new TrackSelector();
for (String flavor : asList(sourceFlavorProperty)) {
trackSelector.addFlavor(flavor);
}
for (String tag : asList(sourceTagsProperty)) {
trackSelector.addTag(tag);
}
Collection<Track> sourceTracks = trackSelector.select(mediaPackage, false);
if (sourceTracks.isEmpty()) {
logger.info("No tracks found in mediapackage {} with specified {} = {}", mediaPackage, SOURCE_FLAVOR_PROPERTY, sourceFlavorProperty);
createResult(mediaPackage, WorkflowOperationResult.Action.SKIP);
}
List<Job> waveformJobs = new ArrayList<>(sourceTracks.size());
for (Track sourceTrack : sourceTracks) {
// Skip over track with no audio stream
if (!sourceTrack.hasAudio()) {
logger.info("Skipping waveform extraction of track {} since it has no audio", sourceTrack.getIdentifier());
continue;
}
try {
// generate waveform
logger.info("Creating waveform extraction job for track '{}' in mediapackage '{}'", sourceTrack.getIdentifier(), mediaPackage);
Job waveformJob = waveformService.createWaveformImage(sourceTrack);
waveformJobs.add(waveformJob);
} catch (MediaPackageException | WaveformServiceException e) {
logger.error("Creating waveform extraction job for track '{}' in media package '{}' failed", sourceTrack.getIdentifier(), mediaPackage, e);
}
}
logger.debug("Waiting for waveform jobs for media package {}", mediaPackage);
if (!waitForStatus(waveformJobs.toArray(new Job[waveformJobs.size()])).isSuccess()) {
cleanupWorkspace(waveformJobs);
throw new WorkflowOperationException(String.format("Waveform extraction jobs for media package '%s' have not completed successfully", mediaPackage.getIdentifier()));
}
try {
// copy waveform attachments into workspace and add them to the media package
for (Job job : waveformJobs) {
String jobPayload = job.getPayload();
if (StringUtils.isEmpty(jobPayload)) {
continue;
}
MediaPackageElement waveformMpe = null;
try {
waveformMpe = MediaPackageElementParser.getFromXml(jobPayload);
URI newURI = workspace.moveTo(waveformMpe.getURI(), mediaPackage.getIdentifier().toString(), waveformMpe.getIdentifier(), "waveform.png");
waveformMpe.setURI(newURI);
} catch (MediaPackageException ex) {
// unexpected job payload
throw new WorkflowOperationException("Can't parse waveform attachment from job " + job.getId());
} catch (NotFoundException ex) {
throw new WorkflowOperationException("Waveform image file '" + waveformMpe.getURI() + "' not found", ex);
} catch (IOException ex) {
throw new WorkflowOperationException("Can't get workflow image file '" + waveformMpe.getURI() + "' from workspace");
}
// set the waveform attachment flavor and add it to the media package
MediaPackageElementFlavor targetFlavor = MediaPackageElementFlavor.parseFlavor(targetFlavorProperty);
if ("*".equals(targetFlavor.getType())) {
targetFlavor = new MediaPackageElementFlavor(waveformMpe.getFlavor().getType(), targetFlavor.getSubtype());
}
if ("*".equals(targetFlavor.getSubtype())) {
targetFlavor = new MediaPackageElementFlavor(targetFlavor.getType(), waveformMpe.getFlavor().getSubtype());
}
waveformMpe.setFlavor(targetFlavor);
for (String tag : asList(targetTagsProperty)) {
waveformMpe.addTag(tag);
}
mediaPackage.add(waveformMpe);
}
} finally {
cleanupWorkspace(waveformJobs);
}
try {
workspace.cleanup(mediaPackage.getIdentifier());
} catch (IOException e) {
throw new WorkflowOperationException(e);
}
logger.info("Waveform workflow operation for mediapackage {} completed", mediaPackage);
return createResult(mediaPackage, WorkflowOperationResult.Action.CONTINUE);
}
Aggregations