无头浏览器和FFMPEG的示例

暂留,防止丢失

public class MainTest {

    public static void main(String[] args) throws IOException, InterruptedException {
        ProcessBuilder processBuilder = new ProcessBuilder();
        processBuilder.directory(new File("D:\\Demo"));
        processBuilder.command("D:\\Development\\Tool\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe",
                "D:\\Demo\\demo.js", "https://www.highcharts.com.cn/demo/highcharts/dynamic-update");
        processBuilder.redirectErrorStream(true);
        Process process = processBuilder.start();
        InputStream inputStream = process.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

        String readLine;
        while ((readLine = bufferedReader.readLine()) != null) {
            System.out.println(readLine);
        }
        bufferedReader.close();
    }

    @Test
    public void testGood() {
        List<String> commend = new ArrayList<>();
        commend.add("D:\\Development\\Tool\\ffmpeg-20190722-817235b-win64-static\\bin\\ffmpeg.exe");
        commend.add("-r");
        commend.add("1");
        commend.add("-i");
        commend.add("D:\\Demo\\h264\\demo%d.jpeg");
        commend.add("-c:v");
        commend.add("libx264");
        commend.add("-vf");
        // 24fps
        commend.add("fps=24");
        commend.add("-pix_fmt");
        commend.add("yuv420p");
        commend.add("D:\\Demo\\h264\\good.mp4");
        start(commend);
    }

    @Test
    public void testAudio() {
        // http://www.imooc.com/wenda/detail/432566
        // 1. ffmpeg.exe -i .\good.mp4 -c:v copy -an .\good-no-audio.mp4
        // 2. ffmpeg.exe -i .\good-no-audio.mp4 -i .\River.mp3 -c copy .\good-mp3.mp4
        // 3. ffmpeg.exe -i .\good-mp3-faded.mp4 -ss 00:00:00 -t 43 .\good-mp3-faded-out.mp4
    }

    /**
     * 把图片拼接成视频,这里取20张图片,生成10秒的视频
     */
    @Test
    public void test3() {
        long startTime = System.currentTimeMillis();
        List<String> commend = new ArrayList<>();
        commend.add("D:\\Development\\Tool\\ffmpeg-20190722-817235b-win64-static\\bin\\ffmpeg.exe");
        //以1fps读入
        commend.add("-r");
        commend.add("1");
        //-threads 2 以两个线程进行运行, 加快处理的速度。
        commend.add("-threads");
        commend.add("2");
        commend.add("-f");
        commend.add("image2");
        commend.add("-i");
        commend.add("D:\\Demo\\images\\demo%2d.png");
        commend.add("-vcodec");
        commend.add("libx264");
        //-y 对输出文件进行覆盖
        commend.add("-y");
        //-r 5 fps设置为5帧/秒(不同位置有不同含义)  %2d标识图片文件的正则
        commend.add("-r");
        commend.add("1");
        commend.add("D:\\Demo\\out-demo2.mp4");
        start(commend);
        long endTime = System.currentTimeMillis();
        System.out.println((endTime - startTime));
    }


    private void start(List commend) {
        Process p = null;
        try {
            ProcessBuilder builder = new ProcessBuilder(commend);
            builder.command(commend);
            p = builder.start();
            p.getOutputStream().close();
            doWaitFor(p);
            p.destroy();
        } catch (Exception e) {
            e.printStackTrace();
            p.destroy();
        }
    }


    public static void doWaitFor(Process p) throws Exception {
        InputStream in = null;
        InputStream err = null;
        in = p.getInputStream();
        err = p.getErrorStream();
        boolean finished = false;

        while (!finished) {
            try {
                Character c;
                while (in.available() > 0) {
                    c = (char) in.read();
                    System.out.print(c);
                }

                while (err.available() > 0) {
                    c = (char) err.read();
                    System.out.print(c);
                }

                p.exitValue();
                finished = true;
            } catch (IllegalThreadStateException var19) {
                Thread.sleep(500L);
            }
        }

    }

}
//创建
var page = require('webpage').create();
//系统变量集合,按照角标,从1开始
var system = require('system');

var index = 0;

address = system.args[1];
watermark = "this is watermark,  this is watermark,  this is watermark,  this is watermark,  this is watermark,  this is watermark";
//使用web浏览器
page.settings.userAgent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1';
//初始化浏览器大小
page.settings.viewportSize = {width: 375, height: 812};
page.settings.resourceTimeout = 180000;
page.onResourceTimeout = function (request) {
    console.log('onResourceTimeout');
    phantom.exit();
};

//加载资源监听
page.onResourceReceived = function (res) {
    console.log('Response (#' + res.id + ', stage =' + res.stage + ', code = ' + res.status + ', redirect= ' + res.redirectURL + ') ');

    setTimeout(function() {
        page.clipRect = {top: 0, left: 0, width: 375, height: 812};
        page.render("demo"+index+".png");
    },100);
    

    index++;
    if (index >= 200) {
        console.log('phantom.exit()');
        phantom.exit();
    }
};
//日志监听console,防止有些内部方法控制台不显示
page.onConsoleMessage = function (msg) {
    console.log('Page console is ' + msg);
};
//用缓存预加载图片,以获取尺寸
var img = new Image();
img.src = address;
var imageHeight = 0;
var imageWidth = 0;
img.onload = function () {
    console.log("img is loaded")
    imageHeight = img.height;
    imageWidth = img.width;
    console.log("img:width= " + img.width + " ,height:" + img.height);
    addWatermark();

};
//error时,url 为网页资源,或加载错误
img.onerror = function () {
    console.log("url load error or url is htmlResource!")
    addWatermark();
};

function addWatermark() {
    page.open(address, function (status) {
        if (status !== 'success') {
            console.log('Unable to load!');
            phantom.exit();
        } else {
            //运行js代码
            page.evaluate(function (imageWidth, imageHeight, watermark) {
                var html = document.body.innerHTML;
                if (imageWidth != 0 && imageHeight != 0) {
                //水印最小宽度为200
                    if (imageWidth < 200) {
                        imageWidth = 200
                    }
                    console.log("--------------------image:=", imageWidth, imageHeight);
                    //编辑抓取的HTML,此处打了一个假水印,放在了图片外边,因为图片过小时,再把水印放里面即便是透明,也看不清了
                    html = "
+ imageWidth + "\">" + watermark + "
"
+ html; } else { html = "
" + watermark + "
"
+ html; } document.body.innerHTML = html; }, imageWidth, imageHeight, watermark); window.setTimeout(function () { //水印宽度适应图片和资源,最小为300 //简单截图示例,保存路径默认为当前脚本所在目录 phantom.exit(); }, 60000); } }); }

你可能感兴趣的:(FFMPEG,phantomjs,FFMPEG)