SpringBoot+Nginx实现视频在线点播

作者:微信小助手

发布时间:2023-10-07T17:38:40

原创声明:本人所发内容及涉及源码,均为亲手所撸,如总结内容有误,欢迎指出




唠嗑部分

视频点播方案如下:

1.播放器通过 http协议从http服务器上下载视频文件进行播放

问题:必须等到视频下载完才可以播放,不支持快进到某个时间点进行播放

2.播放器通过rtmp协议连接媒体服务器以实时流方式播放视频

使用rtmp协议需要架设媒体服务器,造价高,对于直播多采用此方案。

3.播放器使用HLS协议连接http服务器(Nginx、Apache等)实现近实时流方式播放视频

HLS协议规定:基于Http协议,视频封装格式为ts,视频的编码格式为H264,音频编码格式为MP3、AAC或者AC-3

什么是HLS?

HLS的工作方式是:将视频拆分成若干ts格式的小文件,通过m3u8格式的索引文件对这些ts小文件建立索引。一般10秒一个ts文件,播放器连接m3u8文件播放,当快进时通过m3u8即可找到对应的索引文件,并去下载对应的ts文件,从而实现快进、快退以近实时的方式播放视频。

IOS、Android设备、及各大浏览器都支持HLS协议。

今天我们使用SpringBoot+Nginx实现一下视频在线点播




言归正传

1、ffmpeg的使用

FFmpeg被许多开源项目采用,QQ影音、暴风影音、VLC等。

下载:FFmpeg  https://www.ffmpeg.org/download.html#build-windows

将安装们目录配置于环境变量之中

检测是否安装成功

生成m3u8/ts文件

使用ffmpeg生成 m3u8的步骤如下:

第一步:先将avi视频转成mp4

       </ul>
       <pre class="code-snippet__js" data-lang="0"><code><span class="code-snippet_outer">ffmpeg<span class="code-snippet__selector-class">.exe</span> -<span class="code-snippet__selector-tag">i</span> &nbsp;lucene<span class="code-snippet__selector-class">.avi</span> -c:v libx264 -s <span class="code-snippet__number">1280</span>x720 -pix_fmt yuv420p -b:a <span class="code-snippet__number">63</span>k -b:v <span class="code-snippet__number">753</span>k -r <span class="code-snippet__number">18</span> .\lucene.mp4</span></code></pre>
      </section>
     </section>
     <p cid="n51" mdtype="paragraph" style="line-height: 1.6rem;orphans: 4;margin-top: 0.8em;margin-bottom: 0.8em;white-space: pre-wrap;word-spacing: 0.05rem;color: rgb(51, 51, 51);font-family: &quot;JetBrains Mono&quot;, Menlo, Consolas;letter-spacing: normal;text-align: start;"><span style="color: rgb(0, 0, 0);font-family: 微软雅黑;font-size: 14px;letter-spacing: 0.544px;orphans: 2;text-align: justify;word-spacing: 0px;background-color: rgb(255, 255, 255);float: none;display: inline !important;">下面把各参数意思大概讲讲,大概了解意思即可,不再此展开流媒体专业知识的讲解。</span></p>
     <p cid="n52" mdtype="paragraph" style="line-height: 1.6rem;orphans: 4;margin-top: 0.8em;margin-bottom: 0.8em;white-space: pre-wrap;word-spacing: 0.05rem;color: rgb(51, 51, 51);font-family: &quot;JetBrains Mono&quot;, Menlo, Consolas;letter-spacing: normal;text-align: start;"><span style="color: rgb(0, 0, 0);font-family: 微软雅黑;font-size: 14px;letter-spacing: 0.544px;orphans: 2;text-align: justify;word-spacing: 0px;background-color: rgb(255, 255, 255);float: none;display: inline !important;">-c:v 视频编码为x264,x264编码是H264的一种开源编码格式。</span></p>
     <p cid="n53" mdtype="paragraph" style="line-height: 1.6rem;orphans: 4;margin-top: 0.8em;margin-bottom: 0.8em;white-space: pre-wrap;word-spacing: 0.05rem;color: rgb(51, 51, 51);font-family: &quot;JetBrains Mono&quot;, Menlo, Consolas;letter-spacing: normal;text-align: start;"><span style="color: rgb(0, 0, 0);font-family: 微软雅黑;font-size: 14px;letter-spacing: 0.544px;orphans: 2;text-align: justify;word-spacing: 0px;background-color: rgb(255, 255, 255);float: none;display: inline !important;">-s 设置分辨率</span></p>
     <p cid="n54" mdtype="paragraph" style="line-height: 1.6rem;orphans: 4;margin-top: 0.8em;margin-bottom: 0.8em;white-space: pre-wrap;word-spacing: 0.05rem;color: rgb(51, 51, 51);font-family: &quot;JetBrains Mono&quot;, Menlo, Consolas;letter-spacing: normal;text-align: start;"><span style="color: rgb(0, 0, 0);font-family: 微软雅黑;font-size: 14px;letter-spacing: 0.544px;orphans: 2;text-align: justify;word-spacing: 0px;background-color: rgb(255, 255, 255);float: none;display: inline !important;">-pix_fmt yuv420p:设置像素采样方式,主流的采样方式有三种,YUV4:4:4,YUV4:2:2,YUV4:2:0,它的作用是根据采样方式来从码流中还原每个像素点的YUV(亮度信息与色彩信息)值。</span></p>
     <p cid="n55" mdtype="paragraph" style="line-height: 1.6rem;orphans: 4;margin-top: 0.8em;margin-bottom: 0.8em;white-space: pre-wrap;word-spacing: 0.05rem;color: rgb(51, 51, 51);font-family: &quot;JetBrains Mono&quot;, Menlo, Consolas;letter-spacing: normal;text-align: start;"><span style="color: rgb(0, 0, 0);font-family: 微软雅黑;font-size: 14px;letter-spacing: 0.544px;orphans: 2;text-align: justify;word-spacing: 0px;background-color: rgb(255, 255, 255);float: none;display: inline !important;">-b 设置码率,-b:a和-b:v分别表示音频的码率和视频的码率,-b表示音频加视频的总码率。码率对一个视频质量有很大的作用。</span></p>
     <p cid="n56" mdtype="paragraph" style="line-height: 1.6rem;orphans: 4;margin-top: 0.8em;margin-bottom: 0.8em;white-space: pre-wrap;word-spacing: 0.05rem;color: rgb(51, 51, 51);font-family: &quot;JetBrains Mono&quot;, Menlo, Consolas;letter-spacing: normal;text-align: start;"><span style="color: rgb(0, 0, 0);font-family: 微软雅黑;font-size: 14px;letter-spacing: 0.544px;orphans: 2;text-align: justify;word-spacing: 0px;background-color: rgb(255, 255, 255);float: none;display: inline !important;">-r:帧率,表示每秒更新图像画面的次数,通常大于24肉眼就没有连贯与停顿的感觉了。</span></p>
     <p cid="n57" mdtype="paragraph" style="line-height: 1.6rem;orphans: 4;margin-top: 0.8em;margin-bottom: 0.8em;white-space: pre-wrap;word-spacing: 0.05rem;color: rgb(51, 51, 51);font-family: &quot;JetBrains Mono&quot;, Menlo, Consolas;letter-spacing: normal;text-align: start;"><span style="color: rgb(0, 0, 0);font-family: 微软雅黑;font-size: 14px;letter-spacing: 0.544px;orphans: 2;text-align: justify;word-spacing: 0px;background-color: rgb(255, 255, 255);float: none;display: inline !important;">第二步:将mp4生成m3u8</span></p>
     <section style="">
      <section class="code-snippet__fix code-snippet__js">
       <ul class="code-snippet__line-index code-snippet__js">
        
       </ul>
       <pre class="code-snippet__js" data-lang="0"><code><span class="code-snippet_outer">ffmpeg <span class="code-snippet__operator">-</span>i <span class="code-snippet__built_in">F</span><span class="code-snippet__operator">:</span>test<span class="code-snippet__punctuation">\</span>test.mp4 <span class="code-snippet__operator">-</span><span class="code-snippet__built_in">c</span><span class="code-snippet__operator">:</span>v copy <span class="code-snippet__operator">-</span><span class="code-snippet__built_in">c</span><span class="code-snippet__operator">:</span>a copy <span class="code-snippet__operator">-</span>f ssegment <span class="code-snippet__operator">-</span>segment_format mpegts <span class="code-snippet__operator">-</span>segment_list <span class="code-snippet__built_in">F</span><span class="code-snippet__operator">:</span><span class="code-snippet__punctuation">\</span>m3u8<span class="code-snippet__punctuation">\</span>hls<span class="code-snippet__punctuation">\</span>test.m3u8 <span class="code-snippet__operator">-</span>segment_time <span class="code-snippet__number">10</span> <span class="code-snippet__built_in">F</span><span class="code-snippet__operator">:</span><span class="code-snippet__punctuation">\</span>m3u8<span class="code-snippet__punctuation">\</span>hls<span class="code-snippet__punctuation">\</span>test%<span class="code-snippet__number">05</span>d.ts</span></code></pre>
      </section>
     </section>
     <p cid="n59" mdtype="paragraph" style="line-height: 1.6rem;orphans: 4;margin-top: 0.8em;margin-bottom: 0.8em;white-space: pre-wrap;word-spacing: 0.05rem;color: rgb(51, 51, 51);font-family: &quot;JetBrains Mono&quot;, Menlo, Consolas;letter-spacing: normal;text-align: start;"><span style="color: rgb(0, 0, 0);font-family: 微软雅黑;font-size: 14px;letter-spacing: 0.544px;orphans: 2;text-align: justify;word-spacing: 0px;background-color: rgb(255, 255, 255);float: none;display: inline !important;">-hls_time 设置每片的长度,单位为秒</span></p>
     <p cid="n60" mdtype="paragraph" style="line-height: 1.6rem;orphans: 4;margin-top: 0.8em;margin-bottom: 0.8em;white-space: pre-wrap;word-spacing: 0.05rem;color: rgb(51, 51, 51);font-family: &quot;JetBrains Mono&quot;, Menlo, Consolas;letter-spacing: normal;text-align: start;"><span style="color: rgb(0, 0, 0);font-family: 微软雅黑;font-size: 14px;letter-spacing: 0.544px;orphans: 2;text-align: justify;word-spacing: 0px;background-color: rgb(255, 255, 255);float: none;display: inline !important;">-hls_list_size n: 保存的分片的数量,设置为0表示保存所有分片</span></p>
     <p cid="n61" mdtype="paragraph" style="line-height: 1.6rem;orphans: 4;margin-top: 0.8em;margin-bottom: 0.8em;white-space: pre-wrap;word-spacing: 0.05rem;color: rgb(51, 51, 51);font-family: &quot;JetBrains Mono&quot;, Menlo, Consolas;letter-spacing: normal;text-align: start;"><span style="color: rgb(0, 0, 0);font-family: 微软雅黑;font-size: 14px;letter-spacing: 0.544px;orphans: 2;text-align: justify;word-spacing: 0px;background-color: rgb(255, 255, 255);float: none;display: inline !important;">-hls_segment_filename :段文件的名称,%05d表示5位数字</span></p>
     <p cid="n62" mdtype="paragraph" style="line-height: 1.6rem;orphans: 4;margin-top: 0.8em;margin-bottom: 0.8em;white-space: pre-wrap;word-spacing: 0.05rem;color: rgb(51, 51, 51);font-family: &quot;JetBrains Mono&quot;, Menlo, Consolas;letter-spacing: normal;text-align: start;"><span style="color: rgb(0, 0, 0);font-family: 微软雅黑;font-size: 14px;letter-spacing: 0.544px;orphans: 2;text-align: justify;word-spacing: 0px;background-color: rgb(255, 255, 255);float: none;display: inline !important;">生成的效果是:将test.mp4视频文件每10秒生成一个ts文件,最后生成一个m3u8文件,m3u8文件是ts的索引文件。</span></p>
     <p cid="n63" mdtype="paragraph" style="line-height: 1.6rem;orphans: 4;margin-top: 0.8em;margin-bottom: 0.8em;white-space: pre-wrap;word-spacing: 0.05rem;color: rgb(51, 51, 51);font-family: &quot;JetBrains Mono&quot;, Menlo, Consolas;letter-spacing: normal;text-align: start;"><span style="color: rgb(0, 0, 0);font-family: 微软雅黑;font-size: 14px;letter-spacing: 0.544px;orphans: 2;text-align: justify;word-spacing: 0px;background-color: rgb(255, 255, 255);float: none;display: inline !important;">使用VLC打开m3u8文件,测试播放效果,VLC 是一款自由、开源的跨平台多媒体播放器及框架,可播放大多数多媒体文件,以及 DVD、音频 CD、VCD 及各类流媒体协议。</span></p>
     <p cid="n64" mdtype="paragraph" style="line-height: 1.6rem;orphans: 4;margin-top: 0.8em;margin-bottom: 0.8em;white-space: pre-wrap;word-spacing: 0.05rem;color: rgb(51, 51, 51);font-family: &quot;JetBrains Mono&quot;, Menlo, Consolas;letter-spacing: normal;text-align: start;"><span md-inline="strong"><strong>m3u8转MP4文件</strong></span></p>
     <section style="">
      <section class="code-snippet__fix code-snippet__js">
       <ul class="code-snippet__line-index code-snippet__js">
        
       </ul>
       <pre class="code-snippet__js" data-lang="0"><code><span class="code-snippet_outer">ffmpeg <span class="code-snippet__operator">-</span>i <span class="code-snippet__built_in">F</span><span class="code-snippet__operator">:</span><span class="code-snippet__punctuation">\</span>m3u8<span class="code-snippet__punctuation">\</span>hls<span class="code-snippet__punctuation">\</span><span class="code-snippet__number">1679771418348863490</span><span class="code-snippet__punctuation">\</span><span class="code-snippet__number">1679771418348863490.</span>m3u8 <span class="code-snippet__operator">-</span><span class="code-snippet__built_in">c</span> copy aaaa.mp4</span></code></pre>
      </section>
     </section>
     <p cid="n227" mdtype="paragraph" style="line-height: 1.6rem;orphans: 4;margin-top: 0.8em;margin-bottom: 0.8em;white-space: pre-wrap;word-spacing: 0.05rem;color: rgb(51, 51, 51);font-family: &quot;JetBrains Mono&quot;, Menlo, Consolas;letter-spacing: normal;text-align: start;"><span style="font-family: 微软雅黑;font-size: 15px;"><strong>2、Java使用ffmpeg生成m3u8</strong></span></p>
     <section style="">
      <section class="code-snippet__fix code-snippet__js">
       <ul class="code-snippet__line-index code-snippet__js">
        
        
        
        
       </ul>
       <pre class="code-snippet__js" data-lang="0"><code><span class="code-snippet_outer"><span class="code-snippet__attr">file:</span></span></code><code><span class="code-snippet_outer"> &nbsp;upload<span class="code-snippet__operator">-</span>path: <span class="code-snippet__string">"F:<span class="code-snippet__subst">\\</span>movie_online"</span></span></code><code><span class="code-snippet_outer"> &nbsp;<span class="code-snippet__attr">ffmpeg-path:</span> <span class="code-snippet__string">D:\\software\\ffmpeg\\ffmpeg-n5.1-latest-win64-lgpl-shared-5.1\\bin\\ffmpeg.exe</span></span></code><code><span class="code-snippet_outer"> &nbsp;target<span class="code-snippet__operator">-</span>path: <span class="code-snippet__string">"F:<span class="code-snippet__subst">\\</span>m3u8<span class="code-snippet__subst">\\</span>hls<span class="code-snippet__subst">\\</span>"</span></span></code></pre>
      </section>
     </section>
     <p cid="n227" mdtype="paragraph" style="line-height: 1.6rem;orphans: 4;margin-top: 0.8em;margin-bottom: 0.8em;white-space: pre-wrap;word-spacing: 0.05rem;color: rgb(51, 51, 51);font-family: &quot;JetBrains Mono&quot;, Menlo, Consolas;letter-spacing: normal;text-align: start;"><strong>代码</strong></p>
     <p cid="n227" mdtype="paragraph" style="line-height: 1.6rem;orphans: 4;margin-top: 0.8em;margin-bottom: 0.8em;white-space: pre-wrap;word-spacing: 0.05rem;color: rgb(51, 51, 51);font-family: &quot;JetBrains Mono&quot;, Menlo, Consolas;letter-spacing: normal;text-align: start;"><span style="color: rgb(0, 0, 0);font-family: 微软雅黑;font-size: 14px;letter-spacing: 0.544px;orphans: 2;text-align: justify;word-spacing: 0px;background-color: rgb(255, 255, 255);float: none;display: inline !important;">FileHandleUtil.java</span></p>
     <section style="">
      <section class="code-snippet__fix code-snippet__js">
       <ul class="code-snippet__line-index code-snippet__js">
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
       </ul>
       <pre class="code-snippet__js" data-lang="0"><code><span class="code-snippet_outer"><span class="code-snippet__variable">@Component</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">public</span> <span class="code-snippet__class"><span class="code-snippet__keyword">class</span> <span class="code-snippet__title">FileHandleUtil</span> </span>{</span></code><code><span class="code-snippet_outer"><br></span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp;<span class="code-snippet__meta">@Value(<span class="code-snippet__string">"<span class="code-snippet__subst">${file.upload-path}</span>"</span>)</span></span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp;<span class="code-snippet__keyword">private</span> <span class="code-snippet__keyword">String</span> filePath;</span></code><code><span class="code-snippet_outer"><br></span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp;<span class="code-snippet__meta">@Value(<span class="code-snippet__string">"<span class="code-snippet__subst">${file.target-path}</span>"</span>)</span></span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp;<span class="code-snippet__keyword">private</span> <span class="code-snippet__keyword">String</span> targetPath;</span></code><code><span class="code-snippet_outer"><br></span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp;<span class="code-snippet__meta">@Value(<span class="code-snippet__string">"<span class="code-snippet__subst">${file.ffmpeg-path}</span>"</span>)</span></span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp;<span class="code-snippet__keyword">private</span> <span class="code-snippet__keyword">String</span> ffmpegPath;</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp;<span class="code-snippet__comment">// originFile &nbsp;文件全路径 &nbsp; fileName &nbsp; 文件名</span></span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp;<span class="code-snippet__keyword">public</span> <span class="code-snippet__built_in">void</span> <span class="code-snippet__title">mp4TranM3u8Ts</span>(<span class="code-snippet__params"><span class="code-snippet__built_in">String</span> originFile, <span class="code-snippet__built_in">String</span> fileName</span>){</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__type">File</span> <span class="code-snippet__variable">file</span> <span class="code-snippet__operator">=</span> <span class="code-snippet__keyword">new</span> <span class="code-snippet__title">File</span>(originFile);</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp;if (!file.exists()) {</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__keyword">throw</span> <span class="code-snippet__keyword">new</span> <span class="code-snippet__built_in">RuntimeException</span>(<span class="code-snippet__string">"源文件不存在"</span>);</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp;}</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__type">File</span> <span class="code-snippet__variable">targetFile</span> <span class="code-snippet__operator">=</span> <span class="code-snippet__keyword">new</span> <span class="code-snippet__title">File</span>(targetPath + fileName);</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp;if (!targetFile.exists()) {</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;targetFile<span class="code-snippet__selector-class">.mkdir</span>();</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp;}</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__type">String</span> output = targetPath + fileName + File.separator + fileName + <span class="code-snippet__string">".m3u8"</span>;</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__keyword">List</span>&lt;<span class="code-snippet__keyword">String</span>&gt; command = <span class="code-snippet__keyword">new</span> ArrayList&lt;&gt;();</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp;command.<span class="code-snippet__keyword">add</span>(ffmpegPath);</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp;command.<span class="code-snippet__keyword">add</span>(<span class="code-snippet__string">"-i"</span>);</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp;command.<span class="code-snippet__keyword">add</span>(originFile);</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp;command.<span class="code-snippet__keyword">add</span>(<span class="code-snippet__string">"-c:v"</span>);</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp;command.<span class="code-snippet__keyword">add</span>(<span class="code-snippet__string">"copy"</span>);</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp;command.<span class="code-snippet__keyword">add</span>(<span class="code-snippet__string">"-c:a"</span>);</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp;command.<span class="code-snippet__keyword">add</span>(<span class="code-snippet__string">"copy"</span>);</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp;command.<span class="code-snippet__keyword">add</span>(<span class="code-snippet__string">"-f"</span>);</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp;command.<span class="code-snippet__keyword">add</span>(<span class="code-snippet__string">"ssegment"</span>);</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp;command.<span class="code-snippet__keyword">add</span>(<span class="code-snippet__string">"-segment_format"</span>);</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp;command.<span class="code-snippet__keyword">add</span>(<span class="code-snippet__string">"mpegts"</span>);</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp;command.<span class="code-snippet__keyword">add</span>(<span class="code-snippet__string">"-segment_list"</span>);</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp;command.add(<span class="code-snippet__built_in">output</span>);</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp;command.<span class="code-snippet__keyword">add</span>(<span class="code-snippet__string">"-segment_time"</span>);</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp;command.<span class="code-snippet__keyword">add</span>(<span class="code-snippet__string">"10"</span>);</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__type">String</span> ts = targetPath + fileName + File.separator + fileName + <span class="code-snippet__string">"%05d.ts"</span>;</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp;command.<span class="code-snippet__keyword">add</span>(ts);</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp;ProcessUtil.execute(<span class="code-snippet__built_in">command</span>);</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp;}</span></code><code><span class="code-snippet_outer">}</span></code></pre>
      </section>
     </section>
     <p cid="n227" mdtype="paragraph" style="line-height: 1.6rem;orphans: 4;margin-top: 0.8em;margin-bottom: 0.8em;white-space: pre-wrap;word-spacing: 0.05rem;color: rgb(51, 51, 51);font-family: &quot;JetBrains Mono&quot;, Menlo, Consolas;letter-spacing: normal;text-align: start;"><strong><span md-inline="plain"></span></strong><span style="color: rgb(0, 0, 0);font-family: 微软雅黑;font-size: 14px;letter-spacing: 0.544px;orphans: 2;text-align: justify;word-spacing: 0px;background-color: rgb(255, 255, 255);float: none;display: inline !important;">ProcessUtil.java</span></p>
     <section style="">
      <section class="code-snippet__fix code-snippet__js">
       <ul class="code-snippet__line-index code-snippet__js">
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
       </ul>
       <pre class="code-snippet__js" data-lang="0"><code><span class="code-snippet_outer"><span class="code-snippet__keyword">public</span> <span class="code-snippet__class"><span class="code-snippet__keyword">class</span> <span class="code-snippet__title">ProcessUtil</span> </span>{</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp;<span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">static</span> <span class="code-snippet__title">String</span> <span class="code-snippet__title">execute</span>(<span class="code-snippet__params">List&lt;<span class="code-snippet__built_in">String</span>&gt; command</span>) {</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__type">StringBuffer</span> <span class="code-snippet__variable">inputStringBuffer</span> <span class="code-snippet__operator">=</span> <span class="code-snippet__keyword">new</span> <span class="code-snippet__title">StringBuffer</span>();</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__type">StringBuffer</span> <span class="code-snippet__variable">errorStringBuffer</span> <span class="code-snippet__operator">=</span> <span class="code-snippet__keyword">new</span> <span class="code-snippet__title">StringBuffer</span>();</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__keyword">try</span> {</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__type">ProcessBuilder</span> <span class="code-snippet__variable">builder</span> <span class="code-snippet__operator">=</span> <span class="code-snippet__keyword">new</span> <span class="code-snippet__title">ProcessBuilder</span>(command);</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__type">Process</span> <span class="code-snippet__variable">process</span> <span class="code-snippet__operator">=</span> builder.start();</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.<span class="code-snippet__built_in">println</span>(<span class="code-snippet__string">"============inputStream============"</span>);</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__comment">// 处理InputStream</span></span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__type">Thread</span> <span class="code-snippet__variable">t1</span> <span class="code-snippet__operator">=</span> <span class="code-snippet__keyword">new</span> <span class="code-snippet__title">Thread</span>(() -&gt; {</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__type">InputStream</span> <span class="code-snippet__variable">input</span> <span class="code-snippet__operator">=</span> <span class="code-snippet__literal">null</span>;</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__type">InputStreamReader</span> <span class="code-snippet__variable">reader</span> <span class="code-snippet__operator">=</span> <span class="code-snippet__literal">null</span>;</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__type">BufferedReader</span> <span class="code-snippet__variable">buffer</span> <span class="code-snippet__operator">=</span> <span class="code-snippet__literal">null</span>;</span></code><code><span class="code-snippet_outer"><br></span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__keyword">try</span> {</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__attr">input</span> = process.getInputStream()<span class="code-snippet__comment">;</span></span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__attr">reader</span> = new InputStreamReader(input)<span class="code-snippet__comment">;</span></span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__attr">buffer</span> = new BufferedReader(reader)<span class="code-snippet__comment">;</span></span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__type">String</span> inputLine = <span class="code-snippet__string">""</span>;</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__string">while</span> <span class="code-snippet__string">((inputLine</span> <span class="code-snippet__string">=</span> <span class="code-snippet__string">buffer.readLine())</span> <span class="code-snippet__type">!=</span> <span class="code-snippet__literal">null</span><span class="code-snippet__string">)</span> {</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.<span class="code-snippet__built_in">println</span>(inputLine);</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;inputStringBuffer.<span class="code-snippet__built_in">append</span>(inputLine);</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__comment">//退出循环后表示结束流</span></span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.<span class="code-snippet__built_in">println</span>(<span class="code-snippet__string">"===》》退出循环后表示结束流"</span>);</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} <span class="code-snippet__keyword">catch</span> (IOException e) {</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;e<span class="code-snippet__selector-class">.printStackTrace</span>();</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} <span class="code-snippet__keyword">finally</span> {</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__keyword">try</span> {</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__string">if</span> <span class="code-snippet__string">(buffer</span> <span class="code-snippet__type">!=</span> <span class="code-snippet__literal">null</span><span class="code-snippet__string">)</span> {</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;buffer.<span class="code-snippet__built_in">close</span>();</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__string">if</span> <span class="code-snippet__string">(reader</span> <span class="code-snippet__type">!=</span> <span class="code-snippet__literal">null</span><span class="code-snippet__string">)</span> {</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;reader.<span class="code-snippet__built_in">close</span>();</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__string">if</span> <span class="code-snippet__string">(input</span> <span class="code-snippet__type">!=</span> <span class="code-snippet__literal">null</span><span class="code-snippet__string">)</span> {</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__built_in">input</span>.<span class="code-snippet__built_in">close</span>();</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} <span class="code-snippet__keyword">catch</span> (IOException e) {</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;e<span class="code-snippet__selector-class">.printStackTrace</span>();</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;});</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;t1.setName(<span class="code-snippet__string">"deviceName"</span>);</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;t1<span class="code-snippet__selector-class">.start</span>();</span></code><code><span class="code-snippet_outer"><br></span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.<span class="code-snippet__built_in">println</span>(<span class="code-snippet__string">"============errorStream============"</span>);</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__comment">// 处理ErrorStream</span></span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;new <span class="code-snippet__title">Thread</span>(() <span class="code-snippet__punctuation">-&gt;</span> {</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__type">InputStream</span> <span class="code-snippet__variable">input</span> <span class="code-snippet__operator">=</span> <span class="code-snippet__literal">null</span>;</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__type">InputStreamReader</span> <span class="code-snippet__variable">reader</span> <span class="code-snippet__operator">=</span> <span class="code-snippet__literal">null</span>;</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__type">BufferedReader</span> <span class="code-snippet__variable">buffer</span> <span class="code-snippet__operator">=</span> <span class="code-snippet__literal">null</span>;</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__keyword">try</span> {</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__attr">input</span> = process.getErrorStream()<span class="code-snippet__comment">;</span></span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__attr">reader</span> = new InputStreamReader(input)<span class="code-snippet__comment">;</span></span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__attr">buffer</span> = new BufferedReader(reader)<span class="code-snippet__comment">;</span></span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__type">String</span> errorLine = <span class="code-snippet__string">""</span>;</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__string">while</span> <span class="code-snippet__string">((errorLine</span> <span class="code-snippet__string">=</span> <span class="code-snippet__string">buffer.readLine())</span> <span class="code-snippet__type">!=</span> <span class="code-snippet__literal">null</span><span class="code-snippet__string">)</span> {</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.<span class="code-snippet__built_in">println</span>(errorLine);</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;errorStringBuffer.<span class="code-snippet__built_in">append</span>(errorLine);</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} <span class="code-snippet__keyword">catch</span> (IOException e) {</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;e<span class="code-snippet__selector-class">.printStackTrace</span>();</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} <span class="code-snippet__keyword">finally</span> {</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__keyword">try</span> {</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__string">if</span> <span class="code-snippet__string">(buffer</span> <span class="code-snippet__type">!=</span> <span class="code-snippet__literal">null</span><span class="code-snippet__string">)</span> {</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;buffer.<span class="code-snippet__built_in">close</span>();</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__string">if</span> <span class="code-snippet__string">(reader</span> <span class="code-snippet__type">!=</span> <span class="code-snippet__literal">null</span><span class="code-snippet__string">)</span> {</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;reader.<span class="code-snippet__built_in">close</span>();</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__string">if</span> <span class="code-snippet__string">(input</span> <span class="code-snippet__type">!=</span> <span class="code-snippet__literal">null</span><span class="code-snippet__string">)</span> {</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__built_in">input</span>.<span class="code-snippet__built_in">close</span>();</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} <span class="code-snippet__keyword">catch</span> (IOException e) {</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;e<span class="code-snippet__selector-class">.printStackTrace</span>();</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;})<span class="code-snippet__selector-class">.start</span>();</span></code><code><span class="code-snippet_outer"><br></span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/**</span></code><code><span class="code-snippet_outer"><span class="code-snippet__bullet"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *</span> 只会存在一个输入流返回</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; */</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__string">if</span> <span class="code-snippet__string">(inputStringBuffer</span> <span class="code-snippet__type">!=</span> <span class="code-snippet__literal">null</span><span class="code-snippet__string">)</span> {</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__keyword">return</span> inputStringBuffer.toString();</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__string">if</span> <span class="code-snippet__string">(errorStringBuffer</span> <span class="code-snippet__type">!=</span> <span class="code-snippet__literal">null</span><span class="code-snippet__string">)</span> {</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__keyword">return</span> errorStringBuffer.toString();</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</span></code><code><span class="code-snippet_outer"><br></span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp;} <span class="code-snippet__keyword">catch</span> (<span class="code-snippet__built_in">Exception</span> e) {</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;e<span class="code-snippet__selector-class">.printStackTrace</span>();</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__keyword">return</span> <span class="code-snippet__literal">null</span>;</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp;}</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__keyword">return</span> <span class="code-snippet__literal">null</span>;</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp;}</span></code><code><span class="code-snippet_outer">}</span></code></pre>
      </section>
     </section>
     <p cid="n72" mdtype="paragraph" style="line-height: 1.6rem;orphans: 4;margin-top: 0.8em;margin-bottom: 0.8em;white-space: pre-wrap;word-spacing: 0.05rem;color: rgb(51, 51, 51);font-family: &quot;JetBrains Mono&quot;, Menlo, Consolas;letter-spacing: normal;text-align: start;"><span style="color: rgb(0, 0, 0);font-family: 微软雅黑;font-size: 14px;letter-spacing: 0.544px;orphans: 2;text-align: justify;word-spacing: 0px;background-color: rgb(255, 255, 255);float: none;display: inline !important;">文件上传部分省略了,在文件上传后,使用上述工具类将视频转为m3u8/ts文件,保存在配置好的F:\m3u8\hls\目录即可</span></p>
     <p cid="n83" mdtype="paragraph" style="line-height: 1.6rem;orphans: 4;margin-top: 0.8em;margin-bottom: 0.8em;white-space: pre-wrap;word-spacing: 0.05rem;color: rgb(51, 51, 51);font-family: &quot;JetBrains Mono&quot;, Menlo, Consolas;letter-spacing: normal;text-align: start;"><span style="font-family: 微软雅黑;font-size: 15px;"><strong>3、Nginx文件服务器配置</strong></span></p>
     <section style="">
      <section class="code-snippet__fix code-snippet__js">
       <ul class="code-snippet__line-index code-snippet__js">
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
       </ul>
       <pre class="code-snippet__js" data-lang="0"><code><span class="code-snippet_outer">server {</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__keyword">listen</span> &nbsp; &nbsp; &nbsp; <span class="code-snippet__number">80</span>;</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp;server_name &nbsp;localhost;</span></code><code><span class="code-snippet_outer">	 &nbsp; &nbsp;charset utf-8;</span></code><code><span class="code-snippet_outer"><br></span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp;location ^~ <span class="code-snippet__regexp">/hls/</span> {</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;add_header Access-Control-Allow-Origin *;</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;add_header Access<span class="code-snippet__operator">-</span>Control<span class="code-snippet__operator">-</span>Allow<span class="code-snippet__operator">-</span>Headers X<span class="code-snippet__operator">-</span>Requested<span class="code-snippet__operator">-</span><span class="code-snippet__keyword">With</span>;</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;add_header Access<span class="code-snippet__operator">-</span>Control<span class="code-snippet__operator">-</span>Allow<span class="code-snippet__operator">-</span>Methods <span class="code-snippet__keyword">GET</span>,POST,PUT,<span class="code-snippet__keyword">DELETE</span>,OPTIONS;</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;types { &nbsp;</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;application/vnd.apple.mpegurl m3u8; &nbsp;</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__selector-tag">video</span>/mp2t ts; &nbsp;</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;alias &nbsp;<span class="code-snippet__attr">F</span>:<span class="code-snippet__regexp">/m3u8/</span>hls/;</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;expires -1;</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__string">add_header</span> <span class="code-snippet__string">Cache-Control</span> <span class="code-snippet__literal">no</span><span class="code-snippet__string">-cache;</span> &nbsp;</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp;}</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp;}</span></code></pre>
      </section>
     </section>
     <p cid="n86" mdtype="paragraph" style="line-height: 1.6rem;orphans: 4;margin-top: 0.8em;margin-bottom: 0.8em;white-space: pre-wrap;word-spacing: 0.05rem;color: rgb(51, 51, 51);font-family: &quot;JetBrains Mono&quot;, Menlo, Consolas;letter-spacing: normal;text-align: start;"><span style="color: rgb(0, 0, 0);font-family: 微软雅黑;font-size: 14px;letter-spacing: 0.544px;orphans: 2;text-align: justify;word-spacing: 0px;background-color: rgb(255, 255, 255);float: none;display: inline !important;">文件夹目录</span></p>
     <p cid="n107" mdtype="paragraph" style="line-height: 1.6rem;orphans: 4;margin-top: 0.8em;margin-bottom: 0.8em;white-space: pre-wrap;word-spacing: 0.05rem;color: rgb(51, 51, 51);font-family: &quot;JetBrains Mono&quot;, Menlo, Consolas;letter-spacing: normal;text-align: start;"><span md-inline="image" src="https://gitee.com/cxs_git_com/imgs_repository/raw/master/imgs/image-20230717140711254.png" style="min-width: 10px;min-height: 10px;word-break: break-all;font-family: monospace;vertical-align: top;display: inline-block;width: 1080px;"><img class="rich_pages wxw-img" data-ratio="0.4324074074074074" src="/upload/682ae959862dd7107e44bb7899cf4e34.png" data-type="png" data-w="1080" style="border-width: 0px 4px 0px 2px;border-top-style: initial;border-right-style: solid;border-bottom-style: initial;border-left-style: solid;border-top-color: initial;border-right-color: transparent;border-bottom-color: initial;border-left-color: transparent;vertical-align: middle;image-orientation: from-image;cursor: default;border-radius: 2px;display: block;margin: auto;"></span></p>
     <p cid="n104" mdtype="paragraph" style="line-height: 1.6rem;orphans: 4;margin-top: 0.8em;margin-bottom: 0.8em;white-space: pre-wrap;word-spacing: 0.05rem;color: rgb(51, 51, 51);font-family: &quot;JetBrains Mono&quot;, Menlo, Consolas;letter-spacing: normal;text-align: start;"><span style="font-family: 微软雅黑;font-size: 15px;"><strong>4、H5代码</strong></span></p>
     <p cid="n97" mdtype="paragraph" style="line-height: 1.6rem;orphans: 4;margin-top: 0.8em;margin-bottom: 0.8em;white-space: pre-wrap;word-spacing: 0.05rem;color: rgb(51, 51, 51);font-family: &quot;JetBrains Mono&quot;, Menlo, Consolas;letter-spacing: normal;text-align: start;"><span style="color: rgb(0, 0, 0);font-family: 微软雅黑;font-size: 14px;letter-spacing: 0.544px;orphans: 2;text-align: justify;word-spacing: 0px;background-color: rgb(255, 255, 255);float: none;display: inline !important;">需要使用两个库,可在cdn下载,相关资料也会上传群文件</span></p>
     <section style="">
      <section class="code-snippet__fix code-snippet__js">
       <ul class="code-snippet__line-index code-snippet__js">
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
       </ul>
       <pre class="code-snippet__js" data-lang="0"><code><span class="code-snippet_outer"><span class="code-snippet__meta">&lt;!DOCTYPE <span class="code-snippet__keyword">html</span>&gt;</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__tag">&lt;<span class="code-snippet__name">html</span> <span class="code-snippet__attr">lang</span>=<span class="code-snippet__string">"en"</span>&gt;</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__tag">&lt;<span class="code-snippet__name">head</span>&gt;</span></span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp;<span class="code-snippet__tag">&lt;<span class="code-snippet__name">meta</span> <span class="code-snippet__attr">charset</span>=<span class="code-snippet__string">"UTF-8"</span>&gt;</span></span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp;<span class="code-snippet__tag">&lt;<span class="code-snippet__name">title</span>&gt;</span>测试视频播放<span class="code-snippet__tag">&lt;/<span class="code-snippet__name">title</span>&gt;</span></span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp;<span class="code-snippet__tag">&lt;<span class="code-snippet__name">script</span> <span class="code-snippet__attr">type</span>=<span class="code-snippet__string">"text/javascript"</span> <span class="code-snippet__attr">src</span>=<span class="code-snippet__string">"video-6.7.3.js"</span>&gt;</span><span class="code-snippet__tag">&lt;/<span class="code-snippet__name">script</span>&gt;</span></span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp;<span class="code-snippet__tag">&lt;<span class="code-snippet__name">script</span> <span class="code-snippet__attr">type</span>=<span class="code-snippet__string">"text/javascript"</span> <span class="code-snippet__attr">src</span>=<span class="code-snippet__string">"videojs-contrib-hls-5.14.1.min.js"</span>&gt;</span><span class="code-snippet__tag">&lt;/<span class="code-snippet__name">script</span>&gt;</span></span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp;&lt;link href=<span class="code-snippet__string">"video.js_6.7.3.min.css"</span> rel=<span class="code-snippet__string">"stylesheet"</span> <span class="code-snippet__built_in">type</span>=<span class="code-snippet__string">"text/css"</span>/&gt;</span></code><code><span class="code-snippet_outer"><span class="code-snippet__tag">&lt;/<span class="code-snippet__name">head</span>&gt;</span></span></code><code><span class="code-snippet_outer">&lt;<span class="code-snippet__selector-tag">body</span>&gt;</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp;&lt;video <span class="code-snippet__keyword">id</span>=<span class="code-snippet__string">"example-video"</span> width=<span class="code-snippet__string">"800"</span> height=<span class="code-snippet__string">"600"</span> controls poster=<span class="code-snippet__string">"http://127.0.0.1:8888/hls/add.png"</span> <span class="code-snippet__keyword">class</span>=<span class="code-snippet__string">"video-js vjs-default-skin vjs-big-play-centered"</span>&gt;</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp;&lt;<span class="code-snippet__built_in">source</span> src=<span class="code-snippet__string">"http://127.0.0.1/hls/1679771414267805698/1679771414267805698.m3u8"</span> <span class="code-snippet__built_in">type</span>=<span class="code-snippet__string">"application/x-mpegURL"</span>&gt;</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp;&lt;/<span class="code-snippet__selector-tag">video</span>&gt;</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp;&lt;<span class="code-snippet__built_in">input</span> <span class="code-snippet__built_in">type</span>=<span class="code-snippet__string">"button"</span> onclick=<span class="code-snippet__string">"switchvideo()"</span> value=<span class="code-snippet__string">"switch"</span>/&gt;</span></code><code><span class="code-snippet_outer"><span class="code-snippet__tag">&lt;<span class="code-snippet__name">script</span> <span class="code-snippet__attr">type</span>=<span class="code-snippet__string">"text/javascript"</span>&gt;</span></span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp;<span class="code-snippet__keyword">var</span> player = videojs(<span class="code-snippet__string">'example-video'</span>);</span></code><code><span class="code-snippet_outer"><br></span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp;<span class="code-snippet__function"><span class="code-snippet__keyword">function</span> <span class="code-snippet__title">switchvideo</span>(<span class="code-snippet__params"></span>) </span>{</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp;player.src({</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;src: <span class="code-snippet__symbol">'http</span>:<span class="code-snippet__comment">//127.0.0.1/hls/1679769948731760641/1679769948731760641.m3u8',</span></span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__built_in">type</span>: <span class="code-snippet__string">'application/x-mpegURL'</span>,</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="code-snippet__attr">withCredentials:</span> <span class="code-snippet__literal">true</span></span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp;})</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp; &nbsp; &nbsp;player<span class="code-snippet__selector-class">.play</span>();</span></code><code><span class="code-snippet_outer"> &nbsp; &nbsp;}</span></code><code><span class="code-snippet_outer"><span class="code-snippet__tag">&lt;/<span class="code-snippet__name">script</span>&gt;</span></span></code><code><span class="code-snippet_outer">&lt;/<span class="code-snippet__selector-tag">body</span>&gt;</span></code><code><span class="code-snippet_outer">&lt;/<span class="code-snippet__selector-tag">html</span>&gt;</span></code></pre>
      </section>
     </section>
     <p cid="n95" mdtype="paragraph" style="line-height: 1.6rem;orphans: 4;margin-top: 0.8em;margin-bottom: 0.8em;white-space: pre-wrap;word-spacing: 0.05rem;color: rgb(51, 51, 51);font-family: &quot;JetBrains Mono&quot;, Menlo, Consolas;letter-spacing: normal;text-align: start;"><span style="font-family: 微软雅黑;font-size: 15px;"><strong>5、测试</strong></span></p>
     <p cid="n110" mdtype="paragraph" style="line-height: 1.6rem;orphans: 4;margin-top: 0.8em;margin-bottom: 0.8em;white-space: pre-wrap;word-spacing: 0.05rem;color: rgb(51, 51, 51);font-family: &quot;JetBrains Mono&quot;, Menlo, Consolas;letter-spacing: normal;text-align: start;"><span style="color: rgb(0, 0, 0);font-family: 微软雅黑;font-size: 14px;letter-spacing: 0.544px;orphans: 2;text-align: justify;word-spacing: 0px;background-color: rgb(255, 255, 255);float: none;display: inline !important;">在nginx安装目录下启动nginx,打开页面</span></p>
     <p cid="n100" mdtype="paragraph" style="line-height: 1.6rem;orphans: 4;margin-top: 0.8em;margin-bottom: 0.8em;white-space: pre-wrap;word-spacing: 0.05rem;color: rgb(51, 51, 51);font-family: &quot;JetBrains Mono&quot;, Menlo, Consolas;letter-spacing: normal;text-align: start;"><span md-inline="image" src="https://gitee.com/cxs_git_com/imgs_repository/raw/master/imgs/image-20230717140844431.png" style="min-width: 10px;min-height: 10px;word-break: break-all;font-family: monospace;vertical-align: top;display: inline-block;width: 1080px;"><img class="rich_pages wxw-img" data-ratio="0.4601851851851852" src="/upload/43fcd180e3c8b0b107061f5317cab8fa.png" data-type="png" data-w="1080" style="border-width: 0px 4px 0px 2px;border-top-style: initial;border-right-style: solid;border-bottom-style: initial;border-left-style: solid;border-top-color: initial;border-right-color: transparent;border-bottom-color: initial;border-left-color: transparent;vertical-align: middle;image-orientation: from-image;cursor: default;border-radius: 2px;display: block;margin: auto;"></span></p>
    </section>
    


结语

1、完结撒花,制作不易,一键四连再走吧,您的支持永远是我最大的动力!