use of org.opencastproject.videoeditor.api.ProcessFailedException in project opencast by opencast.
the class VideoEditorTest method setUp.
/**
* Setup for the video editor service, including creation of a mock workspace and all dependencies.
*
* @throws Exception
* if setup fails
*/
@Before
public void setUp() throws Exception {
File tmpDir = folder.newFolder(getClass().getName());
// output file
tempFile1 = new File(tmpDir, "testoutput.mp4");
/* mock the workspace for the input/output file */
// workspace.get(new URI(sourceTrackUri));
Workspace workspace = EasyMock.createMock(Workspace.class);
EasyMock.expect(workspace.rootDirectory()).andReturn(tmpDir.getAbsolutePath());
EasyMock.expect(workspace.get(track1.getURI())).andReturn(new File(track1.getURI())).anyTimes();
EasyMock.expect(workspace.get(track2.getURI())).andReturn(new File(track2.getURI())).anyTimes();
EasyMock.expect(workspace.putInCollection(EasyMock.anyString(), EasyMock.anyString(), EasyMock.anyObject(InputStream.class))).andAnswer(() -> {
InputStream in = (InputStream) EasyMock.getCurrentArguments()[2];
IOUtils.copy(in, new FileOutputStream(tempFile1));
return tempFile1.toURI();
});
/* mock the role/org/security dependencies */
User anonymous = new JaxbUser("anonymous", "test", new DefaultOrganization(), new JaxbRole(DefaultOrganization.DEFAULT_ORGANIZATION_ANONYMOUS, new DefaultOrganization()));
UserDirectoryService userDirectoryService = EasyMock.createMock(UserDirectoryService.class);
EasyMock.expect(userDirectoryService.loadUser((String) EasyMock.anyObject())).andReturn(anonymous).anyTimes();
Organization organization = new DefaultOrganization();
OrganizationDirectoryService organizationDirectoryService = EasyMock.createMock(OrganizationDirectoryService.class);
EasyMock.expect(organizationDirectoryService.getOrganization((String) EasyMock.anyObject())).andReturn(organization).anyTimes();
SecurityService securityService = EasyMock.createNiceMock(SecurityService.class);
EasyMock.expect(securityService.getUser()).andReturn(anonymous).anyTimes();
EasyMock.expect(securityService.getOrganization()).andReturn(organization).anyTimes();
/* mock the osgi init for the video editor itself */
BundleContext bc = EasyMock.createNiceMock(BundleContext.class);
File storageDir = folder.newFolder();
logger.info("storageDir: {}", storageDir);
EasyMock.expect(bc.getProperty("org.opencastproject.storage.dir")).andReturn(storageDir.getPath()).anyTimes();
EasyMock.expect(bc.getProperty("org.opencastproject.composer.ffmpegpath")).andReturn(FFMPEG_BINARY).anyTimes();
EasyMock.expect(bc.getProperty(FFmpegAnalyzer.FFPROBE_BINARY_CONFIG)).andReturn("ffprobe").anyTimes();
ComponentContext cc = EasyMock.createNiceMock(ComponentContext.class);
EasyMock.expect(cc.getBundleContext()).andReturn(bc).anyTimes();
EasyMock.replay(bc, cc, workspace, userDirectoryService, organizationDirectoryService, securityService);
/* mock inspector output so that the job will alway pass */
String sourceTrackXml = "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>" + "<track xmlns=\"http://mediapackage.opencastproject.org\" type='presentation/source' id='deadbeef-a926-4ba9-96d9-2fafbcc30d2a'>" + "<audio id='audio-1'><encoder type='MP3 (MPEG audio layer 3)'/><channels>2</channels>" + "<bitrate>96000.0</bitrate></audio><video id='video-1'><device/>" + "<encoder type='FLV / Sorenson Spark / Sorenson H.263 (Flash Video)'/>" + "<bitrate>512000.0</bitrate><framerate>15.0</framerate>" + "<resolution>854x480</resolution></video>" + "<mimetype>video/mpeg</mimetype><url>video.mp4</url></track>";
inspectedTrack = (Track) MediaPackageElementParser.getFromXml(sourceTrackXml);
veditor = new VideoEditorServiceImpl() {
@Override
protected Job inspect(Job job, URI workspaceURI) throws MediaInspectionException, ProcessFailedException {
Job inspectionJob = EasyMock.createNiceMock(Job.class);
try {
EasyMock.expect(inspectionJob.getPayload()).andReturn(MediaPackageElementParser.getAsXml(inspectedTrack));
} catch (MediaPackageException e) {
throw new MediaInspectionException(e);
}
EasyMock.replay(inspectionJob);
return inspectionJob;
}
};
/* set up video editor */
veditor.activate(cc);
veditor.setWorkspace(workspace);
veditor.setSecurityService(securityService);
veditor.setUserDirectoryService(userDirectoryService);
veditor.setSmilService(smilService);
veditor.setOrganizationDirectoryService(organizationDirectoryService);
serviceRegistry = EasyMock.createMock(ServiceRegistry.class);
final Capture<String> type = EasyMock.newCapture();
final Capture<String> operation = EasyMock.newCapture();
final Capture<List<String>> args = EasyMock.newCapture();
EasyMock.expect(serviceRegistry.createJob(capture(type), capture(operation), capture(args), EasyMock.anyFloat())).andAnswer(() -> {
Job job = new JobImpl(0);
logger.error("type: {}", type.getValue());
job.setJobType(type.getValue());
job.setOperation(operation.getValue());
job.setArguments(args.getValue());
job.setPayload(veditor.process(job));
return job;
}).anyTimes();
EasyMock.replay(serviceRegistry);
veditor.setServiceRegistry(serviceRegistry);
}
use of org.opencastproject.videoeditor.api.ProcessFailedException 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.videoeditor.api.ProcessFailedException in project opencast by opencast.
the class VideoEditorServiceImpl method processSmil.
/**
* Splice segments given by smil document for the given track to the new one.
*
* @param job
* processing job
* @param smil
* smil document with media segments description
* @param trackParamGroupId
* @return processed track
* @throws ProcessFailedException
* if an error occured
*/
protected Track processSmil(Job job, Smil smil, String trackParamGroupId) throws ProcessFailedException {
SmilMediaParamGroup trackParamGroup;
ArrayList<String> inputfile = new ArrayList<>();
ArrayList<VideoClip> videoclips = new ArrayList<>();
try {
trackParamGroup = (SmilMediaParamGroup) smil.get(trackParamGroupId);
} catch (SmilException ex) {
// can't be thrown, because we found the Id in processSmil(Smil)
throw new ProcessFailedException("Smil does not contain a paramGroup element with Id " + trackParamGroupId);
}
MediaPackageElementFlavor sourceTrackFlavor = null;
String sourceTrackUri = null;
// get source track metadata
for (SmilMediaParam param : trackParamGroup.getParams()) {
if (SmilMediaParam.PARAM_NAME_TRACK_SRC.equals(param.getName())) {
sourceTrackUri = param.getValue();
} else if (SmilMediaParam.PARAM_NAME_TRACK_FLAVOR.equals(param.getName())) {
sourceTrackFlavor = MediaPackageElementFlavor.parseFlavor(param.getValue());
}
}
File sourceFile;
try {
sourceFile = workspace.get(new URI(sourceTrackUri));
} catch (IOException ex) {
throw new ProcessFailedException("Can't read " + sourceTrackUri);
} catch (NotFoundException ex) {
throw new ProcessFailedException("Workspace does not contain a track " + sourceTrackUri);
} catch (URISyntaxException ex) {
throw new ProcessFailedException("Source URI " + sourceTrackUri + " is not valid.");
}
// inspect input file to retrieve media information
Job inspectionJob;
Track sourceTrack;
try {
inspectionJob = inspect(job, new URI(sourceTrackUri));
sourceTrack = (Track) MediaPackageElementParser.getFromXml(inspectionJob.getPayload());
} catch (URISyntaxException e) {
throw new ProcessFailedException("Source URI " + sourceTrackUri + " is not valid.");
} catch (MediaInspectionException e) {
throw new ProcessFailedException("Media inspection of " + sourceTrackUri + " failed", e);
} catch (MediaPackageException e) {
throw new ProcessFailedException("Deserialization of source track " + sourceTrackUri + " failed", e);
}
// get output file extension
String outputFileExtension = properties.getProperty(VideoEditorProperties.DEFAULT_EXTENSION, ".mp4");
outputFileExtension = properties.getProperty(VideoEditorProperties.OUTPUT_FILE_EXTENSION, outputFileExtension);
if (!outputFileExtension.startsWith(".")) {
outputFileExtension = '.' + outputFileExtension;
}
// create working directory
File tempDirectory = new File(new File(workspace.rootDirectory()), "editor");
tempDirectory = new File(tempDirectory, Long.toString(job.getId()));
String filename = String.format("%s-%s%s", sourceTrackFlavor, sourceFile.getName(), outputFileExtension);
File outputPath = new File(tempDirectory, filename);
if (!outputPath.getParentFile().exists()) {
outputPath.getParentFile().mkdirs();
}
URI newTrackURI;
// default source - add to source table as 0
inputfile.add(sourceFile.getAbsolutePath());
// index = 0
int srcIndex = inputfile.indexOf(sourceFile.getAbsolutePath());
logger.info("Start processing srcfile {}", sourceFile.getAbsolutePath());
try {
// parse body elements
for (SmilMediaObject element : smil.getBody().getMediaElements()) {
// body should contain par elements
if (element.isContainer()) {
SmilMediaContainer container = (SmilMediaContainer) element;
if (SmilMediaContainer.ContainerType.PAR == container.getContainerType()) {
// par element should contain media elements
for (SmilMediaObject elementChild : container.getElements()) {
if (!elementChild.isContainer()) {
SmilMediaElement media = (SmilMediaElement) elementChild;
if (trackParamGroupId.equals(media.getParamGroup())) {
long begin = media.getClipBeginMS();
long end = media.getClipEndMS();
URI clipTrackURI = media.getSrc();
File clipSourceFile = null;
if (clipTrackURI != null) {
try {
clipSourceFile = workspace.get(clipTrackURI);
} catch (IOException ex) {
throw new ProcessFailedException("Can't read " + clipTrackURI);
} catch (NotFoundException ex) {
throw new ProcessFailedException("Workspace does not contain a track " + clipTrackURI);
}
}
int index;
if (clipSourceFile != null) {
// clip has different source
// Look for known tracks
index = inputfile.indexOf(clipSourceFile.getAbsolutePath());
if (index == -1) {
// add new track
inputfile.add(clipSourceFile.getAbsolutePath());
// TODO: inspect each new video file, bad input will throw exc
}
index = inputfile.indexOf(clipSourceFile.getAbsolutePath());
} else {
// default src
index = srcIndex;
}
videoclips.add(new VideoClip(index, begin / 1000.0, end / 1000.0));
}
} else {
throw new ProcessFailedException("Smil container '" + ((SmilMediaContainer) elementChild).getContainerType().toString() + "'is not supportet yet");
}
}
} else {
throw new ProcessFailedException("Smil container '" + container.getContainerType().toString() + "'is not supportet yet");
}
}
}
// remove very short cuts that will look bad
List<VideoClip> cleanclips = sortSegments(videoclips);
String error = null;
// TODO: fetch the largest output resolution from SMIL.head.layout.root-layout
String outputResolution = "";
// When outputResolution is set to WxH, all clips are scaled to that size in the output video.
// TODO: Each clips could have a region id, relative to the root-layout
// Then each clip is zoomed/panned/padded to WxH befor concatenation
FFmpegEdit ffmpeg = new FFmpegEdit(properties);
error = ffmpeg.processEdits(inputfile, outputPath.getAbsolutePath(), outputResolution, cleanclips, sourceTrack.hasAudio(), sourceTrack.hasVideo());
if (error != null) {
FileUtils.deleteQuietly(tempDirectory);
throw new ProcessFailedException("Editing pipeline exited abnormaly! Error: " + error);
}
// create Track for edited file
String newTrackId = idBuilder.createNew().toString();
InputStream in = new FileInputStream(outputPath);
try {
newTrackURI = workspace.putInCollection(COLLECTION_ID, String.format("%s-%s%s", sourceTrackFlavor.getType(), newTrackId, outputFileExtension), in);
} catch (IllegalArgumentException ex) {
throw new ProcessFailedException("Copy track into workspace failed! " + ex.getMessage());
} finally {
IOUtils.closeQuietly(in);
FileUtils.deleteQuietly(tempDirectory);
}
// inspect new Track
try {
inspectionJob = inspect(job, newTrackURI);
} catch (MediaInspectionException e) {
throw new ProcessFailedException("Media inspection of " + newTrackURI + " failed", e);
}
Track editedTrack = (Track) MediaPackageElementParser.getFromXml(inspectionJob.getPayload());
logger.info("Finished editing track {}", editedTrack);
editedTrack.setIdentifier(newTrackId);
editedTrack.setFlavor(new MediaPackageElementFlavor(sourceTrackFlavor.getType(), SINK_FLAVOR_SUBTYPE));
return editedTrack;
} catch (MediaInspectionException ex) {
throw new ProcessFailedException("Inspecting encoded Track failed with: " + ex.getMessage());
} catch (MediaPackageException ex) {
throw new ProcessFailedException("Unable to serialize edited Track! " + ex.getMessage());
} catch (Exception ex) {
throw new ProcessFailedException("Unable to process SMIL: " + ex.getMessage(), ex);
} finally {
FileUtils.deleteQuietly(tempDirectory);
}
}
use of org.opencastproject.videoeditor.api.ProcessFailedException in project opencast by opencast.
the class VideoEditorServiceImpl method process.
@Override
protected String process(Job job) throws Exception {
if (Operation.PROCESS_SMIL.toString().equals(job.getOperation())) {
Smil smil = smilService.fromXml(job.getArguments().get(0)).getSmil();
if (smil == null) {
throw new ProcessFailedException("Smil document is null!");
}
Track editedTrack = processSmil(job, smil, job.getArguments().get(1));
return MediaPackageElementParser.getAsXml(editedTrack);
}
throw new ProcessFailedException("Can't handle this operation: " + job.getOperation());
}
use of org.opencastproject.videoeditor.api.ProcessFailedException in project opencast by opencast.
the class VideoEditorServiceImpl method inspect.
/*
* Inspect the output file
*/
protected Job inspect(Job job, URI workspaceURI) throws MediaInspectionException, ProcessFailedException {
Job inspectionJob;
try {
inspectionJob = inspectionService.inspect(workspaceURI);
} catch (MediaInspectionException e) {
incident().recordJobCreationIncident(job, e);
throw new MediaInspectionException("Media inspection of " + workspaceURI + " failed", e);
}
JobBarrier barrier = new JobBarrier(job, serviceRegistry, inspectionJob);
if (!barrier.waitForJobs().isSuccess()) {
throw new ProcessFailedException("Media inspection of " + workspaceURI + " failed");
}
return inspectionJob;
}
Aggregations