vue视频流播放,支持多种视频格式,如rmvb、mkv

先将视频转码为ts

ffmpeg -i C:\test\3.rmvb -codec: copy -start_number 0 -hls_time 10 -hls_list_size 0 -f hls C:\test\a\output.m3u8

后端配置接口

java 复制代码
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.MalformedURLException;
import java.nio.file.Path;
import java.nio.file.Paths;

@RestController
@RequestMapping("/api/video")
public class VideoController4 {

    private final Path videoStorageLocation = Paths.get("C:\\test\\a");

    @GetMapping("/stream/{fileName}")
    public ResponseEntity<Resource> streamVideo(@PathVariable String fileName) {
        try {
            Path filePath = this.videoStorageLocation.resolve(fileName).normalize();
            System.out.println(filePath);
            Resource resource = new UrlResource(filePath.toUri());

            if (resource.exists()) {
                return ResponseEntity.ok()
                        .header(HttpHeaders.CONTENT_TYPE, "application/x-mpegURL")
                        .body(resource);
            } else {
                return ResponseEntity.notFound().build();
            }
        } catch (MalformedURLException ex) {
            return ResponseEntity.badRequest().build();
        }
    }
}

vue代码

ts 复制代码
<template>
    <div>
        <video ref="videoPlayer" controls >
            
        </video>
    </div>
</template>
import Hls from 'hls.js';
onMounted(() => {
    const video = videoPlayer.value;
    const videoSrc = 'http://localhost:9500/api/video/stream/5.m3u8'; // 替换为你的 .m3u8 视频流地址



    if (Hls.isSupported() && video) {
        const hls = new Hls();
        hls.loadSource(videoSrc);

        hls.attachMedia(video);
        hls.on(Hls.Events.MANIFEST_PARSED, () => {
            // video.play();
        });

    } else if (video && video.canPlayType('application/vnd.apple.mpegurl')) {
        // 如果浏览器原生支持 HLS(如 Safari)
        video.src = videoSrc;
        video.addEventListener('loadedmetadata', () => {
            video.play();
        });

    }
});
相关推荐
众乐乐_200829 分钟前
Java 后端给前端传Long值,精度丢失的问题与解决
java·前端·状态模式
一叶茶39 分钟前
VsCode和AI的前端使用体验:分别使用了Copilot、通义灵码、iflyCode和Trae
前端·vscode·gpt·ai·chatgpt·copilot·deepseek
熊猫钓鱼>_>1 小时前
基于MCP的桥梁设计规范智能解析与校审系统构建实践
前端·easyui·设计规范
qq_346295271 小时前
require/exports 或 import/export的联系和区别,各自的使用场景
javascript
flying robot1 小时前
小结:JavaScript 模块化工具链
javascript
若初&1 小时前
文件上传Ⅲ
前端·web安全
若愚67921 小时前
前端取经路——前端安全:构建坚不可摧的Web应用防线
前端·安全
邪恶的贝利亚1 小时前
定时器设计
java·linux·前端
工业互联网专业1 小时前
基于springboot+vue的机场乘客服务系统
java·vue.js·spring boot·毕业设计·源码·课程设计·机场乘客服务系统