Search in sources :

Example 61 with Track

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());
}
Also used : TrackImpl(org.opencastproject.mediapackage.track.TrackImpl) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) WorkflowOperationResult(org.opencastproject.workflow.api.WorkflowOperationResult) Track(org.opencastproject.mediapackage.Track) Test(org.junit.Test)

Example 62 with Track

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());
}
Also used : SmilMediaVideoImpl(org.opencastproject.smil.entity.media.element.SmilMediaVideoImpl) TrackImpl(org.opencastproject.mediapackage.track.TrackImpl) SmilObject(org.opencastproject.smil.entity.api.SmilObject) AudioStreamImpl(org.opencastproject.mediapackage.track.AudioStreamImpl) VideoStreamImpl(org.opencastproject.mediapackage.track.VideoStreamImpl) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) URI(java.net.URI) SmilResponse(org.opencastproject.smil.api.SmilResponse) SmilMediaAudioImpl(org.opencastproject.smil.entity.media.element.SmilMediaAudioImpl) SmilMediaObject(org.opencastproject.smil.entity.media.api.SmilMediaObject) SmilMediaElement(org.opencastproject.smil.entity.media.element.api.SmilMediaElement) Track(org.opencastproject.mediapackage.Track) SmilMediaContainer(org.opencastproject.smil.entity.media.container.api.SmilMediaContainer) Test(org.junit.Test)

Example 63 with Track

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);
}
Also used : SoxException(org.opencastproject.sox.api.SoxException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) URI(java.net.URI) FileInputStream(java.io.FileInputStream) ConfigurationException(org.osgi.service.cm.ConfigurationException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) SoxException(org.opencastproject.sox.api.SoxException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) File(java.io.File) Track(org.opencastproject.mediapackage.Track)

Example 64 with Track

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());
    }
}
Also used : AudioStream(org.opencastproject.mediapackage.AudioStream) TrackImpl(org.opencastproject.mediapackage.track.TrackImpl) ArrayList(java.util.ArrayList) Job(org.opencastproject.job.api.Job) JobBarrier(org.opencastproject.job.api.JobBarrier) Track(org.opencastproject.mediapackage.Track) Test(org.junit.Test)

Example 65 with Track

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();
}
Also used : MediaSegment(org.opencastproject.silencedetection.api.MediaSegment) SmilResponse(org.opencastproject.smil.api.SmilResponse) Track(org.opencastproject.mediapackage.Track)

Aggregations

Track (org.opencastproject.mediapackage.Track)154 Test (org.junit.Test)56 Job (org.opencastproject.job.api.Job)56 MediaPackage (org.opencastproject.mediapackage.MediaPackage)50 URI (java.net.URI)40 MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)34 WorkflowOperationException (org.opencastproject.workflow.api.WorkflowOperationException)34 WorkflowOperationResult (org.opencastproject.workflow.api.WorkflowOperationResult)34 HashMap (java.util.HashMap)29 ArrayList (java.util.ArrayList)27 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)24 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)21 TrackImpl (org.opencastproject.mediapackage.track.TrackImpl)20 NotFoundException (org.opencastproject.util.NotFoundException)20 IOException (java.io.IOException)19 TrackSelector (org.opencastproject.mediapackage.selector.TrackSelector)17 Attachment (org.opencastproject.mediapackage.Attachment)16 EncodingProfile (org.opencastproject.composer.api.EncodingProfile)15 Catalog (org.opencastproject.mediapackage.Catalog)15 File (java.io.File)14