Stream video using ffmpeg on RTMP server

66 views Asked by At

I have a Nginx rtmp server setup on aws and I want to send video streams to that server how can i configure a ffmeg command to run with subprocess module in python and stream my video and audio on the rtmp server.

currently I am using the below code to run it but only getting video cant get audio

import subprocess 
import cv2 
rtmp_url = "rtmp://ip/route"


path = 0 
cap = cv2.VideoCapture(path)

# gather video info to ffmpeg 
fps = int(cap.get(cv2.CAP_PROP_FPS)) 
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) 
height = int(cap.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),
           '-r', str(fps),
           '-i', '-',
           '-c:v', 'libx264',
           '-pix_fmt', 'yuv420p',
           '-preset', 'ultrafast',
           '-f', 'flv',
           rtmp_url]



# using subprocess and pipe to fetch frame data 
p = subprocess.Popen(command, stdin=subprocess.PIPE)


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

    
    p.stdin.write(frame.tobytes())

what more do i need to add to the ffmpeg command so i can capture audio as well.

1

There are 1 answers

1
Joren Verspeurt On

OpenCV doesn't do the audio part. You'd need another library like PyAudio to add that in. Is there a specific reason you're capturing the video with OpenCV and then piping it to ffmpeg? Couldn't you just use ffmpeg directly (which would then also allow you to capture the audio in the same command). Something like:

ffmpeg -f v4l2 -i /dev/video0 -f alsa -ac 1 -i hw:1 -flags +global_header -ar 44100 -ab 16k -s ?x? -vcodec rawvideo -pix_fmt yuv420p [...] -f flv "rtmp://ip/route"

You'll probably want to change/add some of the parameters to fit your case.