Solve the problem of FFmpeg losing video stream and high frame rate

My solution:

Set up a pull process, a pull queue, a push process, and a push queue respectively.

And queue sharing between processes

Quote from this article: Solve the problem of FFmpeg losing video stream and high frame rate - Codeprj.com

Note: Cold posture backup, prevent deletion, do not read

0x001: Preface


I encountered two problems while testing the program today.
Q1: When ffmpeg records an RTMP stream and saves it as an FLV file, the thread pushing the RTMP stream has ended and been disconnected, but the recording thread is still recording.
Q2: When ffmpeg records an RTMP stream and saves it as an FLV file, the recorded RTMP stream is not pushed or does not have this room channel, but the recording thread is still recording.

Observing the log file, we found that the recording program has been blocked at a certain point in time. After checking, this is because when ffmpeg pulls the RTMP stream, as long as the streaming media server can be accessed normally, it will not exit regardless of whether the currently pulled stream can be accessed normally.
For example: rtmp://192.168.1.100:1935/live/changfang, as long as the server address 192.168.1.100:1935 can be requested normally, ffmpeg will block it regardless of whether the current changfang channel is pushing streams. When you take down the entire server, you will find that the ffmpeg recording program will exit immediately and no longer block. The first idea at this point is to look for the command parameters that set the timeout.

0x002: Solution


When encountering a problem, I searched to see if anyone had encountered a similar situation before. After searching, it was basically how to solve the problem with C++ code, but there were no results related to the ffmpeg command. So I checked the command help, looked for the word timeout, and looked for many parameters. After many attempts, I found that there is  -rw_timeout.

 
 

This is the official description: -rw_timeout <int64> ED..... Timeout for IO operations (in microseconds) (from 0 to I64_MAX) (default 0)


Reading streams is also an IO operation, but two points should be noted here: 1. The parameter unit is microseconds, not seconds. 1 second (s) = 1000000 microseconds (μs) 2. The parameters must be placed before opening the flow, otherwise they will not take effect.

Commands before the change (for the convenience of analysis and viewing, only the most basic commands are left):

ffmpeg -i rtmp://192.168.1.100:1935/live/changfang -c copy -f flv /opt/video/123.flv

Changed command:

ffmpeg -rw_timeout 5000000 -i rtmp://192.168.1.100:1935/live/changfang -c copy -f flv /opt/video/123.flv


In this way, no matter whether the stream push ends or the stream has never been pushed, as long as ffmpeg is opening the stream and the IO operation exceeds the set number of seconds, it will automatically exit. (If the stream you read is an external network stream or the internal network stream is not smooth, it is not recommended that you set this time too short, because it takes time for ffmpeg to start the stream and generate the FLV recording file during recording)

Guess you like

Origin blog.csdn.net/qq_55542491/article/details/130418891