Skip to content

Instantly share code, notes, and snippets.

@takidog
Created October 19, 2019 18:21
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save takidog/2c981c34d5d5b41c0d712f8ef4ac60d3 to your computer and use it in GitHub Desktop.
Save takidog/2c981c34d5d5b41c0d712f8ef4ac60d3 to your computer and use it in GitHub Desktop.
push RTSP stream with ffmpeg by python
I have not found information on the Internet to implement the python push RTSP stream, so I try it.
I think RTMP can get more way to make it.
:)
import cv2
import subprocess as sp
if __name__ == "__main__":
rtsp_server = 'rtsp://example.org:554/...' # push server (output server)
#pull rtsp data, or your cv cap. (input server)
cap = cv2.VideoCapture(
'rtsp://example.org:554/pull from me ')
sizeStr = str(int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))) + \
'x' + str(int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
fps = int(cap.get(cv2.CAP_PROP_FPS))
command = ['ffmpeg',
'-re',
'-s', sizeStr,
'-r', str(fps), # rtsp fps (from input server)
'-i', '-',
# You can change ffmpeg parameter after this item.
'-pix_fmt', 'yuv420p',
'-r', '30', # output fps
'-g', '50',
'-c:v', 'libx264',
'-b:v', '2M',
'-bufsize', '64M',
'-maxrate', "4M",
'-preset', 'veryfast',
'-rtsp_transport', 'tcp',
'-segment_times', '5',
'-f', 'rtsp',
rtsp_server]
process = sp.Popen(command, stdin=sp.PIPE)
while(cap.isOpened()):
ret, frame = cap.read()
ret2, frame2 = cv2.imencode('.png', frame)
process.stdin.write(frame2.tobytes())
@JafTan21
Copy link

JafTan21 commented Aug 5, 2021

what is the error??

ffmpeg version 2021-08-04-git-3b298640e1-full_build-www.gyan.dev Copyright (c) 2000-2021 the FFmpeg developers
built with gcc 10.3.0 (Rev5, Built by MSYS2 project)
configuration: --enable-gpl --enable-version3 --enable-static --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-lzma --enable-libsnappy --enable-zlib --enable-librist --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-libbluray --enable-libcaca --enable-sdl2 --enable-libdav1d --enable-libzvbi --enable-librav1e --enable-libsvtav1 --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-libaom --enable-libopenjpeg --enable-libvpx --enable-libass --enable-frei0r --enable-libfreetype --enable-libfribidi --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-ffnvcodec --enable-nvdec --enable-nvenc --enable-d3d11va --enable-dxva2 --enable-libmfx --enable-libglslang --enable-vulkan --enable-opencl --enable-libcdio --enable-libgme --enable-libmodplug --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libshine --enable-libtheora --enable-libtwolame --enable-libvo-amrwbenc --enable-libilbc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-ladspa --enable-libbs2b --enable-libflite --enable-libmysofa --enable-librubberband --enable-libsoxr --enable-chromaprint
libavutil 57. 3.100 / 57. 3.100
libavcodec 59. 4.100 / 59. 4.100
libavformat 59. 4.101 / 59. 4.101
libavdevice 59. 0.100 / 59. 0.100
libavfilter 8. 1.103 / 8. 1.103
libswscale 6. 0.100 / 6. 0.100
libswresample 4. 0.100 / 4. 0.100
libpostproc 56. 0.100 / 56. 0.100
Input #0, png_pipe, from 'pipe:':
Duration: N/A, bitrate: N/A
Stream #0:0: Video: png, rgb24(pc), 240x160, 25 fps, 25 tbr, 25 tbn
Stream mapping:
Stream #0:0 -> #0:0 (png (native) -> h264 (libx264))
[libx264 @ 0000026773ecab00] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
[libx264 @ 0000026773ecab00] profile High, level 4.1, 4:2:0, 8-bit
[libx264 @ 0000026773ecab00] 264 - core 164 r3065 ae03d92 - H.264/MPEG-4 AVC codec - Copyleft 2003-2021 - http://www.videolan.org/x264.html - options: cabac=1 ref=1 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=2 psy=1 psy_rd=1.00:0.00 mixed_ref=0 me_range=16 chroma_me=1 trellis=0 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=0 threads=5 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=1 keyint=50 keyint_min=5 scenecut=40 intra_refresh=0 rc_lookahead=10 rc=abr mbtree=1 bitrate=2000 ratetol=1.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 vbv_maxrate=4000 vbv_bufsize=64000 nal_hrd=none filler=0 ip_ratio=1.40 aq=1:1.00

@heavenThunder
Copy link

Hi,
It seems you have to pass a source video as argument in line 7.
You can specify a device as follow:
cap = cv2.VideoCapture(0) #This is for devices like webcams
Or
cap = cv2.VideoCapture("~/videos/stream.mp4")

@takidog
Copy link
Author

takidog commented Aug 5, 2021

Hi,
It seems you have to pass a source video as argument in line 7.
You can specify a device as follow:
cap = cv2.VideoCapture(0) #This is for devices like webcams
Or
cap = cv2.VideoCapture("~/videos/stream.mp4")

Yes, you can use any source to push RTSP stream.

This gist just push RTSP stream, any input source is available.

@heavenThunder
Copy link

Hi,
It seems you have to pass a source video as argument in line 7.
You can specify a device as follow:
cap = cv2.VideoCapture(0) #This is for devices like webcams
Or
cap = cv2.VideoCapture("~/videos/stream.mp4")

Yes, you can use any source to push RTSP stream.

This gist just push RTSP stream, any input source is available.

Is there any documentation about using ffmpeg command in the format you posted above? For example, for changing the size video to a minor quality

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment