Search in sources :

Example 16 with TrackImpl

use of org.opencastproject.mediapackage.track.TrackImpl in project opencast by opencast.

the class SoxServiceImpl method addAudioMetadata.

private Track addAudioMetadata(Track audioTrack, List<String> metadata) {
    TrackImpl track = (TrackImpl) audioTrack;
    List<AudioStream> audio = track.getAudio();
    if (audio.size() == 0) {
        audio.add(new AudioStreamImpl());
        logger.info("No audio streams found created new audio stream");
    }
    AudioStreamImpl audioStream = (AudioStreamImpl) audio.get(0);
    if (audio.size() > 1)
        logger.info("Multiple audio streams found, take first audio stream {}", audioStream);
    for (String value : metadata) {
        if (value.startsWith("Pk lev dB")) {
            Float pkLevDb = new Float(StringUtils.substringAfter(value, "Pk lev dB").trim());
            audioStream.setPkLevDb(pkLevDb);
        } else if (value.startsWith("RMS lev dB")) {
            Float rmsLevDb = new Float(StringUtils.substringAfter(value, "RMS lev dB").trim());
            audioStream.setRmsLevDb(rmsLevDb);
        } else if (value.startsWith("RMS Pk dB")) {
            Float rmsPkDb = new Float(StringUtils.substringAfter(value, "RMS Pk dB").trim());
            audioStream.setRmsPkDb(rmsPkDb);
        }
    }
    return track;
}
Also used : AudioStream(org.opencastproject.mediapackage.AudioStream) TrackImpl(org.opencastproject.mediapackage.track.TrackImpl) AudioStreamImpl(org.opencastproject.mediapackage.track.AudioStreamImpl)

Example 17 with TrackImpl

use of org.opencastproject.mediapackage.track.TrackImpl 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 18 with TrackImpl

use of org.opencastproject.mediapackage.track.TrackImpl in project opencast by opencast.

the class AnimateWorkflowOperationHandler method start.

/**
 * {@inheritDoc}
 *
 * @see
 * org.opencastproject.workflow.api.WorkflowOperationHandler#start(org.opencastproject.workflow.api.WorkflowInstance,
 * org.opencastproject.job.api.JobContext)
 */
@Override
public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
    MediaPackage mediaPackage = workflowInstance.getMediaPackage();
    logger.info("Start animate workflow operation for media package {}", mediaPackage);
    WorkflowOperationInstance operation = workflowInstance.getCurrentOperation();
    List<String> arguments;
    // Check required options
    final File animationFile = new File(StringUtils.trimToEmpty(operation.getConfiguration(ANIMATION_FILE_PROPERTY)));
    if (!animationFile.isFile()) {
        throw new WorkflowOperationException(String.format("Animation file `%s` does not exist", animationFile));
    }
    URI animation = animationFile.toURI();
    final MediaPackageElementFlavor targetFlavor;
    try {
        targetFlavor = MediaPackageElementFlavor.parseFlavor(StringUtils.trimToNull(operation.getConfiguration(TARGET_FLAVOR_PROPERTY)));
    } catch (IllegalArgumentException e) {
        throw new WorkflowOperationException("Invalid target flavor", e);
    }
    // Get optional options
    String targetTagsProperty = StringUtils.trimToNull(operation.getConfiguration(TARGET_TAGS_PROPERTY));
    // Check if we have custom command line options
    String cmd = operation.getConfiguration(COMMANDLINE_ARGUMENTS_PROPERTY);
    if (StringUtils.isNotEmpty(cmd)) {
        arguments = Arrays.asList(StringUtils.split(cmd));
    } else {
        // set default encoding
        arguments = new ArrayList<>();
        arguments.add("-t");
        arguments.add("ffmpeg");
        arguments.add("--video-codec");
        arguments.add("libx264-lossless");
        arguments.add("--video-bitrate");
        arguments.add("10000");
        addArgumentIfExists(operation, arguments, WIDTH_PROPERTY, "-w");
        addArgumentIfExists(operation, arguments, HEIGHT_PROPERTY, "-h");
        addArgumentIfExists(operation, arguments, FPS_PROPERTY, "--fps");
    }
    final Map<String, String> metadata = getMetadata(mediaPackage);
    Job job;
    try {
        job = animateService.animate(animation, metadata, arguments);
    } catch (AnimateServiceException e) {
        throw new WorkflowOperationException(String.format("Rendering animation from '%s' in media package '%s' failed", animation, mediaPackage), e);
    }
    if (!waitForStatus(job).isSuccess()) {
        throw new WorkflowOperationException(String.format("Animate job for media package '%s' failed", mediaPackage));
    }
    // put animated clip into media package
    try {
        URI output = new URI(job.getPayload());
        String id = UUID.randomUUID().toString();
        InputStream in = workspace.read(output);
        URI uri = workspace.put(mediaPackage.getIdentifier().toString(), id, FilenameUtils.getName(output.getPath()), in);
        TrackImpl track = new TrackImpl();
        track.setIdentifier(id);
        track.setFlavor(targetFlavor);
        track.setURI(uri);
        Job inspection = mediaInspectionService.enrich(track, true);
        if (!waitForStatus(inspection).isSuccess()) {
            throw new AnimateServiceException(String.format("Animating %s failed", animation));
        }
        track = (TrackImpl) MediaPackageElementParser.getFromXml(inspection.getPayload());
        // add track to media package
        for (String tag : asList(targetTagsProperty)) {
            track.addTag(tag);
        }
        mediaPackage.add(track);
        workspace.delete(output);
    } catch (Exception e) {
        throw new WorkflowOperationException("Error handling animation service output", e);
    }
    try {
        workspace.cleanup(mediaPackage.getIdentifier());
    } catch (IOException e) {
        throw new WorkflowOperationException(e);
    }
    logger.info("Animate workflow operation for media package {} completed", mediaPackage);
    return createResult(mediaPackage, WorkflowOperationResult.Action.CONTINUE);
}
Also used : InputStream(java.io.InputStream) TrackImpl(org.opencastproject.mediapackage.track.TrackImpl) IOException(java.io.IOException) URI(java.net.URI) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) AnimateServiceException(org.opencastproject.animate.api.AnimateServiceException) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) IOException(java.io.IOException) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) AnimateServiceException(org.opencastproject.animate.api.AnimateServiceException) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) Job(org.opencastproject.job.api.Job) File(java.io.File)

Example 19 with TrackImpl

use of org.opencastproject.mediapackage.track.TrackImpl in project opencast by opencast.

the class AnimateWorkflowOperationHandlerTest method setUp.

@Before
public void setUp() throws Exception {
    handler = new AnimateWorkflowOperationHandler() {

        @Override
        protected JobBarrier.Result waitForStatus(Job... jobs) throws IllegalStateException, IllegalArgumentException {
            JobBarrier.Result result = EasyMock.createNiceMock(JobBarrier.Result.class);
            EasyMock.expect(result.isSuccess()).andReturn(true).anyTimes();
            EasyMock.replay(result);
            return result;
        }
    };
    file = new File(getClass().getResource("/dc-episode.xml").toURI());
    MediaPackage mediaPackage = new MediaPackageBuilderImpl().createNew();
    mediaPackage.setIdentifier(new IdImpl("123-456"));
    InputStream in = new FileInputStream(file);
    Catalog catalog = DublinCores.read(in);
    catalog.setFlavor(MediaPackageElements.EPISODE);
    // catalog.setURI(getClass().getResource("/dc-episode.xml").toURI());
    mediaPackage.add(catalog);
    instance = EasyMock.createNiceMock(WorkflowOperationInstanceImpl.class);
    EasyMock.expect(instance.getConfiguration("target-flavor")).andReturn("a/b").anyTimes();
    EasyMock.expect(instance.getConfiguration("target-tags")).andReturn("a,b,c").anyTimes();
    workflow = EasyMock.createMock(WorkflowInstanceImpl.class);
    EasyMock.expect(workflow.getMediaPackage()).andReturn(mediaPackage).anyTimes();
    EasyMock.expect(workflow.getCurrentOperation()).andReturn(instance).anyTimes();
    Job job = new JobImpl(0);
    job.setPayload(file.getAbsolutePath());
    AnimateService animateService = EasyMock.createMock(AnimateService.class);
    EasyMock.expect(animateService.animate(anyObject(), anyObject(), anyObject())).andReturn(job);
    Workspace workspace = EasyMock.createMock(Workspace.class);
    EasyMock.expect(workspace.put(anyString(), anyString(), anyString(), anyObject())).andReturn(file.toURI()).anyTimes();
    EasyMock.expect(workspace.read(anyObject())).andAnswer(() -> getClass().getResourceAsStream("/dc-episode.xml")).anyTimes();
    workspace.cleanup(anyObject(Id.class));
    EasyMock.expectLastCall();
    workspace.delete(anyObject(URI.class));
    EasyMock.expectLastCall();
    job = new JobImpl(1);
    job.setPayload(MediaPackageElementParser.getAsXml(new TrackImpl()));
    MediaInspectionService mediaInspectionService = EasyMock.createMock(MediaInspectionService.class);
    EasyMock.expect(mediaInspectionService.enrich(anyObject(), anyBoolean())).andReturn(job).once();
    EasyMock.replay(animateService, workspace, workflow, mediaInspectionService);
    handler.setAnimateService(animateService);
    handler.setMediaInspectionService(mediaInspectionService);
    handler.setWorkspace(workspace);
}
Also used : JobImpl(org.opencastproject.job.api.JobImpl) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) TrackImpl(org.opencastproject.mediapackage.track.TrackImpl) WorkflowOperationInstanceImpl(org.opencastproject.workflow.api.WorkflowOperationInstanceImpl) MediaInspectionService(org.opencastproject.inspection.api.MediaInspectionService) URI(java.net.URI) IdImpl(org.opencastproject.mediapackage.identifier.IdImpl) FileInputStream(java.io.FileInputStream) Catalog(org.opencastproject.mediapackage.Catalog) WorkflowInstanceImpl(org.opencastproject.workflow.api.WorkflowInstanceImpl) MediaPackageBuilderImpl(org.opencastproject.mediapackage.MediaPackageBuilderImpl) AnimateService(org.opencastproject.animate.api.AnimateService) MediaPackage(org.opencastproject.mediapackage.MediaPackage) Id(org.opencastproject.mediapackage.identifier.Id) Job(org.opencastproject.job.api.Job) File(java.io.File) Workspace(org.opencastproject.workspace.api.Workspace) Before(org.junit.Before)

Example 20 with TrackImpl

use of org.opencastproject.mediapackage.track.TrackImpl in project opencast by opencast.

the class LiveScheduleServiceImpl method buildStreamingTrack.

Track buildStreamingTrack(String uriString, MediaPackageElementFlavor flavor, String mimeType, String resolution, long duration) throws URISyntaxException {
    URI uri = new URI(uriString);
    MediaPackageElementBuilder elementBuilder = MediaPackageElementBuilderFactory.newInstance().newElementBuilder();
    MediaPackageElement element = elementBuilder.elementFromURI(uri, MediaPackageElement.Type.Track, flavor);
    TrackImpl track = (TrackImpl) element;
    // Set duration and mime type
    track.setDuration(duration);
    track.setLive(true);
    track.setMimeType(MimeTypes.parseMimeType(mimeType));
    VideoStreamImpl video = new VideoStreamImpl("video-" + flavor.getType() + "-" + flavor.getSubtype());
    // Set video resolution
    String[] dimensions = resolution.split("x");
    video.setFrameWidth(Integer.parseInt(dimensions[0]));
    video.setFrameHeight(Integer.parseInt(dimensions[1]));
    track.addStream(video);
    logger.debug("Creating live track element of flavor {}, resolution {}, and url {}", new Object[] { flavor, resolution, uriString });
    return track;
}
Also used : MediaPackageElementBuilder(org.opencastproject.mediapackage.MediaPackageElementBuilder) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) TrackImpl(org.opencastproject.mediapackage.track.TrackImpl) VideoStreamImpl(org.opencastproject.mediapackage.track.VideoStreamImpl) URI(java.net.URI)

Aggregations

TrackImpl (org.opencastproject.mediapackage.track.TrackImpl)42 Test (org.junit.Test)21 Track (org.opencastproject.mediapackage.Track)20 MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)17 URI (java.net.URI)14 MediaPackage (org.opencastproject.mediapackage.MediaPackage)13 VideoStreamImpl (org.opencastproject.mediapackage.track.VideoStreamImpl)12 WorkflowOperationResult (org.opencastproject.workflow.api.WorkflowOperationResult)11 Job (org.opencastproject.job.api.Job)9 ArrayList (java.util.ArrayList)7 AudioStream (org.opencastproject.mediapackage.AudioStream)7 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)7 File (java.io.File)6 IOException (java.io.IOException)6 HashMap (java.util.HashMap)6 VideoStream (org.opencastproject.mediapackage.VideoStream)6 InputStream (java.io.InputStream)5 SmilResponse (org.opencastproject.smil.api.SmilResponse)5 NotFoundException (org.opencastproject.util.NotFoundException)5 FileInputStream (java.io.FileInputStream)4