I’m having trouble with my Twitch stream using FFMPEG. My Python script generates video frames, but the stream’s bitrate is really low (about 70 KB) even though I set it to 3000K in FFMPEG.
Here’s what I’m doing:
import cv2
import subprocess as sp
stream_key = 'MY_KEY'
video_file = 'input.mp4'
vid = cv2.VideoCapture(video_file)
success, img = vid.read()
h, w, _ = img.shape
fps = vid.get(cv2.CAP_PROP_FPS)
ffmpeg_cmd = [
'ffmpeg',
'-f', 'rawvideo',
'-pix_fmt', 'rgb24',
'-s', f'{w}x{h}',
'-r', str(fps),
'-i', '-',
'-c:v', 'libx264',
'-b:v', '3000k',
'-preset', 'faster',
'-f', 'flv',
f'rtmp://twitch.tv/app/{stream_key}'
]
process = sp.Popen(ffmpeg_cmd, stdin=sp.PIPE)
while True:
success, img = vid.read()
if not success:
break
process.stdin.write(img.tobytes())
vid.release()
process.stdin.close()
process.wait()
Any ideas on how to fix the bitrate? Or maybe there’s a better way to stream to Twitch using Python? Thanks!