use of org.opencastproject.mediapackage.MediaPackageException in project opencast by opencast.
the class SmilServiceRest method addClip.
@POST
@Path("addClip")
@Produces({ MediaType.APPLICATION_XML, MediaType.TEXT_XML })
@RestQuery(name = "addClip", description = "Add new media element based on given Track information and start / duration parameters. " + "ParentId specifies where to put the new media element.", restParameters = { @RestParameter(name = "smil", description = "SMIL document where to add new media element.", isRequired = true, type = RestParameter.Type.TEXT), @RestParameter(name = "parentId", description = "An element Id, were to add new media.", isRequired = false, type = RestParameter.Type.STRING), @RestParameter(name = "track", description = "Track (MediaPackageElement) to add as media element. " + "Some information like Track source and flavor will be stored in ParamGroup (in SMIL Head) " + "and referenced by paramGroup media element attribute.", isRequired = true, type = RestParameter.Type.TEXT), @RestParameter(name = "start", description = "Track start position in milliseconds.", isRequired = true, type = RestParameter.Type.INTEGER), @RestParameter(name = "duration", description = "Clip duration in milliseconds (should be positive).", isRequired = true, type = RestParameter.Type.INTEGER) }, returnDescription = "Returns new Smil with an media element inside " + "(the new media and metadata elements will be returned as response entities).", reponses = { @RestResponse(responseCode = HttpServletResponse.SC_OK, description = "Add media element to SMIL successfull."), @RestResponse(responseCode = HttpServletResponse.SC_BAD_REQUEST, description = "SMIL document not valid."), @RestResponse(responseCode = HttpServletResponse.SC_BAD_REQUEST, description = "Track not valid."), @RestResponse(responseCode = HttpServletResponse.SC_BAD_REQUEST, description = "SMIL document doesn't contain an element with given parentId."), @RestResponse(responseCode = HttpServletResponse.SC_BAD_REQUEST, description = "Start plus duration is bigger than Track length.") })
public Response addClip(@FormParam("smil") String smil, @FormParam("parentId") String parentId, @FormParam("track") String track, @FormParam("start") long start, @FormParam("duration") long duration) {
SmilResponse smilResponse = null;
Track trackObj = null;
try {
smilResponse = smilService.fromXml(smil);
trackObj = (Track) MediaPackageElementParser.getFromXml(track);
} catch (SmilException ex) {
return Response.status(HttpServletResponse.SC_BAD_REQUEST).entity("SMIL document invalid.").build();
} catch (MediaPackageException ex) {
return Response.status(HttpServletResponse.SC_BAD_REQUEST).entity("Track is not valid.").build();
}
try {
smilResponse = smilService.addClip(smilResponse.getSmil(), parentId, trackObj, start, duration);
return Response.ok(smilResponse).build();
} catch (SmilException ex) {
logger.info(ex.getMessage(), ex);
return Response.status(HttpServletResponse.SC_BAD_REQUEST).entity("SMIL document doesn't contain an element with given parentId.").build();
}
}
use of org.opencastproject.mediapackage.MediaPackageException in project opencast by opencast.
the class SilenceDetectionServiceImpl method detect.
/**
* {@inheritDoc}
*
* @see org.opencastproject.silencedetection.api.SilenceDetectionService#detect(
* org.opencastproject.mediapackage.Track, org.opencastproject.mediapackage.Track[])
*/
@Override
public Job detect(Track sourceTrack, Track[] referenceTracks) throws SilenceDetectionFailedException {
try {
if (sourceTrack == null) {
throw new SilenceDetectionFailedException("Source track is null!");
}
List<String> arguments = new LinkedList<String>();
// put source track as job argument
arguments.add(0, MediaPackageElementParser.getAsXml(sourceTrack));
// put reference tracks as second argument
if (referenceTracks != null) {
arguments.add(1, MediaPackageElementParser.getArrayAsXml(Arrays.asList(referenceTracks)));
}
return serviceRegistry.createJob(getJobType(), Operation.SILENCE_DETECTION.toString(), arguments, jobload);
} catch (ServiceRegistryException ex) {
throw new SilenceDetectionFailedException("Unable to create job! " + ex.getMessage());
} catch (MediaPackageException ex) {
throw new SilenceDetectionFailedException("Unable to serialize track!");
}
}
use of org.opencastproject.mediapackage.MediaPackageException in project opencast by opencast.
the class SilenceDetectionServiceImpl method process.
@Override
protected String process(Job job) throws SilenceDetectionFailedException, SmilException, MediaPackageException {
if (Operation.SILENCE_DETECTION.toString().equals(job.getOperation())) {
// get source track
String sourceTrackXml = StringUtils.trimToNull(job.getArguments().get(0));
if (sourceTrackXml == null) {
throw new SilenceDetectionFailedException("Track not set!");
}
Track sourceTrack = (Track) MediaPackageElementParser.getFromXml(sourceTrackXml);
// run detection on source track
MediaSegments segments = runDetection(sourceTrack);
// get reference tracks if any
List<Track> referenceTracks = null;
if (job.getArguments().size() > 1) {
String referenceTracksXml = StringUtils.trimToNull(job.getArguments().get(1));
if (referenceTracksXml != null) {
referenceTracks = (List<Track>) MediaPackageElementParser.getArrayFromXml(referenceTracksXml);
}
}
if (referenceTracks == null) {
referenceTracks = Arrays.asList(sourceTrack);
}
// create smil XML as result
try {
return generateSmil(segments, referenceTracks).toXML();
} catch (Exception ex) {
throw new SmilException("Failed to create smil document.", ex);
}
}
throw new SilenceDetectionFailedException("Can't handle this operation: " + job.getOperation());
}
use of org.opencastproject.mediapackage.MediaPackageException in project opencast by opencast.
the class DuplicateEventWorkflowOperationHandler method copyMediaPackage.
private MediaPackage copyMediaPackage(MediaPackage source, long copyNumber, String copyNumberPrefix) throws WorkflowOperationException {
// We are not using MediaPackage.clone() here, since it does "too much" for us (e.g. copies all the attachments)
MediaPackage destination;
try {
destination = MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder().createNew();
} catch (MediaPackageException e) {
logger.error("Failed to create media package " + e.getLocalizedMessage());
throw new WorkflowOperationException(e);
}
logger.info("Created mediapackage {}", destination);
destination.setDate(source.getDate());
destination.setSeries(source.getSeries());
destination.setSeriesTitle(source.getSeriesTitle());
destination.setDuration(source.getDuration());
destination.setLanguage(source.getLanguage());
destination.setLicense(source.getLicense());
destination.setTitle(source.getTitle() + " (" + copyNumberPrefix + " " + copyNumber + ")");
return destination;
}
use of org.opencastproject.mediapackage.MediaPackageException in project opencast by opencast.
the class ZipWorkflowOperationHandler method zip.
/**
* Creates a zip archive of all elements in a mediapackage.
*
* @param mediaPackage
* the mediapackage to zip
*
* @return the zip file
*
* @throws IOException
* If an IO exception occurs
* @throws NotFoundException
* If a file referenced in the mediapackage can not be found
* @throws MediaPackageException
* If the mediapackage can not be serialized to xml
* @throws WorkflowOperationException
* If the mediapackage is invalid
*/
protected File zip(MediaPackage mediaPackage, List<MediaPackageElementFlavor> flavorsToZip, boolean compress) throws IOException, NotFoundException, MediaPackageException, WorkflowOperationException {
if (mediaPackage == null) {
throw new WorkflowOperationException("Invalid mediapackage");
}
// Create the temp directory
File mediaPackageDir = new File(tempStorageDir, mediaPackage.getIdentifier().compact());
FileUtils.forceMkdir(mediaPackageDir);
// Link or copy each matching element's file from the workspace to the temp directory
MediaPackageSerializer serializer = new DefaultMediaPackageSerializerImpl(mediaPackageDir);
MediaPackage clone = (MediaPackage) mediaPackage.clone();
for (MediaPackageElement element : clone.getElements()) {
// remove the element if it doesn't match the flavors to zip
boolean remove = true;
for (MediaPackageElementFlavor flavor : flavorsToZip) {
if (flavor.matches(element.getFlavor())) {
remove = false;
break;
}
}
if (remove) {
clone.remove(element);
continue;
}
File elementDir = new File(mediaPackageDir, element.getIdentifier());
FileUtils.forceMkdir(elementDir);
File workspaceFile = workspace.get(element.getURI());
File linkedFile = FileSupport.link(workspaceFile, new File(elementDir, workspaceFile.getName()), true);
try {
element.setURI(serializer.encodeURI(linkedFile.toURI()));
} catch (URISyntaxException e) {
throw new MediaPackageException("unable to serialize a mediapackage element", e);
}
}
// Add the manifest
FileUtils.writeStringToFile(new File(mediaPackageDir, "manifest.xml"), MediaPackageParser.getAsXml(clone), "UTF-8");
// Zip the directory
File zip = new File(tempStorageDir, clone.getIdentifier().compact() + ".zip");
int compressValue = compress ? ZipUtil.DEFAULT_COMPRESSION : ZipUtil.NO_COMPRESSION;
long startTime = System.currentTimeMillis();
ZipUtil.zip(new File[] { mediaPackageDir }, zip, true, compressValue);
long stopTime = System.currentTimeMillis();
logger.debug("Zip file creation took {} seconds", (stopTime - startTime) / 1000);
// Remove the directory
FileUtils.forceDelete(mediaPackageDir);
// Return the zip
return zip;
}
Aggregations