摄像头web网页播放功能: ffmeg和nginx实现

简介: 摄像头web网页播放功能: ffmeg和nginx实现


实现思路

1、Ffmpeg实现视频转码推流

2、nginx流媒体搭建,实现转码播放

3、前端video.js拉流播放测试

具体实操:

1.下载nginx

地址: https://github.com/arut/nginx-rtmp-module

下载带rtmp模块的nginx

2.配置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;
        application live {
            live on;
        }
        application hls {
            live on;#开启实时
            hls on;   #开启hls
            #hls_path temp/hls;  
            hls_path D:/zjiac_code/uploadPath/video;   #切片存放位置
            hls_fragment 5s;  #每个TS文件包含5秒的视频内容
        }
    }
}
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 8072;
        root  html;
        index  index.html;
        location / {
            try_files $uri $uri/ @router;#需要指向下面的@router否则会出现vue的路由在nginx中刷新出现404
            index  index.html index.htm;
        }
        location @router {
            rewrite ^.*$ /index.html last;
        }
        #新增能访问到hls流协议配置        
        location /hls {
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            alias D:/zjiac_code/uploadPath/video;
            expires -1;
            add_header Access-Control-Allow-Origin *;
        }
    }
    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;
        }
        # 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;
    #    }
    #}
}

3.准备一个rtsp流

我的是 rtsp://XX:15@192.168.2.3/Streaming/Channels/101?transportmode=unicast&profile=Profile_1

可以使用vlc播放器测试下是否通。

4. ffmpeg手动推流

ffmpeg -f rtsp -rtsp_transport tcp -i "rtsp://XX:XXX@192.168.2.3/Streaming/Channels/101?transportmode=unicast&profile=Profile_1" -codec copy -f hls -hls_list_size 10 -hls_wrap 20 -hls_time 15  "D:/zjiac_code/uploadPath/video/test.m3u8"

可以看到已经有test.m3u8和ts文件了。

nginx访问测试

使用video.js播放测试

新建个html

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>前端播放m3u8格式视频</title>
    <link href="https://vjs.zencdn.net/7.4.1/video-js.css" rel="stylesheet">
    <script src='https://vjs.zencdn.net/7.4.1/video.js'></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/videojs-contrib-hls/5.15.0/videojs-contrib-hls.min.js" type="text/javascript"></script>
    <!-- videojs-contrib-hls 用于在电脑端播放 如果只需手机播放可以不引入 -->
</head>
<body>
    <style>
        .video-js .vjs-tech {position: relative !important;}
    </style>
    <div>
        <video id="myVideo" class="video-js vjs-default-skin vjs-big-play-centered" controls preload="auto" data-setup='{}' style='width: 100%;height: auto'>
            <source id="source" src="http://localhost:8072/hls/test.m3u8" type="application/x-mpegURL"></source>
        </video>
    </div>
    <div class="qiehuan" style="width:100px;height: 100px;background: red;margin:0 auto;line-height: 100px;color:#fff;text-align: center">切换视频</div>
</body>
<script>
    // videojs 简单使用
    var myVideo = videojs('myVideo', {
        bigPlayButton: true,
        textTrackDisplay: false,
        posterImage: false,
        errorDisplay: false,
    })
    myVideo.play()
    var changeVideo = function (vdoSrc) {
        if (/\.m3u8$/.test(vdoSrc)) { //判断视频源是否是m3u8的格式
            myVideo.src({
                src: vdoSrc,
                type: 'application/x-mpegURL' //在重新添加视频源的时候需要给新的type的值
            })
        } else {
            myVideo.src(vdoSrc)
        }
        myVideo.load();
        myVideo.play();
    }
    var src = 'http://localhost:8072/hls/test.m3u8';
    document.querySelector('.qiehuan').addEventListener('click', function () {
        changeVideo(src);
    })
</script>

查看效果

已经显示播放成功了。


至此,摄像头网页播放的demo完成了,可以集成到vue或者react项目进行使用了。


大功告成!!!

相关文章
|
6天前
|
应用服务中间件 网络安全 nginx
快速上手!使用Docker和Nginx部署Web服务的完美指南
快速上手!使用Docker和Nginx部署Web服务的完美指南
|
21天前
|
数据采集 数据挖掘 Python
使用Python构建简单的Web爬虫:实现网页内容抓取与分析
本文将介绍如何使用Python编写一个简单的Web爬虫,实现对特定网页内容的抓取与分析。通过学习本文,读者将了解到如何利用Python的requests和Beautiful Soup库来获取网页内容,并通过示例演示如何解析HTML结构,提取所需信息。此外,我们还将讨论一些常见的爬虫挑战以及如何避免被网站封禁的策略。
|
1天前
|
运维 前端开发 JavaScript
【专栏:HTML进阶篇】HTML与Web标准:构建可访问与可维护的网页
【4月更文挑战第30天】本文探讨了HTML与Web标准的关系,强调遵循标准对创建高质量、可访问、可维护网页的重要性。通过使用语义化标签、提供文本替代、合理使用表格和列表,可提升网页可访问性;通过结构化文档、添加注释、分离结构与表现,能增强网页可维护性。遵循Web标准,可确保网页在不同设备上的兼容性,并满足各类用户需求。
|
1天前
|
移动开发 JavaScript 前端开发
【专栏:HTML进阶篇】HTML模板与Web组件:可复用的网页元素
【4月更文挑战第30天】HTML模板和Web组件提升网页开发效率和可维护性。HTML模板,如&lt;template&gt;元素和服务器端模板引擎,用于创建可复用的HTML结构。Web组件是自定义的HTML元素,结合影子DOM和模板,实现封装的可重用组件。两者助力构建高效、现代的网页和网站。
|
9天前
|
前端开发
web前端作业-模拟网页CSS
web前端作业-模拟网页CSS
9 0
|
15天前
|
移动开发 前端开发 数据处理
探索前端性能优化的新思路:使用Web Workers提升网页响应速度
传统的前端性能优化方法已经不能完全满足日益增长的网页需求。本文提出了一种新的思路,即利用Web Workers技术来提升网页的响应速度。通过将耗时的计算任务交给Web Workers处理,可以避免主线程阻塞,从而提高网页的用户体验。本文将介绍Web Workers的基本原理、使用方法以及在前端性能优化中的应用实例,帮助开发者更好地理解和运用这一技术。
|
19天前
|
Web App开发 前端开发 JavaScript
网页浏览和编辑DWG快速入门的方法(WEB CAD SDK)
MxDraw云图在线CAD解决方案,包括MxDraw、MxCAD开发包、图纸转换程序和后端服务。支持多种平台和CPU架构,推荐使用最新版Chrome或Edge浏览器。提供AutoCAD各版本dwg格式支持,具备三维和二维编辑功能。提供入门开发指南和功能丰富的示例。用户可下载开发包进行功能演示,包括在线预览和编辑CAD图纸。
网页浏览和编辑DWG快速入门的方法(WEB CAD SDK)
|
10天前
|
开发框架 前端开发 .NET
C#编程与Web开发
【4月更文挑战第21天】本文探讨了C#在Web开发中的应用,包括使用ASP.NET框架、MVC模式、Web API和Entity Framework。C#作为.NET框架的主要语言,结合这些工具,能创建动态、高效的Web应用。实际案例涉及企业级应用、电子商务和社交媒体平台。尽管面临竞争和挑战,但C#在Web开发领域的前景将持续拓展。
|
1天前
|
开发框架 JavaScript 前端开发
【JavaScript 与 TypeScript 技术专栏】TypeScript 在 Web 开发中的前沿应用
【4月更文挑战第30天】TypeScript在Web开发中日益重要,以其强大的类型系统提升代码质量,支持组件化开发,与React、Vue、Angular等框架良好集成。在大型项目管理中,TypeScript助于代码组织和优化,提高团队协作效率。此外,它提升开发体验,提供智能提示和错误检测。众多成功案例证明其前沿应用,未来将在Web开发领域持续发挥关键作用。
|
2天前
|
前端开发 JavaScript 测试技术
【PHP开发专栏】PHP Web开发基础与流程
【4月更文挑战第29天】本文介绍了PHP Web开发的基础和流程,帮助初学者入门。内容包括Web服务器与PHP解释器的工作原理、HTML/CSS/JavaScript基础知识、PHP语法与数据库操作。开发流程涵盖项目规划、环境搭建、数据库设计、代码编写、测试与调试,以及部署与维护。此外,文中还强调了使用框架、代码组织、安全性及性能优化等进阶知识和最佳实践,旨在培养优秀PHP开发者。