Search in sources :

Example 1 with FFmpegEdit

use of org.opencastproject.videoeditor.ffmpeg.FFmpegEdit 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)

Aggregations

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 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 JAXBException (javax.xml.bind.JAXBException)1 MediaInspectionException (org.opencastproject.inspection.api.MediaInspectionException)1 Job (org.opencastproject.job.api.Job)1 MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)1 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)1 Track (org.opencastproject.mediapackage.Track)1 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)1 SmilException (org.opencastproject.smil.api.SmilException)1 SmilMediaObject (org.opencastproject.smil.entity.media.api.SmilMediaObject)1 SmilMediaContainer (org.opencastproject.smil.entity.media.container.api.SmilMediaContainer)1 SmilMediaElement (org.opencastproject.smil.entity.media.element.api.SmilMediaElement)1 SmilMediaParam (org.opencastproject.smil.entity.media.param.api.SmilMediaParam)1 SmilMediaParamGroup (org.opencastproject.smil.entity.media.param.api.SmilMediaParamGroup)1