[英]Spring boot + HTML 5 video streaming
我最近一直在学习Spring引导框架,到目前为止,我对它印象深刻。
但是,我一直在尝试编写一个基本的媒体服务器应用程序,我不完全确定实现一个服务于HTML 5视频源的控制器端点的正确方法。 我目前实现了这样:
@GetMapping(value = "/videosrc", produces = "video/mp4")
@ResponseBody
public FileSystemResource videoSource(@RequestParam(value="id", required=true) int id) {
return new FileSystemResource(new File("path to mp4 file"));
}
HTML 5视频元素如下所示:(使用Thymeleaf)
<video width="auto" height="240" controls style=" margin-left: auto; margin-right: auto; display: block;">
<source th:src="@{/videosrc(id=${video.id})}" type="video/mp4">
</video>
视频显示,但我注意到如果我跳过视频几次它最终会减慢然后冻结浏览器。 我不确定为什么会发生这种情况,但我认为这是因为我没有正确处理请求?
谢谢
您应该考虑一个名为Spring Content的Spring Boot配套项目,它允许您使用非常少的代码创建数字资产管理应用程序。
给你一个看起来像这样的基本想法: -
的pom.xml
<dependency>
<groupId>com.github.paulcwarren</groupId>
<artifactId>spring-content-rest-boot-starter</artifactId>
<version>0.0.10</version>
</dependency>
<dependency>
<groupId>com.github.paulcwarren</groupId>
<artifactId>content-fs-spring-boot-starter</artifactId>
<version>0.0.10</version>
</dependency>
SpringBootApplication.java
@SpringBootApplication
public class YourSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(YourSpringBootApplication.class, args);
}
@Configuration
@EnableFilesystemStores
public static class StoreConfig {
File filesystemRoot() {
// return the root of your video store
}
// this bean is the spring resource loader that will be used by
// the product store
@Bean
public FileSystemResourceLoader fsResourceLoader() throws Exception
{
return new FileSystemResourceLoader(filesystemRoot().getAbsolutePath());
}
}
@StoreRestResource(path="videosrc")
public interface VideoStore extends Store<String> {
//
}
}
请注意,您没有在此编写任何控制器代码,但这足以在/videosrc
创建支持完整CRUD和视频流(即字节范围)的REST视频服务。 创建== POST,读取== GET(包括字节范围支持),更新== PUT,删除==删除。
例如
POST /videosrc/some/path/video1.mp4
将上传的多部分视频存储到/some/path/video.mp4。
Spring Content也可以与Spring Data结合使用,以存储和搜索有关这些视频的元数据。 如果您感兴趣,请在此处和此处查看“入门指南”。
看起来这可能只是Firefox(昆腾)的一个问题 - 它运行正常,但跳过几次似乎冻结了。
我在Google Chrome上进行了测试,效果很好。 也适用于移动浏览器。
我还检查它发送了正确的HTTP标头 - 主要是'Accept-Ranges:bytes'(它是)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.