kkroening/ffmpeg-python

broken pipe when I use stdin.write, it broke

459737087 opened this issue · 0 comments

import cv2
import time
import datetime
import ffmpeg

rtsp_url = 'rtsp_addr'

output_filename = 'output.m3u8'

fps = 5

cap = cv2.VideoCapture(rtsp_url)

width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))


hls = (
    ffmpeg
    .input('pipe:', format='h264', r=fps, s='300x400')
    .output(output_filename, format='hls', hls_time=1, hls_list_size=0)
    .overwrite_output()
    .run_async(pipe_stdin=True)
)

while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
        break

    timestamp = time.time()
    date_time = datetime.datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S.%f')

    hls.stdin.write(frame.tobytes())
    hls.stdin.write(f'{date_time}\n'.encode())


cap.release()
cv2.destroyAllWindows()
hls.stdin.close()
hls.wait()