use of org.opencastproject.util.MimeType 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());
}
}
use of org.opencastproject.util.MimeType in project opencast by opencast.
the class AbstractAttachmentBuilderPlugin method elementFromManifest.
/**
* @see org.opencastproject.mediapackage.elementbuilder.MediaPackageElementBuilderPlugin#elementFromManifest(org.w3c.dom.Node,
* org.opencastproject.mediapackage.MediaPackageSerializer)
*/
@Override
public MediaPackageElement elementFromManifest(Node elementNode, MediaPackageSerializer serializer) throws UnsupportedElementException {
String id = null;
String attachmentFlavor = null;
String reference = null;
URI uri = null;
long size = -1;
Checksum checksum = null;
MimeType mimeType = null;
try {
// id
id = (String) xpath.evaluate("@id", elementNode, XPathConstants.STRING);
// flavor
attachmentFlavor = (String) xpath.evaluate("@type", elementNode, XPathConstants.STRING);
// reference
reference = (String) xpath.evaluate("@ref", elementNode, XPathConstants.STRING);
// url
uri = serializer.decodeURI(new URI(xpath.evaluate("url/text()", elementNode).trim()));
// size
String attachmentSize = xpath.evaluate("size/text()", elementNode).trim();
if (!"".equals(attachmentSize))
size = Long.parseLong(attachmentSize);
// 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);
// create the attachment
AttachmentImpl attachment = (AttachmentImpl) AttachmentImpl.fromURI(uri);
if (StringUtils.isNotEmpty(id))
attachment.setIdentifier(id);
// Add url
attachment.setURI(uri);
// Add reference
if (StringUtils.isNotEmpty(reference))
attachment.referTo(MediaPackageReferenceImpl.fromString(reference));
// Add type/flavor information
if (StringUtils.isNotEmpty(attachmentFlavor)) {
try {
MediaPackageElementFlavor flavor = MediaPackageElementFlavor.parseFlavor(attachmentFlavor);
attachment.setFlavor(flavor);
} catch (IllegalArgumentException e) {
logger.warn("Unable to read attachment flavor: " + e.getMessage());
}
}
// Set the size
if (size > 0)
attachment.setSize(size);
// Set checksum
if (checksum != null)
attachment.setChecksum(checksum);
// Set mimetype
if (mimeType != null)
attachment.setMimeType(mimeType);
// Set the description
String description = xpath.evaluate("description/text()", elementNode);
if (StringUtils.isNotEmpty(description))
attachment.setElementDescription(description.trim());
// Set tags
NodeList tagNodes = (NodeList) xpath.evaluate("tags/tag", elementNode, XPathConstants.NODESET);
for (int i = 0; i < tagNodes.getLength(); i++) {
attachment.addTag(tagNodes.item(i).getTextContent());
}
return specializeAttachment(attachment);
} catch (XPathExpressionException e) {
throw new UnsupportedElementException("Error while reading attachment from manifest: " + e.getMessage());
} catch (NoSuchAlgorithmException e) {
throw new UnsupportedElementException("Unsupported digest algorithm: " + e.getMessage());
} catch (URISyntaxException e) {
throw new UnsupportedElementException("Error while reading attachment file " + uri + ": " + e.getMessage());
}
}
use of org.opencastproject.util.MimeType in project opencast by opencast.
the class TrackBuilderPlugin method elementFromManifest.
/**
* @see org.opencastproject.mediapackage.elementbuilder.MediaPackageElementBuilderPlugin#elementFromManifest(org.w3c.dom.Node,
* org.opencastproject.mediapackage.MediaPackageSerializer)
*/
@Override
public MediaPackageElement elementFromManifest(Node elementNode, MediaPackageSerializer serializer) throws UnsupportedElementException {
String id = null;
MimeType mimeType = null;
MediaPackageElementFlavor flavor = null;
TrackImpl.StreamingProtocol transport = null;
String reference = null;
URI url = null;
long size = -1;
Checksum checksum = null;
try {
// id
id = (String) xpath.evaluate("@id", elementNode, XPathConstants.STRING);
// url
url = serializer.decodeURI(new URI(xpath.evaluate("url/text()", elementNode).trim()));
// 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);
// transport
String transportValue = (String) xpath.evaluate("@transport", elementNode, XPathConstants.STRING);
if (StringUtils.isNotEmpty(transportValue))
transport = TrackImpl.StreamingProtocol.valueOf(transportValue);
// 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);
//
// Build the track
TrackImpl track = TrackImpl.fromURI(url);
if (StringUtils.isNotBlank(id))
track.setIdentifier(id);
// Add url
track.setURI(url);
// Add reference
if (StringUtils.isNotEmpty(reference))
track.referTo(MediaPackageReferenceImpl.fromString(reference));
// Set size
if (size > 0)
track.setSize(size);
// Set checksum
if (checksum != null)
track.setChecksum(checksum);
// Set mimetpye
if (mimeType != null)
track.setMimeType(mimeType);
if (flavor != null)
track.setFlavor(flavor);
// set transport
if (transport != null)
track.setTransport(transport);
// description
String description = (String) xpath.evaluate("description/text()", elementNode, XPathConstants.STRING);
if (StringUtils.isNotBlank(description))
track.setElementDescription(description.trim());
// tags
NodeList tagNodes = (NodeList) xpath.evaluate("tags/tag", elementNode, XPathConstants.NODESET);
for (int i = 0; i < tagNodes.getLength(); i++) {
track.addTag(tagNodes.item(i).getTextContent());
}
// duration
try {
String strDuration = (String) xpath.evaluate("duration/text()", elementNode, XPathConstants.STRING);
if (StringUtils.isNotEmpty(strDuration)) {
long duration = Long.parseLong(strDuration.trim());
track.setDuration(duration);
}
} catch (NumberFormatException e) {
throw new UnsupportedElementException("Duration of track " + url + " is malformatted");
}
// is live
String strLive = (String) xpath.evaluate("live/text()", elementNode, XPathConstants.STRING);
if (StringUtils.isNotEmpty(strLive)) {
boolean live = Boolean.parseBoolean(strLive.trim());
track.setLive(live);
}
// audio settings
Node audioSettingsNode = (Node) xpath.evaluate("audio", elementNode, XPathConstants.NODE);
if (audioSettingsNode != null && audioSettingsNode.hasChildNodes()) {
try {
AudioStreamImpl as = AudioStreamImpl.fromManifest(createStreamID(track), audioSettingsNode, xpath);
track.addStream(as);
} catch (IllegalStateException e) {
throw new UnsupportedElementException("Illegal state encountered while reading audio settings from " + url + ": " + e.getMessage());
} catch (XPathException e) {
throw new UnsupportedElementException("Error while parsing audio settings from " + url + ": " + e.getMessage());
}
}
// video settings
Node videoSettingsNode = (Node) xpath.evaluate("video", elementNode, XPathConstants.NODE);
if (videoSettingsNode != null && videoSettingsNode.hasChildNodes()) {
try {
VideoStreamImpl vs = VideoStreamImpl.fromManifest(createStreamID(track), videoSettingsNode, xpath);
track.addStream(vs);
} catch (IllegalStateException e) {
throw new UnsupportedElementException("Illegal state encountered while reading video settings from " + url + ": " + e.getMessage());
} catch (XPathException e) {
throw new UnsupportedElementException("Error while parsing video settings from " + url + ": " + e.getMessage());
}
}
return track;
} 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());
}
}
use of org.opencastproject.util.MimeType in project opencast by opencast.
the class MediaInspector method inspectTrack.
/**
* Inspects the element that is passed in as uri.
*
* @param trackURI
* the element uri
* @return the inspected track
* @throws org.opencastproject.inspection.api.MediaInspectionException
* if inspection fails
*/
public Track inspectTrack(URI trackURI, Map<String, String> options) throws MediaInspectionException {
logger.debug("inspect(" + trackURI + ") called, using workspace " + workspace);
throwExceptionIfInvalid(options);
try {
// Get the file from the URL (runtime exception if invalid)
File file = null;
try {
file = workspace.get(trackURI);
} catch (NotFoundException notFound) {
throw new MediaInspectionException("Unable to find resource " + trackURI, notFound);
} catch (IOException ioe) {
throw new MediaInspectionException("Error reading " + trackURI + " from workspace", ioe);
}
// 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("Media analyzer returned no metadata from " + file);
} else {
MediaPackageElementBuilder elementBuilder = MediaPackageElementBuilderFactory.newInstance().newElementBuilder();
TrackImpl track;
MediaPackageElement element;
try {
element = elementBuilder.elementFromURI(trackURI, MediaPackageElement.Type.Track, null);
} catch (UnsupportedElementException e) {
throw new MediaInspectionException("Unable to create track element from " + file, e);
}
track = (TrackImpl) element;
// Duration
if (metadata.getDuration() != null && metadata.getDuration() > 0)
track.setDuration(metadata.getDuration());
// Checksum
try {
track.setChecksum(Checksum.create(ChecksumType.DEFAULT_TYPE, file));
} catch (IOException e) {
throw new MediaInspectionException("Unable to read " + file, e);
}
// Mimetype
InputStream is = null;
try {
// Try to get the Mimetype from Apache Tika
is = new FileInputStream(file);
MimeType mimeType = extractContentType(is);
// If Mimetype could not be extracted try to get it from opencast
if (mimeType == null) {
mimeType = MimeTypes.fromURL(file.toURI().toURL());
// 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 (Exception e) {
logger.error("Unable to find mimetype for {}", file.getAbsolutePath());
} finally {
IoSupport.closeQuietly(is);
}
// Audio metadata
try {
addAudioStreamMetadata(track, metadata);
} catch (Exception e) {
throw new MediaInspectionException("Unable to extract audio metadata from " + file, e);
}
// Videometadata
try {
addVideoStreamMetadata(track, metadata);
} catch (Exception e) {
throw new MediaInspectionException("Unable to extract video metadata from " + file, e);
}
return track;
}
} catch (Exception e) {
logger.warn("Error inspecting " + trackURI, e);
if (e instanceof MediaInspectionException) {
throw (MediaInspectionException) e;
} else {
throw new MediaInspectionException(e);
}
}
}
use of org.opencastproject.util.MimeType in project opencast by opencast.
the class MediaInspectionServiceImplTest method testEnrichment.
@Test
public void testEnrichment() throws Exception {
final URI trackUri = getResource("/test.mp4");
for (MediaInspector mi : init(trackUri)) {
Track track = mi.inspectTrack(trackUri, Options.NO_OPTION);
// make changes to metadata
Checksum cs = track.getChecksum();
track.setChecksum(null);
MimeType mt = mimeType("video", "flash");
track.setMimeType(mt);
// test the enrich scenario
Track newTrack = (Track) mi.enrich(track, false, Options.NO_OPTION);
VideoStream[] videoStreams = TrackSupport.byType(newTrack.getStreams(), VideoStream.class);
assertTrue(videoStreams[0].getFrameCount().longValue() > 0);
AudioStream[] audioStreams = TrackSupport.byType(newTrack.getStreams(), AudioStream.class);
assertTrue(audioStreams[0].getFrameCount().longValue() > 0);
assertEquals(newTrack.getChecksum(), cs);
assertEquals(newTrack.getMimeType(), mt);
assertNotNull(newTrack.getDuration());
assertTrue(newTrack.getDuration() > 0);
// test the override scenario
newTrack = (Track) mi.enrich(track, true, Options.NO_OPTION);
assertEquals(newTrack.getChecksum(), cs);
assertNotSame(newTrack.getMimeType(), mt);
assertTrue(newTrack.getDuration() > 0);
}
for (MediaInspector mi : init(trackUri)) {
Track track = mi.inspectTrack(trackUri, Options.NO_OPTION);
// make changes to metadata
Checksum cs = track.getChecksum();
track.setChecksum(null);
MimeType mt = mimeType("video", "flash");
track.setMimeType(mt);
// test the enrich scenario
Track newTrack = (Track) mi.enrich(track, false, Options.NO_OPTION);
VideoStream[] videoStreams = TrackSupport.byType(newTrack.getStreams(), VideoStream.class);
assertTrue(videoStreams[0].getFrameCount().longValue() > 0);
AudioStream[] audioStreams = TrackSupport.byType(newTrack.getStreams(), AudioStream.class);
assertTrue(audioStreams[0].getFrameCount().longValue() > 0);
assertEquals(newTrack.getChecksum(), cs);
assertEquals(newTrack.getMimeType(), mt);
assertNotNull(newTrack.getDuration());
assertTrue(newTrack.getDuration() > 0);
// test the override scenario
newTrack = (Track) mi.enrich(track, true, Options.NO_OPTION);
assertEquals(newTrack.getChecksum(), cs);
assertNotSame(newTrack.getMimeType(), mt);
assertTrue(newTrack.getDuration() > 0);
}
}
Aggregations