importjava.io.IOException;importjava.io.InputStream;importjava.io.OutputStream;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importws.schild.jave.process.ProcessKiller;importws.schild.jave.process.ProcessWrapper;importws.schild.jave.process.ffmpeg.DefaultFFMPEGLocator;publicclassFfmpegCmd{privatestaticfinalLogger LOG =LoggerFactory.getLogger(ProcessWrapper.class);/** The process representing the ffmpeg execution. */privateProcess ffmpeg =null;/**
* A process killer to kill the ffmpeg process with a shutdown hook, useful if the jvm execution
* is shutted down during an ongoing encoding process.
*/privateProcessKiller ffmpegKiller =null;/** A stream reading from the ffmpeg process standard output channel. */privateInputStream inputStream =null;/** A stream writing in the ffmpeg process standard input channel. */privateOutputStream outputStream =null;/** A stream reading from the ffmpeg process standard error channel. */privateInputStream errorStream =null;/**
* Executes the ffmpeg process with the previous given arguments.
*
* @param destroyOnRuntimeShutdown destroy process if the runtime VM is shutdown
* @param openIOStreams Open IO streams for input/output and errorout, should be false when
* destroyOnRuntimeShutdown is false too
* @param ffmpegCmd windows such as (mp4 transform to mov):
* " -i C:\\Users\\hsj\\AppData\\Local\\Temp\\jave\\honer.mp4 -c copy C:\\Users\\hsj\\AppData\\Local\\Temp\\jave\\honer_test.mov "
* @throws IOException If the process call fails.
*/publicvoidexecute(boolean destroyOnRuntimeShutdown,boolean openIOStreams,String ffmpegCmd)throwsIOException{DefaultFFMPEGLocator defaultFFMPEGLocator =newDefaultFFMPEGLocator();StringBuffer cmd =newStringBuffer(defaultFFMPEGLocator.getExecutablePath());//insert blank for delimiter
cmd.append(" ");
cmd.append(ffmpegCmd);String cmdStr =String.format("ffmpegCmd final is :%s", cmd.toString());System.out.println(cmdStr);
LOG.info(cmdStr);Runtime runtime =Runtime.getRuntime();try{
ffmpeg = runtime.exec(cmd.toString());if(destroyOnRuntimeShutdown){
ffmpegKiller =newProcessKiller(ffmpeg);
runtime.addShutdownHook(ffmpegKiller);}if(openIOStreams){
inputStream = ffmpeg.getInputStream();
outputStream = ffmpeg.getOutputStream();
errorStream = ffmpeg.getErrorStream();}}catch(Exception e){
e.printStackTrace();}}/**
* Returns a stream reading from the ffmpeg process standard output channel.
*
* @return A stream reading from the ffmpeg process standard output channel.
*/publicInputStreamgetInputStream(){return inputStream;}/**
* Returns a stream writing in the ffmpeg process standard input channel.
*
* @return A stream writing in the ffmpeg process standard input channel.
*/publicOutputStreamgetOutputStream(){return outputStream;}/**
* Returns a stream reading from the ffmpeg process standard error channel.
*
* @return A stream reading from the ffmpeg process standard error channel.
*/publicInputStreamgetErrorStream(){return errorStream;}/** If there's a ffmpeg execution in progress, it kills it. */publicvoiddestroy(){if(inputStream !=null){try{
inputStream.close();}catch(Throwable t){
LOG.warn("Error closing input stream", t);}
inputStream =null;}if(outputStream !=null){try{
outputStream.close();}catch(Throwable t){
LOG.warn("Error closing output stream", t);}
outputStream =null;}if(errorStream !=null){try{
errorStream.close();}catch(Throwable t){
LOG.warn("Error closing error stream", t);}
errorStream =null;}if(ffmpeg !=null){
ffmpeg.destroy();
ffmpeg =null;}if(ffmpegKiller !=null){Runtime runtime =Runtime.getRuntime();
runtime.removeShutdownHook(ffmpegKiller);
ffmpegKiller =null;}}/**
* Return the exit code of the ffmpeg process If the process is not yet terminated, it waits for
* the termination of the process
*
* @return process exit code
*/publicintgetProcessExitCode(){// Make sure it's terminatedtry{
ffmpeg.waitFor();}catch(InterruptedException ex){
LOG.warn("Interrupted during waiting on process, forced shutdown?", ex);}return ffmpeg.exitValue();}/**close**/publicvoidclose(){destroy();}}
importjava.util.concurrent.ExecutorService;importjava.util.concurrent.LinkedBlockingQueue;importjava.util.concurrent.ThreadPoolExecutor;importjava.util.concurrent.TimeUnit;publicclassThreadPoolExecutorUtils{//多线程publicstaticint core =Runtime.getRuntime().availableProcessors();publicstaticExecutorService pool =newThreadPoolExecutor(core,//核心
core *2,//最大0L,//空闲立即退出TimeUnit.MILLISECONDS,newLinkedBlockingQueue<Runnable>(1024),//无边界阻塞队列newThreadPoolExecutor.AbortPolicy());}