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);
}
}
}
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);
}
}
}
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);
}
}
}
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);
}
Aggregations