This is what I'm trying to do:
(1) Read rtmp stream with ffmpeg(python subprocess) and piping to rawvideo
(2) processing rawvideo pipe from (1) to opencv readable image using numpy
(3) process the image from (2) using opencv for whatever application
(4) Display or (Stream)to processed image from (3)
This is the code I've written:
FFMPEG_BIN = 'ffmpeg'
#SOURCE -> rtmp://yourdomain/rtmpappname/streamkey
SOURCE = 'YOUR RTMP INPUT STREAM HERE URL'
#DESTINATION -> rtmp://yourdomain/rtmpappname/streamkey
DESTINATION = 'YOUR RTMP PROCESSED OUTPUT STREAM URL'
#frame width and height of source stream
FRAME_WIDTH = 1280
FRAME_HEIGHT = 720
import subprocess as sp
#read from rtmp source and output to rawvideo pipe
cmd_video_only_in = [ FFMPEG_BIN, '-i', SOURCE, '-f', 'image2pipe', '-pix_fmt', 'bgr24', '-vcodec', 'rawvideo','-']
video_input_stream = sp.Popen(cmd_video_only_in, stdout=sp.PIPE, bufsize=FRAME_WIDTH*FRAME_HEIGHT*3, shell=False)
#(Optional) stream opencv processed frames to DESTINATION url with audio mapped from source, audio-video sync can be achieved by changing
#the float value 2.00 of -itsoffset to what suits you
#cmd_processed_stream_out = [FFMPEG_BIN,'-y','-thread_queue_size', '1024','-f', 'rawvideo', '-pix_fmt', 'bgr24', '-video_size', str(FRAME_WIDTH)+'X'+str(FRAME_HEIGHT),'-i','-','-itsoffset','2.00','-i', SOURCE,'-map','0:v:0','-map','1:a:0','-vcodec','libx264','-pix_fmt','yuv420p','-acodec','copy','-preset','ultrafast','-f','flv',DESTINATION]
#processed_output_stream = sp.Popen(cmd_processed_stream_out, stdin=sp.PIPE, shell=False)
import cv2
import numpy as np
import time
while (True):
raw_image = video_input_stream.stdout.read(FRAME_WIDTH*FRAME_HEIGHT*3)
image = np.fromstring(raw_image, dtype='uint8')
image = image.reshape((FRAME_HEIGHT,FRAME_WIDTH,3))
#Process the image
#any opencv based image processing can be done here eg. object detection, feature extraction, etc.,
#Display output
cv2.imshow('OUTPUT', image)
#(Optional)send the processed image frames (output) to DESITNATION rtmp url
#processed_output_stream.stdin.write(image.tostring())
video_input_stream.stdout.flush()
if cv2.waitKey(25) & 0xFF == ord('q'):
video_input_stream.terminate()
processed_output_stream.stdin.close()
processed_output_stream.terminate()
break
The output frames which i'm trying to Display (using cv2.imshow) or Stream to rtmp destination are scrolling vertically from bottom to top. How do I solve this vertical scrolling issue?
EDIT1: FULL LOG - streaming output to DESTINATION URL:
$python3 processing.py
ffmpeg version 2.8.11-0ubuntu0.16.04.1 Copyright (c) 2000-2017 the FFmpeg developers
built with gcc 5.4.0 (Ubuntu 5.4.0-6ubuntu1~16.04.4) 20160609
configuration: --prefix=/usr --extra-version=0ubuntu0.16.04.1 --build-suffix=-ffmpeg --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --cc=cc --cxx=g++ --enable-gpl --enable-shared --disable-stripping --disable-decoder=libopenjpeg --disable-decoder=libschroedinger --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmodplug --enable-libmp3lame --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-librtmp --enable-libschroedinger --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxvid --enable-libzvbi --enable-openal --enable-opengl --enable-x11grab --enable-libdc1394 --enable-libiec61883 --enable-libzmq --enable-frei0r --enable-libx264 --enable-libopencv
libavutil 54. 31.100 / 54. 31.100
libavcodec 56. 60.100 / 56. 60.100
libavformat 56. 40.101 / 56. 40.101
libavdevice 56. 4.100 / 56. 4.100
libavfilter 5. 40.101 / 5. 40.101
libavresample 2. 1. 0 / 2. 1. 0
libswscale 3. 1.101 / 3. 1.101
libswresample 1. 2.101 / 1. 2.101
libpostproc 53. 3.100 / 53. 3.100
ffmpeg version 2.8.11-0ubuntu0.16.04.1 Copyright (c) 2000-2017 the FFmpeg developers
built with gcc 5.4.0 (Ubuntu 5.4.0-6ubuntu1~16.04.4) 20160609
configuration: --prefix=/usr --extra-version=0ubuntu0.16.04.1 --build-suffix=-ffmpeg --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --cc=cc --cxx=g++ --enable-gpl --enable-shared --disable-stripping --disable-decoder=libopenjpeg --disable-decoder=libschroedinger --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmodplug --enable-libmp3lame --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-librtmp --enable-libschroedinger --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxvid --enable-libzvbi --enable-openal --enable-opengl --enable-x11grab --enable-libdc1394 --enable-libiec61883 --enable-libzmq --enable-frei0r --enable-libx264 --enable-libopencv
libavutil 54. 31.100 / 54. 31.100
libavcodec 56. 60.100 / 56. 60.100
libavformat 56. 40.101 / 56. 40.101
libavdevice 56. 4.100 / 56. 4.100
libavfilter 5. 40.101 / 5. 40.101
libavresample 2. 1. 0 / 2. 1. 0
libswscale 3. 1.101 / 3. 1.101
libswresample 1. 2.101 / 1. 2.101
libpostproc 53. 3.100 / 53. 3.100
Metadata:
Server NGINX RTMP (github.com/arut/nginx-rtmp-module)
width 1280.00
height 720.00
displayWidth 1280.00
displayHeight 720.00
duration 0.00
framerate 25.00
fps 25.00
videodatarate 0.00
videocodecid 7.00
audiodatarate 0.00
audiocodecid 10.00
Input #0, live_flv, from 'SOURCE URL':
Metadata:
Server : NGINX RTMP (github.com/arut/nginx-rtmp-module)
displayWidth : 1280
displayHeight : 720
fps : 25
profile :
level :
Duration: 00:00:00.00, start: 10435.038000, bitrate: N/A
Stream #0:0: Video: h264 (Main), yuv420p, 1280x720, 25 fps, 25 tbr, 1k tbn, 2k tbc
Stream #0:1: Audio: aac (LC), 44100 Hz, stereo, fltp
Output #0, image2pipe, to 'pipe:':
Metadata:
Server : NGINX RTMP (github.com/arut/nginx-rtmp-module)
displayWidth : 1280
displayHeight : 720
fps : 25
profile :
level :
encoder : Lavf56.40.101
Stream #0:0: Video: rawvideo (BGR[24] / 0x18524742), bgr24, 1280x720, q=2-31, 200 kb/s, 25 fps, 25 tbn, 25 tbc
Metadata:
encoder : Lavc56.60.100 rawvideo
Stream mapping:
Stream #0:0 -> #0:0 (h264 (native) -> rawvideo (native))
Press [q] to stop, [?] for help
Input #0, rawvideo, from 'pipe:':
Duration: N/A, start: 0.000000, bitrate: 552960 kb/s
Stream #0:0: Video: rawvideo (BGR[24] / 0x18524742), bgr24, 1280x720, 552960 kb/s, 25 tbr, 25 tbn, 25 tbc
Metadata:
Server NGINX RTMP (github.com/arut/nginx-rtmp-module)
width 1280.00
height 720.00
displayWidth 1280.00
displayHeight 720.00
duration 0.00
framerate 25.00
fps 25.00
videodatarate 0.00
videocodecid 7.00
audiodatarate 0.00
audiocodecid 10.00
Input #1, live_flv, from 'SOURCE URL':
Metadata:
Server : NGINX RTMP (github.com/arut/nginx-rtmp-module)
displayWidth : 1280
displayHeight : 720
fps : 25
profile :
level :
Duration: 00:00:00.00, start: 10438.591000, bitrate: N/A
Stream #1:0: Video: h264 (Main), yuv420p, 1280x720, 25 fps, 25 tbr, 1k tbn, 2k tbc
Stream #1:1: Audio: aac (LC), 44100 Hz, stereo, fltp
[libx264 @ 0x19c1820] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 AVX2 LZCNT BMI2
[libx264 @ 0x19c1820] profile Constrained Baseline, level 3.1
[libx264 @ 0x19c1820] 264 - core 148 r2643 5c65704 - H.264/MPEG-4 AVC codec - Copyleft 2003-2015 - http://www.videolan.org/x264.html - options: cabac=0 ref=1 deblock=0:0:0 analyse=0:0 me=dia subme=0 psy=1 psy_rd=1.00:0.00 mixed_ref=0 me_range=16 chroma_me=1 trellis=0 8x8dct=0 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=0 threads=12 lookahead_threads=2 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=0 weightp=0 keyint=250 keyint_min=25 scenecut=0 intra_refresh=0 rc=crf mbtree=0 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=0
[flv @ 0x19c0880] Codec for stream 1 does not use global headers but container format requires global headers
Output #0, flv, to 'DESINATION URL':
Metadata:
encoder : Lavf56.40.101
Stream #0:0: Video: h264 (libx264) ([7][0][0][0] / 0x0007), yuv420p, 1280x720, q=-1--1, 25 fps, 1k tbn, 25 tbc
Metadata:
encoder : Lavc56.60.100 libx264
Stream #0:1: Audio: aac ([10][0][0][0] / 0x000A), 44100 Hz, stereo
Stream mapping:
Stream #0:0 -> #0:0 (rawvideo (native) -> h264 (libx264))
Stream #1:1 -> #0:1 (copy)
frame= 3 fps=1.0 q=-0.0 size= 8100kB time=00:00:00.12 bitrate=552960.0kbits/[live_flv @ 0x19b1900] Thread message queue blocking; consider raising the thread_queue_size option (current value: 8)
frame= 45 fps=0.0 q=20.0 size= 427kB time=00:00:01.28 bitrate=2730.9kbits/s
frame= 123 fps= 34 q=-0.0 size= 332100kB time=00:00:04.92 bitrate=552960.0kbits/
frame= 58 fps= 57 q=21.0 size= 608kB time=00:00:01.80 bitrate=2766.4kbits/s
frame= 136 fps= 33 q=-0.0 size= 367200kB time=00:00:05.44 bitrate=552960.0kbits/
frame= 70 fps= 46 q=20.0 size= 737kB time=00:00:02.28 bitrate=2648.5kbits/s
frame= 149 fps= 32 q=-0.0 size= 402300kB time=00:00:05.96 bitrate=552960.0kbits/