简体   繁体   English

Spring boot + HTML 5视频流

[英]Spring boot + HTML 5 video streaming

I've been learning the Spring boot framework recently and so far I'm fairly impressed by it. 我最近一直在学习Spring引导框架,到目前为止,我对它印象深刻。

However, I've been trying to write a basic media server application and I'm not entirely sure what the correct way is to implement a controller endpoint which serves an HTML 5 video source. 但是,我一直在尝试编写一个基本的媒体服务器应用程序,我不完全确定实现一个服务于HTML 5视频源的控制器端点的正确方法。 I've currently implemented this like so: 我目前实现了这样:

@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"));
}

And the HTML 5 video element looks like this: (using Thymeleaf) 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>

The video displays, however I've noticed that if I skip the video a few times it eventually slows down and then freezes up the browser. 视频显示,但我注意到如果我跳过视频几次它最终会减慢然后冻结浏览器。 I'm not sure why this is happening but I'm assuming it's because I'm not handling the request correctly? 我不确定为什么会发生这种情况,但我认为这是因为我没有正确处理请求?

Thanks 谢谢

You should consider a Spring Boot companion project called Spring Content that allows you to create digital asset management applications with very little code. 您应该考虑一个名为Spring Content的Spring Boot配套项目,它允许您使用非常少的代码创建数字资产管理应用程序。

To give you the basic idea that would look something like this:- 给你一个看起来像这样的基本想法: -

pom.xml 的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.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> {
    //
  }
}

Note that you are not writing any controller code here but this is enough to create a REST video service at /videosrc that supports full CRUD and video streaming (ie byte ranges). 请注意,您没有在此编写任何控制器代码,但这足以在/videosrc创建支持完整CRUD和视频流(即字节范围)的REST视频服务。 Create == POST, Read == GET (include byte-range support), Update == PUT, Delete == DELETE. 创建== POST,读取== GET(包括字节范围支持),更新== PUT,删除==删除。

eg 例如

POST /videosrc/some/path/video1.mp4

would store the uploaded multipart video to /some/path/video.mp4. 将上传的多部分视频存储到/some/path/video.mp4。

Spring Content can also be combined with Spring Data to store and search metadata about these videos too. Spring Content也可以与Spring Data结合使用,以存储和搜索有关这些视频的元数据。 If that is of interest then take a look at the Getting Started guides here and here . 如果您感兴趣,请在此处此处查看“入门指南”。

It looks like this might just be an issue with Firefox (Quantum) - it works OK but after skipping a few times seems to freeze up. 看起来这可能只是Firefox(昆腾)的一个问题 - 它运行正常,但跳过几次似乎冻结了。

I tested this on Google chrome and it works fine. 我在Google Chrome上进行了测试,效果很好。 Also works fine on a mobile browser. 也适用于移动浏览器。

I also checked it was sending the correct HTTP headers - mainly 'Accept-Ranges: bytes' (which it was). 我还检查它发送了正确的HTTP标头 - 主要是'Accept-Ranges:bytes'(它是)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM