# Stream video from OpenCV into OBS



## zappso (Mar 4, 2019)

Hi,

Can anyone help me with streaming video from OpenCV (using Python, or C if necessary) into OBS?

I've had a look at the API docs but they're rather bewildering to a new OBS user, and I can't find any similar examples. I think I need to use obs_source_draw() using the frame data I have but I'm not sure how.

Here's some example Python code that captures the camera in OpenCV, displays it and writes the video out to a file. Frame data is in the "frame" variable, I'd like to have this as a custom source:


```
import cv2
import numpy as np

cap = cv2.VideoCapture(0)

ret, frame = cap.read()
h, w, c = frame.shape
print(h, w, c)

fourcc = cv2.VideoWriter_fourcc('m','p','4','v')
out = cv2.VideoWriter('output.mp4', fourcc, 15.0, (w, h))

while(True):
    ret, frame = cap.read()

    out.write(frame)

    frame = cv2.resize(frame, (0,0), fx=0.5, fy=0.5)
    cv2.imshow("Frame", frame)

    ch = cv2.waitKey(1)
    if ch & 0xFF == ord('q'):
        break

cap.release()
out.release()
cv2.destroyAllWindows()
```

Above code is just a simple example. My code is actually more complex than this; I'm doing object recognition and tracking, etc.

Any help would be greatly appreciated!

Many thanks,
Zappso


----------



## caio.jb91 (Sep 8, 2019)

Hi, were you able to do it? I am trying to do a similar thing.


----------



## zappso (Sep 9, 2019)

No. Abandoned OBS and piped output from my OpenCV script into ffmpeg with appropriate arguments for streaming to YT.


----------



## KennethKJ (Mar 7, 2020)

Hi Zappso,

I am trying to do something similar (live camera -> OpenCV -> processing -> YouTube Live streaming). Would you mind sharing your solution using opencv, ffmpeg, and YT? 

Thanks :)


----------



## vbfd (Mar 28, 2020)

Also interested in this. 
Can you achieve realtime performance (sub 1s delay) when using obs + opencv?


----------



## Nishanth in IDE (May 17, 2021)

KennethKJ said:


> Hi Zappso,
> 
> I am trying to do something similar (live camera -> OpenCV -> processing -> YouTube Live streaming). Would you mind sharing your solution using opencv, ffmpeg, and YT?
> 
> Thanks :)


I've made this simple live stream ingestor(RTMP), processing and broadcasting to youtube (RTMP). to minimize/optimize delay an NGINX RTMP server can be used for things like stream authentication, rebroadcasting to multiple endpoints and basic load balancing.








						GitHub - nkottapalli/rtmpstreamer
					

Contribute to nkottapalli/rtmpstreamer development by creating an account on GitHub.




					github.com


----------



## Nishanth in IDE (May 17, 2021)

An example of what you can do with the above framework:


----------

