use of org.opencastproject.mediapackage.MediaPackageElement in project opencast by opencast.
the class CaptionServiceImpl method convert.
/**
* Converts the captions and returns them in a new catalog.
*
* @return the converted catalog
*/
protected MediaPackageElement convert(Job job, MediaPackageElement input, String inputFormat, String outputFormat, String language) throws UnsupportedCaptionFormatException, CaptionConverterException, MediaPackageException {
try {
// check parameters
if (input == null)
throw new IllegalArgumentException("Input element can't be null");
if (StringUtils.isBlank(inputFormat))
throw new IllegalArgumentException("Input format is null");
if (StringUtils.isBlank(outputFormat))
throw new IllegalArgumentException("Output format is null");
// get input file
File captionsFile;
try {
captionsFile = workspace.get(input.getURI());
} catch (NotFoundException e) {
throw new CaptionConverterException("Requested media package element " + input + " could not be found.");
} catch (IOException e) {
throw new CaptionConverterException("Requested media package element " + input + "could not be accessed.");
}
logger.debug("Atempting to convert from {} to {}...", inputFormat, outputFormat);
List<Caption> collection = null;
try {
collection = importCaptions(captionsFile, inputFormat, language);
logger.debug("Parsing to collection succeeded.");
} catch (UnsupportedCaptionFormatException e) {
throw new UnsupportedCaptionFormatException(inputFormat);
} catch (CaptionConverterException e) {
throw e;
}
URI exported;
try {
exported = exportCaptions(collection, job.getId() + "." + FilenameUtils.getExtension(captionsFile.getAbsolutePath()), outputFormat, language);
logger.debug("Exporting captions succeeding.");
} catch (UnsupportedCaptionFormatException e) {
throw new UnsupportedCaptionFormatException(outputFormat);
} catch (IOException e) {
throw new CaptionConverterException("Could not export caption collection.", e);
}
// create catalog and set properties
CaptionConverter converter = getCaptionConverter(outputFormat);
MediaPackageElementBuilder elementBuilder = MediaPackageElementBuilderFactory.newInstance().newElementBuilder();
MediaPackageElement mpe = elementBuilder.elementFromURI(exported, converter.getElementType(), new MediaPackageElementFlavor("captions", outputFormat + (language == null ? "" : "+" + language)));
if (mpe.getMimeType() == null) {
String[] mimetype = FileTypeMap.getDefaultFileTypeMap().getContentType(exported.getPath()).split("/");
mpe.setMimeType(mimeType(mimetype[0], mimetype[1]));
}
if (language != null)
mpe.addTag("lang:" + language);
return mpe;
} catch (Exception e) {
logger.warn("Error converting captions in " + input, e);
if (e instanceof CaptionConverterException) {
throw (CaptionConverterException) e;
} else if (e instanceof UnsupportedCaptionFormatException) {
throw (UnsupportedCaptionFormatException) e;
} else {
throw new CaptionConverterException(e);
}
}
}
use of org.opencastproject.mediapackage.MediaPackageElement in project opencast by opencast.
the class AbstractAssetManagerBasicTest method testGetFileNameFromUrn.
@Test
public void testGetFileNameFromUrn() throws Exception {
MediaPackageElement element = new MediaPackageElementBuilderImpl().newElement(Type.Track, MediaPackageElements.PRESENTER_SOURCE);
Opt<String> fileNameFromUrn = AbstractAssetManager.getFileNameFromUrn(element);
assertTrue(fileNameFromUrn.isNone());
element.setURI(URI.create("file://test.txt"));
fileNameFromUrn = AbstractAssetManager.getFileNameFromUrn(element);
assertTrue(fileNameFromUrn.isNone());
element.setURI(URI.create("urn:matterhorn:uuid:22:uuid2:caption-ger.vtt"));
fileNameFromUrn = AbstractAssetManager.getFileNameFromUrn(element);
assertTrue(fileNameFromUrn.isSome());
assertEquals("caption-ger.vtt", fileNameFromUrn.get());
}
use of org.opencastproject.mediapackage.MediaPackageElement in project opencast by opencast.
the class AbstractAssetManagerSelectTest method testSelectSnapshots.
@Test
public void testSelectSnapshots() throws Exception {
final MediaPackage mp = mkMediaPackage();
final MediaPackageElement mpe = mkCatalog();
mp.add(mpe);
final Snapshot snapshot = am.takeSnapshot(OWNER, mp);
final Version version = snapshot.getVersion();
assertThat("Archival date should not be in the future", snapshot.getArchivalDate(), lessThanOrEqualTo(new Date()));
assertThat("Snapshot should be available", snapshot.getAvailability(), equalTo(Availability.ONLINE));
assertThat("Snapshot should belong to the default organization", snapshot.getOrganizationId(), equalTo(DefaultOrganization.DEFAULT_ORGANIZATION_ID));
final Opt<Asset> asset = am.getAsset(version, mp.getIdentifier().toString(), mpe.getIdentifier());
assertTrue("Asset should be found", asset.isSome());
assertEquals("Media package element part of the asset ID should equal the element's ID", mpe.getIdentifier(), asset.get().getId().getMediaPackageElementId());
assertEquals("Mime types should equal", mpe.getMimeType(), asset.get().getMimeType().get());
assertFalse("Asset should not be found", am.getAsset(version, "id", "id").isSome());
// try to find the catalog of the media package by checksum
final MediaPackage mpCopy = MediaPackageSupport.copy(mp);
am.calcChecksumsForMediaPackageElements(AbstractAssetManager.assetsOnly(mpCopy));
assertEquals("Media package should be set up with a single catalog", 1, mpCopy.getCatalogs().length);
final String checksum = mpCopy.getCatalogs()[0].getChecksum().toString();
assertTrue("Media package element should be retrievable by checksum", am.getDb().findAssetByChecksum(checksum).isSome());
// issue some queries
{
logger.info("Run a failing query");
assertEquals("The result should not contain any records", 0, q.select(q.snapshot()).where(q.mediaPackageId(mp.getIdentifier().toString()).and(q.availability(Availability.ONLINE))).where(q.mediaPackageId("12")).run().getSize());
}
{
logger.info("Run query to find snapshot");
final AResult r = q.select(q.snapshot()).where(q.mediaPackageId(mp.getIdentifier().toString()).and(q.availability(Availability.ONLINE))).run();
assertEquals("The result set should contain exactly one record", 1, r.getSize());
assertEquals("The media package IDs should be equal", mp.getIdentifier().toString(), r.getRecords().head2().getMediaPackageId());
assertTrue("The snapshot should be contained in the record", r.getRecords().head2().getSnapshot().isSome());
assertEquals("The media package IDs should be equal", mp.getIdentifier(), r.getRecords().head2().getSnapshot().get().getMediaPackage().getIdentifier());
}
{
final AResult r = q.select().where(q.mediaPackageId(mp.getIdentifier().toString()).and(q.availability(Availability.ONLINE))).run();
assertEquals("The result should contain one record", 1, r.getSize());
assertTrue("The result should not contain a snapshot", r.getRecords().head2().getSnapshot().isNone());
}
}
use of org.opencastproject.mediapackage.MediaPackageElement in project opencast by opencast.
the class IngestRestService method schedule.
@POST
@Path("schedule/{wdID}")
@RestQuery(name = "schedule", description = "Schedule an event based on the given media package", pathParameters = { @RestParameter(description = "Workflow definition id", isRequired = true, name = "wdID", type = RestParameter.Type.STRING) }, restParameters = { @RestParameter(description = "The media package", isRequired = true, name = "mediaPackage", type = RestParameter.Type.TEXT) }, reponses = { @RestResponse(description = "Event scheduled", responseCode = HttpServletResponse.SC_CREATED), @RestResponse(description = "Media package not valid", responseCode = HttpServletResponse.SC_BAD_REQUEST) }, returnDescription = "")
public Response schedule(@PathParam("wdID") String wdID, MultivaluedMap<String, String> formData) {
if (StringUtils.isBlank(wdID)) {
logger.trace("workflow definition id is not specified");
return Response.status(Response.Status.BAD_REQUEST).build();
}
Map<String, String> wfConfig = getWorkflowConfig(formData);
if (StringUtils.isNotBlank(wdID)) {
wfConfig.put(CaptureParameters.INGEST_WORKFLOW_DEFINITION, wdID);
}
logger.debug("Schedule with workflow definition '{}'", wfConfig.get(WORKFLOW_DEFINITION_ID_PARAM));
String mediaPackageXml = formData.getFirst("mediaPackage");
if (StringUtils.isBlank(mediaPackageXml)) {
logger.debug("Rejected schedule without media package");
return Response.status(Status.BAD_REQUEST).build();
}
MediaPackage mp = null;
try {
mp = factory.newMediaPackageBuilder().loadFromXml(mediaPackageXml);
if (MediaPackageSupport.sanityCheck(mp).isSome()) {
throw new MediaPackageException("Insane media package");
}
} catch (MediaPackageException e) {
logger.debug("Rejected ingest with invalid media package {}", mp);
return Response.status(Status.BAD_REQUEST).build();
}
MediaPackageElement[] mediaPackageElements = mp.getElementsByFlavor(MediaPackageElements.EPISODE);
if (mediaPackageElements.length != 1) {
logger.debug("There can be only one (and exactly one) episode dublin core catalog: https://youtu.be/_J3VeogFUOs");
return Response.status(Status.BAD_REQUEST).build();
}
try {
ingestService.schedule(mp, wdID, wfConfig);
return Response.status(Status.CREATED).build();
} catch (IngestException e) {
return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
} catch (SchedulerConflictException e) {
return Response.status(Status.CONFLICT).entity(e.getMessage()).build();
} catch (NotFoundException | UnauthorizedException | SchedulerException e) {
return Response.serverError().build();
}
}
use of org.opencastproject.mediapackage.MediaPackageElement in project opencast by opencast.
the class IngestServiceImpl method mergeMediaPackageElements.
private void mergeMediaPackageElements(MediaPackage mp, MediaPackage scheduledMp) {
for (MediaPackageElement element : scheduledMp.getElements()) {
// Asset manager media package may have a publication element (for live) if retract live has not run yet
if (!MediaPackageElement.Type.Publication.equals(element.getElementType()) && mp.getElementsByFlavor(element.getFlavor()).length > 0) {
logger.info("Ignore scheduled element '{}', there is already an ingested element with flavor '{}'", element, element.getFlavor());
continue;
}
logger.info("Adding new scheduled element '{}' to ingested mediapackage", element);
mp.add(element);
}
}
Aggregations