file:
upload-path: "F:\\movie_online"
ffmpeg-path: D:\\software\\ffmpeg\\ffmpeg-n5.1-latest-win64-lgpl-shared-5.1\\bin\\ffmpeg.exe
target-path: "F:\\m3u8\\hls\\"
FileHandleUtil.java
@Value("${file.ffmpeg-path}")
private String ffmpegPath;
// originFile 文件全路径 fileName 文件名
public void mp4TranM3u8Ts(String originFile, String fileName){
File file = new File(originFile);
if (!file.exists()) {
throw new RuntimeException("源文件不存在");
File targetFile = new File(targetPath + fileName);
if (!targetFile.exists()) {
targetFile.mkdir();
String output = targetPath + fileName + File.separator + fileName + ".m3u8";
List<String> command = new ArrayList<>();
command.add(ffmpegPath);
command.add("-i");
command.add(originFile);
command.add("-c:v");
command.add("copy");
command.add("-c:a");
command.add("copy");
command.add("-f");
command.add("ssegment");
command.add("-segment_format");
command.add("mpegts");
command.add("-segment_list");
command.add(output);
command.add("-segment_time");
command.add("10");
String ts = targetPath + fileName + File.separator + fileName + "%05d.ts";
command.add(ts);
ProcessUtil.execute(command);
ProcessUtil.java
public class ProcessUtil {
public static String execute(List<String> command) {
StringBuffer inputStringBuffer = new StringBuffer();
StringBuffer errorStringBuffer = new StringBuffer();
try {
ProcessBuilder builder = new ProcessBuilder(command);
Process process = builder.start();
System.out.println("============inputStream============");
// 处理InputStream
Thread t1 = new Thread(() -> {
InputStream input = null;
InputStreamReader reader = null;
BufferedReader buffer = null;
try {
input = process.getInputStream();
reader = new InputStreamReader(input);
buffer = new BufferedReader(reader);
String inputLine = "";
while ((inputLine = buffer.readLine()) != null) {
System.out.println(inputLine);
inputStringBuffer.append(inputLine);
//退出循环后表示结束流
System.out.println("===》》退出循环后表示结束流");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (buffer != null) {
buffer.close();
if (reader != null) {
reader.close();
if (input != null) {
input.close();
} catch (IOException e) {
e.printStackTrace();
t1.setName("deviceName");
t1.start();
System.out.println("============errorStream============");
// 处理ErrorStream
new Thread(() -> {
InputStream input = null;
InputStreamReader reader = null;
BufferedReader buffer = null;
try {
input = process.getErrorStream();
reader = new InputStreamReader(input);
buffer = new BufferedReader(reader);
String errorLine = "";
while ((errorLine = buffer.readLine()) != null) {
System.out.println(errorLine);
errorStringBuffer.append(errorLine);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (buffer != null) {
buffer.close();
if (reader != null) {
reader.close();
if (input != null) {
input.close();
} catch (IOException e) {
e.printStackTrace();
}).start();
* 只会存在一个输入流返回
if (inputStringBuffer != null) {
return inputStringBuffer.toString();
if (errorStringBuffer != null) {
return errorStringBuffer.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
return null;
文件上传部分省略了,在文件上传后,使用上述工具类将视频转为m3u8/ts文件,保存在配置好的F:\m3u8\hls\目录即可
3、Nginx文件服务器配置
server {
listen 80;
server_name localhost;
charset utf-8;
location ^~ /hls/ {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS;
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
alias F:/m3u8/hls/;
expires -1;
add_header Cache-Control no-cache;
文件夹目录
<meta charset="UTF-8">
<title>测试视频播放</title>
<script type="text/javascript" src="video-6.7.3.js"></script>
<script type="text/javascript" src="videojs-contrib-hls-5.14.1.min.js"></script>
<link href="video.js_6.7.3.min.css" rel="stylesheet" type="text/css"/>
</head>
<video id="example-video" width="800" height="600" controls poster="http://127.0.0.1:8888/hls/add.png" class="video-js vjs-default-skin vjs-big-play-centered">
<source src="http://127.0.0.1/hls/1679771414267805698/1679771414267805698.m3u8" type="application/x-mpegURL">
</video>
<input type="button" onclick="switchvideo()" value="switch"/>
<script type="text/javascript">
var player = videojs('example-video');
function switchvideo() {
player.src({
src: 'http://127.0.0.1/hls/1679769948731760641/1679769948731760641.m3u8',
type: 'application/x-mpegURL',
withCredentials: true
player.play();
</script>
</body>
</html>
在nginx安装目录下启动nginx,打开页面:
作者:全栈小白
原文:https://mp.weixin.qq.com/s/3g_b-1-7D37g1Vyk22shng
版权声明:本文内容转自互联网,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至1393616908#qq.com 举报,一经查实,本站将立刻删除。
赞
(0)
即构科技入围【2022中国元宇宙最具商业潜力科技企业榜单】
TDF 和法国电视台在 DTT 和 5G 广播上试用 UHD
Signiant 与 TMT Insights 携手变革媒体流程自动化
SoundHound 推出全套边缘和云连接解决方案 提高语音 AI 的准确性和隐私性
如何打造音视频极致消费体验
HTC黄昭颖的元宇宙关键词:技术积累、沉浸式内容与开放生态
音视频通讯QoS技术及其演进
AI音视频技术在金融场景下的应用实操
Tambur:将 streaming codes 应用视频会议场景进行丢包恢复 | NSDI 2023