use of org.opencastproject.smil.entity.api.Smil in project opencast by opencast.
the class ToolsEndpoint method editVideo.
@POST
@Path("{mediapackageid}/editor.json")
@Consumes(MediaType.APPLICATION_JSON)
@RestQuery(name = "editVideo", description = "Takes editing information from the client side and processes it", returnDescription = "", pathParameters = { @RestParameter(name = "mediapackageid", description = "The id of the media package", isRequired = true, type = RestParameter.Type.STRING) }, reponses = { @RestResponse(description = "Editing information saved and processed", responseCode = HttpServletResponse.SC_OK), @RestResponse(description = "Media package not found", responseCode = HttpServletResponse.SC_NOT_FOUND), @RestResponse(description = "The editing information cannot be parsed", responseCode = HttpServletResponse.SC_BAD_REQUEST) })
public Response editVideo(@PathParam("mediapackageid") final String mediaPackageId, @Context HttpServletRequest request) throws IndexServiceException, NotFoundException {
String details;
try (InputStream is = request.getInputStream()) {
details = IOUtils.toString(is);
} catch (IOException e) {
logger.error("Error reading request body: {}", getStackTrace(e));
return R.serverError();
}
JSONParser parser = new JSONParser();
EditingInfo editingInfo;
try {
JSONObject detailsJSON = (JSONObject) parser.parse(details);
editingInfo = EditingInfo.parse(detailsJSON);
} catch (Exception e) {
logger.warn("Unable to parse concat information ({}): {}", details, ExceptionUtils.getStackTrace(e));
return R.badRequest("Unable to parse details");
}
final Opt<Event> optEvent = getEvent(mediaPackageId);
if (optEvent.isNone()) {
return R.notFound();
} else {
MediaPackage mediaPackage = index.getEventMediapackage(optEvent.get());
Smil smil;
try {
smil = createSmilCuttingCatalog(editingInfo, mediaPackage);
} catch (Exception e) {
logger.warn("Unable to create a SMIL cutting catalog ({}): {}", details, getStackTrace(e));
return R.badRequest("Unable to create SMIL cutting catalog");
}
try {
addSmilToArchive(mediaPackage, smil);
} catch (IOException e) {
logger.warn("Unable to add SMIL cutting catalog to archive: {}", getStackTrace(e));
return R.serverError();
}
if (editingInfo.getPostProcessingWorkflow().isSome()) {
final String workflowId = editingInfo.getPostProcessingWorkflow().get();
try {
final Workflows workflows = new Workflows(assetManager, workspace, workflowService);
workflows.applyWorkflowToLatestVersion($(mediaPackage.getIdentifier().toString()), ConfiguredWorkflow.workflow(workflowService.getWorkflowDefinitionById(workflowId))).run();
} catch (AssetManagerException e) {
logger.warn("Unable to start workflow '{}' on archived media package '{}': {}", workflowId, mediaPackage, getStackTrace(e));
return R.serverError();
} catch (WorkflowDatabaseException e) {
logger.warn("Unable to load workflow '{}' from workflow service: {}", workflowId, getStackTrace(e));
return R.serverError();
} catch (NotFoundException e) {
logger.warn("Workflow '{}' not found", workflowId);
return R.badRequest("Workflow not found");
}
}
}
return R.ok();
}
use of org.opencastproject.smil.entity.api.Smil in project opencast by opencast.
the class ToolsEndpoint method getSegments.
/**
* Analyzes the media package and tries to get information about segments out of it.
*
* @param mediaPackage
* the media package
* @return a list of segments or an empty list if no segments could be found.
*/
private List<Tuple<Long, Long>> getSegments(final MediaPackage mediaPackage) {
List<Tuple<Long, Long>> segments = new ArrayList<>();
for (Catalog smilCatalog : mediaPackage.getCatalogs(adminUIConfiguration.getSmilCatalogFlavor())) {
try {
Smil smil = smilService.fromXml(workspace.get(smilCatalog.getURI())).getSmil();
segments = mergeSegments(segments, getSegmentsFromSmil(smil));
} catch (NotFoundException e) {
logger.warn("File '{}' could not be loaded by workspace service: {}", smilCatalog.getURI(), getStackTrace(e));
} catch (IOException e) {
logger.warn("Reading file '{}' from workspace service failed: {}", smilCatalog.getURI(), getStackTrace(e));
} catch (SmilException e) {
logger.warn("Error while parsing SMIL catalog '{}': {}", smilCatalog.getURI(), getStackTrace(e));
}
}
if (!segments.isEmpty())
return segments;
// Read from silence detection flavors
for (Catalog smilCatalog : mediaPackage.getCatalogs(adminUIConfiguration.getSmilSilenceFlavor())) {
try {
Smil smil = smilService.fromXml(workspace.get(smilCatalog.getURI())).getSmil();
segments = mergeSegments(segments, getSegmentsFromSmil(smil));
} catch (NotFoundException e) {
logger.warn("File '{}' could not be loaded by workspace service: {}", smilCatalog.getURI(), getStackTrace(e));
} catch (IOException e) {
logger.warn("Reading file '{}' from workspace service failed: {}", smilCatalog.getURI(), getStackTrace(e));
} catch (SmilException e) {
logger.warn("Error while parsing SMIL catalog '{}': {}", smilCatalog.getURI(), getStackTrace(e));
}
}
// Check for single segment to ignore
if (segments.size() == 1) {
Tuple<Long, Long> singleSegment = segments.get(0);
if (singleSegment.getA() == 0 && singleSegment.getB() >= mediaPackage.getDuration())
segments.remove(0);
}
return segments;
}
use of org.opencastproject.smil.entity.api.Smil in project opencast by opencast.
the class ToolsEndpointTest method setUpClass.
@BeforeClass
public static void setUpClass() throws Exception {
/* Start of Smil mockups */
// Ugly, but strictly the smil APIs
String trackSrc = "http://mh-allinone.localdomain/archive/archive/mediapackage/0f2a2ada-0584-4d4d-a248-111f654aa217/6ec443e7-b097-4470-a618-5e0d848f5252/0/track.mp4";
URL smilUrl = ToolsEndpoint.class.getResource("/tools/smil1.xml");
String smilString = IOUtils.toString(smilUrl);
String trackParamGroupId = "pg-a6d8e576-495f-44c7-8ed7-b5b47c807f0f";
SmilMediaParam param1 = EasyMock.createNiceMock(SmilMediaParam.class);
EasyMock.expect(param1.getName()).andReturn("track-id").anyTimes();
EasyMock.expect(param1.getValue()).andReturn("track-1").anyTimes();
EasyMock.expect(param1.getId()).andReturn("param-e2f41e7d-caba-401b-a03a-e524296cb235").anyTimes();
SmilMediaParam param2 = EasyMock.createNiceMock(SmilMediaParam.class);
EasyMock.expect(param2.getName()).andReturn("track-src").anyTimes();
EasyMock.expect(param2.getValue()).andReturn(trackSrc).anyTimes();
EasyMock.expect(param2.getId()).andReturn("param-1bd5e839-0a74-4310-b1d2-daba07914f79").anyTimes();
SmilMediaParam param3 = EasyMock.createNiceMock(SmilMediaParam.class);
EasyMock.expect(param3.getName()).andReturn("track-flavor").anyTimes();
EasyMock.expect(param3.getValue()).andReturn("presenter/work").anyTimes();
EasyMock.expect(param3.getId()).andReturn("param-1bd5e839-0a74-4310-b1d2-daba07914f79").anyTimes();
EasyMock.replay(param1, param2, param3);
List<SmilMediaParam> params = new ArrayList<>();
params.add(param1);
params.add(param2);
params.add(param3);
SmilMediaParamGroup group1 = EasyMock.createNiceMock(SmilMediaParamGroup.class);
EasyMock.expect(group1.getParams()).andReturn(params).anyTimes();
EasyMock.expect(group1.getId()).andReturn(trackParamGroupId).anyTimes();
EasyMock.replay(group1);
List<SmilMediaParamGroup> paramGroups = new ArrayList<>();
paramGroups.add(group1);
SmilHead head = EasyMock.createNiceMock(SmilHead.class);
EasyMock.expect(head.getParamGroups()).andReturn(paramGroups).anyTimes();
EasyMock.replay(head);
SmilMediaElement object1 = EasyMock.createNiceMock(SmilMediaElement.class);
EasyMock.expect(object1.isContainer()).andReturn(false).anyTimes();
EasyMock.expect(object1.getParamGroup()).andReturn(trackParamGroupId).anyTimes();
EasyMock.expect(object1.getClipBeginMS()).andReturn(0L).anyTimes();
EasyMock.expect(object1.getClipEndMS()).andReturn(2449L).anyTimes();
EasyMock.expect(object1.getSrc()).andReturn(new URI(trackSrc)).anyTimes();
EasyMock.replay(object1);
SmilMediaElement object2 = EasyMock.createNiceMock(SmilMediaElement.class);
EasyMock.expect(object2.isContainer()).andReturn(false).anyTimes();
EasyMock.expect(object2.getParamGroup()).andReturn(trackParamGroupId).anyTimes();
EasyMock.expect(object2.getClipBeginMS()).andReturn(4922L).anyTimes();
EasyMock.expect(object2.getClipEndMS()).andReturn(11284L).anyTimes();
EasyMock.expect(object2.getSrc()).andReturn(new URI(trackSrc)).anyTimes();
EasyMock.replay(object2);
SmilMediaElement object3 = EasyMock.createNiceMock(SmilMediaElement.class);
EasyMock.expect(object3.isContainer()).andReturn(false).anyTimes();
EasyMock.expect(object3.getParamGroup()).andReturn(trackParamGroupId).anyTimes();
EasyMock.expect(object3.getClipBeginMS()).andReturn(14721L).anyTimes();
EasyMock.expect(object3.getClipEndMS()).andReturn(15963L).anyTimes();
EasyMock.expect(object3.getSrc()).andReturn(new URI(trackSrc)).anyTimes();
EasyMock.replay(object3);
SmilMediaElement object4 = EasyMock.createNiceMock(SmilMediaElement.class);
EasyMock.expect(object4.isContainer()).andReturn(false).anyTimes();
EasyMock.expect(object4.getParamGroup()).andReturn(trackParamGroupId).anyTimes();
EasyMock.expect(object4.getClipBeginMS()).andReturn(15963L).anyTimes();
EasyMock.expect(object4.getClipEndMS()).andReturn(20132L).anyTimes();
EasyMock.expect(object4.getSrc()).andReturn(new URI(trackSrc)).anyTimes();
EasyMock.replay(object4);
List<SmilMediaObject> objects1 = new ArrayList<>();
objects1.add(object1);
List<SmilMediaObject> objects2 = new ArrayList<>();
objects2.add(object2);
List<SmilMediaObject> objects3 = new ArrayList<>();
objects3.add(object3);
List<SmilMediaObject> objects4 = new ArrayList<>();
objects4.add(object4);
SmilMediaContainer objectContainer1 = EasyMock.createNiceMock(SmilMediaContainer.class);
EasyMock.expect(objectContainer1.isContainer()).andReturn(true).anyTimes();
EasyMock.expect(objectContainer1.getContainerType()).andReturn(SmilMediaContainer.ContainerType.PAR).anyTimes();
EasyMock.expect(objectContainer1.getElements()).andReturn(objects1).anyTimes();
EasyMock.replay(objectContainer1);
SmilMediaContainer objectContainer2 = EasyMock.createNiceMock(SmilMediaContainer.class);
EasyMock.expect(objectContainer2.isContainer()).andReturn(true).anyTimes();
EasyMock.expect(objectContainer2.getContainerType()).andReturn(SmilMediaContainer.ContainerType.PAR).anyTimes();
EasyMock.expect(objectContainer2.getElements()).andReturn(objects2).anyTimes();
EasyMock.replay(objectContainer2);
SmilMediaContainer objectContainer3 = EasyMock.createNiceMock(SmilMediaContainer.class);
EasyMock.expect(objectContainer3.isContainer()).andReturn(true).anyTimes();
EasyMock.expect(objectContainer3.getContainerType()).andReturn(SmilMediaContainer.ContainerType.PAR).anyTimes();
EasyMock.expect(objectContainer3.getElements()).andReturn(objects3).anyTimes();
EasyMock.replay(objectContainer3);
SmilMediaContainer objectContainer4 = EasyMock.createNiceMock(SmilMediaContainer.class);
EasyMock.expect(objectContainer4.isContainer()).andReturn(true).anyTimes();
EasyMock.expect(objectContainer4.getContainerType()).andReturn(SmilMediaContainer.ContainerType.PAR).anyTimes();
EasyMock.expect(objectContainer4.getElements()).andReturn(objects4).anyTimes();
EasyMock.replay(objectContainer4);
List<SmilMediaObject> containerObjects = new ArrayList<>();
containerObjects.add(objectContainer1);
containerObjects.add(objectContainer2);
containerObjects.add(objectContainer3);
containerObjects.add(objectContainer4);
SmilBody body = EasyMock.createNiceMock(SmilBody.class);
EasyMock.expect(body.getMediaElements()).andReturn(containerObjects).anyTimes();
EasyMock.replay(body);
smil = EasyMock.createNiceMock(Smil.class);
EasyMock.expect(smil.get(trackParamGroupId)).andReturn(group1).anyTimes();
EasyMock.expect(smil.getBody()).andReturn(body).anyTimes();
EasyMock.expect(smil.getHead()).andReturn(head).anyTimes();
EasyMock.expect(smil.toXML()).andReturn(smilString).anyTimes();
EasyMock.expect(smil.getId()).andReturn("s-ec404c2a-5092-4cd4-8717-7b7bbc244656").anyTimes();
EasyMock.replay(smil);
SmilResponse response = EasyMock.createNiceMock(SmilResponse.class);
EasyMock.expect(response.getSmil()).andReturn(smil).anyTimes();
EasyMock.replay(response);
SmilService smilService = EasyMock.createNiceMock(SmilService.class);
EasyMock.expect(smilService.fromXml((String) EasyMock.anyObject())).andReturn(response).anyTimes();
EasyMock.replay(smilService);
/* End of Smil API mockups */
endpoint = new ToolsEndpoint();
endpoint.setSmilService(smilService);
AdminUIConfiguration adminUIConfiguration = new AdminUIConfiguration();
Hashtable<String, String> dictionary = new Hashtable<>();
dictionary.put(AdminUIConfiguration.OPT_PREVIEW_SUBTYPE, "preview");
dictionary.put(AdminUIConfiguration.OPT_WAVEFORM_SUBTYPE, "waveform");
dictionary.put(AdminUIConfiguration.OPT_SMIL_CATALOG_FLAVOR, "smil/cutting");
dictionary.put(AdminUIConfiguration.OPT_SMIL_SILENCE_FLAVOR, "*/silence");
adminUIConfiguration.updated(dictionary);
endpoint.setAdminUIConfiguration(adminUIConfiguration);
}
use of org.opencastproject.smil.entity.api.Smil in project opencast by opencast.
the class VideoEditorWorkflowOperationHandler method resume.
/**
* {@inheritDoc}
*
* @see org.opencastproject.workflow.api.ResumableWorkflowOperationHandler#resume(org.opencastproject.workflow.api.WorkflowInstance,
* JobContext, java.util.Map)
*/
@Override
public WorkflowOperationResult resume(WorkflowInstance workflowInstance, JobContext context, Map<String, String> properties) throws WorkflowOperationException {
MediaPackage mp = workflowInstance.getMediaPackage();
logger.info("Resume video editor operation for mediapackage {}", mp.getIdentifier().compact());
// Get configuration
WorkflowOperationInstance worflowOperationInstance = workflowInstance.getCurrentOperation();
String sourceTrackFlavorsProperty = StringUtils.trimToNull(worflowOperationInstance.getConfiguration(SOURCE_FLAVORS_PROPERTY));
if (sourceTrackFlavorsProperty == null) {
throw new WorkflowOperationException(format("Required configuration property %s not set.", SOURCE_FLAVORS_PROPERTY));
}
String targetSmilFlavorProperty = StringUtils.trimToNull(worflowOperationInstance.getConfiguration(TARGET_SMIL_FLAVOR_PROPERTY));
if (targetSmilFlavorProperty == null) {
throw new WorkflowOperationException(format("Required configuration property %s not set.", TARGET_SMIL_FLAVOR_PROPERTY));
}
String targetFlavorSybTypeProperty = StringUtils.trimToNull(worflowOperationInstance.getConfiguration(TARGET_FLAVOR_SUBTYPE_PROPERTY));
if (targetFlavorSybTypeProperty == null) {
throw new WorkflowOperationException(format("Required configuration property %s not set.", TARGET_FLAVOR_SUBTYPE_PROPERTY));
}
boolean skipIfNoTrim = BooleanUtils.toBoolean(worflowOperationInstance.getConfiguration(SKIP_NOT_TRIMMED_PROPERTY));
// Get source tracks
TrackSelector trackSelector = new TrackSelector();
for (String flavor : asList(sourceTrackFlavorsProperty)) {
trackSelector.addFlavor(flavor);
}
Collection<Track> sourceTracks = trackSelector.select(mp, false);
if (sourceTracks.isEmpty()) {
throw new WorkflowOperationException(format("No source tracks found in mediapacksge %s with flavors %s.", mp.getIdentifier().compact(), sourceTrackFlavorsProperty));
}
// Get SMIL file
MediaPackageElementFlavor smilTargetFlavor = MediaPackageElementFlavor.parseFlavor(targetSmilFlavorProperty);
Catalog[] smilCatalogs = mp.getCatalogs(smilTargetFlavor);
if (smilCatalogs == null || smilCatalogs.length == 0) {
throw new WorkflowOperationException(format("No SMIL catalog found in mediapackage %s with flavor %s.", mp.getIdentifier().compact(), targetSmilFlavorProperty));
}
File smilFile = null;
Smil smil = null;
try {
smilFile = workspace.get(smilCatalogs[0].getURI());
smil = smilService.fromXml(smilFile).getSmil();
smil = replaceAllTracksWith(smil, sourceTracks.toArray(new Track[sourceTracks.size()]));
InputStream is = null;
try {
is = IOUtils.toInputStream(smil.toXML(), "UTF-8");
// Remove old SMIL
workspace.delete(mp.getIdentifier().compact(), smilCatalogs[0].getIdentifier());
mp.remove(smilCatalogs[0]);
// put modified SMIL into workspace
URI newSmilUri = workspace.put(mp.getIdentifier().compact(), smil.getId(), SMIL_FILE_NAME, is);
Catalog catalog = (Catalog) MediaPackageElementBuilderFactory.newInstance().newElementBuilder().elementFromURI(newSmilUri, MediaPackageElement.Type.Catalog, smilCatalogs[0].getFlavor());
catalog.setIdentifier(smil.getId());
mp.add(catalog);
} catch (Exception ex) {
throw new WorkflowOperationException(ex);
} finally {
IOUtils.closeQuietly(is);
}
} catch (NotFoundException ex) {
throw new WorkflowOperationException(format("Failed to get SMIL catalog %s from mediapackage %s.", smilCatalogs[0].getIdentifier(), mp.getIdentifier().compact()), ex);
} catch (IOException ex) {
throw new WorkflowOperationException(format("Can't open SMIL catalog %s from mediapackage %s.", smilCatalogs[0].getIdentifier(), mp.getIdentifier().compact()), ex);
} catch (SmilException ex) {
throw new WorkflowOperationException(ex);
}
if (skipIfNoTrim) {
// We should not modify the SMIL file as we traverse through its elements, so we make a copy and modify it instead
try {
Smil filteredSmil = smilService.fromXml(smil.toXML()).getSmil();
for (SmilMediaObject element : smil.getBody().getMediaElements()) {
// body should contain par elements
if (element.isContainer()) {
SmilMediaContainer container = (SmilMediaContainer) element;
if (SmilMediaContainer.ContainerType.PAR == container.getContainerType()) {
continue;
}
}
filteredSmil = smilService.removeSmilElement(filteredSmil, element.getId()).getSmil();
}
// one that takes the whole video size
switch(filteredSmil.getBody().getMediaElements().size()) {
case 0:
logger.info("Skipping SMIL job generation for mediapackage '{}', " + "because the SMIL does not define any trimming points", mp.getIdentifier());
return skip(workflowInstance, context);
case 1:
// component represents the whole duration or not, therefore we don't bother to try
if (mp.getDuration() < 0)
break;
SmilMediaContainer parElement = (SmilMediaContainer) filteredSmil.getBody().getMediaElements().get(0);
boolean skip = true;
for (SmilMediaObject elementChild : parElement.getElements()) {
if (!elementChild.isContainer()) {
SmilMediaElement media = (SmilMediaElement) elementChild;
// If they don't represent the whole length, then we break --we have a trimming point
if ((media.getClipBeginMS() != 0) || (media.getClipEndMS() != mp.getDuration())) {
skip = false;
break;
}
}
}
if (skip) {
logger.info("Skipping SMIL job generation for mediapackage '{}', " + "because the trimming points in the SMIL correspond " + "to the beginning and the end of the video", mp.getIdentifier());
return skip(workflowInstance, context);
}
break;
default:
break;
}
} catch (MalformedURLException | SmilException | JAXBException | SAXException e) {
logger.warn("Error parsing input SMIL to determine if it has trimpoints. " + "We will assume it does and go on creating jobs.");
}
}
// Create video edit jobs and run them
List<Job> jobs = null;
try {
logger.info("Create processing jobs for SMIL file: {}", smilCatalogs[0].getIdentifier());
jobs = videoEditorService.processSmil(smil);
if (!waitForStatus(jobs.toArray(new Job[jobs.size()])).isSuccess()) {
throw new WorkflowOperationException(format("Processing SMIL file failed: %s", smilCatalogs[0].getIdentifier()));
}
logger.info("Finished processing of SMIL file: {}", smilCatalogs[0].getIdentifier());
} catch (ProcessFailedException ex) {
throw new WorkflowOperationException(format("Finished processing of SMIL file: %s", smilCatalogs[0].getIdentifier()), ex);
}
// Move edited tracks to work location and set target flavor
Track editedTrack = null;
boolean mpAdded = false;
for (Job job : jobs) {
try {
editedTrack = (Track) MediaPackageElementParser.getFromXml(job.getPayload());
MediaPackageElementFlavor editedTrackFlavor = editedTrack.getFlavor();
editedTrack.setFlavor(new MediaPackageElementFlavor(editedTrackFlavor.getType(), targetFlavorSybTypeProperty));
URI editedTrackNewUri = workspace.moveTo(editedTrack.getURI(), mp.getIdentifier().compact(), editedTrack.getIdentifier(), FilenameUtils.getName(editedTrack.getURI().toString()));
editedTrack.setURI(editedTrackNewUri);
for (Track track : sourceTracks) {
if (track.getFlavor().getType().equals(editedTrackFlavor.getType())) {
mp.addDerived(editedTrack, track);
mpAdded = true;
break;
}
}
if (!mpAdded) {
mp.add(editedTrack);
}
} catch (MediaPackageException ex) {
throw new WorkflowOperationException("Failed to get information about the edited track(s)", ex);
} catch (NotFoundException | IOException | IllegalArgumentException ex) {
throw new WorkflowOperationException("Moving edited track to work location failed.", ex);
} catch (Exception ex) {
throw new WorkflowOperationException(ex);
}
}
logger.info("VideoEdit workflow {} finished", workflowInstance.getId());
return createResult(mp, Action.CONTINUE);
}
use of org.opencastproject.smil.entity.api.Smil in project opencast by opencast.
the class SmilServiceImplTest method testCreateNewSmil.
/**
* Test of createNewSmil methods, of class SmilServiceImpl.
*/
@Test
public void testCreateNewSmil() {
SmilResponse smilResponse = smilService.createNewSmil();
assertNotNull(smilResponse);
Smil smil = smilResponse.getSmil();
assertNotNull(smil);
// TODO: test with MediaPackage
}
Aggregations