Search in sources :

Example 1 with MediaPackageElementBuilder

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

the class CaptionServiceImpl method convert.

/**
 * Converts the captions and returns them in a new catalog.
 *
 * @return the converted catalog
 */
protected MediaPackageElement convert(Job job, MediaPackageElement input, String inputFormat, String outputFormat, String language) throws UnsupportedCaptionFormatException, CaptionConverterException, MediaPackageException {
    try {
        // check parameters
        if (input == null)
            throw new IllegalArgumentException("Input element can't be null");
        if (StringUtils.isBlank(inputFormat))
            throw new IllegalArgumentException("Input format is null");
        if (StringUtils.isBlank(outputFormat))
            throw new IllegalArgumentException("Output format is null");
        // get input file
        File captionsFile;
        try {
            captionsFile = workspace.get(input.getURI());
        } catch (NotFoundException e) {
            throw new CaptionConverterException("Requested media package element " + input + " could not be found.");
        } catch (IOException e) {
            throw new CaptionConverterException("Requested media package element " + input + "could not be accessed.");
        }
        logger.debug("Atempting to convert from {} to {}...", inputFormat, outputFormat);
        List<Caption> collection = null;
        try {
            collection = importCaptions(captionsFile, inputFormat, language);
            logger.debug("Parsing to collection succeeded.");
        } catch (UnsupportedCaptionFormatException e) {
            throw new UnsupportedCaptionFormatException(inputFormat);
        } catch (CaptionConverterException e) {
            throw e;
        }
        URI exported;
        try {
            exported = exportCaptions(collection, job.getId() + "." + FilenameUtils.getExtension(captionsFile.getAbsolutePath()), outputFormat, language);
            logger.debug("Exporting captions succeeding.");
        } catch (UnsupportedCaptionFormatException e) {
            throw new UnsupportedCaptionFormatException(outputFormat);
        } catch (IOException e) {
            throw new CaptionConverterException("Could not export caption collection.", e);
        }
        // create catalog and set properties
        CaptionConverter converter = getCaptionConverter(outputFormat);
        MediaPackageElementBuilder elementBuilder = MediaPackageElementBuilderFactory.newInstance().newElementBuilder();
        MediaPackageElement mpe = elementBuilder.elementFromURI(exported, converter.getElementType(), new MediaPackageElementFlavor("captions", outputFormat + (language == null ? "" : "+" + language)));
        if (mpe.getMimeType() == null) {
            String[] mimetype = FileTypeMap.getDefaultFileTypeMap().getContentType(exported.getPath()).split("/");
            mpe.setMimeType(mimeType(mimetype[0], mimetype[1]));
        }
        if (language != null)
            mpe.addTag("lang:" + language);
        return mpe;
    } catch (Exception e) {
        logger.warn("Error converting captions in " + input, e);
        if (e instanceof CaptionConverterException) {
            throw (CaptionConverterException) e;
        } else if (e instanceof UnsupportedCaptionFormatException) {
            throw (UnsupportedCaptionFormatException) e;
        } else {
            throw new CaptionConverterException(e);
        }
    }
}
Also used : CaptionConverterException(org.opencastproject.caption.api.CaptionConverterException) UnsupportedCaptionFormatException(org.opencastproject.caption.api.UnsupportedCaptionFormatException) NotFoundException(org.opencastproject.util.NotFoundException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) URI(java.net.URI) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) Caption(org.opencastproject.caption.api.Caption) CaptionConverterException(org.opencastproject.caption.api.CaptionConverterException) ConfigurationException(org.osgi.service.cm.ConfigurationException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedCaptionFormatException(org.opencastproject.caption.api.UnsupportedCaptionFormatException) MediaPackageElementBuilder(org.opencastproject.mediapackage.MediaPackageElementBuilder) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) CaptionConverter(org.opencastproject.caption.api.CaptionConverter) File(java.io.File)

Example 2 with MediaPackageElementBuilder

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

the class ComposerServiceImpl method convertImage.

/**
 * Converts an image from <code>sourceImage</code> to a new format.
 *
 * @param job
 *          the associated job
 * @param sourceImage
 *          the source image
 * @param profileId
 *          the identifer of the encoding profile to use
 * @return the image as an attachment or none if the operation does not return an image. This may happen for example
 *         when doing two pass encodings where the first pass only creates metadata for the second one
 * @throws EncoderException
 *           if converting the image fails
 */
private Option<Attachment> convertImage(Job job, Attachment sourceImage, String profileId) throws EncoderException, MediaPackageException {
    logger.info("Converting {}", sourceImage);
    // Get the encoding profile
    final EncodingProfile profile = getProfile(job, profileId);
    // Create the encoding engine
    final EncoderEngine encoderEngine = getEncoderEngine();
    // Finally get the file that needs to be encoded
    File imageFile;
    try {
        imageFile = workspace.get(sourceImage.getURI());
    } catch (NotFoundException e) {
        incident().recordFailure(job, WORKSPACE_GET_NOT_FOUND, e, getWorkspaceMediapackageParams("source image", sourceImage), NO_DETAILS);
        throw new EncoderException("Requested video track " + sourceImage + " was not found", e);
    } catch (IOException e) {
        incident().recordFailure(job, WORKSPACE_GET_IO_EXCEPTION, e, getWorkspaceMediapackageParams("source image", sourceImage), NO_DETAILS);
        throw new EncoderException("Error accessing video track " + sourceImage, e);
    }
    // Do the work
    File output;
    try {
        output = encoderEngine.encode(imageFile, profile, null);
    } catch (EncoderException e) {
        Map<String, String> params = new HashMap<>();
        params.put("image", sourceImage.getURI().toString());
        params.put("profile", profile.getIdentifier());
        incident().recordFailure(job, CONVERT_IMAGE_FAILED, e, params, detailsFor(e, encoderEngine));
        throw e;
    } finally {
        activeEncoder.remove(encoderEngine);
    }
    // encoding did not return a file
    if (!output.exists() || output.length() == 0)
        return none();
    // Put the file in the workspace
    URI workspaceURI = putToCollection(job, output, "converted image file");
    MediaPackageElementBuilder builder = MediaPackageElementBuilderFactory.newInstance().newElementBuilder();
    Attachment attachment = (Attachment) builder.elementFromURI(workspaceURI, Attachment.TYPE, null);
    return some(attachment);
}
Also used : EncoderException(org.opencastproject.composer.api.EncoderException) MediaPackageElementBuilder(org.opencastproject.mediapackage.MediaPackageElementBuilder) EncodingProfile(org.opencastproject.composer.api.EncodingProfile) NotFoundException(org.opencastproject.util.NotFoundException) Attachment(org.opencastproject.mediapackage.Attachment) IOException(java.io.IOException) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap) URI(java.net.URI)

Example 3 with MediaPackageElementBuilder

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

the class WaveformServiceImpl method extractWaveform.

/**
 * Create and run waveform extraction ffmpeg command.
 *
 * @param track source audio/video track with at least one audio channel
 * @return waveform image attachment
 * @throws WaveformServiceException if processing fails
 */
private Attachment extractWaveform(Track track) throws WaveformServiceException {
    if (!track.hasAudio()) {
        throw new WaveformServiceException("Track has no audio");
    }
    // copy source file into workspace
    File mediaFile;
    try {
        mediaFile = workspace.get(track.getURI());
    } catch (NotFoundException e) {
        throw new WaveformServiceException("Error finding the media file in the workspace", e);
    } catch (IOException e) {
        throw new WaveformServiceException("Error reading the media file in the workspace", e);
    }
    String waveformFilePath = FilenameUtils.removeExtension(mediaFile.getAbsolutePath()).concat('-' + track.getIdentifier()).concat("-waveform.png");
    // create ffmpeg command
    String[] command = new String[] { binary, "-nostats", "-i", mediaFile.getAbsolutePath(), "-lavfi", createWaveformFilter(track), "-an", "-vn", "-sn", "-y", waveformFilePath };
    logger.debug("Start waveform ffmpeg process: {}", StringUtils.join(command, " "));
    logger.info("Create waveform image file for track '{}' at {}", track.getIdentifier(), waveformFilePath);
    // run ffmpeg
    ProcessBuilder pb = new ProcessBuilder(command);
    pb.redirectErrorStream(true);
    Process ffmpegProcess = null;
    int exitCode = 1;
    BufferedReader errStream = null;
    try {
        ffmpegProcess = pb.start();
        errStream = new BufferedReader(new InputStreamReader(ffmpegProcess.getInputStream()));
        String line = errStream.readLine();
        while (line != null) {
            logger.debug(line);
            line = errStream.readLine();
        }
        exitCode = ffmpegProcess.waitFor();
    } catch (IOException ex) {
        throw new WaveformServiceException("Start ffmpeg process failed", ex);
    } catch (InterruptedException ex) {
        throw new WaveformServiceException("Waiting for encoder process exited was interrupted unexpectly", ex);
    } finally {
        IoSupport.closeQuietly(ffmpegProcess);
        IoSupport.closeQuietly(errStream);
        if (exitCode != 0) {
            try {
                FileUtils.forceDelete(new File(waveformFilePath));
            } catch (IOException e) {
            // it is ok, no output file was generated by ffmpeg
            }
        }
    }
    if (exitCode != 0)
        throw new WaveformServiceException("The encoder process exited abnormally with exit code " + exitCode);
    // put waveform image into workspace
    FileInputStream waveformFileInputStream = null;
    URI waveformFileUri;
    try {
        waveformFileInputStream = new FileInputStream(waveformFilePath);
        waveformFileUri = workspace.putInCollection(COLLECTION_ID, FilenameUtils.getName(waveformFilePath), waveformFileInputStream);
        logger.info("Copied the created waveform to the workspace {}", waveformFileUri);
    } catch (FileNotFoundException ex) {
        throw new WaveformServiceException(String.format("Waveform image file '%s' not found", waveformFilePath), ex);
    } catch (IOException ex) {
        throw new WaveformServiceException(String.format("Can't write waveform image file '%s' to workspace", waveformFilePath), ex);
    } catch (IllegalArgumentException ex) {
        throw new WaveformServiceException(ex);
    } finally {
        IoSupport.closeQuietly(waveformFileInputStream);
        logger.info("Deleted local waveform image file at {}", waveformFilePath);
        FileUtils.deleteQuietly(new File(waveformFilePath));
    }
    // create media package element
    MediaPackageElementBuilder mpElementBuilder = MediaPackageElementBuilderFactory.newInstance().newElementBuilder();
    // it is up to the workflow operation handler to set the attachment flavor
    Attachment waveformMpe = (Attachment) mpElementBuilder.elementFromURI(waveformFileUri, Type.Attachment, track.getFlavor());
    waveformMpe.setIdentifier(IdBuilderFactory.newInstance().newIdBuilder().createNew().compact());
    return waveformMpe;
}
Also used : InputStreamReader(java.io.InputStreamReader) FileNotFoundException(java.io.FileNotFoundException) NotFoundException(org.opencastproject.util.NotFoundException) FileNotFoundException(java.io.FileNotFoundException) Attachment(org.opencastproject.mediapackage.Attachment) IOException(java.io.IOException) URI(java.net.URI) FileInputStream(java.io.FileInputStream) MediaPackageElementBuilder(org.opencastproject.mediapackage.MediaPackageElementBuilder) WaveformServiceException(org.opencastproject.waveform.api.WaveformServiceException) BufferedReader(java.io.BufferedReader) File(java.io.File)

Example 4 with MediaPackageElementBuilder

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

the class ToolsEndpoint method addSmilToArchive.

/**
 * Adds the SMIL file as {@link Catalog} to the media package and sends the updated media package to the archive.
 *
 * @param mediaPackage
 *          the media package to at the SMIL catalog
 * @param smil
 *          the SMIL catalog
 * @return the updated media package
 * @throws IOException
 *           if the SMIL catalog cannot be read or not be written to the archive
 */
MediaPackage addSmilToArchive(MediaPackage mediaPackage, final Smil smil) throws IOException {
    MediaPackageElementFlavor mediaPackageElementFlavor = adminUIConfiguration.getSmilCatalogFlavor();
    // set default catalog Id if there is none existing
    String catalogId = smil.getId();
    Catalog[] catalogs = mediaPackage.getCatalogs();
    // get the first smil/cutting  catalog-ID to overwrite it with new smil info
    for (Catalog p : catalogs) {
        if (p.getFlavor().matches(mediaPackageElementFlavor)) {
            logger.debug("Set Idendifier for Smil-Catalog to: " + p.getIdentifier());
            catalogId = p.getIdentifier();
            break;
        }
    }
    Catalog catalog = mediaPackage.getCatalog(catalogId);
    URI smilURI;
    try (InputStream is = IOUtils.toInputStream(smil.toXML(), "UTF-8")) {
        smilURI = workspace.put(mediaPackage.getIdentifier().compact(), catalogId, TARGET_FILE_NAME, is);
    } catch (SAXException e) {
        logger.error("Error while serializing the SMIL catalog to XML: {}", e.getMessage());
        throw new IOException(e);
    } catch (JAXBException e) {
        logger.error("Error while serializing the SMIL catalog to XML: {}", e.getMessage());
        throw new IOException(e);
    }
    if (catalog == null) {
        MediaPackageElementBuilder mpeBuilder = MediaPackageElementBuilderFactory.newInstance().newElementBuilder();
        catalog = (Catalog) mpeBuilder.elementFromURI(smilURI, MediaPackageElement.Type.Catalog, adminUIConfiguration.getSmilCatalogFlavor());
        mediaPackage.add(catalog);
    }
    catalog.setURI(smilURI);
    catalog.setIdentifier(catalogId);
    catalog.setMimeType(MimeTypes.XML);
    for (String tag : adminUIConfiguration.getSmilCatalogTags()) {
        catalog.addTag(tag);
    }
    // setting the URI to a new source so the checksum will most like be invalid
    catalog.setChecksum(null);
    try {
        // FIXME SWITCHP-333: Start in new thread
        assetManager.takeSnapshot(DEFAULT_OWNER, mediaPackage);
    } catch (AssetManagerException e) {
        logger.error("Error while adding the updated media package ({}) to the archive: {}", mediaPackage.getIdentifier(), e.getMessage());
        throw new IOException(e);
    }
    return mediaPackage;
}
Also used : MediaPackageElementBuilder(org.opencastproject.mediapackage.MediaPackageElementBuilder) InputStream(java.io.InputStream) JAXBException(javax.xml.bind.JAXBException) IOException(java.io.IOException) AssetManagerException(org.opencastproject.assetmanager.api.AssetManagerException) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) URI(java.net.URI) Catalog(org.opencastproject.mediapackage.Catalog) SAXException(org.xml.sax.SAXException)

Example 5 with MediaPackageElementBuilder

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

the class LiveScheduleServiceImpl method buildStreamingTrack.

Track buildStreamingTrack(String uriString, MediaPackageElementFlavor flavor, String mimeType, String resolution, long duration) throws URISyntaxException {
    URI uri = new URI(uriString);
    MediaPackageElementBuilder elementBuilder = MediaPackageElementBuilderFactory.newInstance().newElementBuilder();
    MediaPackageElement element = elementBuilder.elementFromURI(uri, MediaPackageElement.Type.Track, flavor);
    TrackImpl track = (TrackImpl) element;
    // Set duration and mime type
    track.setDuration(duration);
    track.setLive(true);
    track.setMimeType(MimeTypes.parseMimeType(mimeType));
    VideoStreamImpl video = new VideoStreamImpl("video-" + flavor.getType() + "-" + flavor.getSubtype());
    // Set video resolution
    String[] dimensions = resolution.split("x");
    video.setFrameWidth(Integer.parseInt(dimensions[0]));
    video.setFrameHeight(Integer.parseInt(dimensions[1]));
    track.addStream(video);
    logger.debug("Creating live track element of flavor {}, resolution {}, and url {}", new Object[] { flavor, resolution, uriString });
    return track;
}
Also used : MediaPackageElementBuilder(org.opencastproject.mediapackage.MediaPackageElementBuilder) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) TrackImpl(org.opencastproject.mediapackage.track.TrackImpl) VideoStreamImpl(org.opencastproject.mediapackage.track.VideoStreamImpl) URI(java.net.URI)

Aggregations

MediaPackageElementBuilder (org.opencastproject.mediapackage.MediaPackageElementBuilder)17 URI (java.net.URI)12 IOException (java.io.IOException)11 NotFoundException (org.opencastproject.util.NotFoundException)8 Attachment (org.opencastproject.mediapackage.Attachment)7 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)7 File (java.io.File)6 InputStream (java.io.InputStream)6 MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)6 FileInputStream (java.io.FileInputStream)5 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)5 Catalog (org.opencastproject.mediapackage.Catalog)4 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)4 WorkflowOperationException (org.opencastproject.workflow.api.WorkflowOperationException)4 FileNotFoundException (java.io.FileNotFoundException)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 EncoderException (org.opencastproject.composer.api.EncoderException)3 EncodingProfile (org.opencastproject.composer.api.EncodingProfile)3 MediaPackage (org.opencastproject.mediapackage.MediaPackage)3