use of org.opencastproject.mediapackage.MediaPackageElementBuilder in project opencast by opencast.
the class SilenceDetectionWorkflowOperationHandler method start.
@Override
public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
MediaPackage mp = workflowInstance.getMediaPackage();
logger.debug("Start silence detection workflow operation for mediapackage {}", mp.getIdentifier().compact());
String sourceFlavors = StringUtils.trimToNull(workflowInstance.getCurrentOperation().getConfiguration(SOURCE_FLAVORS_PROPERTY));
String sourceFlavor = StringUtils.trimToNull(workflowInstance.getCurrentOperation().getConfiguration(SOURCE_FLAVOR_PROPERTY));
String smilFlavorSubType = StringUtils.trimToNull(workflowInstance.getCurrentOperation().getConfiguration(SMIL_FLAVOR_SUBTYPE_PROPERTY));
String smilTargetFlavorString = StringUtils.trimToNull(workflowInstance.getCurrentOperation().getConfiguration(SMIL_TARGET_FLAVOR_PROPERTY));
MediaPackageElementFlavor smilTargetFlavor = null;
if (smilTargetFlavorString != null)
smilTargetFlavor = MediaPackageElementFlavor.parseFlavor(smilTargetFlavorString);
if (sourceFlavor == null && sourceFlavors == null) {
throw new WorkflowOperationException(String.format("No %s or %s have been specified", SOURCE_FLAVOR_PROPERTY, SOURCE_FLAVORS_PROPERTY));
}
if (smilFlavorSubType == null && smilTargetFlavor == null) {
throw new WorkflowOperationException(String.format("No %s or %s have been specified", SMIL_FLAVOR_SUBTYPE_PROPERTY, SMIL_TARGET_FLAVOR_PROPERTY));
}
if (sourceFlavors != null && smilTargetFlavor != null) {
throw new WorkflowOperationException(String.format("Can't use %s and %s together", SOURCE_FLAVORS_PROPERTY, SMIL_TARGET_FLAVOR_PROPERTY));
}
final String finalSourceFlavors;
if (smilTargetFlavor != null) {
finalSourceFlavors = sourceFlavor;
} else {
finalSourceFlavors = sourceFlavors;
}
String referenceTracksFlavor = StringUtils.trimToNull(workflowInstance.getCurrentOperation().getConfiguration(REFERENCE_TRACKS_FLAVOR_PROPERTY));
if (referenceTracksFlavor == null)
referenceTracksFlavor = finalSourceFlavors;
TrackSelector trackSelector = new TrackSelector();
for (String flavor : asList(finalSourceFlavors)) {
trackSelector.addFlavor(flavor);
}
Collection<Track> sourceTracks = trackSelector.select(mp, false);
if (sourceTracks.isEmpty()) {
logger.info("No source tracks found, skip silence detection");
return createResult(mp, Action.SKIP);
}
trackSelector = new TrackSelector();
for (String flavor : asList(referenceTracksFlavor)) {
trackSelector.addFlavor(flavor);
}
Collection<Track> referenceTracks = trackSelector.select(mp, false);
if (referenceTracks.isEmpty()) {
// REFERENCE_TRACKS_FLAVOR_PROPERTY was set to wrong value
throw new WorkflowOperationException(String.format("No tracks found filtered by flavor(s) '%s'", referenceTracksFlavor));
}
MediaPackageElementBuilder mpeBuilder = MediaPackageElementBuilderFactory.newInstance().newElementBuilder();
for (Track sourceTrack : sourceTracks) {
// Skip over track with no audio stream
if (!sourceTrack.hasAudio()) {
logger.info("Skipping silence detection of track {} since it has no audio", sourceTrack);
continue;
}
logger.info("Executing silence detection on track {}", sourceTrack.getIdentifier());
try {
Job detectionJob = detetionService.detect(sourceTrack, referenceTracks.toArray(new Track[referenceTracks.size()]));
if (!waitForStatus(detectionJob).isSuccess()) {
throw new WorkflowOperationException("Silence Detection failed");
}
Smil smil = smilService.fromXml(detectionJob.getPayload()).getSmil();
InputStream is = null;
try {
is = IOUtils.toInputStream(smil.toXML(), "UTF-8");
URI smilURI = workspace.put(mp.getIdentifier().compact(), smil.getId(), TARGET_FILE_NAME, is);
MediaPackageElementFlavor smilFlavor = smilTargetFlavor;
if (smilFlavor == null)
smilFlavor = new MediaPackageElementFlavor(sourceTrack.getFlavor().getType(), smilFlavorSubType);
Catalog catalog = (Catalog) mpeBuilder.elementFromURI(smilURI, MediaPackageElement.Type.Catalog, smilFlavor);
catalog.setIdentifier(smil.getId());
mp.add(catalog);
} catch (Exception ex) {
throw new WorkflowOperationException(String.format("Failed to put smil into workspace. Silence detection for track %s failed", sourceTrack.getIdentifier()), ex);
} finally {
IOUtils.closeQuietly(is);
}
logger.info("Finished silence detection on track {}", sourceTrack.getIdentifier());
} catch (SilenceDetectionFailedException ex) {
throw new WorkflowOperationException(String.format("Failed to create silence detection job for track %s", sourceTrack.getIdentifier()));
} catch (SmilException ex) {
throw new WorkflowOperationException(String.format("Failed to get smil from silence detection job for track %s", sourceTrack.getIdentifier()));
}
}
logger.debug("Finished silence detection workflow operation for mediapackage {}", mp.getIdentifier().compact());
return createResult(mp, Action.CONTINUE);
}
use of org.opencastproject.mediapackage.MediaPackageElementBuilder 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());
}
}
Aggregations