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;
}
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;
}
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);
}
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);
}
}
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);
}
}
Aggregations