Implementation of java operation ffmepg

Posted by gwizz on Mon, 11 Nov 2019 20:22:23 +0100

1. preparation

  ffmpeg

Link: https://pan.baidu.com/s/1oh qfxnlw5kmdf8f5etq
Extraction code: rsdn
It's more convenient to open Baidu online mobile App after copying this content

2. View live video through ffmepg

 public void run() {
	      ProcessBuilder builder = null;
	      List commend = new ArrayList();
	      commend.add(this.item.getOsName());
	      //Read data at local frame rate, mainly used for analog capture equipment
	      commend.add("-re");
	      commend.add("-rtsp_transport");
	      commend.add("tcp");
	      //When parsing the wrong network flow, the function will not return for a long time. The timeout is set to 5 seconds
	      commend.add("-stimeout");
	      commend.add("5000000");
	      
	      //Enter the path of the video file you want to process
	      commend.add("-i");
	      commend.add("rstpurl address");
	      commend.add("-buffer_size");
	      commend.add("1024000000");
	      //Set maximum delay
	      commend.add("-max_delay");
	      commend.add("50000000");
	      commend.add("-codec:v");
	      commend.add("libx264");
	      //Specify screen capture frequency
	      commend.add("-r");
	      commend.add("25");
	      commend.add("-rtbufsize");
	      commend.add("10M");
	      commend.add("-s");
	      // Specify resolution
	      commend.add("1024*576");
	      commend.add("-map:v");
	      commend.add("0");
	      //Disable audio recording
	      commend.add("-an");
	      
	      //Forced hls
	      commend.add("-f");
	      commend.add("hls");
	      //HLS playlists
	      commend.add("-hls_list_size");
	      commend.add("6");
	      //Represents the maximum number of TS cycles, i.e. every 10 cycles
	      commend.add("-hls_wrap");
	      commend.add("10");
	      //Length of each piece
	      commend.add("-hls_time");
	      commend.add("10");
	      commend.add("-loglevel");
	      commend.add("8");
	      commend.add("D:/project/direct/40/camera.m3u8");

	      try {
	         builder = new ProcessBuilder(commend);
	         builder.command(commend);
	         Process p = builder.start();
	         p.getOutputStream().close();
	         doWaitFor(p);
	         p.destroy();
	         builder = null;
	         this.doBusiness();
	      } catch (Exception var7) {
	         PrintCatchErrorMsg.Print(var7, "Direct", "run catch", "Exception");
	         this.doBusiness();
	      } finally {
	         commend = null;
	      }

	   }
 public static int doWaitFor(Process p) {
      InputStream in = null;
      InputStream err = null;
      int exitValue = -1;

      try {
         in = p.getInputStream();
         err = p.getErrorStream();
         boolean finished = false;

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

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

               exitValue = p.exitValue();
               finished = true;
            } catch (IllegalThreadStateException var19) {
               Thread.currentThread();
               Thread.sleep(500L);
            }
         }
      } catch (Exception var20) {
      } finally {
         try {
            if (in != null) {
               in.close();
            }
         } catch (IOException var18) {
         }

         if (err != null) {
            try {
               err.close();
            } catch (IOException var17) {
            }
         }

      }

      return exitValue;
   }

The effect is as follows:

Conclusion:

The video screen can be displayed in the above way. The video screen is very clear, and there is no frame dropping problem. It is also very problematic

Problems:

Video delay is about 30s, video cpu cost is large, video analysis is carried out on the ts slice of video, and the size of slice is also considered. The official website says it's recommended about 4s. It's found that there is little B frame, and it's useless to optimize. If there is any good suggestion, please leave a message below or add a group.

Front end reference https://blog.csdn.net/qq_16855077/article/details/89839708 4 front end code part. Note that because the file is local, there will be cross domain problems if other ip direct access. For convenience, I configure cross domain code in nginx, and configure the proxy mapping to local configuration.

Click the link to join the group chat [java exchange group]: https://jq.qq.com/? ﹣ WV = 1027 & K = 5vzxg3k

 

Topics: Mobile network codec Nginx