Search in sources :

Example 26 with EncoderException

use of org.opencastproject.composer.api.EncoderException in project opencast by opencast.

the class ComposerServiceImpl method extractImages.

private List<Attachment> extractImages(Job job, Track sourceTrack, String profileId, Map<String, String> properties, double... times) throws EncoderException {
    logger.info("creating an image using video track {}", sourceTrack.getIdentifier());
    // 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 videoFile = loadTrackIntoWorkspace(job, "video", sourceTrack);
    // Do the work
    List<File> encodingOutput;
    try {
        encodingOutput = encoderEngine.extract(videoFile, profile, properties, times);
        // check for validity of output
        if (encodingOutput == null || encodingOutput.isEmpty()) {
            logger.error("Image extraction from video {} with profile {} failed: no images were produced", sourceTrack.getURI(), profile.getIdentifier());
            throw new EncoderException("Image extraction failed: no images were produced");
        }
    } catch (EncoderException e) {
        Map<String, String> params = new HashMap<>();
        params.put("video", sourceTrack.getURI().toString());
        params.put("profile", profile.getIdentifier());
        params.put("positions", Arrays.toString(times));
        incident().recordFailure(job, IMAGE_EXTRACTION_FAILED, e, params, detailsFor(e, encoderEngine));
        throw e;
    } finally {
        activeEncoder.remove(encoderEngine);
    }
    int i = 0;
    List<URI> workspaceURIs = new LinkedList<>();
    for (File output : encodingOutput) {
        if (!output.exists() || output.length() == 0) {
            logger.warn("Extracted image {} is empty!", output);
            throw new EncoderException("Extracted image " + output.toString() + " is empty!");
        }
        // Put the file in the workspace
        InputStream in = null;
        try {
            in = new FileInputStream(output);
            URI returnURL = workspace.putInCollection(COLLECTION, job.getId() + "_" + i++ + "." + FilenameUtils.getExtension(output.getAbsolutePath()), in);
            logger.debug("Copied image file to the workspace at {}", returnURL);
            workspaceURIs.add(returnURL);
        } catch (Exception e) {
            cleanup(encodingOutput.toArray(new File[encodingOutput.size()]));
            cleanupWorkspace(workspaceURIs.toArray(new URI[workspaceURIs.size()]));
            incident().recordFailure(job, WORKSPACE_PUT_COLLECTION_IO_EXCEPTION, e, getWorkspaceCollectionParams("extracted image file", COLLECTION, output.toURI()), NO_DETAILS);
            throw new EncoderException("Unable to put image file into the workspace", e);
        } finally {
            IOUtils.closeQuietly(in);
        }
    }
    // cleanup
    cleanup(encodingOutput.toArray(new File[encodingOutput.size()]));
    MediaPackageElementBuilder builder = MediaPackageElementBuilderFactory.newInstance().newElementBuilder();
    List<Attachment> imageAttachments = new LinkedList<Attachment>();
    for (URI url : workspaceURIs) {
        Attachment attachment = (Attachment) builder.elementFromURI(url, Attachment.TYPE, null);
        imageAttachments.add(attachment);
    }
    return imageAttachments;
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) EncodingProfile(org.opencastproject.composer.api.EncodingProfile) Attachment(org.opencastproject.mediapackage.Attachment) URI(java.net.URI) LinkedList(java.util.LinkedList) FileInputStream(java.io.FileInputStream) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) MediaInspectionException(org.opencastproject.inspection.api.MediaInspectionException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) EncoderException(org.opencastproject.composer.api.EncoderException) EncoderException(org.opencastproject.composer.api.EncoderException) MediaPackageElementBuilder(org.opencastproject.mediapackage.MediaPackageElementBuilder) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap)

Example 27 with EncoderException

use of org.opencastproject.composer.api.EncoderException in project opencast by opencast.

the class ComposerServiceImpl method getProfile.

private EncodingProfile getProfile(Job job, String profileId) throws EncoderException {
    final EncodingProfile profile = profileScanner.getProfile(profileId);
    if (profile == null) {
        final String msg = String.format("Profile %s is unknown", profileId);
        logger.error(msg);
        incident().recordFailure(job, PROFILE_NOT_FOUND, Collections.map(tuple("profile", profileId)));
        throw new EncoderException(msg);
    }
    return profile;
}
Also used : EncoderException(org.opencastproject.composer.api.EncoderException) EncodingProfile(org.opencastproject.composer.api.EncodingProfile)

Example 28 with EncoderException

use of org.opencastproject.composer.api.EncoderException in project opencast by opencast.

the class ComposerServiceImpl method imageToVideo.

private Option<Track> imageToVideo(Job job, Attachment sourceImage, String profileId, Double time) throws EncoderException, MediaPackageException {
    // Get the encoding profile
    final EncodingProfile profile = getProfile(job, profileId);
    final String targetTrackId = idBuilder.createNew().toString();
    // Get the attachment and make sure it exist
    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 source image " + sourceImage + " is not found");
    } catch (IOException e) {
        incident().recordFailure(job, WORKSPACE_GET_IO_EXCEPTION, e, getWorkspaceMediapackageParams("source image", sourceImage), NO_DETAILS);
        throw new EncoderException("Unable to access source image " + sourceImage);
    }
    // Create the engine
    final EncoderEngine encoderEngine = getEncoderEngine();
    logger.info("Converting image attachment {} into video {}", sourceImage.getIdentifier(), targetTrackId);
    Map<String, String> properties = new HashMap<>();
    if (time == -1)
        time = 0D;
    DecimalFormatSymbols ffmpegFormat = new DecimalFormatSymbols();
    ffmpegFormat.setDecimalSeparator('.');
    DecimalFormat df = new DecimalFormat("0.000", ffmpegFormat);
    properties.put("time", df.format(time));
    File output;
    try {
        output = encoderEngine.encode(imageFile, profile, properties);
    } catch (EncoderException e) {
        Map<String, String> params = new HashMap<>();
        params.put("image", sourceImage.getURI().toString());
        params.put("profile", profile.getIdentifier());
        params.put("properties", properties.toString());
        incident().recordFailure(job, IMAGE_TO_VIDEO_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");
    // Have the compound track inspected and return the result
    Job inspectionJob = inspect(job, workspaceURI);
    Track inspectedTrack = (Track) MediaPackageElementParser.getFromXml(inspectionJob.getPayload());
    inspectedTrack.setIdentifier(targetTrackId);
    if (profile.getMimeType() != null)
        inspectedTrack.setMimeType(MimeTypes.parseMimeType(profile.getMimeType()));
    return some(inspectedTrack);
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) HashMap(java.util.HashMap) DecimalFormat(java.text.DecimalFormat) EncodingProfile(org.opencastproject.composer.api.EncodingProfile) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) URI(java.net.URI) EncoderException(org.opencastproject.composer.api.EncoderException) Job(org.opencastproject.job.api.Job) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap) Track(org.opencastproject.mediapackage.Track)

Example 29 with EncoderException

use of org.opencastproject.composer.api.EncoderException in project opencast by opencast.

the class ComposerServiceImpl method image.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.composer.api.ComposerService#image(Track, String, double...)
 */
@Override
public Job image(Track sourceTrack, String profileId, double... times) throws EncoderException, MediaPackageException {
    if (sourceTrack == null)
        throw new IllegalArgumentException("SourceTrack cannot be null");
    if (times.length == 0)
        throw new IllegalArgumentException("At least one time argument has to be specified");
    List<String> parameters = new ArrayList<>();
    parameters.add(profileId);
    parameters.add(MediaPackageElementParser.getAsXml(sourceTrack));
    parameters.add(Boolean.TRUE.toString());
    for (double time : times) {
        parameters.add(Double.toString(time));
    }
    try {
        final EncodingProfile profile = profileScanner.getProfile(profileId);
        return serviceRegistry.createJob(JOB_TYPE, Operation.Image.toString(), parameters, profile.getJobLoad());
    } catch (ServiceRegistryException e) {
        throw new EncoderException("Unable to create a job", e);
    }
}
Also used : EncoderException(org.opencastproject.composer.api.EncoderException) ArrayList(java.util.ArrayList) EncodingProfile(org.opencastproject.composer.api.EncodingProfile) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException)

Example 30 with EncoderException

use of org.opencastproject.composer.api.EncoderException in project opencast by opencast.

the class ComposerServiceImpl method putToCollection.

private URI putToCollection(Job job, File output, String description) throws EncoderException {
    URI returnURL = null;
    InputStream in = null;
    try {
        in = new FileInputStream(output);
        returnURL = workspace.putInCollection(COLLECTION, job.getId() + "." + FilenameUtils.getExtension(output.getAbsolutePath()), in);
        logger.info("Copied the {} to the workspace at {}", description, returnURL);
        return returnURL;
    } catch (Exception e) {
        incident().recordFailure(job, WORKSPACE_PUT_COLLECTION_IO_EXCEPTION, e, getWorkspaceCollectionParams(description, COLLECTION, output.toURI()), NO_DETAILS);
        cleanupWorkspace(returnURL);
        throw new EncoderException("Unable to put the " + description + " into the workspace", e);
    } finally {
        cleanup(output);
        IOUtils.closeQuietly(in);
    }
}
Also used : EncoderException(org.opencastproject.composer.api.EncoderException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) URI(java.net.URI) FileInputStream(java.io.FileInputStream) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) MediaInspectionException(org.opencastproject.inspection.api.MediaInspectionException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) EncoderException(org.opencastproject.composer.api.EncoderException)

Aggregations

EncoderException (org.opencastproject.composer.api.EncoderException)41 Job (org.opencastproject.job.api.Job)26 IOException (java.io.IOException)19 ArrayList (java.util.ArrayList)19 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)19 EncodingProfile (org.opencastproject.composer.api.EncodingProfile)16 URI (java.net.URI)12 HashMap (java.util.HashMap)12 Track (org.opencastproject.mediapackage.Track)12 File (java.io.File)11 NotFoundException (org.opencastproject.util.NotFoundException)11 HttpResponse (org.apache.http.HttpResponse)10 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)10 HttpPost (org.apache.http.client.methods.HttpPost)10 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)10 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)10 Map (java.util.Map)9 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)7 LinkedList (java.util.LinkedList)6 POST (javax.ws.rs.POST)6