前言
平时想和朋友一起看看动画电影,国内有版权的还好,B站有一起看的功能。但是没版权或者一些小众的想一起看就太麻烦了,只能大家先下载在本地,再连麦同步进度看。太麻烦了,要是有一个“直播间”能看这些电影就好了,于是就搞了这么个项目。
一、框架与环境
技术框架选用的是Nginx、nginx-http-flv-module 、ffmpeg、flv.js。后面两个安装很简单,主要讲Nginx和nginx-http-flv-module的源码安装和配置以及使用。 配置是腾讯云轻量服务器1核2G的Ubuntu 20.04 LTS。注意:配置文件中出现过的端口都记得在防火墙中开放。
二、安装Nginx和nginx-http-flv-module
- 卸载原来的Nginx
- 安装nginx源码安装所需的依赖
g++
sudo apt-get install build-essential
PCRE库
sudo apt-get install libpcre3 libpcre3-dev
zlib库
sudo apt-get install zlib1g-dev
OpenSSL
sudo apt-get install openssl libssl-dev
- 下载源码到同一个目录并且解压
sudo wget http://nginx.org/download/nginx-1.22.1.tar.gz
sudo git clone https://github.com/winshining/nginx-http-flv-module
sudo tar -zxvf nginx-1.22.1.tar.gz
- 切换root用户进入nginx的根目录
su root
cd nginx-1.22.1
- 依次配置、编译、安装
./configure --add-module=../nginx-http-flv-module
make
make install
6、安装好之后退出root再进入/usr/local/nginx/sbin启动ng,输入服务器ip能看到下面图片就成功了
nginx
三、配置nginx.conf
cd /usr/local/nginx/conf
# 修改配置文件前记得留个备份
sudo cp nginx.conf nginx.conf.backup
sudo vim nginx.conf
2.完整配置文件
#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;
}
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;
#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;
}
location /live {
flv_live on; #打开 HTTP 播放 FLV 直播流功能
chunked_transfer_encoding on; #支持 'Transfer-Encoding: chunked' 方式回复
add_header 'Access-Control-Allow-Origin' '*'; #添加额外的 HTTP 头
add_header 'Access-Control-Allow-Credentials' 'true'; #添加额外的 HTTP 头
}
location /hls {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /tmp;
add_header 'Cache-Control' 'no-cache';
}
location /dash {
root /tmp;
add_header 'Cache-Control' 'no-cache';
}
location /stat {
#推流播放和录制统计数据的配置
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /var/www/rtmp; #指定 stat.xsl 的位置
}
location /control {
rtmp_control all; #rtmp 控制模块的配置
}
# 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;
# }
#}
}
rtmp_auto_push on;
rtmp_auto_push_reconnect 1s;
rtmp_socket_dir /tmp;
rtmp {
out_queue 4096;
out_cork 8;
max_streams 128;
timeout 15s;
drop_idle_publisher 15s;
log_interval 5s; #log 模块在 access.log 中记录日志的间隔时间,对调试非常有用
log_size 1m; #log 模块用来记录日志的缓冲区大小
server {
listen 1935;
server_name localhost; #用于虚拟主机名后缀通配
application myliveapp {
live on;
gop_cache on; #打开 GOP 缓存,减少首屏等待时间
}
application hls {
live on;
hls on;
hls_path /tmp/hls;
}
application dash {
live on;
dash on;
dash_path /tmp/dash;
}
}
}
- 进入/usr/local/nginx/sbin目录重启nginx
sudo nginx -s reload
四、ffmpeg推流
- 安装ffmpeg
sudo apt update
sudo apt install ffmpeg
- 推流,看到下图就可以了
cd /usr/video
ffmpeg -re -i FairyTail175.mp4 -c copy -f flv rtmp://ip/myliveapp/streamname
五、拉流
直接用b站的demo网站测试就行了
拉流地址:http://ip/live?port=1935&app=myliveapp&stream=streamname
写在最后
由于不熟悉源码编译和ng,这些配置还是弄了挺久的,把每一次项目都当成一次学习吧。现在能做到的就是基本播放,实际使用还有很多问题,追帧、断流重连、实时更新等问题,先挖着坑。