Search in sources :

Example 6 with TrackSelector

use of org.opencastproject.mediapackage.selector.TrackSelector 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 7 with TrackSelector

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

the class ConcatWorkflowOperationHandler method getTrackSelectors.

private Map<Integer, Tuple<TrackSelector, Boolean>> getTrackSelectors(WorkflowOperationInstance operation) throws WorkflowOperationException {
    Map<Integer, Tuple<TrackSelector, Boolean>> trackSelectors = new HashMap<Integer, Tuple<TrackSelector, Boolean>>();
    for (String key : operation.getConfigurationKeys()) {
        String tags = null;
        String flavor = null;
        Boolean mandatory = true;
        int number = -1;
        if (key.startsWith(SOURCE_TAGS_PREFIX) && !key.endsWith(MANDATORY_SUFFIX)) {
            number = NumberUtils.toInt(key.substring(SOURCE_TAGS_PREFIX.length()), -1);
            tags = operation.getConfiguration(key);
            mandatory = BooleanUtils.toBooleanObject(operation.getConfiguration(SOURCE_TAGS_PREFIX.concat(Integer.toString(number)).concat(MANDATORY_SUFFIX)));
        } else if (key.startsWith(SOURCE_FLAVOR_PREFIX) && !key.endsWith(MANDATORY_SUFFIX)) {
            number = NumberUtils.toInt(key.substring(SOURCE_FLAVOR_PREFIX.length()), -1);
            flavor = operation.getConfiguration(key);
            mandatory = BooleanUtils.toBooleanObject(operation.getConfiguration(SOURCE_FLAVOR_PREFIX.concat(Integer.toString(number)).concat(MANDATORY_SUFFIX)));
        }
        if (number < 0)
            continue;
        Tuple<TrackSelector, Boolean> selectorTuple = trackSelectors.get(number);
        if (selectorTuple == null) {
            selectorTuple = Tuple.tuple(new TrackSelector(), BooleanUtils.toBooleanDefaultIfNull(mandatory, false));
        } else {
            selectorTuple = Tuple.tuple(selectorTuple.getA(), selectorTuple.getB() || BooleanUtils.toBooleanDefaultIfNull(mandatory, false));
        }
        TrackSelector trackSelector = selectorTuple.getA();
        if (StringUtils.isNotBlank(tags)) {
            for (String tag : StringUtils.split(tags, ",")) {
                trackSelector.addTag(tag);
            }
        }
        if (StringUtils.isNotBlank(flavor)) {
            try {
                trackSelector.addFlavor(flavor);
            } catch (IllegalArgumentException e) {
                throw new WorkflowOperationException("Source flavor '" + flavor + "' is malformed");
            }
        }
        trackSelectors.put(number, selectorTuple);
    }
    return trackSelectors;
}
Also used : HashMap(java.util.HashMap) TrackSelector(org.opencastproject.mediapackage.selector.TrackSelector) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) Tuple(org.opencastproject.util.data.Tuple)

Example 8 with TrackSelector

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

the class VideoEditorWorkflowOperationHandlerTest method testEditorResume.

@Test
public void testEditorResume() throws WorkflowOperationException, URISyntaxException, NotFoundException, IOException, ProcessFailedException, ServiceRegistryException, MediaPackageException {
    // filled smil file
    URI episodeSmilURI = VideoEditorWorkflowOperationHandlerTest.class.getResource("/editor_smil_filled.smil").toURI();
    File episodeSmilFile = new File(episodeSmilURI);
    // setup mock services
    EasyMock.expect(workspaceMock.get((URI) EasyMock.anyObject())).andReturn(episodeSmilFile);
    EasyMock.expect(workspaceMock.put((String) EasyMock.anyObject(), (String) EasyMock.anyObject(), (String) EasyMock.anyObject(), (InputStream) EasyMock.anyObject())).andReturn(episodeSmilURI);
    EasyMock.expect(workspaceMock.moveTo((URI) EasyMock.anyObject(), (String) EasyMock.anyObject(), (String) EasyMock.anyObject(), (String) EasyMock.anyObject())).andReturn(URI.create("http://localhost:8080/foo/trimmed.mp4"));
    Job job = EasyMock.createNiceMock(Job.class);
    EasyMock.expect(job.getPayload()).andReturn(MediaPackageElementParser.getAsXml(mpSmil.getTracks()[0])).anyTimes();
    EasyMock.expect(job.getStatus()).andReturn(Job.Status.FINISHED);
    ServiceRegistry serviceRegistry = EasyMock.createNiceMock(ServiceRegistry.class);
    videoEditorWorkflowOperationHandler.setServiceRegistry(serviceRegistry);
    EasyMock.expect(serviceRegistry.getJob(EasyMock.anyLong())).andReturn(job);
    EasyMock.expect(videoEditorServiceMock.processSmil((Smil) EasyMock.anyObject())).andReturn(Arrays.asList(job));
    EasyMock.replay(workspaceMock, job, serviceRegistry, videoEditorServiceMock);
    WorkflowInstanceImpl workflowInstance = getWorkflowInstance(mpSmil, getDefaultConfiguration(true));
    // run test
    WorkflowOperationResult result = videoEditorWorkflowOperationHandler.resume(workflowInstance, null, null);
    Assert.assertNotNull("VideoEditor workflow operation returns null but should be an instantiated WorkflowOperationResult", result);
    EasyMock.verify(workspaceMock, job, serviceRegistry, videoEditorServiceMock);
    // verify trimmed track derived from source track
    WorkflowOperationInstance worflowOperationInstance = workflowInstance.getCurrentOperation();
    String targetFlavorSubtypeProperty = worflowOperationInstance.getConfiguration("target-flavor-subtype");
    String sourceFlavorsProperty = worflowOperationInstance.getConfiguration("source-flavors");
    TrackSelector trackSelector = new TrackSelector();
    trackSelector.addFlavor(sourceFlavorsProperty);
    Collection<Track> sourceTracks = trackSelector.select(result.getMediaPackage(), false);
    Assert.assertTrue("Mediapackage does not contain any tracks matching flavor " + sourceFlavorsProperty, sourceTracks != null && !sourceTracks.isEmpty());
    for (Track sourceTrack : sourceTracks) {
        MediaPackageElementFlavor targetFlavor = MediaPackageElementFlavor.flavor(sourceTrack.getFlavor().getType(), targetFlavorSubtypeProperty);
        Track[] targetTracks = result.getMediaPackage().getTracks(targetFlavor);
        Assert.assertTrue("Media package doesn't contain track with flavor " + targetFlavor.toString(), targetTracks != null && targetTracks.length > 0);
    }
}
Also used : TrackSelector(org.opencastproject.mediapackage.selector.TrackSelector) URI(java.net.URI) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) WorkflowOperationResult(org.opencastproject.workflow.api.WorkflowOperationResult) WorkflowInstanceImpl(org.opencastproject.workflow.api.WorkflowInstanceImpl) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) ServiceRegistry(org.opencastproject.serviceregistry.api.ServiceRegistry) Job(org.opencastproject.job.api.Job) File(java.io.File) Track(org.opencastproject.mediapackage.Track) Test(org.junit.Test)

Example 9 with TrackSelector

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

the class AnalyzeAudioWorkflowOperationHandler method analyze.

private WorkflowOperationResult analyze(MediaPackage src, WorkflowOperationInstance operation) throws SoxException, IOException, NotFoundException, MediaPackageException, WorkflowOperationException, EncoderException {
    MediaPackage mediaPackage = (MediaPackage) src.clone();
    // Check which tags have been configured
    String sourceTagsOption = StringUtils.trimToNull(operation.getConfiguration("source-tags"));
    String sourceFlavorOption = StringUtils.trimToNull(operation.getConfiguration("source-flavor"));
    String sourceFlavorsOption = StringUtils.trimToNull(operation.getConfiguration("source-flavors"));
    boolean forceTranscode = BooleanUtils.toBoolean(operation.getConfiguration("force-transcode"));
    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);
    }
    // Look for elements matching the tag
    Collection<Track> elements = elementSelector.select(mediaPackage, false);
    // Analyze audio for all tracks found
    long totalTimeInQueue = 0;
    List<URI> cleanupURIs = new ArrayList<URI>();
    Map<Job, Track> analyzeJobs = new HashMap<Job, Track>();
    try {
        for (Track track : elements) {
            TrackImpl audioTrack = (TrackImpl) track;
            // Skip video only mismatches
            if (!track.hasAudio()) {
                logger.info("Skipping audio analysis of '{}', since it contains no audio stream", track);
                continue;
            } else if (track.hasVideo() || forceTranscode) {
                audioTrack = (TrackImpl) extractAudioTrack(track);
                audioTrack.setAudio(((TrackImpl) track).getAudio());
                cleanupURIs.add(audioTrack.getURI());
            }
            analyzeJobs.put(soxService.analyze(audioTrack), track);
        }
        if (analyzeJobs.isEmpty()) {
            logger.info("No matching tracks found");
            return createResult(mediaPackage, Action.CONTINUE);
        }
        // Wait for the jobs to return
        if (!waitForStatus(analyzeJobs.keySet().toArray(new Job[analyzeJobs.size()])).isSuccess())
            throw new WorkflowOperationException("One of the analyze jobs did not complete successfully");
        // Process the result
        for (Map.Entry<Job, Track> entry : analyzeJobs.entrySet()) {
            Job job = entry.getKey();
            TrackImpl origTrack = (TrackImpl) entry.getValue();
            // add this receipt's queue time to the total
            totalTimeInQueue += job.getQueueTime();
            if (job.getPayload().length() > 0) {
                TrackImpl analyzed = (TrackImpl) MediaPackageElementParser.getFromXml(job.getPayload());
                // Set metadata on track
                origTrack.setAudio(analyzed.getAudio());
            } else {
                logger.warn("Analyze audio job {} for track {} has no result!", job, origTrack);
            }
        }
    } finally {
        // Clean up temporary audio files from workspace
        for (URI uri : cleanupURIs) {
            workspace.delete(uri);
        }
    }
    WorkflowOperationResult result = createResult(mediaPackage, Action.CONTINUE, totalTimeInQueue);
    logger.debug("Analyze audio operation completed");
    return result;
}
Also used : HashMap(java.util.HashMap) TrackImpl(org.opencastproject.mediapackage.track.TrackImpl) TrackSelector(org.opencastproject.mediapackage.selector.TrackSelector) ArrayList(java.util.ArrayList) URI(java.net.URI) WorkflowOperationResult(org.opencastproject.workflow.api.WorkflowOperationResult) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) 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 10 with TrackSelector

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

the class NormalizeAudioWorkflowOperationHandler method normalize.

private WorkflowOperationResult normalize(MediaPackage src, WorkflowOperationInstance operation) throws SoxException, IOException, NotFoundException, MediaPackageException, WorkflowOperationException, EncoderException {
    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"));
    String targetDecibelString = StringUtils.trimToNull(operation.getConfiguration("target-decibel"));
    if (targetDecibelString == null)
        throw new IllegalArgumentException("target-decibel must be specified");
    boolean forceTranscode = BooleanUtils.toBoolean(operation.getConfiguration("force-transcode"));
    Float targetDecibel;
    try {
        targetDecibel = new Float(targetDecibelString);
    } catch (NumberFormatException e1) {
        throw new WorkflowOperationException("Unable to parse target-decibel " + targetDecibelString);
    }
    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);
    }
    // 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;
    List<URI> cleanupURIs = new ArrayList<URI>();
    Map<Job, Track> normalizeJobs = new HashMap<Job, Track>();
    try {
        for (Track track : elements) {
            TrackImpl audioTrack = (TrackImpl) track;
            // Skip video only mismatches
            if (!track.hasAudio()) {
                logger.info("Skipping audio normalization of '{}', since it contains no audio stream", track);
                continue;
            } else if (track.hasVideo() || forceTranscode) {
                audioTrack = (TrackImpl) extractAudioTrack(track);
                audioTrack.setAudio(((TrackImpl) track).getAudio());
                cleanupURIs.add(audioTrack.getURI());
            }
            // Analyze audio track
            if (audioTrack.getAudio().size() < 1 || audioTrack.getAudio().get(0).getRmsLevDb() == null) {
                logger.info("Audio track {} has no RMS Lev dB metadata, analyze it first", audioTrack);
                Job analyzeJob = soxService.analyze(audioTrack);
                if (!waitForStatus(analyzeJob).isSuccess())
                    throw new WorkflowOperationException("Unable to analyze the audio track " + audioTrack);
                audioTrack = (TrackImpl) MediaPackageElementParser.getFromXml(analyzeJob.getPayload());
                cleanupURIs.add(audioTrack.getURI());
            }
            normalizeJobs.put(soxService.normalize(audioTrack, targetDecibel), track);
        }
        if (normalizeJobs.isEmpty()) {
            logger.info("No matching tracks found");
            return createResult(mediaPackage, Action.CONTINUE);
        }
        // Wait for the jobs to return
        if (!waitForStatus(normalizeJobs.keySet().toArray(new Job[normalizeJobs.size()])).isSuccess())
            throw new WorkflowOperationException("One of the normalize jobs did not complete successfully");
        // Process the result
        for (Map.Entry<Job, Track> entry : normalizeJobs.entrySet()) {
            Job job = entry.getKey();
            TrackImpl origTrack = (TrackImpl) entry.getValue();
            // add this receipt's queue time to the total
            totalTimeInQueue += job.getQueueTime();
            if (job.getPayload().length() > 0) {
                TrackImpl normalizedAudioTrack = (TrackImpl) MediaPackageElementParser.getFromXml(job.getPayload());
                TrackImpl resultTrack = normalizedAudioTrack;
                if (origTrack.hasVideo() || forceTranscode) {
                    cleanupURIs.add(normalizedAudioTrack.getURI());
                    logger.info("Mux normalized audio track {} to video track {}", normalizedAudioTrack, origTrack);
                    Job muxAudioVideo = composerService.mux(origTrack, normalizedAudioTrack, SOX_AREPLACE_PROFILE);
                    if (!waitForStatus(muxAudioVideo).isSuccess())
                        throw new WorkflowOperationException("Muxing normalized audio track " + normalizedAudioTrack + " to video container " + origTrack + " failed");
                    resultTrack = (TrackImpl) MediaPackageElementParser.getFromXml(muxAudioVideo.getPayload());
                    // Set metadata on track
                    extendAudioStream(resultTrack, normalizedAudioTrack);
                }
                adjustFlavorAndTags(targetTags, targetFlavor, origTrack, resultTrack);
                mediaPackage.addDerived(resultTrack, origTrack);
                String fileName = getFileNameFromElements(origTrack, resultTrack);
                resultTrack.setURI(workspace.moveTo(resultTrack.getURI(), mediaPackage.getIdentifier().toString(), resultTrack.getIdentifier(), fileName));
            } else {
                logger.warn("Normalize audio job {} for track {} has no result!", job, origTrack);
            }
        }
    } finally {
        // Clean up temporary audio and video files from workspace
        for (URI uri : cleanupURIs) {
            workspace.delete(uri);
        }
    }
    WorkflowOperationResult result = createResult(mediaPackage, Action.CONTINUE, totalTimeInQueue);
    logger.debug("Normalize audio operation completed");
    return result;
}
Also used : HashMap(java.util.HashMap) TrackImpl(org.opencastproject.mediapackage.track.TrackImpl) TrackSelector(org.opencastproject.mediapackage.selector.TrackSelector) ArrayList(java.util.ArrayList) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) URI(java.net.URI) WorkflowOperationResult(org.opencastproject.workflow.api.WorkflowOperationResult) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) 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)

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