use of org.fagu.fmv.soft.SoftExecutor in project fmv by f-agu.
the class IMSoftProvider method createSoftExecutor.
/**
* @see org.fagu.fmv.soft.find.SoftProvider#createSoftExecutor(org.fagu.fmv.soft.Soft, java.io.File, java.util.List)
*/
@Override
public SoftExecutor createSoftExecutor(Soft soft, File execFile, List<String> parameters) {
if (SOFT_MAGICK_NAME.equalsIgnoreCase(FilenameUtils.getBaseName(soft.getFile().getName()))) {
SoftInfo softInfo = soft.getFirstInfo();
if (softInfo instanceof VersionSoftInfo) {
VersionSoftInfo versionSoftInfo = (VersionSoftInfo) softInfo;
Optional<Version> version = versionSoftInfo.getVersion();
if (version.isPresent() && version.get().isUpperOrEqualsThan(V7)) {
List<String> newParams = new ArrayList<>(parameters);
newParams.add(0, getName());
return new SoftExecutor(this, execFile, newParams);
}
}
}
return super.createSoftExecutor(soft, execFile, parameters);
}
use of org.fagu.fmv.soft.SoftExecutor in project fmv by f-agu.
the class GSExceptionKnowAnalyzeTestCase method testPDFToImage.
/**
* @throws IOException
*/
@Test
@Ignore
public void testPDFToImage() throws IOException {
StringJoiner joiner = new StringJoiner(";");
List<Consumer<SoftExecutor>> consumers = Arrays.asList(null, se -> se.ifExceptionIsKnownDo(ek -> ek.onMessage(joiner::add).doThrow()));
// }));
for (Consumer<SoftExecutor> consumer : consumers) {
runPdfToImage(null, "Permission denied", consumer);
runPdfToImage("cheese.zip", "Undefined format", consumer);
runPdfToImage("mp4.mp4", "Undefined format", consumer);
}
assertEquals("Permission denied;Undefined format;Undefined format", joiner.toString());
}
use of org.fagu.fmv.soft.SoftExecutor in project fmv by f-agu.
the class GSExceptionKnowAnalyzeTestCase method runPdfToImage.
// *************************************
/**
* @param srcResource
* @param expectedMessage
* @param consumer
* @throws IOException
*/
private void runPdfToImage(String srcResource, String expectedMessage, Consumer<SoftExecutor> consumer) throws IOException {
File folder = new File(System.getProperty("java.io.tmpdir"), "gs-pdf2img-test");
try {
FileUtils.deleteDirectory(folder);
folder.mkdirs();
File srcFile = srcResource != null ? Resource.extract(folder, srcResource) : folder;
File outFile = new File(srcFile.getPath() + ".jpg");
try {
Soft gsSoft = GS.search();
List<String> list = new ArrayList<>();
list.add("-sDEVICE=png16m");
list.add("-sOutputFile=" + outFile.getPath());
list.add("-r200");
list.add("-dNOPAUSE");
list.add("-dBATCH");
list.add("-dSAFER");
list.add("-dFirstPage=1");
list.add("-dLastPage=1");
list.add("-dUseCropBox");
list.add(srcFile.getAbsolutePath());
SoftExecutor customizeExecutor = gsSoft.withParameters(list).workingDirectory(srcFile.getParentFile());
// .logCommandLine(System.out::println);
if (consumer != null) {
consumer.accept(customizeExecutor);
}
customizeExecutor.execute();
fail();
} catch (FMVExecuteException e) {
if (e.isKnown()) {
assertEquals(expectedMessage, e.getExceptionKnown().toString());
} else {
new NestedException(e).messageToLines().forEach(System.out::println);
throw e;
}
}
} finally {
FileUtils.deleteDirectory(folder);
}
}
use of org.fagu.fmv.soft.SoftExecutor in project fmv by f-agu.
the class PdfSoftProvider method createSoftExecutor.
/**
* @see org.fagu.fmv.soft.find.SoftProvider#createSoftExecutor(org.fagu.fmv.soft.Soft, java.io.File, java.util.List)
*/
@Override
public SoftExecutor createSoftExecutor(Soft soft, File execFile, List<String> parameters) {
XPdfVersionSoftInfo softInfo = (XPdfVersionSoftInfo) soft.getFirstInfo();
// On Windows & DOS, file.encoding=cp1252 -> do nothing
if (softInfo != null && Provider.XPDF.equals(softInfo.getProvider()) && SystemUtils.IS_OS_WINDOWS && "UTF-8".equals(System.getProperty("file.encoding"))) {
List<String> newParams = new ArrayList<>(parameters);
if (!parameters.contains("-enc")) {
newParams.add(0, "-enc");
newParams.add(1, "UTF-8");
}
SoftExecutor softExecutor = new SoftExecutor(this, execFile, newParams);
softExecutor.charset(StandardCharsets.UTF_8);
// });
return softExecutor;
}
return super.createSoftExecutor(soft, execFile, parameters);
}
use of org.fagu.fmv.soft.SoftExecutor in project fmv by f-agu.
the class ImageMetadatas method extract.
// *****************************************
/**
* @param identifySoft
* @param sourceFiles
* @param logger
* @param customizeExecutor
* @return
* @throws IOException
*/
private static Map<File, ImageMetadatas> extract(Soft identifySoft, Collection<File> sourceFiles, Consumer<String> logger, Consumer<SoftExecutor> customizeExecutor) throws IOException {
Objects.requireNonNull(identifySoft);
if (sourceFiles.isEmpty()) {
return Collections.emptyMap();
}
final String boundary = "BOUNDARY";
// prepare
IMOperation op = new IMOperation();
StringJoiner joiner = new StringJoiner("\n");
joiner.add("==%f==");
joiner.add("%[exif:*]%[date:*]%[xap:*]xy=%x %y");
joiner.add("colorspace=%[colorspace]");
joiner.add("wh=%w %h");
joiner.add("cdepth=%z");
joiner.add("compression=%C");
joiner.add("compressionq=%Q");
joiner.add("resunit=%U");
joiner.add(boundary);
op.ping().format(joiner.toString() + "\n");
int size = sourceFiles.size();
sourceFiles.forEach(file -> op.image(file, "[0]"));
List<String> outputs = new ArrayList<>();
SoftExecutor softExecutor = //
identifySoft.withParameters(op.toList()).addOutReadLine(//
outputs::add).logCommandLine(logger);
if (customizeExecutor != null) {
customizeExecutor.accept(softExecutor);
}
softExecutor.execute();
// parse output
Map<File, ImageMetadatas> outMap = new LinkedHashMap<>(size);
Iterator<File> srcFileIterator = sourceFiles.iterator();
TreeMap<String, String> params = null;
File currentFile = null;
for (String line : outputs) {
if (StringUtils.isBlank(line) || boundary.equals(line)) {
continue;
}
if ((line.startsWith("==") || line.startsWith(boundary + "==")) && line.endsWith("==")) {
if (currentFile != null) {
outMap.put(currentFile, new ImageMetadatas(params));
}
currentFile = srcFileIterator.next();
if (StringUtils.substringBetween(line, "==").equals(currentFile.getName())) {
params = new TreeMap<>();
params.put("filename", currentFile.getName());
continue;
} else {
params = null;
currentFile = null;
}
}
if (params == null) {
continue;
}
int index = line.indexOf('=');
String key = line.substring(0, index);
String value = line.substring(index + 1);
params.put(key.toLowerCase(), value);
}
if (currentFile != null) {
outMap.put(currentFile, new ImageMetadatas(params));
}
return outMap;
}
Aggregations