The new Java project studies online notes -day14 (b)

 

1.2.2 video processing solutions 
how to video processed by the program?
ff mpeg video processing is a viable program that can handle video calls ff mpeg.exe completed by Java.
In java you can use Runtime Process Builder classes and class are two ways to execute an external program, work to master at least one. This project uses Process Builder way to invoke ff mpeg video processing is completed.
Test on Process Builder is as follows:

[AppleScript]  plain text view  Copy the code

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

@Test  

  public void testProcessBuilder(){   

     ProcessBuilder processBuilder = new ProcessBuilder(); //  

    processBuilder.command("ping","127.0.0.1");  

     processBuilder.command("ipconfig");   

     //将标准输入流和错误输入流合并,通过标准输入流读取信息 

       processBuilder.redirectErrorStream(true);   

     try {       

     //启动进程       

     Process start = processBuilder.start();     

       //获取输入流    

        InputStream inputStream = start.getInputStream();  

         //转成字符输入流       

     InputStreamReader inputStreamReader = new InputStreamReader(inputStream,"gbk");    

          int len = 1;    

        char[] c = new char[1024];      

      StringBuffer outputString = new StringBuffer();   

         //读取进程输入流中的内容      

      while ((len= inputStreamReader.read(c))!=1) {       

         String s = new String(c,0,len);       

         outputString.append(s);    

            System.out.print(s);     

       }        

    inputStream.close();  

      } catch (IOException e) { 

           e.printStackTrace();  

      }

    }    

@Test  

  public void testFFmpeg(){  

      ProcessBuilder processBuilder = new ProcessBuilder();      

  //定义命令内容    

    List<String> command = new ArrayList<>();   

     command.add("D:\\Program Files\\ffmpeg‐20180227‐fa0c9d6‐win64‐static\\bin\\ffmpeg.exe");  

      command.add("‐i");     

   command.add("E:\\ffmpeg_test\\1.avi");   

     command.add("‐y");//覆盖输出文件  

      command.add("‐c:v");

        command.add("libx264");

command.add("‐s");    

    command.add("1280x720");   

     command.add("‐pix_fmt");   

     command.add("yuv420p");  

      command.add("‐b:a");   

     command.add("63k");    

    command.add("‐b:v");    

    command.add("753k");    

    command.add("‐r");   

     command.add("18");    

    command.add("E:\\ffmpeg_test\\1.mp4");  

      processBuilder.command(command);   

     //将标准输入流和错误输入流合并,通过标准输入流读取信息 

       processBuilder.redirectErrorStream(true);   

     try {     

       //启动进程  

          Process start = processBuilder.start();  

          //获取输入流      

      InputStream inputStream = start.getInputStream();    

        //转成字符输入流       

     InputStreamReader inputStreamReader = new InputStreamReader(inputStream,"gbk");    

        int len = 1;      

      char[] c = new char[1024];  

          StringBuffer outputString = new StringBuffer();   

         //读取进程输入流中的内容       

     while ((len= inputStreamReader.read(c))!=1) {    

            String s = new String(c,0,len);           

     outputString.append(s);    

            System.out.print(s);       

     }       

     inputStream.close();  

      } catch (IOException e) {    

        e.printStackTrace();   

     }   

 }


Code above has been packaged into tools, see:

 
upper tool class:
Mp4VideoUtil.java complete revolutions avi mp4

HlsVideoUtil.java complete turn hls mp4
were tested for each use of the tools.
 

[AppleScript]  plain text view  Copy the code

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

public static void main(String[] args) throws IOException {  

  //ffmpeg的路径   

 String ffmpeg_path = "D:\\Program Files\\ffmpeg‐20180227‐fa0c9d6‐win64static\\bin\\ffmpeg.exe";//ffmpeg的安装位置   

 //源avi视频的路径 

   String video_path = "E:\\ffmpeg_test\\1.avi"

   //转换后mp4文件的名称  

  String mp4_name = "1.mp4";   

 //转换后mp4文件的路径 

   String mp4_path = "E:\\ffmpeg_test\\";  

  //创建工具类对象    

Mp4VideoUtil videoUtil = new Mp4VideoUtil(ffmpeg_path,video_path,mp4_name,mp4_path);  

  //开始视频转换,成功将返回success 

   String s = videoUtil.generateMp4();  

  System.out.println(s); }

Guess you like

Origin blog.csdn.net/czbkzmj/article/details/91802302