Authorization statement: This article authorizes the official Amazon Cloud Technology article to forward and rewrite the rights, including but not limited to Amazon Cloud Technology official channels such as Developer Centre, Zhihu, self-media platforms, third-party developer media, etc.
This article is based on the following software and hardware tools:
+awsec2
+ frp-0.52.3
+ mediamtx-1.3.0
+ ffmpeg-5.1.4
+ opencv-4.7.0
0. Environment
aws ec2 one
local ubuntu18
Local win10 + Firefox browser
1. aws ec2 preparation
1.1 Console
In this step we create a server at low cost.
On the console home page, select Ireland eu-west-1 for the region.
Create server
Search bar input -> EC2
-> Start instance ->
-> Quick Start: Amazon Linux aws,
->Instance type: Select the eligible free plan, t2.micro
->Key pair: Create a key pair
-> Name: zzzzzzzzzzzzzzzzzzzz2#, RSA, .pem, then save to local. Required for ssh login.
->Create instance
Got the server IP: 54.229.195.3
1.2 Connect to an instance
1.2.1 Console login
Click on the instance ID: i-043213d6d1d2c8752
-> Click Connect -> Connect
1.2.2 ssh
Using MobaXterm as an example
Open MobaXterm -> Session -> SSH
-> Remote host: 54.229.195.3
-> Check Specify username: ec2-user
->Avanced SSH settings
-> Check Use private key
1.3 Deploy frp test
1.3.1 Download executable file
arrive
https://github.com/fatedier/frp/releases
download
The latest version is 0.52.3
wget https://github.com/fatedier/frp/releases/download/v0.52.3/frp_0.52.3_linux_amd64.tar.gz
1.3.2 Unzip
tar -zvxf frp_0.52.3_linux_amd64.tar.gz
1.3.3 Switch directory
cd frp_0.52.3_linux_amd64
1.3.4 Modify configuration
vim frps.toml
Modify to the following:
bindPort = 30000 auth.method = "token" auth.token = "520101" webServer.addr = "0.0.0.0" webServer.port = 30001 webServer.user = "admin" webServer.password = "jian@123"
run
./frps -c frps.toml & amp;
1.3.5 Server open port
Through web configuration, enable custom TCP, 30000, 30001, 30002, 30003
1.3.6 Run
./frps -c frps.ini & amp;
1.3.7 Test web
Open the browser 54.229.195.3:30001
login information:
admin
admin@123
2. Local ubuntu18 deployment frp
2.1 Download executable file
arrive
https://github.com/fatedier/frp/releases
download
The latest version is 0.52.3
wget https://github.com/fatedier/frp/releases/download/v0.52.3/frp_0.52.3_linux_amd64.tar.gz
2.2 Unzip
tar -zvxf frp_0.52.3_linux_amd64.tar.gz
2.3 Switch directory
cd frp_0.52.3_linux_amd64
2.4 Modify configuration
vim frpc.ini
Modify to the following:
serverAddr = "54.229.195.3" serverPort = 30000 auth.method = "token" auth.token = "520101" [[proxies]] name = "rtsp1" type = "tcp" localIP = "127.0.0.1" localPort = 8554 remotePort = 30002
2.5 Run
./frpc -c frpc.toml & amp;
3. Local ubuntu18 deployment mediamtx
Get executable file
https://github.com/bluenviron/mediamtx/releases
Downloaded mediamtx_v1.2.1_linux_amd64.tar.gz
run:
tar -zvxf mediamtx_v1.2.1_linux_amd64.tar.gz cd mediamtx ./mediamtx
4. Local ubuntu18 deployment ffmpeg
4.1 Install dependencies
4.1.1 Compilation dependencies
sudo apt-get update -qq & amp; & amp; sudo apt-get -y install \ autoconf\ automake\ build-essential\ cmake\ git-core \ libass-dev \ libfreetype6-dev \ libgnutls28-dev \ libmp3lame-dev \ libsdl2-dev \ libtool\ libva-dev \ libvdpau-dev\ libvorbis-dev\ libxcb1-dev \ libxcb-shm0-dev \ libxcb-xfixes0-dev \ meson \ ninja-build \ pkg-config\ texinfo\ wget\ yasm\ zlib1g-dev
4.1.2 Plug-in dependencies
sudo apt-get install -y nasm & amp; & amp; \ sudo apt-get install -y libx264-dev & amp; & amp; \ sudo apt-get install -y libx265-dev libnuma-dev & amp; & amp; \ sudo apt-get install -y libvpx-dev & amp; & amp; \ sudo apt-get install -y libfdk-aac-dev & amp; & amp; \ sudo apt-get install -y libopus-dev
4.2 Compilation
4.2.1 Obtain source code
https://ffmpeg.org/download.html#releasesAccording to reference [3], we got “Considering that opencv4.7.0 starts to support ffmpeg5.x version, we downloaded ffmpeg 5.1 version”
download
wget https://ffmpeg.org/releases/ffmpeg-5.1.4.tar.gz tar -zvxf ffmpeg-5.1.4.tar.gz
4.2.2 Configuration
./configure \ --prefix="$HOME/work/ffmpeg/install" \ --extra-libs="-lpthread -lm" \ --ld="g + + " \ --enable-gpl \ --enable-gnutls \ --enable-libass \ --enable-libfdk-aac \ --enable-libfreetype \ --enable-libmp3lame \ --enable-libopus \ --enable-libvorbis \ --enable-libvpx \ --enable-libx264 \ --enable-libx265 \ --enable-nonfree
Compared with the official compilation tutorial, the libsvtav1, libdav1d, and libaom options have been removed.
4.3 Compilation
make -j8
4.4 Installation
make install
4.5 Test
4.5.1 ffmpeg push streaming
#Set environment variables. You can set them after compilation and installation. You can also follow the path rules below to replace the root directory of the installation. The main reason is that it is not a step-by-step compilation method. All this process is not rigorous.
gedit ~/.bashrc
#Add at the end
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/xxjianvm/work/ffmpeg/install/lib/ export PATH=$PATH:/home/xxjianvm/work/ffmpeg/install/bin/
#Activate environment variables
source ~/.bashrc
Download test video
wget http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4
Loop push
ffmpeg -re -stream_loop -1 -i big_buck_bunny.mp4 -c copy -f rtsp rtsp://127.0.0.1:8554/stream
4.5.2 ffplay playback
ffplay rtsp://127.0.0.1:8554/stream
4.5.3 ffplay tested by Amazon video streaming
ffplay rtsp://54.229.195.3:30002/stream
5. opencv
5.1 Prepare source code
https://opencv.org/releases/
downloaded
opencv-4.7.0.tar.gz
Place it in ~/work/opencv
Unzip
cd ~/work/opencv tar -zvxf opencv-4.7.0.tar.gz
5.2 Installation dependencies Required Packages
# compiler ? $ sudo apt-get install build-essential #required? $ sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev #optional? $ sudo apt-get install python3-dev python3-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
5.2.1 error : E: Unable to locate package libjasper-dev
Problem: aliyun’s ubuntu source cannot find libjasper-dev
Solution:
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main" sudo apt update
#5.2.2 Install numpy (skip)
#Since the python3-numpy above is python3.6, install it again with pip3.7
#python3 -m pip install numpy
#
#$ sudo apt-get install -y libgstreamer-plugins-base1.0-dev \
# libpng16-16 \
# build-essential \
# cmake \
#git\
# pkg-config \
# libjpeg-dev \
# libgtk2.0-dev \
# libv4l-dev \
# libatlas-base-dev \
# gfortran \
# libhdf5-dev \
# libtiff5-dev \
# libtbb-dev \
#libeigen3-dev
5.3 Compile and install
5.3.1 Compile command
$ cd opencv-4.7.0/ $ mkdir build & amp; & amp; cd build/ $ cmake -DCMAKE_BUILD_TYPE=RELEASE \ -DCMAKE_INSTALL_PREFIX=/home/xxjian/work/opencv/install/ \ -DPYTHON_DEFAULT_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)") \ -DPYTHON3_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)") \ -DPYTHON3_NUMPY_INCLUDE_DIRS=$(python3 -c "import numpy; print (numpy.get_include())") \ -DPYTHON3_PACKAGES_PATH=$(python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") \ \ -DBUILD_DOCS=OFF \ -DBUILD_EXAMPLES=OFF \ -DBUILD_TESTS=OFF \ -DBUILD_PERF_TESTS=OFF \ -DFFMPEG_DIR=/home/xxjianvm/work/ffmpeg/install \ ..
5.3.2 Compilation
$ make -j8
5.3.3 Installation
$ sudo make install
5.4 Verification
$ python3 import cv2 cv2.__version__
6. Run the script based on python-opencv for pull processing and then push flow
6.1 pyopencv_ffmpeg.py
Create pyopencv_ffmpeg.py and add the following content:
import cv2 import subprocess import time import numpy as np '''Pull the stream url address, specify where to pull the stream''' # video_capture = cv2.VideoCapture(0, cv2.CAP_DSHOW) # Own camera pull_url = 'rtsp://127.0.0.1:8554/stream' # "rtsp_address" video_capture = cv2.VideoCapture(pull_url) # Call the rtsp protocol stream of the camera # pull_url = "rtmp_address" '''Push URL address, specify where to push various processed streams (video frames) using opencv''' push_url = "rtsp://127.0.0.1:8554/stream/ai" width = int(video_capture.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT)) fps = int(video_capture.get(cv2.CAP_PROP_FPS)) # Error setting option framerate to value 0. print("width", width, "height", height, "fps:", fps) # command = [r'D:\Softwares\ffmpeg-5.1-full_build\bin\ffmpeg.exe', # Windows needs to specify the ffmpeg address command = ['ffmpeg', # No need to specify for linux '-y', '-an', '-f', 'rawvideo', '-vcodec','rawvideo', '-pix_fmt', 'bgr24', #Pixel format '-s', "{}x{}".format(width, height), '-r', str(fps), # The fps of your own camera is 0. If you use your own notebook camera, you can set it to 15, 20, or 25. '-i', '-', '-c:v', 'libx264', #Video encoding method '-pix_fmt', 'yuv420p', '-preset', 'ultrafast', '-f', 'rtsp', # flv rtsp '-rtsp_transport', 'tcp', # Use TCP streaming, this line must be included in Linux push_url] # rtsp rtmp pipe = subprocess.Popen(command, shell=False, stdin=subprocess.PIPE) def frame_handler(frame): kernel = np.ones((5,5),np.float32)/25 dst = cv2.filter2D(frame,-1,kernel) return dst process_this_frame = True while True: # True or video_capture.isOpened(): # Grab a single frame of video ret, frame = video_capture.read() # handle the video capture frame start = time.time() frame = frame_handler(frame) # Display the resulting image. Linux requires commenting this line of code # cv2.imshow('Video', frame) # Hit 'q' on the keyboard to quit! if cv2.waitKey(delay=1) & amp; 0xFF == ord('q'): # delay=100ms is 0.1s. If the dealy time is too long, such as 1000ms, the stream will not be pushed successfully! break pipe.stdin.write(frame.tostring()) # pipe.stdin.write(frame.tobytes()) video_capture.release() cv2.destroyAllWindows() pipe.terminate()
6.2 Running tests
python3 pyopencv_ffmpeg.py
7. Observe the effect
Create two consoles and enter the following commands respectively:
ffplay rtsp://54.229.195.3:30002/stream
ffplay rtsp://54.229.195.3:30002/stream/ai
Since the current computer processing power is relatively weak, if too many operations are added to the processing function, the push will be unsuccessful, so the processing power is still a bit weak. In addition, there is no sound when streaming through opencv.
The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. OpenCV skill tree Home page Overview 24098 people are learning the system