Java call ffmpeg command-line push flow encountered problems

Source: Internet
Author: User

1.Java Call command line, if no additional environment variables, do not specify a working path, runtime has two methods

 Public Process exec (String command)  Public Process exec (String cmdarray[])

FFmpeg push stream Local video command as follows


Ffmpeg-re-i test.mp4 <param> "<url> socks=***"//Agent

If you have an agent with exec (String command), even if you enclose the double quotation marks, it will not be successful, and the <url> part will be used as the input video file location. It is recommended to use the Exec (String cmdarray[]) method.
When using EXEC (String cmdarray[]), internal processing is when there are spaces in the middle, with double quotation marks at the end, using specific analysis.

2. After invoking the command line, the program may push the stream for a while, then the program blocks, the background process ffmpeg is not dead yet.
Runtime.exec () executes when the JVM produces a child process that establishes three channel links to the JVM: standard input, standard output, standard error.
Java-native systems have a limited buffer pool for standard inputs and outputs, so the error of fast writes to standard output and fast reads from standard input can cause child process deadlocks.
The output stream of the child process, which is the input stream of the JVM. The child process continues to output to the console, and if Java does not empty the input stream in a timely manner, it causes the buffer to be full, causing a deadlock.
The solution is to clear the input stream in a timely manner, and open two threads to read Process.getinputstream () and Process.geterrorstream (). For FFmpeg only need Process.geterrorstream () to read it out.

Process process = Runtime.getruntime (). exec (command);
New BufferedReader (new InputStreamReader (Process.geterrorstream ())); while NULL ) { System. out . println (line); }
Process.waitfor ();

or Processbuilder redirect standard error

Processbuilder Processbuilder =NewProcessbuilder (commands); Processbuilder.redirecterrorstream (true); Process Process=Processbuilder.start ();            Processcontext.getinstance (). addprocess (process); BufferedReader BR=NewBufferedReader (NewInputStreamReader (Process.getinputstream ())); String Line=NULL;  while(line = Br.readline ())! =NULL) {System. out. println (line); }
Process.waitfor ();

3. When testing on a virtual machine, the simulated server is momentarily disconnected (unplug the network cable), the program running on the virtual machine is stuck.
This situation on the normal server is not a problem, only on their own virtual machine problems, unexplained, it is recommended that this case on the physical machine test.

4.ffmpeg pushes local video files to high CPU, each process is about 78-80%.
The CPU is mainly occupied by the image transcoding. If it is a local video and does not need to transcode the image, use the parameter-vcodec copy without transcoding just copy. Will significantly reduce CPU utilization (only about 2%).
If there is a need for transcoding, you can try the parameter-threads 2.

Help:
The return value of the 1.FFMEPG push stream (Process.exitvalue ()), 0 for success, and other failures.
2. Wait for the child process with process.waitfor ().
3.-SS * *:* *:* * Skip the specified length of time, the parameter location is required.

Java call ffmpeg command-line push flow encountered problems

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.