请求指导:基于Spring Boot/Node.js搭建自适应码率视频流服务器
搭建自适应码率视频流服务器(Spring Boot / Node.js 方案)
自适应码率视频流的核心是通过HLS/DASH协议,让客户端根据当前网络带宽自动切换不同分辨率/码率的视频流。下面是两种落地方案:
Spring Boot 实现方案
1. 前置准备
- 安装FFmpeg:用于将原始视频转码为多码率的HLS分片文件
- 创建Spring Boot项目,引入
spring-boot-starter-web依赖
2. 预转码视频生成HLS流
用FFmpeg生成包含多码率的HLS索引和分片,示例命令:
ffmpeg -i input.mp4 \ -vf scale=640:360 -c:v libx264 -crf 28 -b:v 800k -c:a aac -b:a 128k 360p.m3u8 \ -vf scale=1280:720 -c:v libx264 -crf 23 -b:v 2500k -c:a aac -b:a 192k 720p.m3u8 \ -master_pl_name master.m3u8
执行后会生成360p、720p的分片文件,以及主索引master.m3u8——客户端将通过这个索引文件自动选择适配网络的码率。
3. 配置静态资源目录
在application.properties中指定视频文件的存储路径:
spring.web.resources.static-locations=file:/your/local/video/storage/path/ spring.web.resources.cache.period=0 # 开发环境关闭缓存,生产环境可按需调整
4. 可选:自定义流接口(权限/动态逻辑)
如果需要添加权限验证或动态生成索引,可编写Controller:
import org.springframework.core.io.FileSystemResource; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import java.io.File; @RestController @RequestMapping("/stream") public class VideoStreamController { private static final String VIDEO_STORAGE = "/your/local/video/storage/path/"; @GetMapping("/{filename}") public ResponseEntity<FileSystemResource> getVideoFile(@PathVariable String filename) { File file = new File(VIDEO_STORAGE + filename); if (!file.exists()) { return ResponseEntity.notFound().build(); } MediaType mediaType = filename.endsWith(".m3u8") ? MediaType.parseMediaType("application/vnd.apple.mpegurl") : MediaType.parseMediaType("video/MP2T"); return ResponseEntity.ok() .contentType(mediaType) .body(new FileSystemResource(file)); } }
5. 客户端播放
使用兼容HLS的播放器(如video.js)加载主索引文件:
<video id="video-player" class="video-js" controls preload="auto" width="1280" height="720"> <source src="http://your-server-ip/stream/master.m3u8" type="application/vnd.apple.mpegurl"> </video> <script src="https://vjs.zencdn.net/8.6.1/video.min.js"></script> <link href="https://vjs.zencdn.net/8.6.1/video-js.css" rel="stylesheet" />
Node.js 实现方案
1. 前置准备
- 安装FFmpeg(同上)
- 初始化Node.js项目:
npm init -y - 安装Express框架:
npm install express
2. 视频转码(同Spring Boot步骤)
用相同的FFmpeg命令生成多码率HLS流文件。
3. 搭建Express服务
创建server.js:
const express = require('express'); const path = require('path'); const app = express(); const PORT = 3000; // 配置视频文件存储目录 const VIDEO_STORAGE = '/your/local/video/storage/path/'; app.use('/stream', express.static(VIDEO_STORAGE, { setHeaders: (res, filePath) => { if (filePath.endsWith('.m3u8')) { res.setHeader('Content-Type', 'application/vnd.apple.mpegurl'); } else if (filePath.endsWith('.ts')) { res.setHeader('Content-Type', 'video/MP2T'); } } })); app.listen(PORT, () => { console.log(`Stream server running at http://localhost:${PORT}`); });
4. 启动服务
node server.js
客户端播放方式与Spring Boot方案完全一致。
关键说明
- 自适应码率的切换逻辑由客户端播放器完成,服务器仅需提供正确的HLS/DASH文件和MIME类型。
- 生产环境可优化点:添加CDN加速、缓存策略、视频分片按需生成、访问权限控制等。
- FFmpeg参数可调整:CRF值越小画质越好(文件也越大),码率可根据目标设备网络环境灵活设置。
内容的提问来源于stack exchange,提问作者Usama Shoaib
相关产品推荐
相关产品推荐

