Search in sources :

Example 1 with UnknownFileTypeException

use of org.opencastproject.util.UnknownFileTypeException in project opencast by opencast.

the class MediaInspector method enrichElement.

/**
 * Enriches the media package element metadata such as the mime type, the file size etc. The method mutates the
 * argument element.
 *
 * @param element
 *          the media package element
 * @param override
 *          <code>true</code> to overwrite existing metadata
 * @return the enriched element
 * @throws MediaInspectionException
 *           if enriching fails
 */
private MediaPackageElement enrichElement(final MediaPackageElement element, final boolean override, final Map<String, String> options) throws MediaInspectionException {
    try {
        File file;
        try {
            file = workspace.get(element.getURI());
        } catch (NotFoundException e) {
            throw new MediaInspectionException("Unable to find " + element.getURI() + " in the workspace", e);
        } catch (IOException e) {
            throw new MediaInspectionException("Error accessing " + element.getURI() + " in the workspace", e);
        }
        // Checksum
        if (element.getChecksum() == null || override) {
            try {
                element.setChecksum(Checksum.create(ChecksumType.DEFAULT_TYPE, file));
            } catch (IOException e) {
                throw new MediaInspectionException("Error generating checksum for " + element.getURI(), e);
            }
        }
        // Mimetype
        if (element.getMimeType() == null || override) {
            try {
                element.setMimeType(MimeTypes.fromURI(file.toURI()));
            } catch (UnknownFileTypeException e) {
                logger.info("unable to determine the mime type for {}", file.getName());
            }
        }
        logger.info("Successfully inspected element {}", element);
        return element;
    } catch (Exception e) {
        logger.warn("Error enriching element " + element, e);
        if (e instanceof MediaInspectionException) {
            throw (MediaInspectionException) e;
        } else {
            throw new MediaInspectionException(e);
        }
    }
}
Also used : MediaInspectionException(org.opencastproject.inspection.api.MediaInspectionException) UnknownFileTypeException(org.opencastproject.util.UnknownFileTypeException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) File(java.io.File) 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)

Example 2 with UnknownFileTypeException

use of org.opencastproject.util.UnknownFileTypeException 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 3 with UnknownFileTypeException

use of org.opencastproject.util.UnknownFileTypeException in project opencast by opencast.

the class TimelinePreviewsServiceImpl method generatePreviewImages.

/**
 * Starts generation of timeline preview images for the given video track
 * and returns an attachment containing one image that contains all the
 * timeline preview images.
 *
 * @param job
 * @param track the element to analyze
 * @param imageCount number of preview images that will be generated
 * @return an attachment containing the resulting timeline previews image
 * @throws TimelinePreviewsException
 * @throws org.opencastproject.mediapackage.MediaPackageException
 */
protected Attachment generatePreviewImages(Job job, Track track, int imageCount) throws TimelinePreviewsException, MediaPackageException {
    // Make sure the element can be analyzed using this analysis implementation
    if (!track.hasVideo()) {
        logger.error("Element {} is not a video track", track.getIdentifier());
        throw new TimelinePreviewsException("Element is not a video track");
    }
    try {
        if (track.getDuration() == null)
            throw new MediaPackageException("Track " + track + " does not have a duration");
        double duration = track.getDuration() / 1000.0;
        double seconds = duration / (double) (imageCount);
        seconds = seconds <= 0.0 ? 1.0 : seconds;
        // calculate number of tiles for row and column in tiled image
        int imageSize = (int) Math.ceil(Math.sqrt(imageCount));
        Attachment composedImage = createPreviewsFFmpeg(track, seconds, resolutionX, resolutionY, imageSize, imageSize, duration);
        if (composedImage == null)
            throw new IllegalStateException("Unable to compose image");
        // Set the mimetype
        try {
            composedImage.setMimeType(MimeTypes.parseMimeType(mimetype));
        } catch (IllegalArgumentException e) {
            logger.warn("Invalid mimetype provided for timeline previews image");
            try {
                composedImage.setMimeType(MimeTypes.fromURI(composedImage.getURI()));
            } catch (UnknownFileTypeException ex) {
                logger.warn("No valid mimetype could be found for timeline previews image");
            }
        }
        composedImage.getProperties().put("imageCount", String.valueOf(imageCount));
        return composedImage;
    } catch (Exception e) {
        logger.warn("Error creating timeline preview images for " + track, e);
        if (e instanceof TimelinePreviewsException) {
            throw (TimelinePreviewsException) e;
        } else {
            throw new TimelinePreviewsException(e);
        }
    }
}
Also used : TimelinePreviewsException(org.opencastproject.timelinepreviews.api.TimelinePreviewsException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) UnknownFileTypeException(org.opencastproject.util.UnknownFileTypeException) Attachment(org.opencastproject.mediapackage.Attachment) TimelinePreviewsException(org.opencastproject.timelinepreviews.api.TimelinePreviewsException) ConfigurationException(org.osgi.service.cm.ConfigurationException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) FileNotFoundException(java.io.FileNotFoundException) UnknownFileTypeException(org.opencastproject.util.UnknownFileTypeException)

Example 4 with UnknownFileTypeException

use of org.opencastproject.util.UnknownFileTypeException in project opencast by opencast.

the class ThemeWorkflowOperationHandler method addElement.

private void addElement(MediaPackage mediaPackage, final MediaPackageElementFlavor flavor, final List<String> tags, InputStream file, String filename, Type type) throws IOException {
    MediaPackageElement element = elementBuilderFactory.newElementBuilder().newElement(type, flavor);
    element.setIdentifier(UUID.randomUUID().toString());
    for (String tag : tags) {
        element.addTag(tag);
    }
    URI uri = workspace.put(mediaPackage.getIdentifier().compact(), element.getIdentifier(), filename, file);
    element.setURI(uri);
    try {
        MimeType mimeType = MimeTypes.fromString(filename);
        element.setMimeType(mimeType);
    } catch (UnknownFileTypeException e) {
        logger.warn("Unable to detect the mime type of file {}", filename);
    }
    mediaPackage.add(element);
}
Also used : MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) UnknownFileTypeException(org.opencastproject.util.UnknownFileTypeException) URI(java.net.URI) MimeType(org.opencastproject.util.MimeType)

Aggregations

UnknownFileTypeException (org.opencastproject.util.UnknownFileTypeException)4 IOException (java.io.IOException)3 NotFoundException (org.opencastproject.util.NotFoundException)3 File (java.io.File)2 URI (java.net.URI)2 MediaInspectionException (org.opencastproject.inspection.api.MediaInspectionException)2 MediaAnalyzerException (org.opencastproject.inspection.ffmpeg.api.MediaAnalyzerException)2 UnsupportedElementException (org.opencastproject.mediapackage.UnsupportedElementException)2 MimeType (org.opencastproject.util.MimeType)2 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 Hashtable (java.util.Hashtable)1 MediaContainerMetadata (org.opencastproject.inspection.ffmpeg.api.MediaContainerMetadata)1 Attachment (org.opencastproject.mediapackage.Attachment)1 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)1 MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)1 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)1 Stream (org.opencastproject.mediapackage.Stream)1 TrackImpl (org.opencastproject.mediapackage.track.TrackImpl)1