Spring Boot + HTML5 视频流传输

9

最近我一直在学习Spring Boot框架,到目前为止,我对它的表现还算满意。

然而,我一直在尝试编写一个基本的媒体服务器应用程序,但我不确定实现控制器端点以提供HTML5视频源的正确方法是什么。我目前的实现方式如下:

@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>

视频可以正常播放,但我发现如果我跳过视频几次,它最终会变慢,然后冻结浏览器。我不确定为什么会发生这种情况,但我猜测是因为我没有正确处理请求?谢谢。
2个回答

7
你应该考虑使用名为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> {
    //
  }
}

请注意,您在这里没有编写任何控制器代码,但这已足以创建一个REST视频服务,位于/videosrc,支持完整的CRUD和视频流式传输(即字节范围)。 创建== POST,读取== GET(包括字节范围支持),更新== PUT,删除== DELETE。

例如:

POST /videosrc/some/path/video1.mp4

将上传的多部分视频存储到/some/path/video.mp4中。

Spring Content还可以与Spring Data结合使用,存储和搜索有关这些视频的元数据。如果感兴趣,请查看此处此处的入门指南。


1

看起来这可能只是Firefox(Quantum)的问题 - 它可以正常工作,但跳过几次后似乎会冻结。

我在Google Chrome上测试了一下,它可以正常工作。在移动浏览器上也可以正常工作。

我还检查了它是否发送了正确的HTTP头 - 主要是“Accept-Ranges: bytes”(它确实发送了)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接