use of org.opencastproject.mediapackage.Track in project opencast by opencast.
the class NormalizeAudioWorkflowOperationHandlerTest method testAudioContainer.
@Test
public void testAudioContainer() throws Exception {
operationInstance.setConfiguration("source-tags", "");
operationInstance.setConfiguration("source-flavor", "*/container-audio");
operationInstance.setConfiguration("source-flavors", "");
operationInstance.setConfiguration("target-flavor", "*/normalized");
operationInstance.setConfiguration("target-tags", "norm");
operationInstance.setConfiguration("force-transcode", "true");
operationInstance.setConfiguration("target-decibel", "-30");
WorkflowOperationResult result = operationHandler.start(instance, null);
Assert.assertEquals(Action.CONTINUE, result.getAction());
Assert.assertEquals("Resulting mediapackage has the wrong number of tracks", 4, result.getMediaPackage().getElements().length);
Track[] tracks = result.getMediaPackage().getTracks(new MediaPackageElementFlavor("presentation", "normalized"));
Assert.assertEquals("Resulting mediapackage has the wrong number of tracks", 1, tracks.length);
Assert.assertTrue(tracks[0].containsTag("norm"));
TrackImpl audioVideo = (TrackImpl) tracks[0];
Assert.assertEquals(-30f, audioVideo.getAudio().get(0).getRmsLevDb().floatValue(), 0.001d);
tracks = result.getMediaPackage().getTracks(new MediaPackageElementFlavor("presentation", "container-audio"));
Assert.assertEquals("Resulting mediapackage has the wrong number of tracks", 1, tracks.length);
audioVideo = (TrackImpl) tracks[0];
Assert.assertNull(audioVideo.getAudio().get(0).getRmsLevDb());
}
use of org.opencastproject.mediapackage.Track in project opencast by opencast.
the class SmilServiceImplTest method testAddClip.
/**
* Test of addClip(s) methods, of class SmilServiceImpl.
*/
@Test
public void testAddClip() throws Exception {
TrackImpl videoTrack = new TrackImpl();
videoTrack.setIdentifier("track-1");
videoTrack.setFlavor(new MediaPackageElementFlavor("source", "presentation"));
videoTrack.setURI(new URI("http://hostname/video.mp4"));
videoTrack.addStream(new VideoStreamImpl());
videoTrack.setDuration(1000000000000L);
SmilResponse smilResponse = smilService.createNewSmil();
smilResponse = smilService.addParallel(smilResponse.getSmil());
SmilMediaContainer par = (SmilMediaContainer) smilResponse.getEntity();
// add video track into parallel element
smilResponse = smilService.addClip(smilResponse.getSmil(), par.getId(), videoTrack, 1000L, 1000000L);
// logger.info(smilResponse.getSmil().toXML());
SmilMediaObject media = null;
for (SmilObject entity : smilResponse.getEntities()) {
if (entity instanceof SmilMediaObject) {
media = (SmilMediaObject) entity;
break;
}
}
assertNotNull(media);
assertEquals(media.getId(), ((SmilMediaContainer) smilResponse.getSmil().getBody().getMediaElements().get(0)).getElements().get(0).getId());
assertTrue(media instanceof SmilMediaVideoImpl);
assertSame(((SmilMediaElement) media).getMediaType(), SmilMediaElement.MediaType.VIDEO);
// 1000 milliseconds = 1 second
assertEquals(1000L, ((SmilMediaElement) media).getClipBeginMS());
// duration is 1000000 milliseconds = 1000 soconds
// start + duration = 1s + 1000s = 1001s
assertEquals(1001000L, ((SmilMediaElement) media).getClipEndMS());
TrackImpl audioTrack = new TrackImpl();
audioTrack.setIdentifier("track-2");
audioTrack.setFlavor(new MediaPackageElementFlavor("source", "presenter"));
audioTrack.setURI(new URI("http://hostname/audio.mp3"));
audioTrack.addStream(new AudioStreamImpl());
audioTrack.setDuration(1000000000000L);
// add audio track into parallel element
smilResponse = smilService.addClip(smilResponse.getSmil(), par.getId(), audioTrack, 1000L, 1000000L);
// logger.info(smilResponse.getSmil().toXML());
media = null;
for (SmilObject entity : smilResponse.getEntities()) {
if (entity instanceof SmilMediaObject) {
media = (SmilMediaObject) entity;
break;
}
}
assertNotNull(media);
assertEquals(media.getId(), ((SmilMediaContainer) smilResponse.getSmil().getBody().getMediaElements().get(0)).getElements().get(1).getId());
assertTrue(media instanceof SmilMediaAudioImpl);
assertSame(((SmilMediaElement) media).getMediaType(), SmilMediaElement.MediaType.AUDIO);
// 1000 milliseconds = 1 second
assertEquals(1000L, ((SmilMediaElement) media).getClipBeginMS());
// duration is 1000000 milliseconds = 1000 soconds
// start + duration = 1s + 1000s = 1001s
assertEquals(1001000L, ((SmilMediaElement) media).getClipEndMS());
// add new par
smilResponse = smilService.addParallel(smilResponse.getSmil());
par = (SmilMediaContainer) smilResponse.getEntity();
// add tracks (as array) to par
smilResponse = smilService.addClips(smilResponse.getSmil(), par.getId(), new Track[] { audioTrack, videoTrack }, 15000L, 1000L);
// logger.info(smilResponse.getSmil().toXML());
assertSame(2, smilResponse.getEntitiesCount());
assertTrue(smilResponse.getEntities()[0] instanceof SmilMediaElement);
// get audio element
SmilMediaElement mediaElement = (SmilMediaElement) smilResponse.getEntities()[0];
assertTrue(mediaElement.getMediaType() == SmilMediaElement.MediaType.AUDIO);
// 15000ms = 15s
assertEquals(15000L, mediaElement.getClipBeginMS());
// start + duration = 15s + 1s = 16s
assertEquals(16000L, mediaElement.getClipEndMS());
// get video element
mediaElement = (SmilMediaElement) smilResponse.getEntities()[1];
assertTrue(mediaElement.getMediaType() == SmilMediaElement.MediaType.VIDEO);
// 15000ms = 15s
assertEquals(15000L, mediaElement.getClipBeginMS());
// start + duration = 15s + 1s = 16s
assertEquals(16000L, mediaElement.getClipEndMS());
}
use of org.opencastproject.mediapackage.Track in project opencast by opencast.
the class SoxServiceImpl method normalize.
private Option<Track> normalize(Job job, TrackImpl audioTrack, Float targetRmsLevDb) throws SoxException {
if (!audioTrack.hasAudio())
throw new SoxException("No audio stream available");
if (audioTrack.hasVideo())
throw new SoxException("It must not have a video stream");
if (audioTrack.getAudio().size() < 1)
throw new SoxException("No audio stream metadata available");
if (audioTrack.getAudio().get(0).getRmsLevDb() == null)
throw new SoxException("No RMS Lev dB metadata available");
final String targetTrackId = idBuilder.createNew().toString();
Float rmsLevDb = audioTrack.getAudio().get(0).getRmsLevDb();
// Get the tracks and make sure they exist
final File audioFile;
try {
audioFile = workspace.get(audioTrack.getURI());
} catch (NotFoundException e) {
throw new SoxException("Requested audio track " + audioTrack + " is not found");
} catch (IOException e) {
throw new SoxException("Unable to access audio track " + audioTrack);
}
String outDir = audioFile.getAbsoluteFile().getParent();
String outFileName = FilenameUtils.getBaseName(audioFile.getName()) + "_" + UUID.randomUUID().toString();
String suffix = "-norm." + FilenameUtils.getExtension(audioFile.getName());
File normalizedFile = new File(outDir, outFileName + suffix);
logger.info("Normalizing audio track {} to {}", audioTrack.getIdentifier(), targetTrackId);
// Do the work
ArrayList<String> command = new ArrayList<String>();
command.add(binary);
command.add(audioFile.getAbsolutePath());
command.add(normalizedFile.getAbsolutePath());
command.add("remix");
command.add("-");
command.add("gain");
if (targetRmsLevDb > rmsLevDb)
command.add("-l");
command.add(new Float(targetRmsLevDb - rmsLevDb).toString());
command.add("stats");
List<String> normalizeResult = launchSoxProcess(command);
if (normalizedFile.length() == 0)
throw new SoxException("Normalization failed: Output file is empty!");
// Put the file in the workspace
URI returnURL = null;
InputStream in = null;
try {
in = new FileInputStream(normalizedFile);
returnURL = workspace.putInCollection(COLLECTION, job.getId() + "." + FilenameUtils.getExtension(normalizedFile.getAbsolutePath()), in);
logger.info("Copied the normalized file to the workspace at {}", returnURL);
if (normalizedFile.delete()) {
logger.info("Deleted the local copy of the normalized file at {}", normalizedFile.getAbsolutePath());
} else {
logger.warn("Unable to delete the normalized output at {}", normalizedFile);
}
} catch (Exception e) {
throw new SoxException("Unable to put the normalized file into the workspace", e);
} finally {
IOUtils.closeQuietly(in);
FileSupport.deleteQuietly(normalizedFile);
}
Track normalizedTrack = (Track) audioTrack.clone();
normalizedTrack.setURI(returnURL);
normalizedTrack.setIdentifier(targetTrackId);
// Add audio metadata and return audio track
normalizedTrack = addAudioMetadata(normalizedTrack, normalizeResult);
return some(normalizedTrack);
}
use of org.opencastproject.mediapackage.Track in project opencast by opencast.
the class SoxServiceTest method testNormalizeIncreaseAudio.
@Test
public void testNormalizeIncreaseAudio() throws Exception {
if (!soxInstalled)
return;
assertTrue(source.isFile());
String sourceTrackXml = "<track xmlns=\"http://mediapackage.opencastproject.org\" id=\"track-1\" type=\"presentation/source\"><mimetype>audio/flac</mimetype>" + "<url>http://localhost:8080/workflow/samples/camera.mpg</url>" + "<checksum type=\"md5\">43b7d843b02c4a429b2f547a4f230d31</checksum><duration>14546</duration>" + "<audio><device type=\"UFG03\" version=\"30112007\" vendor=\"Unigraf\" />" + "<encoder type=\"H.264\" version=\"7.4\" vendor=\"Apple Inc\" /><channels>2</channels>" + "<bitdepth>16</bitdepth><rmsleveldb>-27.78</rmsleveldb><samplingrate>44100</samplingrate></audio></track>";
Track sourceTrack = (Track) MediaPackageElementParser.getFromXml(sourceTrackXml);
List<Job> jobs = new ArrayList<Job>();
for (int i = 0; i < 10; i++) {
jobs.add(soxService.normalize(sourceTrack, -25f));
}
boolean success = new JobBarrier(null, serviceRegistry, jobs.toArray(new Job[jobs.size()])).waitForJobs().isSuccess();
assertTrue(success);
for (Job j : jobs) {
// Always check the service registry for the latest version of the job
Job job = serviceRegistry.getJob(j.getId());
TrackImpl track = (TrackImpl) MediaPackageElementParser.getFromXml(job.getPayload());
AudioStream audioStream = track.getAudio().get(0);
assertEquals(-25f, audioStream.getRmsLevDb().floatValue(), 0.9);
assertEquals(Job.Status.FINISHED, job.getStatus());
}
}
use of org.opencastproject.mediapackage.Track in project opencast by opencast.
the class SilenceDetectionServiceImpl method generateSmil.
/**
* Create a smil from given parameters.
*
* @param segments media segment list with timestamps
* @param referenceTracks tracks to put as media segment source files
* @return generated smil
* @throws SmilException if smil creation failed
*/
protected Smil generateSmil(MediaSegments segments, List<Track> referenceTracks) throws SmilException {
SmilResponse smilResponse = smilService.createNewSmil();
Track[] referenceTracksArr = referenceTracks.toArray(new Track[referenceTracks.size()]);
for (MediaSegment segment : segments.getMediaSegments()) {
smilResponse = smilService.addParallel(smilResponse.getSmil());
String parId = smilResponse.getEntity().getId();
smilResponse = smilService.addClips(smilResponse.getSmil(), parId, referenceTracksArr, segment.getSegmentStart(), segment.getSegmentStop() - segment.getSegmentStart());
}
return smilResponse.getSmil();
}
Aggregations