HLS-Nginx流媒体安装使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wn1245343496/article/details/79381931

1.Homebrew安装

打开终端,输入下列命令

man brew
如果Mac已经安装了, 会显示一些命令的帮助信息. 此时输入Q退出即可, 直接进入第2步安装nginx.
反之, 如果没有安装,执行命令

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

如果安装后, 想要卸载

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"

2.安装nginx

先clone nginx项目到本地

brew tap homebrew/nginx

执行安装rtmp模块:

brew install nginx-full --with-rtmp-module

此时, nginx和rtmp模块就安装好了 输入命令:

nginx
http://localhost:8080
如果出现下图, 则表示安装成功

image
如果终端上提示

nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use).....

则表示8080端口被占用了, 查看端口PID

lsof -i tcp:8080

根据端口PID, kill掉(这儿的12407换成你自己8080端口的PID)
image

kill 12407

然后重新执行nginx,打开http://localhost:8080

3.配置nginx和rtmp

在终端输入

open -t /usr/local/etc/nginx/nginx.conf

,直接滚到最后一行, 在最后一个},后面添加
image

rtmp {
    server {
        listen 1935;
        application live1 {
            live on;
            record off;
        }
    }
}

然后重启nginx,输入以下命令

nginx -s reload

处理错误

nginx -s reload如果执行命令的时候, 报如下错误:

nginx: [emerg] unknown directive “rtmp” in /usr/local/etc/nginx/nginx.conf:119

解决方法:

说明没有安装rtmp模块,卸载nginx,再次安装nginx

brew uninstall nginx-full  // 删除nginx
brew install nginx-full --with-rtmp-module  // 安装

如果报下面这个错误:

nginx: [error] open() "/usr/local/var/run/nginx.pid" failed (2: No such file or directory)

解决方法:

找到你的nginx.conf的文件夹目录,然后运行这个
nginx -c /usr/local/etc/nginx/nginx.conf命令,
再运行nginx -s reload,就可以了

4.配置nginx和hls

在配置文件添加如下代码,(红框标注的地方)
image

ocation /hls {
            #Serve HLS config
            types {
                application/vnd.apple.mpegurl    m3u8;
                video/mp2t ts;
            }
            root /usr/local/var/www;
            add_header Cache-Control    no-cache;
        }

image

application hls{
             live on;
             hls on;
             hls_path /usr/local/var/www/hls;
             hls_fragment 1s;
        }

创建hls文件夹

mkdir /usr/local/var/www/hls

5、安装ffmpeg

执行命令

brew install ffmpeg

ffmpeg推rtmp流

ffmpeg -re -i 视频的据对路径 -vcodec copy -acodec copy  -f flv rtmp://localhost:1935/live1/room

将视频推流到服务器后,打开VLC,rtmp://localhost:1935/live1/room
如果要运行在手机上,则为:rtmp://电脑的ip:1935/live1/room

ffmpeg推hls流

ffmpeg -re -i 视频的据对路径 -vcodec copy -acodec copy  -f flv rtmp://localhost:1935/hls/room

将视频推流到服务器后,打开VLC
http://localhost:1935/hls/room.m3u8 或者 rtmp://localhost:1935/hls/test

如果要运行在手机上,则为:http://电脑的ip:1935/hls/room.m3u8 或者rtmp://电脑的ip:1935/hls/test

6、http点播

前往文件夹 : /usr/local/var/www(如下图)
image
将要点播的视频拖入到www文件夹下:image
打开浏览器 :http://电脑的ip:8080/4K.mp4

猜你喜欢

转载自blog.csdn.net/wn1245343496/article/details/79381931