1:先去ffmpeg官网下载其压缩包  http://ffmpeg.org/download.html

 

2.将压缩包拷贝到你想的任意位置并解压,正常解压出来的文件名过长,也可能会有空格,我们最好将文件夹下的文件拷贝出来到一个新的文件夹FFmpeg下

 

 

 3.配置环境变量

 

4.查看你的ffmpeg是否安装好,win+r 输入cmd,在里面输入ffmpeg -version,显示如下则安装成功

 

 

 5.在你的项目下新增工具类ProcessExec 一下是工具类里的代码

 1 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 2 import java.io.BufferedReader;
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5 import java.io.InputStreamReader;
 6 import java.util.ArrayList;
 7 import java.util.HashMap;
 8 import java.util.List;
 9 import java.util.Map;
10 import java.util.concurrent.ExecutorService;
11 import java.util.concurrent.Executors;
12 
13 public class ProcessExec {
14 
15     private Process process;
16 
17     public void execute(Map<String,String> dto)
18              {
19                  StringBuffer waterlogo = new StringBuffer();
20                  waterlogo.append("-i ");
21                  if(null!=dto.get("input_path")&&StringUtils.isNotEmpty(dto.get("input_path"))){
22                      waterlogo.append(dto.get("input_path"));
23                  }
24                  waterlogo.append("  -vf \"movie=");
25                  if (null!=dto.get("logo")&&StringUtils.isNotEmpty(dto.get("logo"))){
26                      waterlogo.append(dto.get("logo"));
27                  }
28                  waterlogo.append(",scale= 60: 30");
29                  waterlogo.append(" [watermark]; [in][watermark] overlay=main_w-overlay_w-10:main_h-overlay_h-10 [out]\" ");
30                  if (null!=dto.get("video_converted_path")&&StringUtils.isNotEmpty(dto.get("video_converted_path"))){
31                      waterlogo.append(dto.get("video_converted_path"));
32                  }
33                  System.out.println(waterlogo);
34                  Runtime run = Runtime.getRuntime();
35                  String ffmegPath = null;
36                  if (StringUtils.isNotEmpty(dto.get("ffmpeg_path"))){
37                      ffmegPath = dto.get("ffmpeg_path");
38                  }
39 // 执行命
40                  try {
41                      java.lang.Process process = run.exec(ffmegPath+waterlogo);
42 // 异步读取输出
43                      InputStream inputStream = process.getInputStream();
44                      InputStream errorStream = process.getErrorStream();
45                     /* BufferedReader br=new BufferedReader(new InputStreamReader(inputStream,"gbk"));
46                      String str1="";
47                      while((str=br.readLine())!=null){
48                          System.out.println(str1);
49                      }*/
50 
51                      ExecutorService service = Executors.newFixedThreadPool(2);
52 
53                     ResultStreamHandler inputStreamHandler = new ResultStreamHandler(inputStream);
54                     ResultStreamHandler errorStreamHandler = new ResultStreamHandler(errorStream);
55 
56                     service.execute(inputStreamHandler);
57                     service.execute(errorStreamHandler);
58 
59                     process.waitFor();
60                     service.shutdownNow();
61                  } catch (IOException e) {
62                      e.printStackTrace();
63                  } catch (InterruptedException e) {
64                      e.printStackTrace();
65                  }
66              }
67 
68 
69 }

6.新增控制台输入代码 ResultStreamHandler 

 

 1 import java.io.BufferedReader;
 2 import java.io.IOException;
 3 import java.io.InputStream;
 4 import java.io.InputStreamReader;
 5 
 6 public class ResultStreamHandler
 7         implements Runnable {
 8     private InputStream inputStream;
 9 
10     ResultStreamHandler( InputStream inputStream ) {
11         this.inputStream = inputStream;
12     }
13 
14     public void run() {
15 
16         BufferedReader bufferedReader = null;
17         try {
18             bufferedReader = new BufferedReader( new InputStreamReader( inputStream,"gbk" ) );
19             String line = null;
20 
21             while ( ( line = bufferedReader.readLine() ) != null ) {
22                 System.out.println( line );
23             }
24         }
25         catch ( Throwable t ) {
26         }
27         finally {
28             try {
29                 bufferedReader.close();
30             }
31             catch ( IOException e ) {
32             }
33         }
34     }
35 
36 }

7.调用添加水印工具类代码

import java.util.HashMap;

public class test {

    public static void main(String[] args) {
        ProcessExec ps = new ProcessExec();
        HashMap<String, String> dto=new HashMap<String, String>();
        dto.put("ffmpeg_path","G:\\Program Files\\FFmpeg\\bin\\ffmpeg.exe ");//必填:此处是ffmpeg.exe所在位置,也就FFmpeg文件夹bin目录下的ffmpeg.exe
        dto.put("input_path", "G:\\05如何将牛人身上的技能都榨干?.mp4");//必填;此处是你要处理的视频位置
        dto.put("video_converted_path", "G:\\video\\TEST1.mp4");//必填;此处是完成添加水印后输入视频的位置并重新命名该视频
        dto.put("logo", "D\\\\:/20160512155254687.png");//必填;此处是你要添加的水印位置,注意此处图片位置一定要加上转译符,否则识别不了盘符
        ps.execute(dto);

    }

}

 

 

 

注:工具类源代码部分出自文章https://blog.csdn.net/lzhnightwind/article/details/84689159,本文对此进行了一点小小的改动。

 

版权声明:本文为xisita0706原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/xisita0706/p/12580226.html