use of androidx.media3.transformer.Transformer in project knetbuilder by Rothamsted.
the class Parser method start.
public void start() throws InvalidPluginArgumentException {
GeneralOutputEvent so = new GeneralOutputEvent("Starting PubMed/MEDLINE parser...", Parser.getCurrentMethodName());
fireEventOccurred(so);
XMLParser xmlParser = new XMLParser();
Transformer writer = new Transformer(graph);
Set<Abstract> abs = new HashSet<Abstract>();
try {
// Input: Directory with PubMed XML files
if (args.getUniqueValue(FileArgumentDefinition.INPUT_FILE) != null) {
File file = new File((String) args.getUniqueValue(FileArgumentDefinition.INPUT_FILE));
fireEventOccurred(new GeneralOutputEvent("Start parsing file " + file.getAbsolutePath(), Parser.getCurrentMethodName()));
abs.addAll(xmlParser.parseMedlineXML(file));
fireEventOccurred(new GeneralOutputEvent("Number of publications found: " + abs.size(), Parser.getCurrentMethodName()));
}
// Input: File with integer pmids
if (args.getUniqueValue(ArgumentNames.PMID_FILE_ARG) != null) {
File idFile = new File((String) args.getUniqueValue(ArgumentNames.PMID_FILE_ARG));
HashSet<String> pmidSet = new HashSet<>();
fireEventOccurred(new GeneralOutputEvent("Retrieving PMIDs from " + "file: " + idFile.getAbsolutePath(), Parser.getCurrentMethodName()));
BufferedReader in = new BufferedReader(new FileReader(idFile));
String inputLine = null;
while ((inputLine = in.readLine()) != null) {
inputLine = inputLine.trim();
if (!inputLine.equals("")) {
// check if more than one value separated by ;
String[] ids = inputLine.split(" ");
for (String id : ids) {
pmidSet.add(id.trim());
}
}
}
abs.addAll(xmlParser.parsePMIDs(pmidSet));
in.close();
fireEventOccurred(new GeneralOutputEvent("Number of publications found: " + pmidSet.size(), Parser.getCurrentMethodName()));
}
// Input: Import cited publications
Boolean isImpOnlyCitedPub = (Boolean) args.getUniqueValue(ArgumentNames.IMP_CITED_PUB_ARG);
if (isImpOnlyCitedPub) {
fireEventOccurred(new GeneralOutputEvent("Retrieving PMIDs " + "cited in the Ondex graph...", Parser.getCurrentMethodName()));
HashSet<String> pmidSet = new HashSet<String>();
ConceptClass ccPub = graph.getMetaData().getConceptClass(MetaData.CC_PUBLICATION);
for (ONDEXConcept c : graph.getConceptsOfConceptClass(ccPub)) {
for (ConceptAccession accession : c.getConceptAccessions()) {
if (accession.getElementOf().getId().equalsIgnoreCase(MetaData.CV_NLM)) {
pmidSet.add(accession.getAccession());
}
}
}
// System.out.println("pmidSet: " + pmidSet.iterator().next());
abs.addAll(xmlParser.parsePMIDs(pmidSet));
fireEventOccurred(new GeneralOutputEvent("Number of publications found: " + pmidSet.size(), Parser.getCurrentMethodName()));
}
fireEventOccurred(new GeneralOutputEvent("Adding " + abs.size() + " unique publication concepts to Ondex graph...", Parser.getCurrentMethodName()));
writer.writeConcepts(abs);
xmlParser = null;
writer.cleanUp();
writer = null;
fireEventOccurred(new GeneralOutputEvent("Finished PubMed/MEDLINE parsing" + " with success!", Parser.getCurrentMethodName()));
} catch (Exception e) {
fireEventOccurred(new GeneralOutputEvent("Error: PubMed/MEDLINE import did not" + " finish! " + e.getMessage(), Parser.getCurrentMethodName()));
e.printStackTrace();
}
}
use of androidx.media3.transformer.Transformer in project media by androidx.
the class TransformerActivity method createTransformer.
@RequiresNonNull({ "playerView", "debugTextView", "informationTextView", "transformationStopwatch", "progressViewGroup", "debugFrame" })
private Transformer createTransformer(@Nullable Bundle bundle, String filePath) {
Transformer.Builder transformerBuilder = new Transformer.Builder(/* context= */
this);
if (bundle != null) {
TransformationRequest.Builder requestBuilder = new TransformationRequest.Builder();
requestBuilder.setFlattenForSlowMotion(bundle.getBoolean(ConfigurationActivity.SHOULD_FLATTEN_FOR_SLOW_MOTION));
@Nullable String audioMimeType = bundle.getString(ConfigurationActivity.AUDIO_MIME_TYPE);
if (audioMimeType != null) {
requestBuilder.setAudioMimeType(audioMimeType);
}
@Nullable String videoMimeType = bundle.getString(ConfigurationActivity.VIDEO_MIME_TYPE);
if (videoMimeType != null) {
requestBuilder.setVideoMimeType(videoMimeType);
}
int resolutionHeight = bundle.getInt(ConfigurationActivity.RESOLUTION_HEIGHT, /* defaultValue= */
C.LENGTH_UNSET);
if (resolutionHeight != C.LENGTH_UNSET) {
requestBuilder.setResolution(resolutionHeight);
}
Matrix transformationMatrix = getTransformationMatrix(bundle);
if (!transformationMatrix.isIdentity()) {
requestBuilder.setTransformationMatrix(transformationMatrix);
}
requestBuilder.experimental_setEnableHdrEditing(bundle.getBoolean(ConfigurationActivity.ENABLE_HDR_EDITING));
transformerBuilder.setTransformationRequest(requestBuilder.build()).setRemoveAudio(bundle.getBoolean(ConfigurationActivity.SHOULD_REMOVE_AUDIO)).setRemoveVideo(bundle.getBoolean(ConfigurationActivity.SHOULD_REMOVE_VIDEO));
}
return transformerBuilder.addListener(new Transformer.Listener() {
@Override
public void onTransformationCompleted(MediaItem mediaItem) {
TransformerActivity.this.onTransformationCompleted(filePath);
}
@Override
public void onTransformationError(MediaItem mediaItem, TransformationException exception) {
TransformerActivity.this.onTransformationError(exception);
}
}).setDebugViewProvider(new DemoDebugViewProvider()).build();
}
use of androidx.media3.transformer.Transformer in project media by androidx.
the class AndroidTestUtil method runTransformer.
/**
* Transforms the {@code uriString} with the {@link Transformer}.
*
* @param context The {@link Context}.
* @param testId An identifier for the test.
* @param transformer The {@link Transformer} that performs the transformation.
* @param uriString The uri (as a {@link String}) that will be transformed.
* @param timeoutSeconds The transformer timeout. An assertion confirms this is not exceeded.
* @return The {@link TransformationResult}.
* @throws Exception The cause of the transformation not completing.
*/
public static TransformationResult runTransformer(Context context, String testId, Transformer transformer, String uriString, int timeoutSeconds) throws Exception {
AtomicReference<@NullableType Exception> exceptionReference = new AtomicReference<>();
CountDownLatch countDownLatch = new CountDownLatch(1);
Transformer testTransformer = transformer.buildUpon().addListener(new Transformer.Listener() {
@Override
public void onTransformationCompleted(MediaItem inputMediaItem) {
countDownLatch.countDown();
}
@Override
public void onTransformationError(MediaItem inputMediaItem, TransformationException exception) {
exceptionReference.set(exception);
countDownLatch.countDown();
}
}).build();
Uri uri = Uri.parse(uriString);
File outputVideoFile = createExternalCacheFile(context, /* fileName= */
testId + "-output.mp4");
InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> {
try {
testTransformer.startTransformation(MediaItem.fromUri(uri), outputVideoFile.getAbsolutePath());
} catch (IOException e) {
exceptionReference.set(e);
}
});
assertWithMessage("Transformer timed out after " + timeoutSeconds + " seconds.").that(countDownLatch.await(timeoutSeconds, SECONDS)).isTrue();
@Nullable Exception exception = exceptionReference.get();
if (exception != null) {
throw exception;
}
TransformationResult result = new TransformationResult.Builder(testId).setFileSizeBytes(outputVideoFile.length()).build();
writeTransformationResultToFile(context, result);
return result;
}
use of androidx.media3.transformer.Transformer in project media by androidx.
the class TransformerEndToEndTest method startTransformation_withIoError_completesWithError.
@Test
public void startTransformation_withIoError_completesWithError() throws Exception {
Transformer transformer = createTransformerBuilder().build();
MediaItem mediaItem = MediaItem.fromUri("asset:///non-existing-path.mp4");
transformer.startTransformation(mediaItem, outputPath);
TransformationException exception = TransformerTestRunner.runUntilError(transformer);
assertThat(exception).hasCauseThat().hasCauseThat().isInstanceOf(IOException.class);
assertThat(exception.errorCode).isEqualTo(TransformationException.ERROR_CODE_IO_FILE_NOT_FOUND);
}
use of androidx.media3.transformer.Transformer in project media by androidx.
the class TransformerEndToEndTest method startTransformation_fromSpecifiedThread_completesSuccessfully.
@Test
public void startTransformation_fromSpecifiedThread_completesSuccessfully() throws Exception {
HandlerThread anotherThread = new HandlerThread("AnotherThread");
anotherThread.start();
Looper looper = anotherThread.getLooper();
Transformer transformer = createTransformerBuilder().setLooper(looper).build();
MediaItem mediaItem = MediaItem.fromUri(URI_PREFIX + FILE_AUDIO_VIDEO);
AtomicReference<Exception> exception = new AtomicReference<>();
CountDownLatch countDownLatch = new CountDownLatch(1);
new Handler(looper).post(() -> {
try {
transformer.startTransformation(mediaItem, outputPath);
TransformerTestRunner.runUntilCompleted(transformer);
} catch (Exception e) {
exception.set(e);
} finally {
countDownLatch.countDown();
}
});
countDownLatch.await();
assertThat(exception.get()).isNull();
DumpFileAsserts.assertOutput(context, testMuxer, getDumpFileName(FILE_AUDIO_VIDEO));
}
Aggregations