Search in sources :

Example 11 with SmilException

use of org.opencastproject.smil.api.SmilException in project opencast by opencast.

the class SmilServiceRest method addClip.

@POST
@Path("addClip")
@Produces({ MediaType.APPLICATION_XML, MediaType.TEXT_XML })
@RestQuery(name = "addClip", description = "Add new media element based on given Track information and start / duration parameters. " + "ParentId specifies where to put the new media element.", restParameters = { @RestParameter(name = "smil", description = "SMIL document where to add new media element.", 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 = "track", description = "Track (MediaPackageElement) to add as media element. " + "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.", isRequired = true, type = RestParameter.Type.INTEGER), @RestParameter(name = "duration", description = "Clip duration in milliseconds (should be positive).", isRequired = true, type = RestParameter.Type.INTEGER) }, returnDescription = "Returns new Smil with an media element inside " + "(the new media and metadata elements will be returned as response entities).", reponses = { @RestResponse(responseCode = HttpServletResponse.SC_OK, description = "Add media element to SMIL successfull."), @RestResponse(responseCode = HttpServletResponse.SC_BAD_REQUEST, description = "SMIL document not valid."), @RestResponse(responseCode = HttpServletResponse.SC_BAD_REQUEST, description = "Track 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 addClip(@FormParam("smil") String smil, @FormParam("parentId") String parentId, @FormParam("track") String track, @FormParam("start") long start, @FormParam("duration") long duration) {
    SmilResponse smilResponse = null;
    Track trackObj = null;
    try {
        smilResponse = smilService.fromXml(smil);
        trackObj = (Track) MediaPackageElementParser.getFromXml(track);
    } catch (SmilException ex) {
        return Response.status(HttpServletResponse.SC_BAD_REQUEST).entity("SMIL document invalid.").build();
    } catch (MediaPackageException ex) {
        return Response.status(HttpServletResponse.SC_BAD_REQUEST).entity("Track is not valid.").build();
    }
    try {
        smilResponse = smilService.addClip(smilResponse.getSmil(), parentId, trackObj, 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 12 with SmilException

use of org.opencastproject.smil.api.SmilException in project opencast by opencast.

the class SilenceDetectionServiceImpl method process.

@Override
protected String process(Job job) throws SilenceDetectionFailedException, SmilException, MediaPackageException {
    if (Operation.SILENCE_DETECTION.toString().equals(job.getOperation())) {
        // get source track
        String sourceTrackXml = StringUtils.trimToNull(job.getArguments().get(0));
        if (sourceTrackXml == null) {
            throw new SilenceDetectionFailedException("Track not set!");
        }
        Track sourceTrack = (Track) MediaPackageElementParser.getFromXml(sourceTrackXml);
        // run detection on source track
        MediaSegments segments = runDetection(sourceTrack);
        // get reference tracks if any
        List<Track> referenceTracks = null;
        if (job.getArguments().size() > 1) {
            String referenceTracksXml = StringUtils.trimToNull(job.getArguments().get(1));
            if (referenceTracksXml != null) {
                referenceTracks = (List<Track>) MediaPackageElementParser.getArrayFromXml(referenceTracksXml);
            }
        }
        if (referenceTracks == null) {
            referenceTracks = Arrays.asList(sourceTrack);
        }
        // create smil XML as result
        try {
            return generateSmil(segments, referenceTracks).toXML();
        } catch (Exception ex) {
            throw new SmilException("Failed to create smil document.", ex);
        }
    }
    throw new SilenceDetectionFailedException("Can't handle this operation: " + job.getOperation());
}
Also used : SilenceDetectionFailedException(org.opencastproject.silencedetection.api.SilenceDetectionFailedException) MediaSegments(org.opencastproject.silencedetection.api.MediaSegments) SmilException(org.opencastproject.smil.api.SmilException) Track(org.opencastproject.mediapackage.Track) SmilException(org.opencastproject.smil.api.SmilException) ConfigurationException(org.osgi.service.cm.ConfigurationException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) SilenceDetectionFailedException(org.opencastproject.silencedetection.api.SilenceDetectionFailedException)

Example 13 with SmilException

use of org.opencastproject.smil.api.SmilException in project opencast by opencast.

the class VideoEditorServiceImpl method processSmil.

/**
 * Splice segments given by smil document for the given track to the new one.
 *
 * @param job
 *          processing job
 * @param smil
 *          smil document with media segments description
 * @param trackParamGroupId
 * @return processed track
 * @throws ProcessFailedException
 *           if an error occured
 */
protected Track processSmil(Job job, Smil smil, String trackParamGroupId) throws ProcessFailedException {
    SmilMediaParamGroup trackParamGroup;
    ArrayList<String> inputfile = new ArrayList<>();
    ArrayList<VideoClip> videoclips = new ArrayList<>();
    try {
        trackParamGroup = (SmilMediaParamGroup) smil.get(trackParamGroupId);
    } catch (SmilException ex) {
        // can't be thrown, because we found the Id in processSmil(Smil)
        throw new ProcessFailedException("Smil does not contain a paramGroup element with Id " + trackParamGroupId);
    }
    MediaPackageElementFlavor sourceTrackFlavor = null;
    String sourceTrackUri = null;
    // get source track metadata
    for (SmilMediaParam param : trackParamGroup.getParams()) {
        if (SmilMediaParam.PARAM_NAME_TRACK_SRC.equals(param.getName())) {
            sourceTrackUri = param.getValue();
        } else if (SmilMediaParam.PARAM_NAME_TRACK_FLAVOR.equals(param.getName())) {
            sourceTrackFlavor = MediaPackageElementFlavor.parseFlavor(param.getValue());
        }
    }
    File sourceFile;
    try {
        sourceFile = workspace.get(new URI(sourceTrackUri));
    } catch (IOException ex) {
        throw new ProcessFailedException("Can't read " + sourceTrackUri);
    } catch (NotFoundException ex) {
        throw new ProcessFailedException("Workspace does not contain a track " + sourceTrackUri);
    } catch (URISyntaxException ex) {
        throw new ProcessFailedException("Source URI " + sourceTrackUri + " is not valid.");
    }
    // inspect input file to retrieve media information
    Job inspectionJob;
    Track sourceTrack;
    try {
        inspectionJob = inspect(job, new URI(sourceTrackUri));
        sourceTrack = (Track) MediaPackageElementParser.getFromXml(inspectionJob.getPayload());
    } catch (URISyntaxException e) {
        throw new ProcessFailedException("Source URI " + sourceTrackUri + " is not valid.");
    } catch (MediaInspectionException e) {
        throw new ProcessFailedException("Media inspection of " + sourceTrackUri + " failed", e);
    } catch (MediaPackageException e) {
        throw new ProcessFailedException("Deserialization of source track " + sourceTrackUri + " failed", e);
    }
    // get output file extension
    String outputFileExtension = properties.getProperty(VideoEditorProperties.DEFAULT_EXTENSION, ".mp4");
    outputFileExtension = properties.getProperty(VideoEditorProperties.OUTPUT_FILE_EXTENSION, outputFileExtension);
    if (!outputFileExtension.startsWith(".")) {
        outputFileExtension = '.' + outputFileExtension;
    }
    // create working directory
    File tempDirectory = new File(new File(workspace.rootDirectory()), "editor");
    tempDirectory = new File(tempDirectory, Long.toString(job.getId()));
    String filename = String.format("%s-%s%s", sourceTrackFlavor, sourceFile.getName(), outputFileExtension);
    File outputPath = new File(tempDirectory, filename);
    if (!outputPath.getParentFile().exists()) {
        outputPath.getParentFile().mkdirs();
    }
    URI newTrackURI;
    // default source - add to source table as 0
    inputfile.add(sourceFile.getAbsolutePath());
    // index = 0
    int srcIndex = inputfile.indexOf(sourceFile.getAbsolutePath());
    logger.info("Start processing srcfile {}", sourceFile.getAbsolutePath());
    try {
        // parse body elements
        for (SmilMediaObject element : smil.getBody().getMediaElements()) {
            // body should contain par elements
            if (element.isContainer()) {
                SmilMediaContainer container = (SmilMediaContainer) element;
                if (SmilMediaContainer.ContainerType.PAR == container.getContainerType()) {
                    // par element should contain media elements
                    for (SmilMediaObject elementChild : container.getElements()) {
                        if (!elementChild.isContainer()) {
                            SmilMediaElement media = (SmilMediaElement) elementChild;
                            if (trackParamGroupId.equals(media.getParamGroup())) {
                                long begin = media.getClipBeginMS();
                                long end = media.getClipEndMS();
                                URI clipTrackURI = media.getSrc();
                                File clipSourceFile = null;
                                if (clipTrackURI != null) {
                                    try {
                                        clipSourceFile = workspace.get(clipTrackURI);
                                    } catch (IOException ex) {
                                        throw new ProcessFailedException("Can't read " + clipTrackURI);
                                    } catch (NotFoundException ex) {
                                        throw new ProcessFailedException("Workspace does not contain a track " + clipTrackURI);
                                    }
                                }
                                int index;
                                if (clipSourceFile != null) {
                                    // clip has different source
                                    // Look for known tracks
                                    index = inputfile.indexOf(clipSourceFile.getAbsolutePath());
                                    if (index == -1) {
                                        // add new track
                                        inputfile.add(clipSourceFile.getAbsolutePath());
                                    // TODO: inspect each new video file, bad input will throw exc
                                    }
                                    index = inputfile.indexOf(clipSourceFile.getAbsolutePath());
                                } else {
                                    // default src
                                    index = srcIndex;
                                }
                                videoclips.add(new VideoClip(index, begin / 1000.0, end / 1000.0));
                            }
                        } else {
                            throw new ProcessFailedException("Smil container '" + ((SmilMediaContainer) elementChild).getContainerType().toString() + "'is not supportet yet");
                        }
                    }
                } else {
                    throw new ProcessFailedException("Smil container '" + container.getContainerType().toString() + "'is not supportet yet");
                }
            }
        }
        // remove very short cuts that will look bad
        List<VideoClip> cleanclips = sortSegments(videoclips);
        String error = null;
        // TODO: fetch the largest output resolution from SMIL.head.layout.root-layout
        String outputResolution = "";
        // When outputResolution is set to WxH, all clips are scaled to that size in the output video.
        // TODO: Each clips could have a region id, relative to the root-layout
        // Then each clip is zoomed/panned/padded to WxH befor concatenation
        FFmpegEdit ffmpeg = new FFmpegEdit(properties);
        error = ffmpeg.processEdits(inputfile, outputPath.getAbsolutePath(), outputResolution, cleanclips, sourceTrack.hasAudio(), sourceTrack.hasVideo());
        if (error != null) {
            FileUtils.deleteQuietly(tempDirectory);
            throw new ProcessFailedException("Editing pipeline exited abnormaly! Error: " + error);
        }
        // create Track for edited file
        String newTrackId = idBuilder.createNew().toString();
        InputStream in = new FileInputStream(outputPath);
        try {
            newTrackURI = workspace.putInCollection(COLLECTION_ID, String.format("%s-%s%s", sourceTrackFlavor.getType(), newTrackId, outputFileExtension), in);
        } catch (IllegalArgumentException ex) {
            throw new ProcessFailedException("Copy track into workspace failed! " + ex.getMessage());
        } finally {
            IOUtils.closeQuietly(in);
            FileUtils.deleteQuietly(tempDirectory);
        }
        // inspect new Track
        try {
            inspectionJob = inspect(job, newTrackURI);
        } catch (MediaInspectionException e) {
            throw new ProcessFailedException("Media inspection of " + newTrackURI + " failed", e);
        }
        Track editedTrack = (Track) MediaPackageElementParser.getFromXml(inspectionJob.getPayload());
        logger.info("Finished editing track {}", editedTrack);
        editedTrack.setIdentifier(newTrackId);
        editedTrack.setFlavor(new MediaPackageElementFlavor(sourceTrackFlavor.getType(), SINK_FLAVOR_SUBTYPE));
        return editedTrack;
    } catch (MediaInspectionException ex) {
        throw new ProcessFailedException("Inspecting encoded Track failed with: " + ex.getMessage());
    } catch (MediaPackageException ex) {
        throw new ProcessFailedException("Unable to serialize edited Track! " + ex.getMessage());
    } catch (Exception ex) {
        throw new ProcessFailedException("Unable to process SMIL: " + ex.getMessage(), ex);
    } finally {
        FileUtils.deleteQuietly(tempDirectory);
    }
}
Also used : ArrayList(java.util.ArrayList) NotFoundException(org.opencastproject.util.NotFoundException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) MediaInspectionException(org.opencastproject.inspection.api.MediaInspectionException) SmilException(org.opencastproject.smil.api.SmilException) SmilMediaObject(org.opencastproject.smil.entity.media.api.SmilMediaObject) SmilMediaParamGroup(org.opencastproject.smil.entity.media.param.api.SmilMediaParamGroup) Job(org.opencastproject.job.api.Job) SmilMediaContainer(org.opencastproject.smil.entity.media.container.api.SmilMediaContainer) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) FFmpegEdit(org.opencastproject.videoeditor.ffmpeg.FFmpegEdit) SmilMediaParam(org.opencastproject.smil.entity.media.param.api.SmilMediaParam) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) FileInputStream(java.io.FileInputStream) URISyntaxException(java.net.URISyntaxException) SmilException(org.opencastproject.smil.api.SmilException) ConfigurationException(org.osgi.service.cm.ConfigurationException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) MediaInspectionException(org.opencastproject.inspection.api.MediaInspectionException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) JAXBException(javax.xml.bind.JAXBException) ProcessFailedException(org.opencastproject.videoeditor.api.ProcessFailedException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) SmilMediaElement(org.opencastproject.smil.entity.media.element.api.SmilMediaElement) ProcessFailedException(org.opencastproject.videoeditor.api.ProcessFailedException) File(java.io.File) Track(org.opencastproject.mediapackage.Track)

Example 14 with SmilException

use of org.opencastproject.smil.api.SmilException in project opencast by opencast.

the class SilenceDetectionWorkflowOperationHandler method start.

@Override
public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
    MediaPackage mp = workflowInstance.getMediaPackage();
    logger.debug("Start silence detection workflow operation for mediapackage {}", mp.getIdentifier().compact());
    String sourceFlavors = StringUtils.trimToNull(workflowInstance.getCurrentOperation().getConfiguration(SOURCE_FLAVORS_PROPERTY));
    String sourceFlavor = StringUtils.trimToNull(workflowInstance.getCurrentOperation().getConfiguration(SOURCE_FLAVOR_PROPERTY));
    String smilFlavorSubType = StringUtils.trimToNull(workflowInstance.getCurrentOperation().getConfiguration(SMIL_FLAVOR_SUBTYPE_PROPERTY));
    String smilTargetFlavorString = StringUtils.trimToNull(workflowInstance.getCurrentOperation().getConfiguration(SMIL_TARGET_FLAVOR_PROPERTY));
    MediaPackageElementFlavor smilTargetFlavor = null;
    if (smilTargetFlavorString != null)
        smilTargetFlavor = MediaPackageElementFlavor.parseFlavor(smilTargetFlavorString);
    if (sourceFlavor == null && sourceFlavors == null) {
        throw new WorkflowOperationException(String.format("No %s or %s have been specified", SOURCE_FLAVOR_PROPERTY, SOURCE_FLAVORS_PROPERTY));
    }
    if (smilFlavorSubType == null && smilTargetFlavor == null) {
        throw new WorkflowOperationException(String.format("No %s or %s have been specified", SMIL_FLAVOR_SUBTYPE_PROPERTY, SMIL_TARGET_FLAVOR_PROPERTY));
    }
    if (sourceFlavors != null && smilTargetFlavor != null) {
        throw new WorkflowOperationException(String.format("Can't use %s and %s together", SOURCE_FLAVORS_PROPERTY, SMIL_TARGET_FLAVOR_PROPERTY));
    }
    final String finalSourceFlavors;
    if (smilTargetFlavor != null) {
        finalSourceFlavors = sourceFlavor;
    } else {
        finalSourceFlavors = sourceFlavors;
    }
    String referenceTracksFlavor = StringUtils.trimToNull(workflowInstance.getCurrentOperation().getConfiguration(REFERENCE_TRACKS_FLAVOR_PROPERTY));
    if (referenceTracksFlavor == null)
        referenceTracksFlavor = finalSourceFlavors;
    TrackSelector trackSelector = new TrackSelector();
    for (String flavor : asList(finalSourceFlavors)) {
        trackSelector.addFlavor(flavor);
    }
    Collection<Track> sourceTracks = trackSelector.select(mp, false);
    if (sourceTracks.isEmpty()) {
        logger.info("No source tracks found, skip silence detection");
        return createResult(mp, Action.SKIP);
    }
    trackSelector = new TrackSelector();
    for (String flavor : asList(referenceTracksFlavor)) {
        trackSelector.addFlavor(flavor);
    }
    Collection<Track> referenceTracks = trackSelector.select(mp, false);
    if (referenceTracks.isEmpty()) {
        // REFERENCE_TRACKS_FLAVOR_PROPERTY was set to wrong value
        throw new WorkflowOperationException(String.format("No tracks found filtered by flavor(s) '%s'", referenceTracksFlavor));
    }
    MediaPackageElementBuilder mpeBuilder = MediaPackageElementBuilderFactory.newInstance().newElementBuilder();
    for (Track sourceTrack : sourceTracks) {
        // Skip over track with no audio stream
        if (!sourceTrack.hasAudio()) {
            logger.info("Skipping silence detection of track {} since it has no audio", sourceTrack);
            continue;
        }
        logger.info("Executing silence detection on track {}", sourceTrack.getIdentifier());
        try {
            Job detectionJob = detetionService.detect(sourceTrack, referenceTracks.toArray(new Track[referenceTracks.size()]));
            if (!waitForStatus(detectionJob).isSuccess()) {
                throw new WorkflowOperationException("Silence Detection failed");
            }
            Smil smil = smilService.fromXml(detectionJob.getPayload()).getSmil();
            InputStream is = null;
            try {
                is = IOUtils.toInputStream(smil.toXML(), "UTF-8");
                URI smilURI = workspace.put(mp.getIdentifier().compact(), smil.getId(), TARGET_FILE_NAME, is);
                MediaPackageElementFlavor smilFlavor = smilTargetFlavor;
                if (smilFlavor == null)
                    smilFlavor = new MediaPackageElementFlavor(sourceTrack.getFlavor().getType(), smilFlavorSubType);
                Catalog catalog = (Catalog) mpeBuilder.elementFromURI(smilURI, MediaPackageElement.Type.Catalog, smilFlavor);
                catalog.setIdentifier(smil.getId());
                mp.add(catalog);
            } catch (Exception ex) {
                throw new WorkflowOperationException(String.format("Failed to put smil into workspace. Silence detection for track %s failed", sourceTrack.getIdentifier()), ex);
            } finally {
                IOUtils.closeQuietly(is);
            }
            logger.info("Finished silence detection on track {}", sourceTrack.getIdentifier());
        } catch (SilenceDetectionFailedException ex) {
            throw new WorkflowOperationException(String.format("Failed to create silence detection job for track %s", sourceTrack.getIdentifier()));
        } catch (SmilException ex) {
            throw new WorkflowOperationException(String.format("Failed to get smil from silence detection job for track %s", sourceTrack.getIdentifier()));
        }
    }
    logger.debug("Finished silence detection workflow operation for mediapackage {}", mp.getIdentifier().compact());
    return createResult(mp, Action.CONTINUE);
}
Also used : SilenceDetectionFailedException(org.opencastproject.silencedetection.api.SilenceDetectionFailedException) InputStream(java.io.InputStream) TrackSelector(org.opencastproject.mediapackage.selector.TrackSelector) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) URI(java.net.URI) Catalog(org.opencastproject.mediapackage.Catalog) SmilException(org.opencastproject.smil.api.SmilException) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) SilenceDetectionFailedException(org.opencastproject.silencedetection.api.SilenceDetectionFailedException) MediaPackageElementBuilder(org.opencastproject.mediapackage.MediaPackageElementBuilder) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) Smil(org.opencastproject.smil.entity.api.Smil) SmilException(org.opencastproject.smil.api.SmilException) Job(org.opencastproject.job.api.Job) Track(org.opencastproject.mediapackage.Track)

Aggregations

SmilException (org.opencastproject.smil.api.SmilException)14 SmilResponse (org.opencastproject.smil.api.SmilResponse)7 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)6 Track (org.opencastproject.mediapackage.Track)6 Smil (org.opencastproject.smil.entity.api.Smil)5 IOException (java.io.IOException)4 POST (javax.ws.rs.POST)4 Path (javax.ws.rs.Path)4 Produces (javax.ws.rs.Produces)4 InputStream (java.io.InputStream)3 URI (java.net.URI)3 JAXBException (javax.xml.bind.JAXBException)3 Job (org.opencastproject.job.api.Job)3 Catalog (org.opencastproject.mediapackage.Catalog)3 MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)3 SmilMediaObject (org.opencastproject.smil.entity.media.api.SmilMediaObject)3 SmilMediaContainer (org.opencastproject.smil.entity.media.container.api.SmilMediaContainer)3 NotFoundException (org.opencastproject.util.NotFoundException)3 RestQuery (org.opencastproject.util.doc.rest.RestQuery)3 File (java.io.File)2