FFmpeg

发布时间 2023-08-31 17:38:56作者: 我不想学编丿程

FFmpeg

安装

Springboot对FFFmpeg继承 jave2

https://github.com/a-schild/jave2

依赖:

<dependency>
 <groupId>ws.schild</groupId>
 <artifactId>jave-all-deps</artifactId>
 <version>3.3.1</version>
</dependency>

命令:

将 实时流 转换为 m3u8文件

ffmpeg -i rtsp://192.168.1.19:554/channel=0,stream=0 -c copy -f hls -hls_time 10 -hls_list_size 0 -hls_segment_filename saved_%5d.ts index.m3u8

前端视频插件hls.js

前端实现播放实时监控视频笔记(hls http-flv)

<script >
import Hls from 'hls.js'

export default {


  methods: {

    switchHlsvideo: function () {
      const video = document.getElementById('video')
      const url = 'http://localhost:8089/download/index.m3u8' // 直播视频源(.m3u8)文件地址
      if (Hls.isSupported()) {
        // 如果支持 hls.js(MediaSource Extensions)
        var hls = new Hls()
        hls.loadSource(url)
        hls.attachMedia(video)
        // 自动播放
        hls.on(Hls.Events.MANIFEST_PARSED, function () {
          video.play()
        })
      } else if (video.canPlayType('application/vnd.apple.mpegurl')) {
        // 如果支持原生播放
        video.src = url
        // 自动播放
        video.addEventListener('canplay', function () {
          video.play()
        })
      }
    },
    switchvideo: function () {
      var player = videojs('example-video');
      //player.play();
      //切换视频
      function switchvideo() {
        player.src({
          src: '"http://localhost:8089/download/index.m3u8"',
          type: 'application/x-mpegURL',
          withCredentials: true
        });
        player.play();
      }
    }
  }

}

</script>

<template>

  <video id="video" src="" controls ></video>



  <!-- <video id=example-video width=800 height=600 class="video-js vjs-default-skin vjs-big-play-
                                                            centered" controls
        poster="http://127.0.0.1:8089/video/add.jpg">

        <source src="http://localhost:8089/download/index.m3u8" type="application/x-mpegURL">

</video> -->

  <!-- <input type="button"  value="switch" ></input> -->
  <button @click="switchvideo"> 切换视频1</button>
  <button @click="switchHlsvideo"> 切换视频 hls.js</button>
</template>

<style scoped>
header {
  line-height: 1.5;
}

.logo {
  display: block;
  margin: 0 auto 2rem;
}

@media (min-width: 1024px) {
  header {
    display: flex;
    place-items: center;
    padding-right: calc(var(--section-gap) / 2);
  }

  .logo {
    margin: 0 2rem 0 0;
  }

  header .wrapper {
    display: flex;
    place-items: flex-start;
    flex-wrap: wrap;
  }
}
</style>

Nginx搭建

https://blog.csdn.net/weixin_46560589/article/details/125661743

#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       8089;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

        # 文件不能下载 设置缓存
        proxy_buffering on;
        proxy_max_temp_file_size 20M;


        location /download/ {

            alias F:\\Java\\utils\\demo\\;

            add_header Content-Disposition: "attachment";

            add_header Content-Type application/octet-stream;

            sendfile on;   # 开启高效文件传输模式

            # 允许请求地址跨域 * 做为通配符
            add_header 'Access-Control-Allow-Origin' '*';
            # 设置请求方法跨域
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
            # 设置是否允许 cookie 传输
            add_header 'Access-Control-Allow-Credentials' 'true';
            # 设置请求头 这里为什么不设置通配符 * 因为不支持
            add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,X-Data-Type,X-Requested-With,X-Data-Type,X-Auth-Token';
            
            # 设置 options 请求处理
            if ( $request_method = 'OPTIONS' ) { 
                return 200;
            }

		
         

        }





        #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;
    #    }
    #}

}