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();
        });

    }
});
相关推荐
羲云6 分钟前
Claude 越界事件之后:Agent 权限设计为什么不能只靠提示词
前端
行者全栈架构师20 分钟前
【鸿蒙心迹】鸿蒙网络请求架构实战——@ohos.net.http 到 Axios 封装、拦截器与统一错误处理(HarmonyOS 7.x)
前端·算法·架构
全栈Agent 小李27 分钟前
【无标题】
前端·后端·agent·ai编程·全栈·cursor·mcp
羲云27 分钟前
别让 AI 凭记忆做调研:给 WorkBuddy 接上可核验的实时搜索
前端
只睡四小时35 分钟前
AI 生成 PPTX:11 页课件编译出 429 个形状
javascript·python·pptx·ai生成ppt·ooxml
deli0071 小时前
连接池到底配多大?QPS 和 RT 算一遍,再跑 1 万次请求实测超时率
前端
风花一世月1 小时前
🏮 我把三坊七巷做成了能飞的 3D 地图(901 个真实建筑轮廓、20 处景点,在线直接玩)
前端
whi1 小时前
一个小工具,解决了一个困扰 Vue 开发者多年的类型检查难题
vue.js·typescript
特立独行的猫A1 小时前
用仓颉语言写一个串口调试助手:cj-tauri 实战
前端
小凯在掘金1 小时前
Shadow DOM 和 Virtual DOM ?别再傻傻分不清
前端·html