Search in sources :

Example 1 with EncodingProfile

use of org.opencastproject.composer.api.EncodingProfile in project opencast by opencast.

the class EncodeWorkflowOperationHandler method encode.

/**
 * Encode tracks from MediaPackage using profiles stored in properties and updates current MediaPackage.
 *
 * @param src
 *          The source media package
 * @param operation
 *          the current workflow operation
 * @return the operation result containing the updated media package
 * @throws EncoderException
 *           if encoding fails
 * @throws WorkflowOperationException
 *           if errors occur during processing
 * @throws IOException
 *           if the workspace operations fail
 * @throws NotFoundException
 *           if the workspace doesn't contain the requested file
 */
private WorkflowOperationResult encode(MediaPackage src, WorkflowOperationInstance operation) throws EncoderException, IOException, NotFoundException, MediaPackageException, WorkflowOperationException {
    MediaPackage mediaPackage = (MediaPackage) src.clone();
    // Check which tags have been configured
    String sourceTagsOption = StringUtils.trimToNull(operation.getConfiguration("source-tags"));
    String targetTagsOption = StringUtils.trimToNull(operation.getConfiguration("target-tags"));
    String sourceFlavorOption = StringUtils.trimToNull(operation.getConfiguration("source-flavor"));
    String sourceFlavorsOption = StringUtils.trimToNull(operation.getConfiguration("source-flavors"));
    String targetFlavorOption = StringUtils.trimToNull(operation.getConfiguration("target-flavor"));
    AbstractMediaPackageElementSelector<Track> elementSelector = new TrackSelector();
    // Make sure either one of tags or flavors are provided
    if (StringUtils.isBlank(sourceTagsOption) && StringUtils.isBlank(sourceFlavorOption) && StringUtils.isBlank(sourceFlavorsOption)) {
        logger.info("No source tags or flavors have been specified, not matching anything");
        return createResult(mediaPackage, Action.CONTINUE);
    }
    // Select the source flavors
    for (String flavor : asList(sourceFlavorsOption)) {
        try {
            elementSelector.addFlavor(MediaPackageElementFlavor.parseFlavor(flavor));
        } catch (IllegalArgumentException e) {
            throw new WorkflowOperationException("Source flavor '" + flavor + "' is malformed");
        }
    }
    // Support legacy "source-flavor" option
    if (StringUtils.isNotBlank(sourceFlavorOption)) {
        String flavor = StringUtils.trim(sourceFlavorOption);
        try {
            elementSelector.addFlavor(MediaPackageElementFlavor.parseFlavor(flavor));
        } catch (IllegalArgumentException e) {
            throw new WorkflowOperationException("Source flavor '" + flavor + "' is malformed");
        }
    }
    // Select the source tags
    for (String tag : asList(sourceTagsOption)) {
        elementSelector.addTag(tag);
    }
    // Find the encoding profile
    String profilesOption = StringUtils.trimToNull(operation.getConfiguration("encoding-profiles"));
    List<EncodingProfile> profiles = new ArrayList<EncodingProfile>();
    for (String profileName : asList(profilesOption)) {
        EncodingProfile profile = composerService.getProfile(profileName);
        if (profile == null)
            throw new WorkflowOperationException("Encoding profile '" + profileName + "' was not found");
        profiles.add(profile);
    }
    // Support legacy "encoding-profile" option
    String profileOption = StringUtils.trimToNull(operation.getConfiguration("encoding-profile"));
    if (StringUtils.isNotBlank(profileOption)) {
        String profileId = StringUtils.trim(profileOption);
        EncodingProfile profile = composerService.getProfile(profileId);
        if (profile == null)
            throw new WorkflowOperationException("Encoding profile '" + profileId + "' was not found");
        profiles.add(profile);
    }
    // Make sure there is at least one profile
    if (profiles.isEmpty())
        throw new WorkflowOperationException("No encoding profile was specified");
    // Target tags
    List<String> targetTags = asList(targetTagsOption);
    // Target flavor
    MediaPackageElementFlavor targetFlavor = null;
    if (StringUtils.isNotBlank(targetFlavorOption)) {
        try {
            targetFlavor = MediaPackageElementFlavor.parseFlavor(targetFlavorOption);
        } catch (IllegalArgumentException e) {
            throw new WorkflowOperationException("Target flavor '" + targetFlavorOption + "' is malformed");
        }
    }
    // Look for elements matching the tag
    Collection<Track> elements = elementSelector.select(mediaPackage, false);
    // Encode all tracks found
    long totalTimeInQueue = 0;
    Map<Job, JobInformation> encodingJobs = new HashMap<Job, JobInformation>();
    for (Track track : elements) {
        // Encode the track with all profiles
        for (EncodingProfile profile : profiles) {
            // Check if the track supports the output type of the profile
            MediaType outputType = profile.getOutputType();
            if (outputType.equals(MediaType.Audio) && !track.hasAudio()) {
                logger.info("Skipping encoding of '{}', since it lacks an audio stream", track);
                continue;
            } else if (outputType.equals(MediaType.Visual) && !track.hasVideo()) {
                logger.info("Skipping encoding of '{}', since it lacks a video stream", track);
                continue;
            }
            logger.info("Encoding track {} using encoding profile '{}'", track, profile);
            // Start encoding and wait for the result
            encodingJobs.put(composerService.parallelEncode(track, profile.getIdentifier()), new JobInformation(track, profile));
        }
    }
    if (encodingJobs.isEmpty()) {
        logger.info("No matching tracks found");
        return createResult(mediaPackage, Action.CONTINUE);
    }
    // Wait for the jobs to return
    if (!waitForStatus(encodingJobs.keySet().toArray(new Job[encodingJobs.size()])).isSuccess()) {
        throw new WorkflowOperationException("One of the encoding jobs did not complete successfully");
    }
    // Process the result
    for (Map.Entry<Job, JobInformation> entry : encodingJobs.entrySet()) {
        Job job = entry.getKey();
        Track track = entry.getValue().getTrack();
        // add this receipt's queue time to the total
        totalTimeInQueue += job.getQueueTime();
        // it is allowed for compose jobs to return an empty payload. See the EncodeEngine interface
        if (job.getPayload().length() > 0) {
            List<Track> composedTracks = (List<Track>) MediaPackageElementParser.getArrayFromXml(job.getPayload());
            // Adjust the target tags
            for (Track encodedTrack : composedTracks) {
                for (String tag : targetTags) {
                    logger.trace("Tagging composed track {} with '{}'", encodedTrack.toString(), tag);
                    encodedTrack.addTag(tag);
                }
            }
            // Adjust the target flavor. Make sure to account for partial updates
            if (targetFlavor != null) {
                String flavorType = targetFlavor.getType();
                String flavorSubtype = targetFlavor.getSubtype();
                if ("*".equals(flavorType))
                    flavorType = track.getFlavor().getType();
                if ("*".equals(flavorSubtype))
                    flavorSubtype = track.getFlavor().getSubtype();
                for (Track encodedTrack : composedTracks) {
                    encodedTrack.setFlavor(new MediaPackageElementFlavor(flavorType, flavorSubtype));
                    logger.debug("Composed track {} has flavor '{}'", encodedTrack.toString(), encodedTrack.getFlavor());
                }
            }
            // store new tracks to mediaPackage
            for (Track encodedTrack : composedTracks) {
                mediaPackage.addDerived(encodedTrack, track);
                String fileName = getFileNameFromElements(track, encodedTrack);
                encodedTrack.setURI(workspace.moveTo(encodedTrack.getURI(), mediaPackage.getIdentifier().toString(), encodedTrack.getIdentifier(), fileName));
            }
        }
    }
    WorkflowOperationResult result = createResult(mediaPackage, Action.CONTINUE, totalTimeInQueue);
    logger.debug("Parallel encode operation completed");
    return result;
}
Also used : HashMap(java.util.HashMap) TrackSelector(org.opencastproject.mediapackage.selector.TrackSelector) ArrayList(java.util.ArrayList) EncodingProfile(org.opencastproject.composer.api.EncodingProfile) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) WorkflowOperationResult(org.opencastproject.workflow.api.WorkflowOperationResult) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) MediaType(org.opencastproject.composer.api.EncodingProfile.MediaType) ArrayList(java.util.ArrayList) List(java.util.List) Job(org.opencastproject.job.api.Job) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap) Track(org.opencastproject.mediapackage.Track)

Example 2 with EncodingProfile

use of org.opencastproject.composer.api.EncodingProfile in project opencast by opencast.

the class ImageWorkflowOperationHandler method configure.

/**
 * Get and parse the configuration options.
 */
private Cfg configure(MediaPackage mp, WorkflowOperationInstance woi) throws WorkflowOperationException {
    final List<EncodingProfile> profiles = getOptConfig(woi, OPT_PROFILES).toStream().bind(asList.toFn()).map(fetchProfile(composerService)).toList();
    final List<String> targetImageTags = getOptConfig(woi, OPT_TARGET_TAGS).toStream().bind(asList.toFn()).toList();
    final Opt<MediaPackageElementFlavor> targetImageFlavor = getOptConfig(woi, OPT_TARGET_FLAVOR).map(MediaPackageElementFlavor.parseFlavor.toFn());
    final List<Track> sourceTracks;
    {
        // get the source flavors
        final Stream<MediaPackageElementFlavor> sourceFlavors = getOptConfig(woi, OPT_SOURCE_FLAVORS).toStream().bind(Strings.splitCsv).append(getOptConfig(woi, OPT_SOURCE_FLAVOR)).map(MediaPackageElementFlavor.parseFlavor.toFn());
        // get the source tags
        final Stream<String> sourceTags = getOptConfig(woi, OPT_SOURCE_TAGS).toStream().bind(Strings.splitCsv);
        // fold both into a selector
        final TrackSelector trackSelector = sourceTags.apply(tagFold(sourceFlavors.apply(flavorFold(new TrackSelector()))));
        // select the tracks based on source flavors and tags and skip those that don't have video
        sourceTracks = $(trackSelector.select(mp, true)).filter(Filters.hasVideo.toFn()).each(new Fx<Track>() {

            @Override
            public void apply(Track track) {
                if (track.getDuration() == null) {
                    chuck(new WorkflowOperationException(format("Track %s cannot tell its duration", track)));
                }
            }
        }).toList();
    }
    final List<MediaPosition> positions = parsePositions(getConfig(woi, OPT_POSITIONS));
    final long endMargin = getOptConfig(woi, OPT_END_MARGIN).bind(Strings.toLong).getOr(END_MARGIN_DEFAULT);
    // 
    return new Cfg(sourceTracks, positions, profiles, targetImageFlavor, targetImageTags, getTargetBaseNameFormat(woi, OPT_TARGET_BASE_NAME_FORMAT_SECOND), getTargetBaseNameFormat(woi, OPT_TARGET_BASE_NAME_FORMAT_PERCENT), endMargin);
}
Also used : TrackSelector(org.opencastproject.mediapackage.selector.TrackSelector) EncodingProfile(org.opencastproject.composer.api.EncodingProfile) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) Fx(com.entwinemedia.fn.Fx) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) Stream(com.entwinemedia.fn.Stream) Track(org.opencastproject.mediapackage.Track)

Example 3 with EncodingProfile

use of org.opencastproject.composer.api.EncodingProfile 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;
}
Also used : HashMap(java.util.HashMap) TrackSelector(org.opencastproject.mediapackage.selector.TrackSelector) ArrayList(java.util.ArrayList) SMILElement(org.w3c.dom.smil.SMILElement) Attachment(org.opencastproject.mediapackage.Attachment) WorkflowOperationResult(org.opencastproject.workflow.api.WorkflowOperationResult) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) SMILParElement(org.w3c.dom.smil.SMILParElement) Job(org.opencastproject.job.api.Job) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) SMILDocument(org.w3c.dom.smil.SMILDocument) NodeList(org.w3c.dom.NodeList) EncodingProfile(org.opencastproject.composer.api.EncodingProfile) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) MediaPackage(org.opencastproject.mediapackage.MediaPackage) Track(org.opencastproject.mediapackage.Track)

Example 4 with EncodingProfile

use of org.opencastproject.composer.api.EncodingProfile 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);
}
Also used : Attachment(org.opencastproject.mediapackage.Attachment) Segment(org.opencastproject.metadata.mpeg7.Segment) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) Job(org.opencastproject.job.api.Job) HashSet(java.util.HashSet) MediaTimePoint(org.opencastproject.metadata.mpeg7.MediaTimePoint) EncodingProfile(org.opencastproject.composer.api.EncodingProfile) Catalog(org.opencastproject.mediapackage.Catalog) Mpeg7Catalog(org.opencastproject.metadata.mpeg7.Mpeg7Catalog) LinkedList(java.util.LinkedList) MediaTimePoint(org.opencastproject.metadata.mpeg7.MediaTimePoint) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) Mpeg7Catalog(org.opencastproject.metadata.mpeg7.Mpeg7Catalog) MediaPackageReference(org.opencastproject.mediapackage.MediaPackageReference) Video(org.opencastproject.metadata.mpeg7.Video) MediaPackageReferenceImpl(org.opencastproject.mediapackage.MediaPackageReferenceImpl) Track(org.opencastproject.mediapackage.Track)

Example 5 with EncodingProfile

use of org.opencastproject.composer.api.EncodingProfile in project opencast by opencast.

the class ComposeWorkflowOperationHandlerTest method testComposeEncodedTrack.

@Test
public void testComposeEncodedTrack() throws Exception {
    // set up mock profile
    profile = EasyMock.createNiceMock(EncodingProfile.class);
    EasyMock.expect(profile.getIdentifier()).andReturn(PROFILE_ID);
    EasyMock.expect(profile.getApplicableMediaType()).andReturn(MediaType.Stream);
    EasyMock.expect(profile.getOutputType()).andReturn(MediaType.AudioVisual);
    EasyMock.expect(profile.getMimeType()).andReturn(MimeTypes.MPEG4.toString()).times(2);
    profileList = new EncodingProfile[] { profile };
    EasyMock.replay(profile);
    // set up mock composer service
    composerService = EasyMock.createNiceMock(ComposerService.class);
    EasyMock.expect(composerService.getProfile(PROFILE_ID)).andReturn(profile);
    EasyMock.expect(composerService.encode((Track) EasyMock.anyObject(), (String) EasyMock.anyObject())).andReturn(job);
    EasyMock.replay(composerService);
    operationHandler.setComposerService(composerService);
    // operation configuration
    String targetTags = "engage,rss";
    Map<String, String> configurations = new HashMap<String, String>();
    configurations.put("source-flavors", "presentation/source");
    configurations.put("target-tags", targetTags);
    configurations.put("target-flavor", "presenter/delivery");
    configurations.put("encoding-profiles", "flash.http");
    // run the operation handler
    WorkflowOperationResult result = getWorkflowOperationResult(mp, configurations);
    // check track metadata
    MediaPackage mpNew = result.getMediaPackage();
    Track trackEncoded = mpNew.getTrack(ENCODED_TRACK_ID);
    Assert.assertEquals("presenter/delivery", trackEncoded.getFlavor().toString());
    Assert.assertArrayEquals(targetTags.split("\\W"), trackEncoded.getTags());
    Assert.assertEquals(SOURCE_TRACK_ID, trackEncoded.getReference().getIdentifier());
}
Also used : HashMap(java.util.HashMap) ComposerService(org.opencastproject.composer.api.ComposerService) MediaPackage(org.opencastproject.mediapackage.MediaPackage) EncodingProfile(org.opencastproject.composer.api.EncodingProfile) WorkflowOperationResult(org.opencastproject.workflow.api.WorkflowOperationResult) Track(org.opencastproject.mediapackage.Track) Test(org.junit.Test)

Aggregations

EncodingProfile (org.opencastproject.composer.api.EncodingProfile)38 EncoderException (org.opencastproject.composer.api.EncoderException)16 Track (org.opencastproject.mediapackage.Track)15 HashMap (java.util.HashMap)14 Job (org.opencastproject.job.api.Job)13 ArrayList (java.util.ArrayList)12 Test (org.junit.Test)12 Map (java.util.Map)10 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)10 File (java.io.File)9 URI (java.net.URI)8 IOException (java.io.IOException)7 WorkflowOperationException (org.opencastproject.workflow.api.WorkflowOperationException)7 Attachment (org.opencastproject.mediapackage.Attachment)6 MediaPackage (org.opencastproject.mediapackage.MediaPackage)6 MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)6 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)6 NotFoundException (org.opencastproject.util.NotFoundException)6 LinkedList (java.util.LinkedList)5 WorkflowOperationResult (org.opencastproject.workflow.api.WorkflowOperationResult)5