Search in sources :

Example 1 with UnsupportedElementException

use of org.opencastproject.mediapackage.UnsupportedElementException in project opencast by opencast.

the class MediaInspector method enrichTrack.

/**
 * Enriches the track's metadata and can be executed in an asynchronous way.
 *
 * @param originalTrack
 *          the original track
 * @param override
 *          <code>true</code> to override existing metadata
 * @return the media package element
 * @throws MediaInspectionException
 */
private MediaPackageElement enrichTrack(final Track originalTrack, final boolean override, final Map<String, String> options) throws MediaInspectionException {
    try {
        URI originalTrackUrl = originalTrack.getURI();
        MediaPackageElementFlavor flavor = originalTrack.getFlavor();
        logger.debug("enrich(" + originalTrackUrl + ") called");
        // Get the file from the URL
        File file = null;
        try {
            file = workspace.get(originalTrackUrl);
        } catch (NotFoundException e) {
            throw new MediaInspectionException("File " + originalTrackUrl + " was not found and can therefore not be " + "inspected", e);
        } catch (IOException e) {
            throw new MediaInspectionException("Error accessing " + originalTrackUrl, e);
        }
        // TODO: Try to guess the extension from the container's metadata
        if ("".equals(FilenameUtils.getExtension(file.getName()))) {
            throw new MediaInspectionException("Can not inspect files without a filename extension");
        }
        MediaContainerMetadata metadata = getFileMetadata(file, getAccurateFrameCount(options));
        if (metadata == null) {
            throw new MediaInspectionException("Unable to acquire media metadata for " + originalTrackUrl);
        } else {
            TrackImpl track = null;
            try {
                track = (TrackImpl) MediaPackageElementBuilderFactory.newInstance().newElementBuilder().elementFromURI(originalTrackUrl, MediaPackageElement.Type.Track, flavor);
            } catch (UnsupportedElementException e) {
                throw new MediaInspectionException("Unable to create track element from " + file, e);
            }
            // init the new track with old
            track.setChecksum(originalTrack.getChecksum());
            track.setDuration(originalTrack.getDuration());
            track.setElementDescription(originalTrack.getElementDescription());
            track.setFlavor(flavor);
            track.setIdentifier(originalTrack.getIdentifier());
            track.setMimeType(originalTrack.getMimeType());
            track.setReference(originalTrack.getReference());
            track.setSize(file.length());
            track.setURI(originalTrackUrl);
            for (String tag : originalTrack.getTags()) {
                track.addTag(tag);
            }
            // enrich the new track with basic info
            if (track.getDuration() == null || override)
                track.setDuration(metadata.getDuration());
            if (track.getChecksum() == null || override) {
                try {
                    track.setChecksum(Checksum.create(ChecksumType.DEFAULT_TYPE, file));
                } catch (IOException e) {
                    throw new MediaInspectionException("Unable to read " + file, e);
                }
            }
            // Add the mime type if it's not already present
            if (track.getMimeType() == null || override) {
                try {
                    MimeType mimeType = MimeTypes.fromURI(track.getURI());
                    // The mimetype library doesn't know about audio/video metadata, so the type might be wrong.
                    if ("audio".equals(mimeType.getType()) && metadata.hasVideoStreamMetadata()) {
                        mimeType = MimeTypes.parseMimeType("video/" + mimeType.getSubtype());
                    } else if ("video".equals(mimeType.getType()) && !metadata.hasVideoStreamMetadata()) {
                        mimeType = MimeTypes.parseMimeType("audio/" + mimeType.getSubtype());
                    }
                    track.setMimeType(mimeType);
                } catch (UnknownFileTypeException e) {
                    logger.info("Unable to detect the mimetype for track {} at {}", track.getIdentifier(), track.getURI());
                }
            }
            // find all streams
            Dictionary<String, Stream> streamsId2Stream = new Hashtable<String, Stream>();
            for (Stream stream : originalTrack.getStreams()) {
                streamsId2Stream.put(stream.getIdentifier(), stream);
            }
            // audio list
            try {
                addAudioStreamMetadata(track, metadata);
            } catch (Exception e) {
                throw new MediaInspectionException("Unable to extract audio metadata from " + file, e);
            }
            // video list
            try {
                addVideoStreamMetadata(track, metadata);
            } catch (Exception e) {
                throw new MediaInspectionException("Unable to extract video metadata from " + file, e);
            }
            logger.info("Successfully inspected track {}", track);
            return track;
        }
    } catch (Exception e) {
        logger.warn("Error enriching track " + originalTrack, e);
        if (e instanceof MediaInspectionException) {
            throw (MediaInspectionException) e;
        } else {
            throw new MediaInspectionException(e);
        }
    }
}
Also used : MediaContainerMetadata(org.opencastproject.inspection.ffmpeg.api.MediaContainerMetadata) TrackImpl(org.opencastproject.mediapackage.track.TrackImpl) Hashtable(java.util.Hashtable) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) URI(java.net.URI) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) MimeType(org.opencastproject.util.MimeType) MediaAnalyzerException(org.opencastproject.inspection.ffmpeg.api.MediaAnalyzerException) UnsupportedElementException(org.opencastproject.mediapackage.UnsupportedElementException) MediaInspectionException(org.opencastproject.inspection.api.MediaInspectionException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) UnknownFileTypeException(org.opencastproject.util.UnknownFileTypeException) MediaInspectionException(org.opencastproject.inspection.api.MediaInspectionException) UnsupportedElementException(org.opencastproject.mediapackage.UnsupportedElementException) UnknownFileTypeException(org.opencastproject.util.UnknownFileTypeException) Stream(org.opencastproject.mediapackage.Stream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) File(java.io.File)

Example 2 with UnsupportedElementException

use of org.opencastproject.mediapackage.UnsupportedElementException in project opencast by opencast.

the class InspectWorkflowOperationHandler method start.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.workflow.api.WorkflowOperationHandler#start(org.opencastproject.workflow.api.WorkflowInstance,
 *      JobContext)
 */
@Override
public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
    MediaPackage mediaPackage = (MediaPackage) workflowInstance.getMediaPackage().clone();
    // Inspect the tracks
    long totalTimeInQueue = 0;
    WorkflowOperationInstance operation = workflowInstance.getCurrentOperation();
    boolean rewrite = "true".equalsIgnoreCase(operation.getConfiguration(OPT_OVERWRITE));
    boolean acceptNoMedia = "true".equalsIgnoreCase(operation.getConfiguration(OPT_ACCEPT_NO_MEDIA));
    final Map<String, String> options = new HashMap<String, String>();
    if ("true".equalsIgnoreCase(operation.getConfiguration(OPT_ACCURATE_FRAME_COUNT))) {
        logger.info("Using accurate frame count for inspection media package {}", mediaPackage);
        options.put(MediaInspectionOptions.OPTION_ACCURATE_FRAME_COUNT, Boolean.TRUE.toString());
    }
    // Test if there are tracks in the mediapackage
    if (mediaPackage.getTracks().length == 0) {
        logger.warn("Recording {} contains no media", mediaPackage);
        if (!acceptNoMedia)
            throw new WorkflowOperationException("Mediapackage " + mediaPackage + " contains no media");
    }
    for (Track track : mediaPackage.getTracks()) {
        logger.info("Inspecting track '{}' of {}", track.getIdentifier(), mediaPackage);
        Job inspectJob = null;
        Track inspectedTrack;
        if (track != null && track.getURI() != null && (track.getURI().toString().endsWith(".vtt") || track.getURI().toString().endsWith(".srt"))) {
            inspectedTrack = (Track) track.clone();
            inspectedTrack.setMimeType(MimeType.mimeType("text", "vtt"));
            logger.info("Track '{}' of {} contains captions", track.getIdentifier(), mediaPackage);
        } else {
            try {
                inspectJob = inspectionService.enrich(track, rewrite, options);
                if (!waitForStatus(inspectJob).isSuccess()) {
                    throw new WorkflowOperationException("Track " + track + " could not be inspected");
                }
            } catch (MediaInspectionException e) {
                throw new WorkflowOperationException("Error inspecting media package", e);
            } catch (MediaPackageException e) {
                throw new WorkflowOperationException("Error parsing media package", e);
            }
            // add this receipt's queue and execution times to the total
            long timeInQueue = inspectJob.getQueueTime() == null ? 0 : inspectJob.getQueueTime();
            totalTimeInQueue += timeInQueue;
            try {
                inspectedTrack = (Track) MediaPackageElementParser.getFromXml(inspectJob.getPayload());
            } catch (MediaPackageException e) {
                throw new WorkflowOperationException("Unable to parse track from job " + inspectJob.getId(), e);
            }
            if (inspectedTrack == null)
                throw new WorkflowOperationException("Track " + track + " could not be inspected");
            if (inspectedTrack.getStreams().length == 0)
                throw new WorkflowOperationException(format("Track %s does not contain any streams", track));
        }
        // Replace the original track with the inspected one
        try {
            mediaPackage.remove(track);
            mediaPackage.add(inspectedTrack);
        } catch (UnsupportedElementException e) {
            logger.error("Error adding {} to media package", inspectedTrack, e);
        }
    }
    // Update dublin core with metadata
    try {
        updateDublinCore(mediaPackage);
    } catch (Exception e) {
        logger.warn("Unable to update dublin core data: {}", e.getMessage(), e);
        throw new WorkflowOperationException(e.getMessage());
    }
    return createResult(mediaPackage, Action.CONTINUE, totalTimeInQueue);
}
Also used : MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) HashMap(java.util.HashMap) UnsupportedElementException(org.opencastproject.mediapackage.UnsupportedElementException) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) MediaInspectionException(org.opencastproject.inspection.api.MediaInspectionException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) MediaInspectionException(org.opencastproject.inspection.api.MediaInspectionException) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) UnsupportedElementException(org.opencastproject.mediapackage.UnsupportedElementException) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) Job(org.opencastproject.job.api.Job) Track(org.opencastproject.mediapackage.Track)

Example 3 with UnsupportedElementException

use of org.opencastproject.mediapackage.UnsupportedElementException in project opencast by opencast.

the class PublicationBuilderPlugin method elementFromManifest.

@Override
public MediaPackageElement elementFromManifest(Node elementNode, MediaPackageSerializer serializer) throws UnsupportedElementException {
    String id = null;
    MimeType mimeType = null;
    MediaPackageElementFlavor flavor = null;
    String reference = null;
    String channel = null;
    URI url = null;
    long size = -1;
    Checksum checksum = null;
    try {
        // id
        id = (String) xpath.evaluate("@id", elementNode, XPathConstants.STRING);
        if (StringUtils.isEmpty(id)) {
            throw new UnsupportedElementException("Unvalid or missing id argument!");
        }
        // url
        url = serializer.decodeURI(new URI(xpath.evaluate("url/text()", elementNode).trim()));
        // channel
        channel = xpath.evaluate("@channel", elementNode).trim();
        if (StringUtils.isEmpty(channel)) {
            throw new UnsupportedElementException("Unvalid or missing channel argument!");
        }
        // reference
        reference = (String) xpath.evaluate("@ref", elementNode, XPathConstants.STRING);
        // size
        String trackSize = xpath.evaluate("size/text()", elementNode).trim();
        if (!"".equals(trackSize))
            size = Long.parseLong(trackSize);
        // flavor
        String flavorValue = (String) xpath.evaluate("@type", elementNode, XPathConstants.STRING);
        if (StringUtils.isNotEmpty(flavorValue))
            flavor = MediaPackageElementFlavor.parseFlavor(flavorValue);
        // checksum
        String checksumValue = (String) xpath.evaluate("checksum/text()", elementNode, XPathConstants.STRING);
        String checksumType = (String) xpath.evaluate("checksum/@type", elementNode, XPathConstants.STRING);
        if (StringUtils.isNotEmpty(checksumValue) && checksumType != null)
            checksum = Checksum.create(checksumType.trim(), checksumValue.trim());
        // mimetype
        String mimeTypeValue = (String) xpath.evaluate("mimetype/text()", elementNode, XPathConstants.STRING);
        if (StringUtils.isNotEmpty(mimeTypeValue)) {
            mimeType = MimeTypes.parseMimeType(mimeTypeValue);
        } else {
            throw new UnsupportedElementException("Unvalid or missing mimetype argument!");
        }
        // Build the publication element
        PublicationImpl publication = new PublicationImpl(id, channel, url, mimeType);
        if (StringUtils.isNotBlank(id))
            publication.setIdentifier(id);
        // Add url
        publication.setURI(url);
        // Add reference
        if (StringUtils.isNotEmpty(reference))
            publication.referTo(MediaPackageReferenceImpl.fromString(reference));
        // Set size
        if (size > 0)
            publication.setSize(size);
        // Set checksum
        if (checksum != null)
            publication.setChecksum(checksum);
        // Set mimetpye
        if (mimeType != null)
            publication.setMimeType(mimeType);
        if (flavor != null)
            publication.setFlavor(flavor);
        // description
        String description = (String) xpath.evaluate("description/text()", elementNode, XPathConstants.STRING);
        if (StringUtils.isNotBlank(description))
            publication.setElementDescription(description.trim());
        // tags
        NodeList tagNodes = (NodeList) xpath.evaluate("tags/tag", elementNode, XPathConstants.NODESET);
        for (int i = 0; i < tagNodes.getLength(); i++) {
            publication.addTag(tagNodes.item(i).getTextContent());
        }
        return publication;
    } catch (XPathExpressionException e) {
        throw new UnsupportedElementException("Error while reading track information from manifest: " + e.getMessage());
    } catch (NoSuchAlgorithmException e) {
        throw new UnsupportedElementException("Unsupported digest algorithm: " + e.getMessage());
    } catch (URISyntaxException e) {
        throw new UnsupportedElementException("Error while reading presenter track " + url + ": " + e.getMessage());
    }
}
Also used : XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) URISyntaxException(java.net.URISyntaxException) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) URI(java.net.URI) MimeType(org.opencastproject.util.MimeType) PublicationImpl(org.opencastproject.mediapackage.PublicationImpl) UnsupportedElementException(org.opencastproject.mediapackage.UnsupportedElementException) Checksum(org.opencastproject.util.Checksum)

Example 4 with UnsupportedElementException

use of org.opencastproject.mediapackage.UnsupportedElementException in project opencast by opencast.

the class ExecuteServiceImpl method runCommand.

private String runCommand(List<String> command, File outFile, Type expectedType) throws ExecuteException {
    Process p = null;
    int result = 0;
    try {
        logger.info("Running command {}", command.get(0));
        logger.debug("Starting subprocess {} with arguments {}", command.get(0), StringUtils.join(command.subList(1, command.size()), ", "));
        ProcessBuilder pb = new ProcessBuilder(command);
        pb.redirectErrorStream(true);
        p = pb.start();
        result = p.waitFor();
        logger.debug("Command {} finished with result {}", command.get(0), result);
        if (result == 0) {
            // Read the command output
            if (outFile != null) {
                if (outFile.isFile()) {
                    URI newURI = workspace.putInCollection(ExecuteService.COLLECTION, outFile.getName(), new FileInputStream(outFile));
                    if (outFile.delete()) {
                        logger.debug("Deleted the local copy of the encoded file at {}", outFile.getAbsolutePath());
                    } else {
                        logger.warn("Unable to delete the encoding output at {}", outFile.getAbsolutePath());
                    }
                    return MediaPackageElementParser.getAsXml(MediaPackageElementBuilderFactory.newInstance().newElementBuilder().elementFromURI(newURI, expectedType, null));
                } else {
                    throw new ExecuteException("Expected output file does not exist: " + outFile.getAbsolutePath());
                }
            }
            return "";
        } else {
            // 'Scanner' reads tokens delimited by an specific character (set).
            // By telling a Scanner to use the 'beginning of the input boundary' character as delimiter, which of course
            // will never find, yields the whole String as the next token.
            String line;
            try (Scanner scanner = new Scanner(p.getInputStream())) {
                scanner.useDelimiter("\\A");
                line = scanner.next();
            } catch (NoSuchElementException e) {
                line = "";
            }
            throw new ExecuteException(String.format("Process %s returned error code %d with this output:\n%s", command.get(0), result, line.trim()));
        }
    } catch (InterruptedException e) {
        throw new ExecuteException("The executor thread has been unexpectedly interrupted", e);
    } catch (IOException e) {
        // Only log the first argument, the executable, as other arguments may contain sensitive values
        // e.g. MySQL password/user, paths, etc. that should not be shown to caller
        logger.error("Could not start subprocess {}", command.get(0));
        throw new ExecuteException("Could not start subprocess: " + command.get(0), e);
    } catch (UnsupportedElementException e) {
        throw new ExecuteException("Couldn't create a new MediaPackage element of type " + expectedType.toString(), e);
    } catch (ConfigurationException e) {
        throw new ExecuteException("Couldn't instantiate a new MediaPackage element builder", e);
    } catch (MediaPackageException e) {
        throw new ExecuteException("Couldn't serialize a new Mediapackage element of type " + expectedType.toString(), e);
    } finally {
        IoSupport.closeQuietly(p);
    }
}
Also used : Scanner(java.util.Scanner) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) IOException(java.io.IOException) URI(java.net.URI) FileInputStream(java.io.FileInputStream) UnsupportedElementException(org.opencastproject.mediapackage.UnsupportedElementException) ConfigurationException(org.opencastproject.util.ConfigurationException) ExecuteException(org.opencastproject.execute.api.ExecuteException) NoSuchElementException(java.util.NoSuchElementException)

Example 5 with UnsupportedElementException

use of org.opencastproject.mediapackage.UnsupportedElementException in project opencast by opencast.

the class EventCommentParser method commentFromManifest.

private static EventComment commentFromManifest(Node commentNode, UserDirectoryService userDirectoryService) throws UnsupportedElementException {
    try {
        // id
        Long id = null;
        Double idAsDouble = ((Number) xpath.evaluate("@id", commentNode, XPathConstants.NUMBER)).doubleValue();
        if (!idAsDouble.isNaN())
            id = idAsDouble.longValue();
        final String eventId = (String) xpath.evaluate("@eventId", commentNode, STRING);
        final String organization = (String) xpath.evaluate("@organization", commentNode, STRING);
        // text
        String text = (String) xpath.evaluate("text/text()", commentNode, XPathConstants.STRING);
        // Author
        Node authorNode = (Node) xpath.evaluate("author", commentNode, XPathConstants.NODE);
        User author = userFromManifest(authorNode, userDirectoryService);
        // ResolvedStatus
        Boolean resolved = BooleanUtils.toBoolean((Boolean) xpath.evaluate("@resolved", commentNode, XPathConstants.BOOLEAN));
        // Reason
        String reason = (String) xpath.evaluate("reason/text()", commentNode, XPathConstants.STRING);
        if (StringUtils.isNotBlank(reason))
            reason = reason.trim();
        // CreationDate
        String creationDateString = (String) xpath.evaluate("creationDate/text()", commentNode, XPathConstants.STRING);
        Date creationDate = new Date(DateTimeSupport.fromUTC(creationDateString));
        // ModificationDate
        String modificationDateString = (String) xpath.evaluate("modificationDate/text()", commentNode, XPathConstants.STRING);
        Date modificationDate = new Date(DateTimeSupport.fromUTC(modificationDateString));
        // Create comment
        EventComment comment = EventComment.create(Option.option(id), eventId, organization, text.trim(), author, reason, resolved, creationDate, modificationDate);
        // Replies
        NodeList replyNodes = (NodeList) xpath.evaluate("replies/reply", commentNode, XPathConstants.NODESET);
        for (int i = 0; i < replyNodes.getLength(); i++) {
            comment.addReply(replyFromManifest(replyNodes.item(i), userDirectoryService));
        }
        return comment;
    } catch (XPathExpressionException e) {
        throw new UnsupportedElementException("Error while reading comment information from manifest", e);
    } catch (Exception e) {
        if (e instanceof UnsupportedElementException)
            throw (UnsupportedElementException) e;
        throw new UnsupportedElementException("Error while reading comment creation or modification date information from manifest", e);
    }
}
Also used : User(org.opencastproject.security.api.User) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) Date(java.util.Date) XPathExpressionException(javax.xml.xpath.XPathExpressionException) UnsupportedElementException(org.opencastproject.mediapackage.UnsupportedElementException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) UnsupportedElementException(org.opencastproject.mediapackage.UnsupportedElementException)

Aggregations

UnsupportedElementException (org.opencastproject.mediapackage.UnsupportedElementException)11 URI (java.net.URI)6 XPathExpressionException (javax.xml.xpath.XPathExpressionException)6 MimeType (org.opencastproject.util.MimeType)6 NodeList (org.w3c.dom.NodeList)5 IOException (java.io.IOException)4 URISyntaxException (java.net.URISyntaxException)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)4 Checksum (org.opencastproject.util.Checksum)4 FileInputStream (java.io.FileInputStream)3 MediaInspectionException (org.opencastproject.inspection.api.MediaInspectionException)3 TrackImpl (org.opencastproject.mediapackage.track.TrackImpl)3 NotFoundException (org.opencastproject.util.NotFoundException)3 Node (org.w3c.dom.Node)3 File (java.io.File)2 InputStream (java.io.InputStream)2 Date (java.util.Date)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 MediaAnalyzerException (org.opencastproject.inspection.ffmpeg.api.MediaAnalyzerException)2