use of org.opencastproject.composer.api.EncoderException in project opencast by opencast.
the class ComposerRestService method imageToVideo.
/**
* Transforms an image attachment to a video track
*
* @param sourceAttachmentXml
* The source image attachment
* @param profileId
* The profile to use for encoding
* @param timeString
* the length of the resulting video track in seconds
* @return A {@link Response} with the resulting track in the response body
* @throws Exception
*/
@POST
@Path("imagetovideo")
@Produces(MediaType.TEXT_XML)
@RestQuery(name = "imagetovideo", description = "Starts an image converting process to a video, based on the specified encoding profile ID and the source image attachment", restParameters = { @RestParameter(description = "The resulting video time in seconds", isRequired = false, name = "time", type = Type.STRING, defaultValue = "1"), @RestParameter(description = "The attachment containing the image to convert", isRequired = true, name = "sourceAttachment", type = Type.TEXT), @RestParameter(description = "The encoding profile to use", isRequired = true, name = "profileId", type = Type.STRING) }, reponses = { @RestResponse(description = "Results in an xml document containing the video track", responseCode = HttpServletResponse.SC_OK), @RestResponse(description = "If required parameters aren't set or if sourceAttachment isn't from the type Attachment", responseCode = HttpServletResponse.SC_BAD_REQUEST) }, returnDescription = "")
public Response imageToVideo(@FormParam("sourceAttachment") String sourceAttachmentXml, @FormParam("profileId") String profileId, @FormParam("time") @DefaultValue("1") String timeString) throws Exception {
// Ensure that the POST parameters are present
if (StringUtils.isBlank(sourceAttachmentXml) || StringUtils.isBlank(profileId))
return Response.status(Response.Status.BAD_REQUEST).entity("sourceAttachment and profileId must not be null").build();
// parse time
Double time;
try {
time = Double.parseDouble(timeString);
} catch (Exception e) {
logger.info("Unable to parse time {} as long value!", timeString);
return Response.status(Response.Status.BAD_REQUEST).entity("Could not parse time: invalid format").build();
}
// Deserialize the source track
MediaPackageElement sourceAttachment = MediaPackageElementParser.getFromXml(sourceAttachmentXml);
if (!Attachment.TYPE.equals(sourceAttachment.getElementType()))
return Response.status(Response.Status.BAD_REQUEST).entity("sourceAttachment element must be of type attachment").build();
try {
// Asynchronously convert the specified attachment to a video
Job job = composerService.imageToVideo((Attachment) sourceAttachment, profileId, time);
return Response.ok().entity(new JaxbJob(job)).build();
} catch (EncoderException e) {
logger.warn("Unable to convert image to video: " + e.getMessage());
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
}
use of org.opencastproject.composer.api.EncoderException in project opencast by opencast.
the class ComposerRestService method trim.
/**
* Trims a track to a new length.
*
* @param sourceTrackAsXml
* The source track
* @param profileId
* the encoding profile to use for trimming
* @param start
* the new trimming start time
* @param duration
* the new video duration
* @return A response containing the job for this encoding job in the response body.
* @throws Exception
*/
@POST
@Path("trim")
@Produces(MediaType.TEXT_XML)
@RestQuery(name = "trim", description = "Starts a trimming process, based on the specified track, start time and duration in ms", restParameters = { @RestParameter(description = "The track containing the stream", isRequired = true, name = "sourceTrack", type = Type.TEXT, defaultValue = "${this.videoTrackDefault}"), @RestParameter(description = "The encoding profile to use for trimming", isRequired = true, name = "profileId", type = Type.STRING, defaultValue = "trim.work"), @RestParameter(description = "The start time in milisecond", isRequired = true, name = "start", type = Type.STRING, defaultValue = "0"), @RestParameter(description = "The duration in milisecond", isRequired = true, name = "duration", type = Type.STRING, defaultValue = "10000") }, reponses = { @RestResponse(description = "Results in an xml document containing the job for the trimming task", responseCode = HttpServletResponse.SC_OK), @RestResponse(description = "If the start time is negative or exceeds the track duration", responseCode = HttpServletResponse.SC_BAD_REQUEST), @RestResponse(description = "If the duration is negative or, including the new start time, exceeds the track duration", responseCode = HttpServletResponse.SC_BAD_REQUEST) }, returnDescription = "")
public Response trim(@FormParam("sourceTrack") String sourceTrackAsXml, @FormParam("profileId") String profileId, @FormParam("start") long start, @FormParam("duration") long duration) throws Exception {
// Ensure that the POST parameters are present
if (StringUtils.isBlank(sourceTrackAsXml) || StringUtils.isBlank(profileId))
return Response.status(Response.Status.BAD_REQUEST).entity("sourceTrack and profileId must not be null").build();
// Deserialize the track
MediaPackageElement sourceElement = MediaPackageElementParser.getFromXml(sourceTrackAsXml);
if (!Track.TYPE.equals(sourceElement.getElementType()))
return Response.status(Response.Status.BAD_REQUEST).entity("sourceTrack element must be of type track").build();
// Make sure the trim times make sense
Track sourceTrack = (Track) sourceElement;
if (sourceTrack.getDuration() == null)
return Response.status(Response.Status.BAD_REQUEST).entity("sourceTrack element does not have a duration").build();
if (start < 0) {
start = 0;
} else if (duration <= 0) {
duration = (sourceTrack.getDuration() - start);
} else if (start + duration > sourceTrack.getDuration()) {
duration = (sourceTrack.getDuration() - start);
}
try {
// Asynchronously encode the specified tracks
Job job = composerService.trim(sourceTrack, profileId, start, duration);
return Response.ok().entity(new JaxbJob(job)).build();
} catch (EncoderException e) {
logger.warn("Unable to trim the track: " + e.getMessage());
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
}
use of org.opencastproject.composer.api.EncoderException in project opencast by opencast.
the class ComposerRestService method concat.
/**
* Concat multiple tracks having the same codec to a single track.
*
* @param sourceTracksXml
* an array of track to concat in order of the array as XML
* @param profileId
* The encoding profile to use
* @param outputDimension
* The output dimension as JSON
* @return A {@link Response} with the resulting track in the response body
* @throws Exception
*/
@POST
@Path("concat")
@Produces(MediaType.TEXT_XML)
@RestQuery(name = "concat", description = "Starts a video concating process from multiple videos, based on the specified encoding profile ID and the source tracks", restParameters = { @RestParameter(description = "The source tracks to concat as XML", isRequired = true, name = "sourceTracks", type = Type.TEXT), @RestParameter(description = "The encoding profile to use", isRequired = true, name = "profileId", type = Type.STRING), @RestParameter(description = "The resolution dimension of the concat video as JSON", isRequired = false, name = "outputDimension", type = Type.STRING), @RestParameter(description = "The frame rate of the concat video (should be positive, e.g. 25.0). Negative values and zero will deactivate frame rate operation.", isRequired = false, name = "outputFrameRate", type = Type.STRING) }, reponses = { @RestResponse(description = "Results in an xml document containing the video track", responseCode = HttpServletResponse.SC_OK), @RestResponse(description = "If required parameters aren't set or if sourceTracks aren't from the type Track or not at least two tracks are present", responseCode = HttpServletResponse.SC_BAD_REQUEST) }, returnDescription = "")
public Response concat(@FormParam("sourceTracks") String sourceTracksXml, @FormParam("profileId") String profileId, @FormParam("outputDimension") String outputDimension, @FormParam("outputFrameRate") String outputFrameRate) throws Exception {
// Ensure that the POST parameters are present
if (StringUtils.isBlank(sourceTracksXml) || StringUtils.isBlank(profileId))
return Response.status(Response.Status.BAD_REQUEST).entity("sourceTracks and profileId must not be null").build();
// Deserialize the source track
List<? extends MediaPackageElement> tracks = MediaPackageElementParser.getArrayFromXml(sourceTracksXml);
if (tracks.size() < 2)
return Response.status(Response.Status.BAD_REQUEST).entity("At least two tracks must be set to concat").build();
for (MediaPackageElement elem : tracks) {
if (!Track.TYPE.equals(elem.getElementType()))
return Response.status(Response.Status.BAD_REQUEST).entity("sourceTracks must be of type track").build();
}
float fps = NumberUtils.toFloat(outputFrameRate, -1.0f);
try {
// Asynchronously concat the specified tracks together
Dimension dimension = null;
if (StringUtils.isNotBlank(outputDimension)) {
dimension = Serializer.dimension(JsonObj.jsonObj(outputDimension));
}
Job job = null;
if (fps > 0) {
job = composerService.concat(profileId, dimension, fps, tracks.toArray(new Track[tracks.size()]));
} else {
job = composerService.concat(profileId, dimension, tracks.toArray(new Track[tracks.size()]));
}
return Response.ok().entity(new JaxbJob(job)).build();
} catch (EncoderException e) {
logger.warn("Unable to concat videos: " + e.getMessage());
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
}
use of org.opencastproject.composer.api.EncoderException in project opencast by opencast.
the class ComposerServiceRemoteImpl method mux.
/**
* {@inheritDoc}
*
* @see org.opencastproject.composer.api.ComposerService#mux(org.opencastproject.mediapackage.Track,
* org.opencastproject.mediapackage.Track, java.lang.String)
*/
@Override
public Job mux(Track sourceVideoTrack, Track sourceAudioTrack, String profileId) throws EncoderException {
HttpPost post = new HttpPost("/mux");
try {
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
params.add(new BasicNameValuePair("videoSourceTrack", MediaPackageElementParser.getAsXml(sourceVideoTrack)));
params.add(new BasicNameValuePair("audioSourceTrack", MediaPackageElementParser.getAsXml(sourceAudioTrack)));
params.add(new BasicNameValuePair("profileId", profileId));
post.setEntity(new UrlEncodedFormEntity(params));
} catch (Exception e) {
throw new EncoderException("Unable to assemble a remote composer request", e);
}
HttpResponse response = null;
try {
response = getResponse(post);
if (response != null) {
String content = EntityUtils.toString(response.getEntity());
Job r = JobParser.parseJob(content);
logger.info("Muxing job {} started on a remote composer", r.getId());
return r;
}
} catch (IOException e) {
throw new EncoderException(e);
} finally {
closeConnection(response);
}
throw new EncoderException("Unable to mux tracks " + sourceVideoTrack + " and " + sourceAudioTrack + " using a remote composer");
}
use of org.opencastproject.composer.api.EncoderException in project opencast by opencast.
the class ComposerServiceRemoteImpl method parallelEncode.
/**
* {@inheritDoc}
*/
@Override
public Job parallelEncode(Track sourceTrack, String profileId) throws EncoderException {
HttpPost post = new HttpPost("/parallelencode");
try {
List<BasicNameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("sourceTrack", MediaPackageElementParser.getAsXml(sourceTrack)));
params.add(new BasicNameValuePair("profileId", profileId));
post.setEntity(new UrlEncodedFormEntity(params));
} catch (Exception e) {
throw new EncoderException("Unable to assemble a remote composer request for track " + sourceTrack, e);
}
HttpResponse response = null;
try {
response = getResponse(post);
if (response != null) {
String content = EntityUtils.toString(response.getEntity());
Job r = JobParser.parseJob(content);
logger.info("Encoding job {} started on a remote composer", r.getId());
return r;
}
} catch (Exception e) {
throw new EncoderException("Unable to encode track " + sourceTrack + " using a remote composer service", e);
} finally {
closeConnection(response);
}
throw new EncoderException("Unable to encode track " + sourceTrack + " using a remote composer service");
}
Aggregations