/** * Executes the ffmpeg process with the previous given arguments. * * @return The standard output of the child process. * @throws IOException If the process call fails. */ public String execute() throws IOException { CommandLine cmdLine = new CommandLine(ffmpegExecutablePath); int fileNumber = 0; Map<String, File> map = new HashMap<String, File>(); for (int i = 0; i < args.size(); i++) { final String arg = args.get(i); final Boolean isFile = argIsFile.get(i); if (isFile) { String key = "file" + fileNumber; map.put(key, new File(arg)); cmdLine.addArgument("${" + key + "}", false); fileNumber++; } else { cmdLine.addArgument(arg); } } cmdLine.setSubstitutionMap(map); LOG.fine("Execute: " + cmdLine); DefaultExecutor executor = new DefaultExecutor(); // 5minutes wait ExecuteWatchdog watchdog = new ExecuteWatchdog(60 * 1000 * 5); executor.setWatchdog(watchdog); ByteArrayOutputStream out = new ByteArrayOutputStream(); executor.setStreamHandler(new PumpStreamHandler(out)); int[] exitValues = {0, 1}; executor.setExitValues(exitValues); executor.execute(cmdLine); return out.toString(); }
public int retrievePageCount(File source) { File target = new File( System.getProperty("java.io.tmpdir"), FilenameUtils.removeExtension(source.getName()) + ".pdf"); Map<String, Object> args = newHashMap(); args.put("source", source); args.put("targetDir", target.getParentFile()); CommandLine cmd = new CommandLine("loffice"); cmd.addArgument("--headless"); cmd.addArgument("--convert-to"); cmd.addArgument("pdf"); cmd.addArgument("--outdir"); cmd.addArgument("${targetDir}"); cmd.addArgument("${source}"); cmd.setSubstitutionMap(args); try { DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); ExecuteWatchdog watchdog = new ExecuteWatchdog(10L * 1000L); executor.setWatchdog(watchdog); logger.trace("About to execute command {}", cmd.toString()); executor.execute(cmd, resultHandler); resultHandler.waitFor(); final int exitValue = resultHandler.getExitValue(); if (exitValue != 0) { logger.error( "Unable to convert Microsoft Word file {} to PDF. Exit code = {}", source.getAbsolutePath(), exitValue); return -1; } final int pageCount = pdfDocumentInspector.retrievePageCount(target); deleteFileIfNeeded(target); return pageCount; } catch (IOException | InterruptedException e) { logger.error("Unable to create a PDF from {}", source.getAbsolutePath(), e); deleteFileIfNeeded(target); return -1; } }
public String toString() { CommandLine cmdLine = new CommandLine(ffmpegExecutablePath); int fileNumber = 0; Map<String, File> map = new HashMap<String, File>(); for (int i = 0; i < args.size(); i++) { final String arg = args.get(i); final Boolean isFile = argIsFile.get(i); if (isFile) { String key = "file" + fileNumber; map.put(key, new File(arg)); cmdLine.addArgument("${" + key + "}", false); fileNumber++; } else { cmdLine.addArgument(arg); } } cmdLine.setSubstitutionMap(map); return cmdLine.toString(); }