I'm trying to write video frames to an RTMP stream using FFMPEG and Python subsystem. The code will try to get videos in a 'ReceivedRecording' then it is stream to a RTMP streaming server using nginx. My method seems to work, but at times, the code will stop running due to
[flv @ 0x55b933694b40] Failed to update header with correct duration.
[flv @ 0x55b933694b40] Failed to update header with correct filesize.
and
Conversion failed
then
BrokenPipeError: [Errno 32] Broken pipe
Here my implementation of the task:
import subprocess
import cv2
rtmp_url = "rtmp://..."
path = 'ReceivedRecording'
received_video_path = 'ReceivedRecording'
while True:
    video_files = [filenames for filenames in sorted(
        os.listdir(received_video_path))]
    # Loop through the videos and concatenate them
    for filename in video_files[:len(video_files)-1]:
        video = cv2.VideoCapture(os.path.join(received_video_path, filename))
        if p is None:
            fps = int(video.get(cv2.CAP_PROP_FPS))
            width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
            height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
            # command and params for ffmpeg
            command = ['ffmpeg',
                        '-y',
                        '-f', 'rawvideo',
                        '-vcodec', 'rawvideo',
                        '-pix_fmt', 'bgr24',
                        '-s', "{}x{}".format(width, height),
                        '-re',
                        '-r', '5',
                        '-i', '-',
                        # '-filter:v', 'setpts=4.0*PTS',
                        '-c:v', 'libx264',
                        '-pix_fmt', 'yuv420p',
                        '-preset', 'ultrafast',
                        '-tune','zerolatency',
                        '-vsync','vfr',
                        # '-crf','23',
                        '-f', 'flv',
                        rtmp_url]
            
            # using subprocess and pipe to fetch frame data
            p = subprocess.Popen(command, stdin=subprocess.PIPE)
        else:
            # Loop through the frames of each video
            while True:
                start_time = time.time()
                ret, frame = video.read()
                if not ret:
                    # End of video, move to next video
                    video.release()
                    break
                p.stdin.write(frame.tobytes())
            os.remove(os.path.join(received_video_path, filename))
Here is my nginx rtmp settings:
rtmp {
    server {
        listen 1935;
        chunk_size 7096;
        application live {
            live on;
            record off;
            push rtmp://...;
        }
    }
}
Here is the log file:
av_interleaved_write_frame(): Connection reset by peer
No more output streams to write to, finishing.
[flv @ 0x5561d1ca9b40] Failed to update header with correct duration.
[flv @ 0x5561d1ca9b40] Failed to update header with correct filesize.
Error writing trailer of rtmp://...: Connection reset by peer
frame=    1 fps=0.0 q=20.0 Lsize=    1024kB time=00:00:00.00 bitrate=8390776.0kbits/s speed=0.00619x
video:1053kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
Input file #0 (pipe:):
  Input stream #0:0 (video): 1 packets read (11059200 bytes); 1 frames decoded;
  Total: 1 packets (11059200 bytes) demuxed
Output file #0 (rtmp://...):
  Output stream #0:0 (video): 1 frames encoded; 1 packets muxed (1077799 bytes);
  Total: 1 packets (1077799 bytes) muxed
1 frames successfully decoded, 0 decoding errors
[AVIOContext @ 0x5561d1cad380] Statistics: 0 seeks, 35 writeouts
[rtmp @ 0x5561d1cb7b80] Deleting stream...
[libx264 @ 0x5561d1caae40] frame I:1     Avg QP:20.00  size:1077192
[libx264 @ 0x5561d1caae40] mb I  I16..4: 100.0%  0.0%  0.0%
[libx264 @ 0x5561d1caae40] coded y,uvDC,uvAC intra: 92.2% 50.5% 10.2%
[libx264 @ 0x5561d1caae40] i16 v,h,dc,p: 34% 16% 37% 12%
[libx264 @ 0x5561d1caae40] i8c dc,h,v,p: 35% 23% 33%  9%
[libx264 @ 0x5561d1caae40] kb/s:43087.68
[AVIOContext @ 0x5561d1ca6a80] Statistics: 11059200 bytes read, 0 seeks
Conversion failed!