Search in sources :

Example 91 with Track

use of org.opencastproject.mediapackage.Track in project opencast by opencast.

the class IndexServiceImpl method updateMpAssetFlavor.

/**
 * Update the flavor of newly added asset with the passed metadata
 *
 * @param assetList
 *          the list of assets to update
 * @param mp
 *          the mediapackage to update
 * @param assetMetadata
 *          a set of mapping metadata for the asset list
 * @param overwriteExisting
 *          true if the existing asset of the same flavor should be overwritten
 * @return mediapackage updated with assets
 */
@SuppressWarnings("unchecked")
protected MediaPackage updateMpAssetFlavor(List<String> assetList, MediaPackage mp, JSONArray assetMetadata, Boolean overwriteExisting) {
    // Create JSONObject data map
    JSONObject assetDataMap = new JSONObject();
    for (int i = 0; i < assetMetadata.size(); i++) {
        try {
            assetDataMap.put(((JSONObject) assetMetadata.get(i)).get("id"), assetMetadata.get(i));
        } catch (Exception e) {
            throw new IllegalArgumentException("Unable to parse metadata", e);
        }
    }
    // Find the correct flavor for each asset.
    for (String assetOrig : assetList) {
        // expecting file assets to contain postfix "track_trackpart.0"
        String asset = assetOrig;
        String assetNumber = null;
        String[] assetNameParts = asset.split(Pattern.quote("."));
        if (assetNameParts.length > 1) {
            asset = assetNameParts[0];
            assetNumber = assetNameParts[1];
        }
        try {
            if ((assetMetadata != null) && (assetDataMap.get(asset) != null)) {
                String type = (String) ((JSONObject) assetDataMap.get(asset)).get("type");
                String flavorType = (String) ((JSONObject) assetDataMap.get(asset)).get("flavorType");
                String flavorSubType = (String) ((JSONObject) assetDataMap.get(asset)).get("flavorSubType");
                if (patternNumberedAsset.matcher(flavorSubType).matches() && (assetNumber != null)) {
                    flavorSubType = assetNumber;
                }
                MediaPackageElementFlavor newElemflavor = new MediaPackageElementFlavor(flavorType, flavorSubType);
                if (patternAttachment.matcher(type).matches()) {
                    if (overwriteExisting) {
                        // remove existing attachments of the new flavor
                        Attachment[] existing = mp.getAttachments(newElemflavor);
                        for (int i = 0; i < existing.length; i++) {
                            mp.remove(existing[i]);
                            logger.info("Overwriting existing asset {} {}", type, newElemflavor);
                        }
                    }
                    // correct the flavor of the new attachment
                    Attachment[] elArray = mp.getAttachments(new MediaPackageElementFlavor(assetOrig, "*"));
                    elArray[0].setFlavor(newElemflavor);
                    logger.info("Updated asset {} {}", type, newElemflavor);
                } else if (patternCatalog.matcher(type).matches()) {
                    if (overwriteExisting) {
                        // remove existing catalogs of the new flavor
                        Catalog[] existing = mp.getCatalogs(newElemflavor);
                        for (int i = 0; i < existing.length; i++) {
                            mp.remove(existing[i]);
                            logger.info("Overwriting existing asset {} {}", type, newElemflavor);
                        }
                    }
                    Catalog[] catArray = mp.getCatalogs(new MediaPackageElementFlavor(assetOrig, "*"));
                    if (catArray.length > 1) {
                        throw new IllegalArgumentException("More than one " + asset + " found, only one expected.");
                    }
                    catArray[0].setFlavor(newElemflavor);
                    logger.info("Update asset {} {}", type, newElemflavor);
                } else if (patternTrack.matcher(type).matches()) {
                    // Overwriting of existing tracks of same flavor is currently not allowed.
                    // TODO: allow overwriting of existing tracks of same flavor
                    Track[] trackArray = mp.getTracks(new MediaPackageElementFlavor(assetOrig, "*"));
                    if (trackArray.length > 1) {
                        throw new IllegalArgumentException("More than one " + asset + " found, only one expected.");
                    }
                    trackArray[0].setFlavor(newElemflavor);
                    logger.info("Update asset {} {}", type, newElemflavor);
                } else {
                    logger.warn("Unknown asset type {} {} for field {}", type, newElemflavor, asset);
                }
            }
        } catch (Exception e) {
            // Assuming a parse error versus a file error and logging the error type
            logger.warn("Unable to process asset metadata {}", assetMetadata.toJSONString(), e);
            throw new IllegalArgumentException("Unable to parse metadata", e);
        }
    }
    return mp;
}
Also used : JSONObject(org.json.simple.JSONObject) Attachment(org.opencastproject.mediapackage.Attachment) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) SchedulerException(org.opencastproject.scheduler.api.SchedulerException) IngestException(org.opencastproject.ingest.api.IngestException) WebApplicationException(javax.ws.rs.WebApplicationException) MetadataParsingException(org.opencastproject.metadata.dublincore.MetadataParsingException) EventCommentException(org.opencastproject.event.comment.EventCommentException) IOException(java.io.IOException) JSONException(org.codehaus.jettison.json.JSONException) SearchIndexException(org.opencastproject.matterhorn.search.SearchIndexException) ParseException(java.text.ParseException) SeriesException(org.opencastproject.series.api.SeriesException) WorkflowException(org.opencastproject.workflow.api.WorkflowException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) IndexServiceException(org.opencastproject.index.service.exception.IndexServiceException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) WorkflowDatabaseException(org.opencastproject.workflow.api.WorkflowDatabaseException) AssetManagerException(org.opencastproject.assetmanager.api.AssetManagerException) Track(org.opencastproject.mediapackage.Track)

Example 92 with Track

use of org.opencastproject.mediapackage.Track in project opencast by opencast.

the class EventIndexUtils method generatePublicationDoc.

/**
 * Generate the document structure for the publication element
 *
 * @param publication
 *          the source publication element
 * @return a map representing the ES document structure of the publication element
 */
private static HashMap<String, Object> generatePublicationDoc(Publication publication) {
    HashMap<String, Object> pMap = new HashMap<String, Object>();
    // Add first level elements
    pMap.put(PublicationIndexSchema.CHANNEL, publication.getChannel());
    addObjectStringtToMap(pMap, PublicationIndexSchema.MIMETYPE, publication.getMimeType());
    // Attachments
    Attachment[] attachments = publication.getAttachments();
    HashMap<String, Object>[] attachmentsArray = new HashMap[attachments.length];
    for (int i = 0; i < attachmentsArray.length; i++) {
        Attachment attachment = attachments[i];
        HashMap<String, Object> element = new HashMap<String, Object>();
        element.put(PublicationIndexSchema.ELEMENT_ID, attachment.getIdentifier());
        addObjectStringtToMap(element, PublicationIndexSchema.ELEMENT_MIMETYPE, attachment.getMimeType());
        addObjectStringtToMap(element, PublicationIndexSchema.ELEMENT_TYPE, attachment.getElementType());
        element.put(PublicationIndexSchema.ELEMENT_TAG, attachment.getTags());
        addObjectStringtToMap(element, PublicationIndexSchema.ELEMENT_URL, attachment.getURI());
        element.put(PublicationIndexSchema.ELEMENT_SIZE, attachment.getSize());
        attachmentsArray[i] = element;
    }
    pMap.put(PublicationIndexSchema.ATTACHMENT, attachmentsArray);
    // Catalogs
    Catalog[] catalogs = publication.getCatalogs();
    HashMap<String, Object>[] catalogsArray = new HashMap[catalogs.length];
    for (int i = 0; i < catalogsArray.length; i++) {
        Catalog catalog = catalogs[i];
        HashMap<String, Object> element = new HashMap<String, Object>();
        element.put(PublicationIndexSchema.ELEMENT_ID, catalog.getIdentifier());
        addObjectStringtToMap(element, PublicationIndexSchema.ELEMENT_MIMETYPE, catalog.getMimeType());
        addObjectStringtToMap(element, PublicationIndexSchema.ELEMENT_TYPE, catalog.getElementType());
        element.put(PublicationIndexSchema.ELEMENT_TAG, catalog.getTags());
        addObjectStringtToMap(element, PublicationIndexSchema.ELEMENT_URL, catalog.getURI());
        element.put(PublicationIndexSchema.ELEMENT_SIZE, catalog.getSize());
        catalogsArray[i] = element;
    }
    pMap.put(PublicationIndexSchema.CATALOG, catalogsArray);
    // Tracks
    Track[] tracks = publication.getTracks();
    HashMap<String, Object>[] tracksArray = new HashMap[tracks.length];
    for (int i = 0; i < tracksArray.length; i++) {
        Track track = tracks[i];
        HashMap<String, Object> element = new HashMap<String, Object>();
        element.put(PublicationIndexSchema.ELEMENT_ID, track.getIdentifier());
        addObjectStringtToMap(element, PublicationIndexSchema.ELEMENT_MIMETYPE, track.getMimeType());
        addObjectStringtToMap(element, PublicationIndexSchema.ELEMENT_TYPE, track.getElementType());
        element.put(PublicationIndexSchema.ELEMENT_TAG, track.getTags());
        addObjectStringtToMap(element, PublicationIndexSchema.ELEMENT_URL, track.getURI());
        element.put(PublicationIndexSchema.ELEMENT_SIZE, track.getSize());
        element.put(PublicationIndexSchema.TRACK_DURATION, track.getDuration());
        tracksArray[i] = element;
    }
    pMap.put(PublicationIndexSchema.TRACK, tracksArray);
    return pMap;
}
Also used : HashMap(java.util.HashMap) Attachment(org.opencastproject.mediapackage.Attachment) Catalog(org.opencastproject.mediapackage.Catalog) Track(org.opencastproject.mediapackage.Track)

Example 93 with Track

use of org.opencastproject.mediapackage.Track in project opencast by opencast.

the class ConcatWorkflowOperationHandlerTest method testConcat2EncodedTracksWithFlavor.

@Test
public void testConcat2EncodedTracksWithFlavor() throws Exception {
    setMockups();
    // operation configuration
    String targetTags = "engage,rss";
    Map<String, String> configurations = new HashMap<String, String>();
    configurations.put("source-flavor-part-0", "presentation/source");
    configurations.put("source-flavor-part-1", "presenter/source");
    configurations.put("target-tags", targetTags);
    configurations.put("target-flavor", "presenter/concat");
    configurations.put("encoding-profile", "concat");
    configurations.put("output-resolution", "1900x1080");
    // run the operation handler
    WorkflowOperationResult result = getWorkflowOperationResult(mp, configurations);
    // check track metadata
    MediaPackage mpNew = result.getMediaPackage();
    Track trackEncoded = mpNew.getTrack(ENCODED_TRACK_ID);
    Assert.assertEquals("presenter/concat", trackEncoded.getFlavor().toString());
    Assert.assertArrayEquals(targetTags.split("\\W"), trackEncoded.getTags());
}
Also used : HashMap(java.util.HashMap) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowOperationResult(org.opencastproject.workflow.api.WorkflowOperationResult) Track(org.opencastproject.mediapackage.Track) Test(org.junit.Test)

Example 94 with Track

use of org.opencastproject.mediapackage.Track in project opencast by opencast.

the class ImageWorkflowOperationHandlerTest method track.

private Track track(long duration) {
    final Track t = EasyMock.createNiceMock(Track.class);
    EasyMock.expect(t.getDuration()).andReturn(duration).anyTimes();
    EasyMock.replay(t);
    return t;
}
Also used : Track(org.opencastproject.mediapackage.Track)

Example 95 with Track

use of org.opencastproject.mediapackage.Track in project opencast by opencast.

the class ComposerServiceImpl method parallelEncode.

/**
 * Encodes audio and video track to a file. If both an audio and a video track are given, they are muxed together into
 * one movie container.
 *
 * @param job
 *          Job in which context the encoding is done
 * @param mediaTrack
 *          Source track
 * @param profileId
 *          the encoding profile
 * @return the encoded track or none if the operation does not return a track. This may happen for example when doing
 *         two pass encodings where the first pass only creates metadata for the second one
 * @throws EncoderException
 *           if encoding fails
 */
private List<Track> parallelEncode(Job job, Track mediaTrack, String profileId) throws EncoderException, MediaPackageException {
    if (job == null) {
        throw new EncoderException("The Job parameter must not be null");
    }
    // Get the tracks and make sure they exist
    final File mediaFile = loadTrackIntoWorkspace(job, "source", mediaTrack);
    // Create the engine
    final EncodingProfile profile = getProfile(profileId);
    final EncoderEngine encoderEngine = getEncoderEngine();
    // List of encoded tracks
    LinkedList<Track> encodedTracks = new LinkedList<>();
    // Do the work
    int i = 0;
    Map<String, File> source = new HashMap<>();
    source.put("video", mediaFile);
    List<File> outputFiles = encoderEngine.process(source, profile, null);
    activeEncoder.remove(encoderEngine);
    for (File encodingOutput : outputFiles) {
        // Put the file in the workspace
        URI returnURL;
        final String targetTrackId = idBuilder.createNew().toString();
        try (InputStream in = new FileInputStream(encodingOutput)) {
            returnURL = workspace.putInCollection(COLLECTION, job.getId() + "-" + i + "." + FilenameUtils.getExtension(encodingOutput.getAbsolutePath()), in);
            logger.info("Copied the encoded file to the workspace at {}", returnURL);
            if (encodingOutput.delete()) {
                logger.info("Deleted the local copy of the encoded file at {}", encodingOutput.getAbsolutePath());
            } else {
                logger.warn("Unable to delete the encoding output at {}", encodingOutput);
            }
        } catch (Exception e) {
            throw new EncoderException("Unable to put the encoded file into the workspace", e);
        }
        // Have the encoded track inspected and return the result
        Job inspectionJob = inspect(job, returnURL);
        Track inspectedTrack = (Track) MediaPackageElementParser.getFromXml(inspectionJob.getPayload());
        inspectedTrack.setIdentifier(targetTrackId);
        List<String> tags = profile.getTags();
        for (String tag : tags) {
            if (encodingOutput.getName().endsWith(profile.getSuffix(tag)))
                inspectedTrack.addTag(tag);
        }
        encodedTracks.add(inspectedTrack);
        i++;
    }
    return encodedTracks;
}
Also used : HashMap(java.util.HashMap) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) EncodingProfile(org.opencastproject.composer.api.EncodingProfile) URI(java.net.URI) LinkedList(java.util.LinkedList) FileInputStream(java.io.FileInputStream) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) MediaInspectionException(org.opencastproject.inspection.api.MediaInspectionException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) EncoderException(org.opencastproject.composer.api.EncoderException) EncoderException(org.opencastproject.composer.api.EncoderException) Job(org.opencastproject.job.api.Job) File(java.io.File) 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