Search in sources :

Example 21 with MediaPackageException

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

the class SmilServiceRest method createNewSmil.

@POST
@Path("create")
@Produces({ MediaType.APPLICATION_XML, MediaType.TEXT_XML })
@RestQuery(name = "create", description = "Create new SMIL. Add some MediaPackage metadata.", restParameters = { @RestParameter(name = "mediaPackage", description = "MediaPackage for metadata.", isRequired = false, type = RestParameter.Type.TEXT) }, returnDescription = "Returns new SmilResponse with SMIL document inside.", reponses = { @RestResponse(responseCode = HttpServletResponse.SC_OK, description = "Create new SMIL successfull"), @RestResponse(responseCode = HttpServletResponse.SC_BAD_REQUEST, description = "Given mediaPackage is not valid") })
public Response createNewSmil(@FormParam("mediaPackage") String mediaPackage) {
    SmilResponse smilResponse = null;
    try {
        if (mediaPackage != null && !mediaPackage.isEmpty()) {
            MediaPackage mp = MediaPackageParser.getFromXml(mediaPackage);
            smilResponse = smilService.createNewSmil(mp);
        } else {
            smilResponse = smilService.createNewSmil();
        }
        return Response.ok(smilResponse).build();
    } catch (MediaPackageException ex) {
        logger.info(ex.getMessage(), ex);
        return Response.status(HttpServletResponse.SC_BAD_REQUEST).entity("MediaPackage not valid.").build();
    }
}
Also used : MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) SmilResponse(org.opencastproject.smil.api.SmilResponse) MediaPackage(org.opencastproject.mediapackage.MediaPackage) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Example 22 with MediaPackageException

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

the class SmilServiceRest method addClips.

@POST
@Path("addClips")
@Produces({ MediaType.APPLICATION_XML, MediaType.TEXT_XML })
@RestQuery(name = "addClips", description = "Add new media elements based on given Tracks information and start / duration parameters. " + "ParentId specifies where to put the new media.", restParameters = { @RestParameter(name = "smil", description = "SMIL document where to add new media elements.", isRequired = true, type = RestParameter.Type.TEXT), @RestParameter(name = "parentId", description = "An element Id, were to add new media. ", isRequired = false, type = RestParameter.Type.STRING), @RestParameter(name = "tracks", description = "Tracks (MediaPackageElements) to add as media elements." + "Some information like Track source and flavor will be stored in ParamGroup (in SMIL Head) " + "and referenced by paramGroup media element attribute.", isRequired = true, type = RestParameter.Type.TEXT), @RestParameter(name = "start", description = "Track start position in milliseconds. " + "The start position will be applied to each media element.", isRequired = true, type = RestParameter.Type.INTEGER), @RestParameter(name = "duration", description = "Clip duration in milliseconds (should be positive). " + "The duration will be applied to each media element.", isRequired = true, type = RestParameter.Type.INTEGER) }, returnDescription = "Returns new Smil with new media elements inside " + "(the new media and metadata elements will be returned as response entities).", reponses = { @RestResponse(responseCode = HttpServletResponse.SC_OK, description = "Add media elements to SMIL successfull."), @RestResponse(responseCode = HttpServletResponse.SC_BAD_REQUEST, description = "SMIL document not valid."), @RestResponse(responseCode = HttpServletResponse.SC_BAD_REQUEST, description = "Tracks are not valid."), @RestResponse(responseCode = HttpServletResponse.SC_BAD_REQUEST, description = "SMIL document doesn't contain an element with given parentId."), @RestResponse(responseCode = HttpServletResponse.SC_BAD_REQUEST, description = "Start plus duration is bigger than Track length.") })
public Response addClips(@FormParam("smil") String smil, @FormParam("parentId") String parentId, @FormParam("tracks") String tracks, @FormParam("start") long start, @FormParam("duration") long duration) {
    SmilResponse smilResponse = null;
    List<Track> tracksList = null;
    try {
        smilResponse = smilService.fromXml(smil);
        tracksList = (List<Track>) MediaPackageElementParser.getArrayFromXml(tracks);
    } catch (SmilException ex) {
        logger.info(ex.getMessage(), ex);
        return Response.status(HttpServletResponse.SC_BAD_REQUEST).entity("SMIL document invalid.").build();
    } catch (MediaPackageException ex) {
        logger.error(ex.getMessage(), ex);
        return Response.status(HttpServletResponse.SC_BAD_REQUEST).entity("Tracks are not valid.").build();
    }
    Track[] tracksArr = tracksList.toArray(new Track[tracksList.size()]);
    try {
        smilResponse = smilService.addClips(smilResponse.getSmil(), parentId, tracksArr, start, duration);
        return Response.ok(smilResponse).build();
    } catch (SmilException ex) {
        logger.info(ex.getMessage(), ex);
        return Response.status(HttpServletResponse.SC_BAD_REQUEST).entity("SMIL document doesn't contain an element with given parentId.").build();
    }
}
Also used : MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) SmilResponse(org.opencastproject.smil.api.SmilResponse) SmilException(org.opencastproject.smil.api.SmilException) Track(org.opencastproject.mediapackage.Track) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Example 23 with MediaPackageException

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

the class TimelinePreviewsWorkflowOperationHandler method cleanupWorkspace.

/**
 * Remove all files created by the given jobs
 * @param jobs
 */
private void cleanupWorkspace(List<Job> jobs) {
    for (Job job : jobs) {
        String jobPayload = job.getPayload();
        if (StringUtils.isNotEmpty(jobPayload)) {
            try {
                MediaPackageElement timelinepreviewsMpe = MediaPackageElementParser.getFromXml(jobPayload);
                URI timelinepreviewsUri = timelinepreviewsMpe.getURI();
                workspace.delete(timelinepreviewsUri);
            } catch (MediaPackageException ex) {
                // unexpected job payload
                logger.error("Can't parse timeline previews attachment from job {}", job.getId());
            } catch (NotFoundException ex) {
            // this is ok, because we want delete the file
            } catch (IOException ex) {
                logger.warn("Deleting timeline previews image file from workspace failed: {}", ex.getMessage());
            // this is ok, because workspace cleaner will remove old files if they exist
            }
        }
    }
}
Also used : MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) NotFoundException(org.opencastproject.util.NotFoundException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Job(org.opencastproject.job.api.Job) URI(java.net.URI)

Example 24 with MediaPackageException

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

the class TextAnalyzerServiceImpl method extract.

/**
 * Starts text extraction on the image and returns a receipt containing the final result in the form of an
 * Mpeg7Catalog.
 *
 * @param image
 *          the element to analyze
 * @param block
 *          <code>true</code> to make this operation synchronous
 * @return a receipt containing the resulting mpeg-7 catalog
 * @throws TextAnalyzerException
 */
private Catalog extract(Job job, Attachment image) throws TextAnalyzerException, MediaPackageException {
    final Attachment attachment = image;
    final URI imageUrl = attachment.getURI();
    File imageFile = null;
    try {
        Mpeg7CatalogImpl mpeg7 = Mpeg7CatalogImpl.newInstance();
        logger.info("Starting text extraction from {}", imageUrl);
        try {
            imageFile = workspace.get(imageUrl);
        } catch (NotFoundException e) {
            throw new TextAnalyzerException("Image " + imageUrl + " not found in workspace", e);
        } catch (IOException e) {
            throw new TextAnalyzerException("Unable to access " + imageUrl + " in workspace", e);
        }
        VideoText[] videoTexts = analyze(imageFile, image.getIdentifier());
        // Create a temporal decomposition
        MediaTime mediaTime = new MediaTimeImpl(0, 0);
        Video avContent = mpeg7.addVideoContent(image.getIdentifier(), mediaTime, null);
        TemporalDecomposition<VideoSegment> temporalDecomposition = (TemporalDecomposition<VideoSegment>) avContent.getTemporalDecomposition();
        // Add a segment
        VideoSegment videoSegment = temporalDecomposition.createSegment("segment-0");
        videoSegment.setMediaTime(mediaTime);
        // Add the video text to the spacio temporal decomposition of the segment
        SpatioTemporalDecomposition spatioTemporalDecomposition = videoSegment.createSpatioTemporalDecomposition(true, false);
        for (VideoText videoText : videoTexts) {
            spatioTemporalDecomposition.addVideoText(videoText);
        }
        logger.info("Text extraction of {} finished, {} lines found", attachment.getURI(), videoTexts.length);
        URI uri;
        InputStream in;
        try {
            in = mpeg7CatalogService.serialize(mpeg7);
        } catch (IOException e) {
            throw new TextAnalyzerException("Error serializing mpeg7", e);
        }
        try {
            uri = workspace.putInCollection(COLLECTION_ID, job.getId() + ".xml", in);
        } catch (IOException e) {
            throw new TextAnalyzerException("Unable to put mpeg7 into the workspace", e);
        }
        Catalog catalog = (Catalog) MediaPackageElementBuilderFactory.newInstance().newElementBuilder().newElement(Catalog.TYPE, MediaPackageElements.TEXTS);
        catalog.setURI(uri);
        logger.debug("Created MPEG7 catalog for {}", imageUrl);
        return catalog;
    } catch (Exception e) {
        logger.warn("Error extracting text from " + imageUrl, e);
        if (e instanceof TextAnalyzerException) {
            throw (TextAnalyzerException) e;
        } else {
            throw new TextAnalyzerException(e);
        }
    } finally {
        try {
            workspace.delete(imageUrl);
        } catch (Exception e) {
            logger.warn("Unable to delete temporary text analysis image {}: {}", imageUrl, e);
        }
    }
}
Also used : InputStream(java.io.InputStream) NotFoundException(org.opencastproject.util.NotFoundException) Attachment(org.opencastproject.mediapackage.Attachment) IOException(java.io.IOException) URI(java.net.URI) VideoText(org.opencastproject.metadata.mpeg7.VideoText) Catalog(org.opencastproject.mediapackage.Catalog) TextExtractorException(org.opencastproject.textextractor.api.TextExtractorException) ConfigurationException(org.osgi.service.cm.ConfigurationException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) TextAnalyzerException(org.opencastproject.textanalyzer.api.TextAnalyzerException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) TextAnalyzerException(org.opencastproject.textanalyzer.api.TextAnalyzerException) VideoSegment(org.opencastproject.metadata.mpeg7.VideoSegment) MediaTimeImpl(org.opencastproject.metadata.mpeg7.MediaTimeImpl) Video(org.opencastproject.metadata.mpeg7.Video) MediaTime(org.opencastproject.metadata.mpeg7.MediaTime) Mpeg7CatalogImpl(org.opencastproject.metadata.mpeg7.Mpeg7CatalogImpl) TemporalDecomposition(org.opencastproject.metadata.mpeg7.TemporalDecomposition) SpatioTemporalDecomposition(org.opencastproject.metadata.mpeg7.SpatioTemporalDecomposition) File(java.io.File) SpatioTemporalDecomposition(org.opencastproject.metadata.mpeg7.SpatioTemporalDecomposition)

Example 25 with MediaPackageException

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

the class TimelinePreviewsServiceRemote method createTimelinePreviewImages.

/**
 * Takes the given track and returns the job that will create timeline preview images using a remote service.
 *
 * @param sourceTrack the track to create preview images from
 * @param imageCount number of preview images that will be generated
 * @return a job that will create timeline preview images
 * @throws MediaPackageException if the serialization of the given track fails
 * @throws TimelinePreviewsException if the job can't be created for any reason
 */
@Override
public Job createTimelinePreviewImages(Track sourceTrack, int imageCount) throws MediaPackageException, TimelinePreviewsException {
    HttpPost post = new HttpPost("/create");
    try {
        List<BasicNameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair("track", MediaPackageElementParser.getAsXml(sourceTrack)));
        params.add(new BasicNameValuePair("imageCount", Integer.toString(imageCount)));
        post.setEntity(new UrlEncodedFormEntity(params));
    } catch (Exception e) {
        throw new TimelinePreviewsException(e);
    }
    HttpResponse response = null;
    try {
        response = getResponse(post);
        if (response != null) {
            try {
                Job receipt = JobParser.parseJob(response.getEntity().getContent());
                logger.info("Create timeline preview images from {}", sourceTrack);
                return receipt;
            } catch (Exception e) {
                throw new TimelinePreviewsException("Unable to create timeline preview images from " + sourceTrack + " using a remote service", e);
            }
        }
    } finally {
        closeConnection(response);
    }
    throw new TimelinePreviewsException("Unable to create timeline preview images from " + sourceTrack + " using a remote service");
}
Also used : TimelinePreviewsException(org.opencastproject.timelinepreviews.api.TimelinePreviewsException) HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) Job(org.opencastproject.job.api.Job) TimelinePreviewsException(org.opencastproject.timelinepreviews.api.TimelinePreviewsException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException)

Aggregations

MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)72 Job (org.opencastproject.job.api.Job)42 IOException (java.io.IOException)34 MediaPackage (org.opencastproject.mediapackage.MediaPackage)33 NotFoundException (org.opencastproject.util.NotFoundException)31 URI (java.net.URI)25 ArrayList (java.util.ArrayList)25 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)21 WorkflowOperationException (org.opencastproject.workflow.api.WorkflowOperationException)20 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)19 Track (org.opencastproject.mediapackage.Track)17 MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)15 File (java.io.File)13 HashMap (java.util.HashMap)13 InputStream (java.io.InputStream)12 HttpResponse (org.apache.http.HttpResponse)12 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)12 HttpPost (org.apache.http.client.methods.HttpPost)12 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)12 EncoderException (org.opencastproject.composer.api.EncoderException)12