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

    }
});
相关推荐
yuanyxh5 小时前
Mac 软件推荐
前端·javascript·程序员
万少5 小时前
AtomCode开发微信小程序《谁去呀》 全流程
前端·javascript·后端
某人辛木5 小时前
Web自动化测试
前端·python·pycharm·pytest
Kagol6 小时前
Superpowers GSD gstack AgentSkills深度测评
前端·人工智能
excel7 小时前
JavaScript 字符串与模板字面量:从表象到本质理解
前端
京东云开发者7 小时前
当AI成为导演-如何用AI创作动漫短剧
前端
李白的天不白8 小时前
使用 SmartAdmin 进行前后端开发
java·前端
乘风gg8 小时前
🤡PUA AI Coding 工具 的 10 条终极语录
前端·ai编程·claude
学Linux的语莫8 小时前
Vue 3 入门教程
前端·javascript·vue.js
怕浪猫8 小时前
第一章、Chrome DevTools Protocol (CDP) 详解
前端·javascript·chrome