基本思想:学习视频编解码,先从环境开始~
其实可以不用编译,因为系统库含有ffmpeg库可以直接跳到ngnix环境搭建~
一、先编译x264源码
ubuntu@ubuntu:~$ git clone https://github.com/mirror/x264
ubuntu@ubuntu:~/x264$ mkdir build
ubuntu@ubuntu:~/x264$./configure --enable-static --prefix=build/
ubuntu@ubuntu:~/x264$make
ubuntu@ubuntu:~/x264$make install
ubuntu@ubuntu:~/x264/build$ tree -l
.
├── bin
│?? └── x264
├── include
│?? ├── x264_config.h
│?? └── x264.h
└── lib
├── libx264.a
└── pkgconfig
└── x264.pc
4 directories, 5 file
二、编译ffmpeg源码
ubuntu@ubuntu:~/FFmpeg$ export PKG_CONFIG_PATH=../x264/build/lib/pkgconfig
ubuntu@ubuntu:~/FFmpeg$ ./configure --prefix=build/ --enable-static --enable-encoder=libx264 --enable-libx264 --enable-nonfree --enable-muxers --enable-decoders --enable-demuxers --enable-parsers --enable-protocols --enable-neon --enable-hwaccels --enable-gpl --extra-cflags="-I../x264/build/include" --extra-ldflags="-L../x264/build/lib"
ubuntu@ubuntu:~/FFmpeg$ make -j8
ubuntu@ubuntu:~/FFmpeg/build$ tree -L 2
.
├── bin
│?? ├── ffmpeg
│?? ├── ffplay
│?? └── ffprobe
├── include
│?? ├── libavcodec
│?? ├── libavdevice
│?? ├── libavfilter
│?? ├── libavformat
│?? ├── libavutil
│?? ├── libswresample
│?? └── libswscale
├── lib
│?? ├── libavcodec.a
│?? ├── libavdevice.a
│?? ├── libavfilter.a
│?? ├── libavformat.a
│?? ├── libavutil.a
│?? ├── libswresample.a
│?? ├── libswscale.a
│?? └── pkgconfig
└── share
├── doc
├── ffmpeg
└── man
15 directories, 10 files
ubuntu@ubuntu:~/FFmpeg/build$ tree -L 2
.
├── bin
│?? ├── ffmpeg
│?? ├── ffplay
│?? └── ffprobe
├── include
│?? ├── libavcodec
│?? ├── libavdevice
│?? ├── libavfilter
│?? ├── libavformat
│?? ├── libavutil
│?? ├── libswresample
│?? └── libswscale
├── lib
│?? ├── libavcodec.a
│?? ├── libavdevice.a
│?? ├── libavfilter.a
│?? ├── libavformat.a
│?? ├── libavutil.a
│?? ├── libswresample.a
│?? ├── libswscale.a
│?? └── pkgconfig
└── share
├── doc
├── ffmpeg
└── man
15 directories, 10 files
三、搭建一下linux的nginx+ffmpeg流媒体服务器
ubuntu@ubuntu:~$ wget http://nginx.org/download/nginx-1.19.9.tar.gz
ubuntu@ubuntu:~$ tar -zxvf nginx-1.19.9.tar.gz
ubuntu@ubuntu:~$ cd nginx-1.19.9/
ubuntu@ubuntu:~/nginx-1.19.9$
ubuntu@ubuntu:~/nginx-1.19.9$ ./configure --add-module=nginx-rtmp-module
ubuntu@ubuntu:~/nginx-1.19.9$ make
ubuntu@ubuntu:~/nginx-1.19.9$ sudo make install
ubuntu@ubuntu:~/nginx-1.19.9$ sudo cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bk
ubuntu@ubuntu:~/nginx-1.19.9$ sudo gedit /usr/local/nginx/conf/nginx.conf
覆盖内容
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
rtmp {
server {
listen 1935;
chunk_size 4000;
application play {
play /usr/local/nginx/html/play;
}
application hls {
live on;
hls on;
hls_path /usr/local/nginx/html/hls;
hls_fragment 1s;
hls_playlist_length 4s;
}
application live {
live on;
}
}
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
# XML stylesheet to view RTMP stats.
# Copy stat.xsl wherever you want
# and put the full directory path here
root /usr/local/nginx-rtmp-module;
}
location /hls {
# Serve HLS fragments
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /usr/local/nginx/html;
add_header Cache-Control no-cache;
}
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
测试一下
ubuntu@ubuntu:~/nginx-1.19.9$ cd /usr/local/nginx/sbin/
ubuntu@ubuntu:/usr/local/nginx/sbin$ sudo ./nginx -c /usr/local/nginx/conf/nginx.conf
ubuntu@ubuntu:/usr/local/nginx/sbin$ ps -ef | grep nginx
root 31560 1667 0 21:58 ? 00:00:00 nginx: master process ./nginx -c /usr/local/nginx/conf/nginx.conf
nobody 31561 31560 0 21:58 ? 00:00:00 nginx: worker process
nobody 31562 31560 0 21:58 ? 00:00:00 nginx: cache manager process
ubuntu 31564 6052 0 21:58 pts/1 00:00:00 grep --color=auto nginx
ubuntu@ubuntu:/usr/local/nginx/sbin$
测试一下 輸入192.168.10.151:80
四、测试Clion代码
cmakelists.txt
cmake_minimum_required(VERSION 3.21)
project(untitled1)
find_package(OpenCV REQUIRED)
set(CMAKE_CXX_STANDARD 14)
add_library(libavformat STATIC IMPORTED)
set_target_properties(libavformat PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/FFmpeg/lib/libavformat.a)
add_library(libavdevice STATIC IMPORTED)
set_target_properties(libavdevice PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/FFmpeg/lib/libavdevice.a)
add_library(libavcodec STATIC IMPORTED)
set_target_properties(libavcodec PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/FFmpeg/lib/libavcodec.a)
add_library(libavfilter STATIC IMPORTED)
set_target_properties(libavfilter PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/FFmpeg/lib/libavfilter.a)
add_library(libavutil STATIC IMPORTED)
set_target_properties(libavutil PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/FFmpeg/lib/libavutil.a)
add_library(libswresample STATIC IMPORTED)
set_target_properties(libswresample PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/FFmpeg/lib/libswresample.a)
add_library(libswscale STATIC IMPORTED)
set_target_properties(libswscale PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/FFmpeg/lib/libswscale.a)
add_library(libx264 STATIC IMPORTED)
set_target_properties(libx264 PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/X264/lib/libx264.a)
add_executable(untitled1 main.cpp)
target_link_libraries(untitled1
${OpenCV_LIBS}
libavformat
libavdevice
libavcodec
libavfilter
libavutil
libswresample
libswscale
libx264
-pthread
-lz
-llzma
-ldl
)
#-pthread
#-lz
#-lX11
#-llzma
#-lva
#-lbz2
#-lva-drm
#-lva-x11
#-lvdpau
#-lx264
假设不编译库,可以使用这样
cmake_minimum_required(VERSION 3.21)
project(untitled1)
find_package(OpenCV REQUIRED)
set(CMAKE_CXX_STANDARD 14)
add_executable(untitled1 main.cpp)
target_link_libraries(untitled1
${OpenCV_LIBS}
-lavformat -lavcodec -lswscale -lavutil -lz
)
main.cpp
#include <iostream>
#include <vector>
#include <opencv2/highgui.hpp>
#include <opencv2/video.hpp>
#include <opencv2/opencv.hpp>
extern "C"
{
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/imgutils.h>
#include <libswscale/swscale.h>
}
using namespace std;
using namespace cv;
int main() {
const char* out_url = "rtmp://192.168.10.151:1935/live/livestream";
// 注册所有网络协议
avformat_network_init();
// 输出的数据结构
AVFrame* yuv = NULL;
Mat frame;
// 1.使用opencv 打开usb 摄像头
VideoCapture video_ptr;
video_ptr.open(0);
if (!video_ptr.isOpened()) {
cout << "camera open usb camera error" << endl;
return -1;
}
cout << "open usb camera successful." << endl;
int width = video_ptr.get(CAP_PROP_FRAME_WIDTH);
int height = video_ptr.get(CAP_PROP_FRAME_HEIGHT);
int fps = video_ptr.get(CAP_PROP_FPS);
// 如果fps为0,这里就设置25。因为在fps=0时,调用avcodec_open2返回-22,
// 参数不合法
if (0 == fps) { fps = 25; }
// 2.初始化格式转换上下文
SwsContext* sws_context = NULL;
sws_context = sws_getCachedContext(sws_context,
width, height, AV_PIX_FMT_BGR24, // 源格式
width, height, AV_PIX_FMT_YUV420P, // 目标格式
SWS_BICUBIC, // 尺寸变化使用算法
0, 0, 0);
if (NULL == sws_context) {
cout << "sws_getCachedContext error" << endl;
return -1;
}
// 3.初始化输出的数据结构
yuv = av_frame_alloc();
yuv->format = AV_PIX_FMT_YUV420P;
yuv->width = width;
yuv->height = height;
yuv->pts = 0;
// 分配yuv空间
int ret_code = av_frame_get_buffer(yuv, 32);
if (0 != ret_code) {
cout << " yuv init fail" << endl;
return -1;
}
// 4.初始化编码上下文
// 4.1找到编码器
const AVCodec* codec = avcodec_find_encoder(AV_CODEC_ID_H264);
if (NULL == codec) {
cout << "Can't find h264 encoder." << endl;
return -1;
}
// 4.2创建编码器上下文
AVCodecContext* codec_context = avcodec_alloc_context3(codec);
if (NULL == codec_context) {
cout << "avcodec_alloc_context3 failed." << endl;
return -1;
}
// 4.3配置编码器参数
// vc->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
codec_context->codec_id = codec->id;
codec_context->thread_count = 8;
// 压缩后每秒视频的bit流 50k
codec_context->bit_rate = 50 * 1024 * 8;
codec_context->width = width;
codec_context->height = height;
codec_context->time_base = { 1, fps };
codec_context->framerate = { fps, 1 };
// 画面组的大小,多少帧一个关键帧
codec_context->gop_size = 50;
codec_context->max_b_frames = 0;
codec_context->pix_fmt = AV_PIX_FMT_YUV420P;
codec_context->qmin = 10;
codec_context->qmax = 51;
AVDictionary* codec_options = nullptr;
//(baseline | high | high10 | high422 | high444 | main)
av_dict_set(&codec_options, "profile", "baseline", 0);
av_dict_set(&codec_options, "preset", "superfast", 0);
av_dict_set(&codec_options, "tune", "zerolatency", 0);
// 4.4打开编码器上下文
ret_code = avcodec_open2(codec_context, codec, &codec_options);
if (0 != ret_code) {
return -1;
}
cout << "avcodec_open2 success!" << endl;
// 5.输出封装器和视频流配置
// 5.1创建输出封装器上下文
// rtmp flv封装器
AVFormatContext* format_context = nullptr;
ret_code = avformat_alloc_output_context2(&format_context, 0, "flv", out_url);
if (0 != ret_code) {
return -1;
}
// 5.2添加视频流
AVStream* vs = avformat_new_stream(format_context, NULL);
if (NULL == vs) {
cout << "avformat_new_stream failed." << endl;
return -1;
}
vs->codecpar->codec_tag = 0;
// 从编码器复制参数
avcodec_parameters_from_context(vs->codecpar, codec_context);
av_dump_format(format_context, 0, out_url, 1);
// 打开rtmp 的网络输出IO
ret_code = avio_open(&format_context->pb, out_url, AVIO_FLAG_WRITE);
if (0 != ret_code) {
cout << "avio_open failed." << endl;
return -1;
}
// 写入封装头
ret_code = avformat_write_header(format_context, NULL);
if (0 != ret_code) {
cout << "avformat_write_header failed." << endl;
return -1;
}
AVPacket pack;
memset(&pack, 0, sizeof(pack));
int vpts = 0;
uint8_t* in_data[AV_NUM_DATA_POINTERS] = { 0 };
int in_size[AV_NUM_DATA_POINTERS] = { 0 };
for (;;) {
// 读取rtsp视频帧,解码视频帧
video_ptr >> frame;
// If the frame is empty, break immediately
if (frame.empty()) break;
imshow("video", frame);
waitKey(1);
// rgb to yuv
in_data[0] = frame.data;
// 一行(宽)数据的字节数
in_size[0] = frame.cols * frame.elemSize();
int h = sws_scale(sws_context, in_data, in_size, 0, frame.rows,
yuv->data, yuv->linesize);
if (h <= 0) { continue; }
// h264编码
yuv->pts = vpts;
vpts++;
ret_code = avcodec_send_frame(codec_context, yuv);
if (0 != ret_code) { continue; }
ret_code = avcodec_receive_packet(codec_context, &pack);
if (0 != ret_code || pack.buf !=nullptr) {//
cout << "avcodec_receive_packet." << endl;
}
else {
cout << "avcodec_receive_packet contiune." << endl;
continue;
}
// 推流
pack.pts = av_rescale_q(pack.pts, codec_context->time_base, vs->time_base);
pack.dts = av_rescale_q(pack.dts, codec_context->time_base, vs->time_base);
pack.duration = av_rescale_q(pack.duration,
codec_context->time_base,
vs->time_base);
ret_code = av_interleaved_write_frame(format_context, &pack);
if (0 != ret_code)
{
cout << "pack is error" << endl;
}
av_packet_unref(&pack);
frame.release();
}
av_dict_free(&codec_options);
avcodec_free_context(&codec_context);
av_frame_free(&yuv);
avio_close(format_context->pb);
avformat_free_context(format_context);
sws_freeContext(sws_context);
video_ptr.release();
destroyAllWindows();
return 0;
}
测试结果 一推一拉
?开始基础知识学习之旅~
|