我最近一直在学习 Spring boot 框架,到目前为止我对它印象深刻。
但是,我一直在尝试编写一个基本的媒体服务器应用程序,但我并不完全确定实现为 HTML 5 视频源提供服务的 Controller 端点的正确方法是什么。我目前是这样实现的:
@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 配套项目这使您可以使用很少的代码创建数字 Assets 管理应用程序。
给你一个看起来像这样的基本想法:-
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> {
//
}
}
请注意,您没有在此处编写任何 Controller 代码,但这足以在 /videosrc
中创建支持完整 CRUD 和视频流(即字节范围)的 REST 视频服务。创建 == POST,读取 == GET(包括字节范围支持),更新 == PUT,删除 == DELETE。
例如
POST/videosrc/some/path/video1.mp4
会将上传的多段视频存储到/some/path/video.mp4。
Spring Content 也可以与 Spring Data 结合使用,以存储和搜索有关这些视频的元数据。如果对此感兴趣,请查看入门指南 here和 here .
关于java - Spring Boot + HTML 5 视频流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49608146/