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

    }
});
相关推荐
伍哥的传说42 分钟前
鸿蒙系统(HarmonyOS)应用开发之手势锁屏密码锁(PatternLock)
前端·华为·前端框架·harmonyos·鸿蒙
yugi98783844 分钟前
前端跨域问题解决Access to XMLHttpRequest at xxx from has been blocked by CORS policy
前端
浪裡遊1 小时前
Sass详解:功能特性、常用方法与最佳实践
开发语言·前端·javascript·css·vue.js·rust·sass
旧曲重听12 小时前
最快实现的前端灰度方案
前端·程序人生·状态模式
默默coding的程序猿2 小时前
3.前端和后端参数不一致,后端接不到数据的解决方案
java·前端·spring·ssm·springboot·idea·springcloud
夏梦春蝉2 小时前
ES6从入门到精通:常用知识点
前端·javascript·es6
归于尽2 小时前
useEffect玩转React Hooks生命周期
前端·react.js
G等你下课2 小时前
React useEffect 详解与运用
前端·react.js
我想说一句2 小时前
当饼干遇上代码:一场HTTP与Cookie的奇幻漂流 🍪🌊
前端·javascript
funnycoffee1232 小时前
Huawei 6730 Switch software upgrade example版本升级
java·前端·华为