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;
}
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;
}
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());
}
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;
}
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;
}
Aggregations