use of org.opencastproject.workflow.api.WorkflowOperationException in project opencast by opencast.
the class PartialImportWorkflowOperationHandler method extractImage.
private Attachment extractImage(Track presentationTrack, double time, List<MediaPackageElement> elementsToClean) throws EncoderException, MediaPackageException, WorkflowOperationException, NotFoundException {
Job extractImageJob = composerService.image(presentationTrack, PREVIEW_PROFILE, time);
if (!waitForStatus(extractImageJob).isSuccess())
throw new WorkflowOperationException("Extract image frame video job did not complete successfully");
// Get the latest copy
try {
extractImageJob = serviceRegistry.getJob(extractImageJob.getId());
} catch (ServiceRegistryException e) {
throw new WorkflowOperationException(e);
}
Attachment composedImages = (Attachment) MediaPackageElementParser.getArrayFromXml(extractImageJob.getPayload()).get(0);
elementsToClean.add(composedImages);
return composedImages;
}
use of org.opencastproject.workflow.api.WorkflowOperationException in project opencast by opencast.
the class PartialImportWorkflowOperationHandler method concat.
private WorkflowOperationResult concat(MediaPackage src, WorkflowOperationInstance operation, List<MediaPackageElement> elementsToClean) throws EncoderException, IOException, NotFoundException, MediaPackageException, WorkflowOperationException, ServiceRegistryException {
final MediaPackage mediaPackage = (MediaPackage) src.clone();
final Long operationId = operation.getId();
//
// read config options
final Opt<String> presenterFlavor = getOptConfig(operation, SOURCE_PRESENTER_FLAVOR);
final Opt<String> presentationFlavor = getOptConfig(operation, SOURCE_PRESENTATION_FLAVOR);
final String smilFlavor = getConfig(operation, SOURCE_SMIL_FLAVOR);
final String concatEncodingProfile = getConfig(operation, CONCAT_ENCODING_PROFILE);
final Opt<String> concatOutputFramerate = getOptConfig(operation, CONCAT_OUTPUT_FRAMERATE);
final String trimEncodingProfile = getConfig(operation, TRIM_ENCODING_PROFILE);
final MediaPackageElementFlavor targetPresenterFlavor = parseTargetFlavor(getConfig(operation, TARGET_PRESENTER_FLAVOR), "presenter");
final MediaPackageElementFlavor targetPresentationFlavor = parseTargetFlavor(getConfig(operation, TARGET_PRESENTATION_FLAVOR), "presentation");
final Opt<EncodingProfile> forceProfile = getForceEncodingProfile(operation);
final boolean forceEncoding = BooleanUtils.toBoolean(getOptConfig(operation, FORCE_ENCODING).getOr("false"));
final boolean forceDivisible = BooleanUtils.toBoolean(getOptConfig(operation, ENFORCE_DIVISIBLE_BY_TWO).getOr("false"));
final List<String> requiredExtensions = getRequiredExtensions(operation);
// Skip the worklow if no presenter and presentation flavor has been configured
if (presenterFlavor.isNone() && presentationFlavor.isNone()) {
logger.warn("No presenter and presentation flavor has been set.");
return createResult(mediaPackage, Action.SKIP);
}
final EncodingProfile concatProfile = composerService.getProfile(concatEncodingProfile);
if (concatProfile == null) {
throw new WorkflowOperationException("Concat encoding profile '" + concatEncodingProfile + "' was not found");
}
float outputFramerate = -1.0f;
if (concatOutputFramerate.isSome()) {
if (NumberUtils.isNumber(concatOutputFramerate.get())) {
logger.info("Using concat output framerate");
outputFramerate = NumberUtils.toFloat(concatOutputFramerate.get());
} else {
throw new WorkflowOperationException("Unable to parse concat output frame rate!");
}
}
final EncodingProfile trimProfile = composerService.getProfile(trimEncodingProfile);
if (trimProfile == null) {
throw new WorkflowOperationException("Trim encoding profile '" + trimEncodingProfile + "' was not found");
}
//
// get tracks
final TrackSelector presenterTrackSelector = mkTrackSelector(presenterFlavor);
final TrackSelector presentationTrackSelector = mkTrackSelector(presentationFlavor);
final List<Track> originalTracks = new ArrayList<Track>();
final List<Track> presenterTracks = new ArrayList<Track>();
final List<Track> presentationTracks = new ArrayList<Track>();
// Collecting presenter tracks
for (Track t : presenterTrackSelector.select(mediaPackage, false)) {
logger.info("Found partial presenter track {}", t);
originalTracks.add(t);
presenterTracks.add(t);
}
// Collecting presentation tracks
for (Track t : presentationTrackSelector.select(mediaPackage, false)) {
logger.info("Found partial presentation track {}", t);
originalTracks.add(t);
presentationTracks.add(t);
}
// flavor_type -> job
final Map<String, Job> jobs = new HashMap<String, Job>();
// get SMIL catalog
final SMILDocument smilDocument = getSmilDocumentFromMediaPackage(mediaPackage, smilFlavor);
final SMILParElement parallel = (SMILParElement) smilDocument.getBody().getChildNodes().item(0);
final NodeList sequences = parallel.getTimeChildren();
final float trackDurationInSeconds = parallel.getDur();
final long trackDurationInMs = Math.round(trackDurationInSeconds * 1000f);
for (int i = 0; i < sequences.getLength(); i++) {
final SMILElement item = (SMILElement) sequences.item(i);
for (final String mediaType : new String[] { NODE_TYPE_AUDIO, NODE_TYPE_VIDEO }) {
final List<Track> tracks = new ArrayList<Track>();
final VCell<String> sourceType = VCell.cell(EMPTY_VALUE);
final long position = processChildren(0, tracks, item.getChildNodes(), originalTracks, sourceType, mediaType, elementsToClean, operationId);
if (tracks.isEmpty()) {
logger.debug("The tracks list was empty.");
continue;
}
final Track lastTrack = tracks.get(tracks.size() - 1);
if (position < trackDurationInMs) {
final double extendingTime = (trackDurationInMs - position) / 1000d;
if (extendingTime > 0) {
if (!lastTrack.hasVideo()) {
logger.info("Extending {} audio track end by {} seconds with silent audio", sourceType.get(), extendingTime);
tracks.add(getSilentAudio(extendingTime, elementsToClean, operationId));
} else {
logger.info("Extending {} track end with last image frame by {} seconds", sourceType.get(), extendingTime);
Attachment tempLastImageFrame = extractLastImageFrame(lastTrack, elementsToClean);
tracks.add(createVideoFromImage(tempLastImageFrame, extendingTime, elementsToClean));
}
}
}
if (tracks.size() < 2) {
logger.debug("There were less than 2 tracks, copying track...");
if (sourceType.get().startsWith(PRESENTER_KEY)) {
createCopyOfTrack(mediaPackage, tracks.get(0), targetPresenterFlavor);
} else if (sourceType.get().startsWith(PRESENTATION_KEY)) {
createCopyOfTrack(mediaPackage, tracks.get(0), targetPresentationFlavor);
} else {
logger.warn("Can't handle unkown source type '{}' for unprocessed track", sourceType.get());
}
continue;
}
for (final Track t : tracks) {
if (!t.hasVideo() && !t.hasAudio()) {
logger.error("No audio or video stream available in the track with flavor {}! {}", t.getFlavor(), t);
throw new WorkflowOperationException("No audio or video stream available in the track " + t.toString());
}
}
if (sourceType.get().startsWith(PRESENTER_KEY)) {
logger.info("Concatenating {} track", PRESENTER_KEY);
jobs.put(sourceType.get(), startConcatJob(concatProfile, tracks, outputFramerate, forceDivisible));
} else if (sourceType.get().startsWith(PRESENTATION_KEY)) {
logger.info("Concatenating {} track", PRESENTATION_KEY);
jobs.put(sourceType.get(), startConcatJob(concatProfile, tracks, outputFramerate, forceDivisible));
} else {
logger.warn("Can't handle unknown source type '{}'!", sourceType.get());
}
}
}
// Wait for the jobs to return
if (jobs.size() > 0) {
if (!JobUtil.waitForJobs(serviceRegistry, jobs.values()).isSuccess()) {
throw new WorkflowOperationException("One of the concat jobs did not complete successfully");
}
} else {
logger.info("No concatenating needed for presenter and presentation tracks, took partial source elements");
}
// All the jobs have passed, let's update the media package
long queueTime = 0L;
MediaPackageElementFlavor adjustedTargetPresenterFlavor = targetPresenterFlavor;
MediaPackageElementFlavor adjustedTargetPresentationFlavor = targetPresentationFlavor;
for (final Entry<String, Job> job : jobs.entrySet()) {
final Opt<Job> concatJob = JobUtil.update(serviceRegistry, job.getValue());
if (concatJob.isSome()) {
final String concatPayload = concatJob.get().getPayload();
if (concatPayload != null) {
final Track concatTrack;
try {
concatTrack = (Track) MediaPackageElementParser.getFromXml(concatPayload);
} catch (MediaPackageException e) {
throw new WorkflowOperationException(e);
}
final String fileName;
// Adjust the target flavor.
if (job.getKey().startsWith(PRESENTER_KEY)) {
if (!concatTrack.hasVideo()) {
fileName = PRESENTER_KEY.concat(FLAVOR_AUDIO_SUFFIX);
adjustedTargetPresenterFlavor = deriveAudioFlavor(targetPresenterFlavor);
} else {
fileName = PRESENTER_KEY;
adjustedTargetPresenterFlavor = targetPresenterFlavor;
}
concatTrack.setFlavor(adjustedTargetPresenterFlavor);
} else if (job.getKey().startsWith(PRESENTATION_KEY)) {
if (!concatTrack.hasVideo()) {
fileName = PRESENTATION_KEY.concat(FLAVOR_AUDIO_SUFFIX);
adjustedTargetPresentationFlavor = deriveAudioFlavor(targetPresentationFlavor);
} else {
fileName = PRESENTATION_KEY;
adjustedTargetPresentationFlavor = targetPresentationFlavor;
}
concatTrack.setFlavor(adjustedTargetPresentationFlavor);
} else {
fileName = UNKNOWN_KEY;
}
concatTrack.setURI(workspace.moveTo(concatTrack.getURI(), mediaPackage.getIdentifier().toString(), concatTrack.getIdentifier(), fileName + "." + FilenameUtils.getExtension(concatTrack.getURI().toString())));
logger.info("Concatenated track {} got flavor '{}'", concatTrack, concatTrack.getFlavor());
mediaPackage.add(concatTrack);
queueTime += concatJob.get().getQueueTime();
} else {
// If there is no payload, then the item has not been distributed.
logger.warn("Concat job {} does not contain a payload", concatJob);
}
} else {
logger.warn("Concat job {} could not be updated since it cannot be found", job.getValue());
}
}
// Trim presenter and presentation source track if longer than the duration from the SMIL catalog
queueTime += checkForTrimming(mediaPackage, trimProfile, targetPresentationFlavor, trackDurationInSeconds, elementsToClean);
queueTime += checkForTrimming(mediaPackage, trimProfile, deriveAudioFlavor(targetPresentationFlavor), trackDurationInSeconds, elementsToClean);
queueTime += checkForTrimming(mediaPackage, trimProfile, targetPresenterFlavor, trackDurationInSeconds, elementsToClean);
queueTime += checkForTrimming(mediaPackage, trimProfile, deriveAudioFlavor(targetPresenterFlavor), trackDurationInSeconds, elementsToClean);
adjustAudioTrackTargetFlavor(mediaPackage, targetPresenterFlavor);
adjustAudioTrackTargetFlavor(mediaPackage, targetPresentationFlavor);
queueTime += checkForMuxing(mediaPackage, targetPresenterFlavor, targetPresentationFlavor, false, elementsToClean);
queueTime += checkForEncodeToStandard(mediaPackage, forceEncoding, forceProfile, requiredExtensions, targetPresenterFlavor, targetPresentationFlavor, elementsToClean);
final WorkflowOperationResult result = createResult(mediaPackage, Action.CONTINUE, queueTime);
logger.debug("Partial import operation completed");
return result;
}
use of org.opencastproject.workflow.api.WorkflowOperationException in project opencast by opencast.
the class PrepareAVWorkflowOperationHandler method prepare.
/**
* Prepares a video track. If the mediapackage is specified, the prepared track will be added to it.
*
* @param videoTrack
* the track containing the video
* @param mediaPackage
* the mediapackage
* @return the rewritten track
* @throws WorkflowOperationException
* @throws NotFoundException
* @throws IOException
* @throws EncoderException
* @throws MediaPackageException
*/
private Track prepare(Track videoTrack, MediaPackage mediaPackage, String encodingProfile) throws WorkflowOperationException, NotFoundException, IOException, EncoderException, MediaPackageException {
Track composedTrack = null;
logger.info("Encoding video only track {} to work version", videoTrack);
Job job = composerService.encode(videoTrack, encodingProfile);
if (!waitForStatus(job).isSuccess()) {
throw new WorkflowOperationException("Rewriting container for video track " + videoTrack + " failed");
}
composedTrack = (Track) MediaPackageElementParser.getFromXml(job.getPayload());
if (mediaPackage != null) {
mediaPackage.add(composedTrack);
String fileName = getFileNameFromElements(videoTrack, composedTrack);
// Note that the composed track must have an ID before being moved to the mediapackage in the working file
// repository. This ID is generated when the track is added to the mediapackage. So the track must be added
// to the mediapackage before attempting to move the file.
composedTrack.setURI(workspace.moveTo(composedTrack.getURI(), mediaPackage.getIdentifier().toString(), composedTrack.getIdentifier(), fileName));
}
return composedTrack;
}
use of org.opencastproject.workflow.api.WorkflowOperationException in project opencast by opencast.
the class SegmentPreviewsWorkflowOperationHandler method createPreviews.
/**
* Encode tracks from MediaPackage using profiles stored in properties and updates current MediaPackage.
*
* @param mediaPackage
* @param properties
* @return the operation result containing the updated mediapackage
* @throws EncoderException
* @throws ExecutionException
* @throws InterruptedException
* @throws IOException
* @throws NotFoundException
* @throws WorkflowOperationException
*/
private WorkflowOperationResult createPreviews(final MediaPackage mediaPackage, WorkflowOperationInstance operation) throws EncoderException, InterruptedException, ExecutionException, NotFoundException, MediaPackageException, IOException, WorkflowOperationException {
long totalTimeInQueue = 0;
// Read the configuration properties
String sourceVideoFlavor = StringUtils.trimToNull(operation.getConfiguration("source-flavor"));
String sourceTags = StringUtils.trimToNull(operation.getConfiguration("source-tags"));
String targetImageTags = StringUtils.trimToNull(operation.getConfiguration("target-tags"));
String targetImageFlavor = StringUtils.trimToNull(operation.getConfiguration("target-flavor"));
String encodingProfileName = StringUtils.trimToNull(operation.getConfiguration("encoding-profile"));
String referenceFlavor = StringUtils.trimToNull(operation.getConfiguration("reference-flavor"));
String referenceTags = StringUtils.trimToNull(operation.getConfiguration("reference-tags"));
// Find the encoding profile
EncodingProfile profile = composerService.getProfile(encodingProfileName);
if (profile == null)
throw new IllegalStateException("Encoding profile '" + encodingProfileName + "' was not found");
List<String> sourceTagSet = asList(sourceTags);
// Select the tracks based on the tags and flavors
Set<Track> videoTrackSet = new HashSet<>();
for (Track track : mediaPackage.getTracksByTags(sourceTagSet)) {
if (sourceVideoFlavor == null || (track.getFlavor() != null && sourceVideoFlavor.equals(track.getFlavor().toString()))) {
if (!track.hasVideo())
continue;
videoTrackSet.add(track);
}
}
if (videoTrackSet.size() == 0) {
logger.debug("Mediapackage {} has no suitable tracks to extract images based on tags {} and flavor {}", mediaPackage, sourceTags, sourceVideoFlavor);
return createResult(mediaPackage, Action.CONTINUE);
} else {
// Determine the tagset for the reference
List<String> referenceTagSet = asList(referenceTags);
// Determine the reference master
for (Track t : videoTrackSet) {
// Try to load the segments catalog
MediaPackageReference trackReference = new MediaPackageReferenceImpl(t);
Catalog[] segmentCatalogs = mediaPackage.getCatalogs(MediaPackageElements.SEGMENTS, trackReference);
Mpeg7Catalog mpeg7 = null;
if (segmentCatalogs.length > 0) {
mpeg7 = loadMpeg7Catalog(segmentCatalogs[0]);
if (segmentCatalogs.length > 1)
logger.warn("More than one segments catalog found for track {}. Resuming with the first one ({})", t, mpeg7);
} else {
logger.debug("No segments catalog found for track {}", t);
continue;
}
// Check the catalog's consistency
if (mpeg7.videoContent() == null || mpeg7.videoContent().next() == null) {
logger.info("Segments catalog {} contains no video content", mpeg7);
continue;
}
Video videoContent = mpeg7.videoContent().next();
TemporalDecomposition<? extends Segment> decomposition = videoContent.getTemporalDecomposition();
// Are there any segments?
if (decomposition == null || !decomposition.hasSegments()) {
logger.info("Segments catalog {} contains no video content", mpeg7);
continue;
}
// Is a derived track with the configured reference flavor available?
MediaPackageElement referenceMaster = getReferenceMaster(mediaPackage, t, referenceFlavor, referenceTagSet);
// Create the preview images according to the mpeg7 segments
if (t.hasVideo() && mpeg7 != null) {
Iterator<? extends Segment> segmentIterator = decomposition.segments();
List<MediaTimePoint> timePointList = new LinkedList<>();
while (segmentIterator.hasNext()) {
Segment segment = segmentIterator.next();
MediaTimePoint tp = segment.getMediaTime().getMediaTimePoint();
timePointList.add(tp);
}
// convert to time array
double[] timeArray = new double[timePointList.size()];
for (int i = 0; i < timePointList.size(); i++) timeArray[i] = (double) timePointList.get(i).getTimeInMilliseconds() / 1000;
Job job = composerService.image(t, profile.getIdentifier(), timeArray);
if (!waitForStatus(job).isSuccess()) {
throw new WorkflowOperationException("Extracting preview image from " + t + " failed");
}
// Get the latest copy
try {
job = serviceRegistry.getJob(job.getId());
} catch (ServiceRegistryException e) {
throw new WorkflowOperationException(e);
}
// add this receipt's queue time to the total
totalTimeInQueue += job.getQueueTime();
List<? extends MediaPackageElement> composedImages = MediaPackageElementParser.getArrayFromXml(job.getPayload());
Iterator<MediaTimePoint> it = timePointList.iterator();
for (MediaPackageElement element : composedImages) {
Attachment composedImage = (Attachment) element;
if (composedImage == null)
throw new IllegalStateException("Unable to compose image");
// Add the flavor, either from the operation configuration or from the composer
if (targetImageFlavor != null) {
composedImage.setFlavor(MediaPackageElementFlavor.parseFlavor(targetImageFlavor));
logger.debug("Preview image has flavor '{}'", composedImage.getFlavor());
}
// Set the mimetype
if (profile.getMimeType() != null)
composedImage.setMimeType(MimeTypes.parseMimeType(profile.getMimeType()));
// Add tags
for (String tag : asList(targetImageTags)) {
logger.trace("Tagging image with '{}'", tag);
composedImage.addTag(tag);
}
// Refer to the original track including a timestamp
MediaPackageReferenceImpl ref = new MediaPackageReferenceImpl(referenceMaster);
ref.setProperty("time", it.next().toString());
composedImage.setReference(ref);
// store new image in the mediaPackage
mediaPackage.add(composedImage);
String fileName = getFileNameFromElements(t, composedImage);
composedImage.setURI(workspace.moveTo(composedImage.getURI(), mediaPackage.getIdentifier().toString(), composedImage.getIdentifier(), fileName));
}
}
}
}
return createResult(mediaPackage, Action.CONTINUE, totalTimeInQueue);
}
use of org.opencastproject.workflow.api.WorkflowOperationException in project opencast by opencast.
the class ImageToVideoWorkflowOperationHandlerTest method testOneImageToVideoWithMissingParams.
@Test
public void testOneImageToVideoWithMissingParams() throws Exception {
setMockups();
// operation configuration
String targetTags = "engage,rss";
Map<String, String> configurations = new HashMap<String, String>();
configurations.put("source-flavor", "image/intro");
configurations.put("source-tag", "intro");
configurations.put("target-tags", targetTags);
configurations.put("target-flavor", "video/intro");
configurations.put("profile", "image-movie");
try {
// run the operation handler
WorkflowOperationResult result = getWorkflowOperationResult(mp, configurations);
Assert.fail();
} catch (WorkflowOperationException e) {
Assert.assertNotNull("Duration is required!", e);
}
}
Aggregations