Nginx-http-flv-module流媒体服务器搭建+模拟推流+flv.js在前端html和Vue中播放HTTP-FLV视频流

news2024/9/28 1:21:27

场景

Windows上搭建Nginx RTMP服务器并使用FFmpeg实现本地视频推流:

Windows上搭建Nginx RTMP服务器并使用FFmpeg实现本地视频推流_win nginx-rtmp最新版_霸道流氓气质的博客-CSDN博客

Vue中使用vue-video-player和videojs-flash插件实现播放rtmp视频文件流:

Vue中使用vue-video-player和videojs-flash插件实现播放rtmp视频文件流_videojs-flash vue_霸道流氓气质的博客-CSDN博客

前面使用Vue播放RTMP流时,需要借助于flash。这种方式对于flash插件的安装

与后续浏览器支持等都不是很好。可以采用将rtmp的视频流转换成HTTP-FLV的方式,

然后前端通过bilibili的flv.js实现播放。

nginx-http-flv-module

nginx-http-flv-module: 基于nginx-rtmp-module的流媒体服务器。具备nginx-rtmp-module的所有功能,增加了HTTP-FLV,GOP缓存和VHOST(一个IP对应多个域名)的功能。Media streaming server based on nginx-rtmp-module. In addtion to the features nginx-rtmp-module provides, HTTP-FLV, GOP cache and VHOST (one IP for multi domain names) are supported now.

中文说明

nginx-http-flv-module/README.CN.md at master · winshining/nginx-http-flv-module · GitHub

一款基于 nginx-rtmp-module 的流媒体服务器。

nginx-http-flv-module 具备nginx-rtmp-module 的所有功能,并且对比如下:

 

支持的播放器

VLC (RTMP & HTTP-FLV) / OBS (RTMP & HTTP-FLV) / JW Player (RTMP) / flv.js (HTTP-FLV).

注意事项

1、Adobe 将在 2020 年 12 月 31 日之后停止对 flash 播放器 的官方支持,

主流浏览器随后将移除 flash 播放器,使用 flash 播放器的插件将不再可用。

2、flv.js 只能运行在支持 Media Source Extensions 的浏览器上。

3、nginx-http-flv-module 包含了 nginx-rtmp-module 所有的功能,

所以不要将 nginx-http-flv-module 和 nginx-rtmp-module 一起编译。

4、如果使用 flv.js 播放流,那么请保证发布的流被正确编码,

因为 flv.js 只支持 H.264 编码的视频和 AAC/MP3 编码的音频。

5、其中的 add_header ‘Access-Control-Allow-Origin’ ‘*’ 与 add_header ‘Access-Control-Allow-Credentials’ ‘true’ 很重要,

主要解决了前端通过HTTP方式拉流是的跨域问题。

其他注意事项和详细细节可见官方github。

注:

博客:
霸道流氓气质的博客_CSDN博客-C#,架构之路,SpringBoot领域博主

实现

1、官方示例以及推荐等都是在Linux服务器上。

 

但是如果需要在本地Windows电脑上进行调试或者服务器就必须是Windows服务器的话,

需要编译Windows版本,或者直接网络上搜索下载已经编译好的Windows版本。

比如:

https://download.csdn.net/download/codebooks/12793877

但是是否可用需要自己下载验证。

以下载上面的为例,下载解压后,自带run.bat运行脚本

脚本内容为:

nginx.exe -c conf/http-flv.conf

这里是指定走配置文件conf/http-flv.conf

所以如果要修改的话也是修改http-flv.conf文件,而不是原来默认的nginx.conf文件了。

双击启动脚本启动后,可以从任务管理器中验证是否启动成功,或者通过其他查看日志等方式验证。

2、配置文件

关注两个地方,比如这里的官方例子

假设在 http 配置块中的 listen 配置项是:

http {
    ...
    server {
        listen 8080; #不是默认的 80 端口
        ...

        location /live {
            flv_live on;
        }
    }
}

在 rtmp 配置块中的 listen 配置项是:

rtmp {
    ...
    server {
        listen 1985; #不是默认的 1935 端口
        ...

        application myapp {
            live on;
        }
    }
}

并且发布的流的名称是 mystream,那么基于 HTTP 的播放 url 是:

http://example.com:8080/live?port=1985&app=myapp&stream=mystream

下面附官方提示的配置文件示例

​
worker_processes  1; #运行在 Windows 上时,设置为 1,因为 Windows 不支持 Unix domain socket
#worker_processes  auto; #1.3.8 和 1.2.5 以及之后的版本

#worker_cpu_affinity  0001 0010 0100 1000; #只能用于 FreeBSD 和 Linux
#worker_cpu_affinity  auto; #1.9.10 以及之后的版本

error_log logs/error.log error;

#如果此模块被编译为动态模块并且要使用与 RTMP 相关的功
#能时,必须指定下面的配置项并且它必须位于 events 配置
#项之前,否则 NGINX 启动时不会加载此模块或者加载失败

#load_module modules/ngx_http_flv_live_module.so;

events {
    worker_connections  4096;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    keepalive_timeout  65;

    server {
        listen       80;

        location / {
            root   /var/www;
            index  index.html index.htm;
        }

        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 的位置
        }

        #如果需要 JSON 风格的 stat, 不用指定 stat.xsl
        #但是需要指定一个新的配置项 rtmp_stat_format

        #location /stat {
        #    rtmp_stat all;
        #    rtmp_stat_format json;
        #}

        location /control {
            rtmp_control all; #rtmp 控制模块的配置
        }
    }
}

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 www.test.*; #用于虚拟主机名后缀通配

        application myapp {
            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;
        }
    }

    server {
        listen 1935;
        server_name *.test.com; #用于虚拟主机名前缀通配

        application myapp {
            live on;
            gop_cache on; #打开 GOP 缓存,减少首屏等待时间
        }
    }

    server {
        listen 1935;
        server_name www.test.com; #用于虚拟主机名完全匹配

        application myapp {
            live on;
            gop_cache on; #打开 GOP 缓存,减少首屏等待时间
        }
    }
}

​

然后上面下载的Windows编译版本的配置文件http-flv.conf的内容为

​
worker_processes  1;

error_log logs/error.log error;

events {
    worker_connections  4096;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    keepalive_timeout  65;

    server {
        listen       800;

        location / {
            root   www;
            index  index.html index.htm;
        }

        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 temp;
            add_header 'Cache-Control' 'no-cache';
        }

        location /dash {
            root temp;
            add_header 'Cache-Control' 'no-cache';
        }

        location /stat {
            #推流播放和录制统计数据的配置

            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }

        location /stat.xsl {
            root www/rtmp; #指定stat.xsl的位置
        }

        #如果需要JSON风格的stat, 不用指定stat.xsl
        #但是需要指定一个新的配置项rtmp_stat_format

        #location /stat {
        #    rtmp_stat all;
        #    rtmp_stat_format json;
        #}

        location /control {
            rtmp_control all; #rtmp控制模块的配置
        }
    }
}

rtmp_auto_push on;
rtmp_auto_push_reconnect 1s;
rtmp_socket_dir temp;

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 www.test.*; #用于虚拟主机名后缀通配
  server_name 127.0.0.1;
        application myapp {
            live on;
            gop_cache on; #打开GOP缓存,减少首屏等待时间
        }

        application hls {
            live on;
            hls on;
            hls_path temp/hls;
        }

        application dash {
            live on;
            dash on;
            dash_path temp/dash;
        }
    }
}

​

3、Windows上本地模拟推送RTMP视频流到nginx-http-flv-module

参考上面使用FFMpeg推送的流程,修改模拟推流bat脚本为

ffmpeg.exe -re -i D:\WorkSpace\Other\FFmpegDemo\test\1.mp4 -vcodec libx264 -acodec aac -f flv rtmp://127.0.0.1:1935/myapp/badao
pause

推流成功之后,使用VLC进行拉流测试,网络串流地址为

http://127.0.0.1:800/live?port=1935&app=myapp&stream=badao

注意这里的

800对应http配置的端口

live对应http配置的location后面的/live

port=1935对应rtmp中配置的端口

app=myapp对应的myapp是rtmp中配置的application myapp

stream=badao的badao是对应推流时的rtmp://127.0.0.1:1935/myapp/badao的badao

拉流推流效果。

 

为了模拟时间延迟效果,可以使用其他推流工具,截图桌面时间显示进行推流,测试延迟效果。

Docker和docker-compose中部署nginx-rtmp实现流媒体服务与oob和ffmpeg推流测试:

Docker和docker-compose中部署nginx-rtmp实现流媒体服务与oob和ffmpeg推流测试_docker 推流_霸道流氓气质的博客-CSDN博客

这里可以参考使用oob进行模拟推流测试效果

 

4、bilibili的flv.js

GitHub - bilibili/flv.js: HTML5 FLV Player

可以看到其特点是

HTTP FLV低延迟实时流播放

 

在html中使用flv.js播放http-flv

将flv.min.js下载到本地

https://github.com/bilibili/flv.js/releases/download/v1.5.0/flv.min.js

然后新建html

<!DOCTYPE html>
<html>

<head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
    <title>flv.js demo</title>
    <style>
        .mainContainer {
            display: block;
            width: 1024px;
            margin-left: auto;
            margin-right: auto;
        }

        .urlInput {
            display: block;
            width: 100%;
            margin-left: auto;
            margin-right: auto;
            margin-top: 8px;
            margin-bottom: 8px;
        }

        .centeredVideo {
            display: block;
            width: 100%;
            height: 576px;
            margin-left: auto;
            margin-right: auto;
            margin-bottom: auto;
        }

        .controls {
            display: block;
            width: 100%;
            text-align: center;
            margin-left: auto;
            margin-right: auto;
        }
    </style>
</head>

<body>

<p class="mainContainer">
    <video name="videoElement" id="videoElement" class="centeredVideo" controls muted autoplay width="1024"
           height="576">
        Your browser is too old which doesn't support HTML5 video.
    </video>
</p>

<script src="js/flv.min.js"></script>

<script>

    function start() {
        if (flvjs.isSupported()) {
            var videoElement = document.getElementById('videoElement');
            var flvPlayer = flvjs.createPlayer({
                type: 'flv',
               url:'http://127.0.0.1:800/live?port=1935&app=myapp&stream=badao'
            });
            flvPlayer.attachMediaElement(videoElement);
            flvPlayer.load();
            flvPlayer.play();
        }
    }

    document.addEventListener('DOMContentLoaded', function () {
        start();
    });
</script>

</body>

</html>

将url设置为上面的拉流地址,注意flv.min.js的路径

浏览器中访问该html看下效果

 

5、在Vue中使用flv.js播放http-flv

安装依赖

npm install --save flv.js

新建页面修改代码

<template lang="html">
  <div id="app">
    <video
      id="videoLive"
      crossorigin="anonymous"
      controls
      autoplay
      width="100%"
      height="100%"
      style="object-fit: fill"
    ></video>
  </div>
</template>

<script>
import flvjs from "flv.js";
export default {
  name: "flvPlayer",
  data() {
    return {
      flvPlayer: null,
    };
  },
  mounted() {< BR>   this.createVideo('http://127.0.0.1:800/live?port=1935&app=myapp&stream=badao',"videoLive")
  },
  methods: {
    createVideo(url, elementId) {
      if (flvjs.isSupported()) {
        let videoElement = document.getElementById(elementId);
        this.flvPlayer = flvjs.createPlayer({
          type: "flv",
          enableWorker: true, //浏览器端开启flv.js的worker,多进程运行flv.js
          isLive: true, //直播模式
          hasAudio: false, //关闭音频
          hasVideo: true,
          stashInitialSize: 128,
          enableStashBuffer: true, //播放flv时,设置是否启用播放缓存,只在直播起作用。
          url: url,
        });
        this.flvPlayer.attachMediaElement(videoElement);
        this.flvPlayer.load();
        this.flvPlayer.play();
      }
    },
  },
};
</script>

<style lang="css">
</style>

运行查看效果

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/399296.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

类型转换(C++)

文章目录1. 为什么需要类型转换2. C语言的类型转换2.1 隐式类型转换2.2 显式类型转换2.3 特点3. C的类型转换3.1 static_cast3.2 reinterpret_cat3.3 const_cast3.4 dynamic_cast转型向下转型的安全问题3.5 explicit4. RTTI5. 常见题目1. 为什么需要类型转换 类型转换是将一个…

数据库-基础篇-8-事务

事务简介&#xff1a;事务是一组操作的集合&#xff0c;它是一个不可分割的工作单位&#xff0c;事务会把所有的操作作为一个整体一起向系统提交或撤销操作请求&#xff0c;即这些操作要么同时成功要么同时失败。 默认MySQL的事务是自动提交的&#xff0c;也就是说&#xff0c…

S3C2440移植Linux4.19.275内核以及过程中遇到的问题

目录 1 问题一&#xff1a;内核移植时MTD分区问题 2 问题二&#xff1a;uboot的MTDPARTS_DEFAULT定义的MTD分区&#xff0c;bootargs中的文件系统分区&#xff0c;内核的mtd_partition smdk_default_nand_part定义的分区&#xff0c;三者要对应起来 3 问题三&#xff1a;ubo…

kafka:linux 安装 kafka集群

kafka运行依赖于 jdk、zookeeper&#xff0c;kafka可视化工具选择kafka-eagle。所以要装的组件有&#xff1a;jdk、zookeeper、kafka、kafka-eagle一、安装jdk下载linux版本的jdk包&#xff0c;比如&#xff1a;jdk-8u192-linux-x64.tar.gz。将其复制到 /opt 目录下并解压&…

设计模式(十八)----行为型模式之策略模式

1、概述 先看下面的图片&#xff0c;我们去旅游选择出行模式有很多种&#xff0c;可以骑自行车、可以坐汽车、可以坐火车、可以坐飞机。 作为一个程序猿&#xff0c;开发需要选择一款开发工具&#xff0c;当然可以进行代码开发的工具有很多&#xff0c;可以选择Idea进行开发&a…

第十届省赛——7外卖店优先级

题目&#xff1a;“饱了么”外卖系统中维护着N 家外卖店&#xff0c;编号1~N。每家外卖店都有一个优先级&#xff0c;初始时(0 时刻) 优先级都为0。每经过1 个时间单位&#xff0c;如果外卖店没有订单&#xff0c;则优先级会减少1&#xff0c;最低减到0&#xff1b;而如果外卖店…

New Bing的详细申请步骤与实际功能测试效果展示

文章目录前言申请方式申请出错解决方案测试总结前言 微软表示&#xff0c;new bing正在使用对话式人工智能来创造一种新的浏览网络的方式。用户将能够像ChatGPT那样与Bing聊天&#xff0c;用自然语言提出问题和接受答案。 同时&#xff0c;微软宣布了其搜索引擎Bing的新版本&a…

汽车微控制器芯片F280039CPZRQ1、F280039CSPM、F280039CSPN规格参数

F280039CPZRQ1、F280039CSPM、F280039CSPN是C2000实时微控制器系列中的一款器件。C2000微控制器是可扩展、超低延迟器件&#xff0c;旨在提高电力电子设备的效率&#xff0c;包括但不限于&#xff1a;高功率密度、高开关频率&#xff0c;并支持使用 GaN和SiC技术。F280039CPZRQ…

6个常用Pycharm插件推荐,老手100%都用过

人生苦短 我用python 有些插件是下载后需要重启Pycharm才生效的 免费领源码、安装包&#xff1a;扣扣qun 903971231 PyCharm 本身已经足够优秀&#xff0c; 就算不使用插件&#xff0c; 也可以吊打市面上 90%的 Python 编辑器。 如果硬要我推荐几款实用的话&#xff0c; 那么…

Beats:在 Docker 中同时部署 Metricbeat 和 Elasticsearch

在本教程中&#xff0c;我们将部署一个 metricbeat 来监控正在运行的容器的健康状况和系统指标。 为什么需要监控&#xff0c;为什么需要 Metricbeat&#xff1f; 一个常见的问题&#xff0c;但很少有人回答。 首先&#xff0c;无论我们部署的是 docker 容器还是老式的金属箱&…

代码随想录算法训练营第二十三天 | 669. 修剪二叉搜索树

打卡第23天&#xff0c;这一章节二叉树最后一天&#xff0c;难度渐渐上来了。 今日任务 669.修剪二叉搜索树108.将有序数组转换为二叉搜索树538.把二叉搜索树转换为累加树 669. 修剪二叉搜索树 给你二叉搜索树的根节点 root &#xff0c;同时给定最小边界low 和最大边界 high。…

中介效应分析-方法和模型发展【论文详解】

中介效应分析-方法和模型发展 – 潘登同学的论文精读 文章目录中介效应分析-方法和模型发展 -- 潘登同学的论文精读检验中介效应流程直接效应、间接效应与总效应完全中介与部分中介Stata代码考虑自变量X对因变量Y的影响, 如果X通过影响变量M而对Y产生影响, 则称M为中介变量。Yc…

VMware15配置NAT模式联通网络

最近为了测试C# 开发的桌面应用程序悬浮球的兼容性&#xff0c;在虚拟机上安装了win7系统和xp系统&#xff0c;之前也安装过黑苹果系统&#xff0c;但是win系统倒是第一次安装&#xff0c;在win7系统联网的时候&#xff0c;踩了一些坑&#xff0c;整理纪录一下。 设置主物理机配…

【JVM篇1】认识JVM,内存区域划分,类加载机制

目录 一、JVM内存区域划分 ①程序计数器(每个线程都有一个) ②栈&#xff1a;保存了局部变量和方法调用的信息(每一个线程都有一个栈) 如果不停地调用方法却没有返回值&#xff0c;会产生什么结果 ③堆(每一个进程都有一个堆&#xff0c;线程共享一个堆) 如何区分一个变量是…

【C++】C++11新特性——基础特性

文章目录一、列表初始化1.1 {}初始化1.2 initializer_list类型二、类型推导2.1 auto2.2 auto注意事项2.3 decltype三、新增与改进3.1 nullptr3.2 范围for3.3 array3.4 forward_list3.5 unordered系列3.6 final与override一、列表初始化 1.1 {}初始化 C11 引入了一个新的初始化…

[数据结构与算法(严蔚敏 C语言第二版)]第1章 绪论(章节题库+答案解析)

练习 选择题 算法的计算量的大小称为计算的&#xff08; &#xff09;。 A&#xff0e;效率 B&#xff0e;复杂性 C&#xff0e;现实性 D&#xff0e;难度 计算机算法指的是解决问题的步骤序列&#xff0c;它必须具备&#xff08; &#xff09;三个特性。 A&#xff0e;可执行…

“快速掌握如何用FFmpeg在Python中截取定时间隔的MP4视频画面“

目录 简介&#xff1a; 源代码&#xff1a; 源代码说明&#xff1a; 这段代码中&#xff0c;首先定义了输入视频文件名、字体文件路径和输出图像文件名格式。然后使用subprocess模块的call函数调用FFmpeg命令。FFmpeg命令被定义为一个列表&#xff0c;其中每个元素都是命令中…

RocketMQ5.0.0消息消费<二> _ 消息队列负载均衡机制

目录 一、消费队列负载均衡概览 二、消费队列负载均衡实现 1. 负载均衡UML 2. 启动RebalanceService线程 3. PUSH模式负载均衡 三、负载均衡策略 四、参考资料 一、消费队列负载均衡概览 RocketMQ默认一个主题下有4个消费队列&#xff0c;集群模式下同一消费组内要求每个…

合并链表相关的练习

目录 一、合并两个有序链表 二、两数相加 一、合并两个有序链表 将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 示例 1&#xff1a; 输入&#xff1a;l1 [1,2,4], l2 [1,3,4] 输出&#xff1a;[1,1,2,3,4,4] 示例 2&…

熬夜30天吃透这九大Java核心专题,我收割了3个大厂offer

这次一共收割了3个大厂offer&#xff0c;分别是蚂蚁金服、美团和网易&#xff0c;特意分享这次对我帮助非常大的宝典资料&#xff0c;一共涉及九大核心专题&#xff0c;分别是计算机网络、操作系统、MySQL、Linux、JAVA、JVM、Redis、消息队列与分布式、网站优化相关&#xff0…