Search in sources :

Example 1 with Stream

use of org.opencastproject.mediapackage.Stream 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 Stream

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

the class EventsEndpoint method getEventMedia.

@GET
@Path("{eventId}/media")
@Produces({ "application/json", "application/v1.0.0+json" })
public Response getEventMedia(@HeaderParam("Accept") String acceptHeader, @PathParam("eventId") String id) throws Exception {
    ArrayList<TrackImpl> tracks = new ArrayList<>();
    for (final Event event : indexService.getEvent(id, externalIndex)) {
        final MediaPackage mp = indexService.getEventMediapackage(event);
        for (Track track : mp.getTracks()) {
            if (track instanceof TrackImpl) {
                tracks.add((TrackImpl) track);
            }
        }
        List<JValue> tracksJson = new ArrayList<>();
        for (Track track : tracks) {
            List<Field> fields = new ArrayList<>();
            if (track.getChecksum() != null)
                fields.add(f("checksum", v(track.getChecksum().toString())));
            if (track.getDescription() != null)
                fields.add(f("description", v(track.getDescription())));
            if (track.getDuration() != null)
                fields.add(f("duration", v(track.getDuration())));
            if (track.getElementDescription() != null)
                fields.add(f("element-description", v(track.getElementDescription())));
            if (track.getFlavor() != null)
                fields.add(f("flavor", v(track.getFlavor().toString())));
            if (track.getIdentifier() != null)
                fields.add(f("identifier", v(track.getIdentifier())));
            if (track.getMimeType() != null)
                fields.add(f("identifier", v(track.getMimeType().toString())));
            fields.add(f("size", v(track.getSize())));
            if (track.getStreams() != null) {
                List<Field> streams = new ArrayList<>();
                for (Stream stream : track.getStreams()) {
                    streams.add(f(stream.getIdentifier(), getJsonStream(stream)));
                }
                fields.add(f("streams", obj(streams)));
            }
            if (track.getTags() != null) {
                List<JValue> tags = new ArrayList<>();
                for (String tag : track.getTags()) {
                    tags.add(v(tag));
                }
                fields.add(f("tags", arr(tags)));
            }
            if (track.getURI() != null)
                fields.add(f("uri", v(track.getURI().toString())));
            tracksJson.add(obj(fields));
        }
        return ApiResponses.Json.ok(ApiVersion.VERSION_1_0_0, arr(tracksJson));
    }
    return ApiResponses.notFound("Cannot find an event with id '%s'.", id);
}
Also used : Field(com.entwinemedia.fn.data.json.Field) MetadataField(org.opencastproject.metadata.dublincore.MetadataField) TrackImpl(org.opencastproject.mediapackage.track.TrackImpl) JValue(com.entwinemedia.fn.data.json.JValue) ArrayList(java.util.ArrayList) MediaPackage(org.opencastproject.mediapackage.MediaPackage) Event(org.opencastproject.index.service.impl.index.event.Event) AudioStream(org.opencastproject.mediapackage.AudioStream) Stream(org.opencastproject.mediapackage.Stream) VideoStream(org.opencastproject.mediapackage.VideoStream) Track(org.opencastproject.mediapackage.Track) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 3 with Stream

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

the class TrackImpl method toManifest.

/**
 * @see org.opencastproject.mediapackage.AbstractMediaPackageElement#toManifest(org.w3c.dom.Document,
 *      MediaPackageSerializer)
 */
@Override
public Node toManifest(Document document, MediaPackageSerializer serializer) throws MediaPackageException {
    Node node = super.toManifest(document, serializer);
    // duration
    if (duration != null && duration >= 0) {
        Node durationNode = document.createElement("duration");
        durationNode.appendChild(document.createTextNode(Long.toString(duration)));
        node.appendChild(durationNode);
    }
    Node liveNode = document.createElement("live");
    liveNode.appendChild(document.createTextNode(Boolean.toString(live)));
    node.appendChild(liveNode);
    for (Stream s : audio) node.appendChild(s.toManifest(document, serializer));
    for (Stream s : video) node.appendChild(s.toManifest(document, serializer));
    return node;
}
Also used : Node(org.w3c.dom.Node) AudioStream(org.opencastproject.mediapackage.AudioStream) Stream(org.opencastproject.mediapackage.Stream) VideoStream(org.opencastproject.mediapackage.VideoStream)

Aggregations

Stream (org.opencastproject.mediapackage.Stream)3 AudioStream (org.opencastproject.mediapackage.AudioStream)2 VideoStream (org.opencastproject.mediapackage.VideoStream)2 TrackImpl (org.opencastproject.mediapackage.track.TrackImpl)2 Field (com.entwinemedia.fn.data.json.Field)1 JValue (com.entwinemedia.fn.data.json.JValue)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 Hashtable (java.util.Hashtable)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 Event (org.opencastproject.index.service.impl.index.event.Event)1 MediaInspectionException (org.opencastproject.inspection.api.MediaInspectionException)1 MediaAnalyzerException (org.opencastproject.inspection.ffmpeg.api.MediaAnalyzerException)1 MediaContainerMetadata (org.opencastproject.inspection.ffmpeg.api.MediaContainerMetadata)1