Linux ProcessBuilder+FFMPEG提取视频缩略图

2016-04-20 21:03:56 1109 次浏览

当命令行参数过多的时候,使用bash -c 'command'执行起来比较靠谱
package org.lyh.library;

import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lvyahui on 2015/7/15.
 */
public class VideoUtils {
    public static final String FFMPEG_EXECUTOR = "ffmpeg";
    public static final int THUMBNAIL_WIDTH = 400;
    public static final int THUMBNAIL_HEIGHT = 300;

    public static boolean extractThumbnail(File inputFile,String thumbnailOutput){
        List command = new ArrayList();
//        File ffmpegExe = new File(FFMPEG_EXECUTOR);
//        if(!ffmpegExe.exists()){
//            System.out.println("ffmpeg not supoort");
//            return false;
//        }

//        System.out.println(ffmpegExe.getAbsolutePath());
        System.out.println(inputFile.getAbsolutePath());
//        command.add(ffmpegExe.getAbsolutePath());
        command.add(FFMPEG_EXECUTOR);
        command.add("-i");
        command.add(inputFile.getAbsolutePath());
        command.add("-y");
        command.add("-f");
        command.add("image2");
        command.add("-ss");
        command.add("10");
        command.add("-t");
        command.add("0.001");
        command.add("-s");
        command.add(THUMBNAIL_WIDTH+"*"+THUMBNAIL_HEIGHT);
        command.add(thumbnailOutput);

//        String cmd = "'".concat(StringUtils.join(command.iterator(),' ')).concat("'");
        String cmd = StringUtils.join(command.iterator(),' ');
        System.out.println(cmd);
        ProcessBuilder builder = new ProcessBuilder("/bin/sh","-c",cmd);
//        builder.command(command);
        builder.redirectErrorStream(true);
        try {
            long startTime = System.currentTimeMillis();
            Process process = builder.start();
            System.out.println("delay"+(System.currentTimeMillis()-startTime));
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    public static void main(String[] args) {
        extractThumbnail(new File("/data/slsxpt/web/upload/video/3d6d735531cdf2498f6ed722d80f4686.mp4"),"/data/slsxpt/web/upload/thumbnail/3d6d735531cdf2498f6ed722d80f4686.jpg");
        extractThumbnail(new File("/data/slsxpt/web/upload/video/ff8085cecb506336279f3693244d98a8.mp4"),"/data/slsxpt/web/upload/thumbnail/ff8085cecb506336279f3693244d98a8.jpg");
    }
}

评论