【问题标题】:Spring boot + HTML 5 video streamingSpring Boot + HTML 5 视频流
【发布时间】:2018-09-11 11:39:12
【问题描述】:

我最近一直在学习 Spring Boot 框架,到目前为止我对它印象深刻。

但是,我一直在尝试编写一个基本的媒体服务器应用程序,但我不完全确定实现一个为 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>

视频显示,但是我注意到如果我跳过视频几次,它最终会变慢,然后冻结浏览器。我不确定为什么会这样,但我假设是因为我没有正确处理请求?

谢谢

【问题讨论】:

    标签: java html spring-boot html5-video thymeleaf


    【解决方案1】:

    您应该考虑一个名为 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,删除 == DELETE。

    例如

    POST /videosrc/some/path/video1.mp4

    会将上传的分段视频存储到 /some/path/video.mp4。

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

    【讨论】:

      【解决方案2】:

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

      我在 Google chrome 上对此进行了测试,效果很好。在移动浏览器上也能正常工作。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-22
        • 1970-01-01
        • 2021-02-12
        相关资源
        最近更新 更多