春季启动+ HTML 5视频流 [英] Spring boot + HTML 5 video streaming

查看:88
本文介绍了春季启动+ HTML 5视频流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been learning the Spring boot framework recently and so far I'm fairly impressed by it.

但是,我一直在尝试编写基本的媒体服务器应用程序,但我不完全确定实现提供HTML 5视频源的控制器端点的正确方法是什么.我目前已经这样实现了:

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

HTML 5视频元素如下所示:(使用Thymeleaf)

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

谢谢

推荐答案

您应该考虑一个名为 Spring Content ,使您只需很少的代码即可创建数字资产管理应用程序.

You should consider a Spring Boot companion project called Spring Content that allows you to create digital asset management applications with very little code.

为您提供看起来像这样的基本概念:-

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> {
    //
  }
}

请注意,您无需在此处编写任何控制器代码,但这足以在/videosrc处创建支持完整CRUD和视频流(即字节范围)的REST视频服务.创建== POST,读取== GET(包括字节范围支持),更新== PUT,删除== DELETE.

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 (i.e. byte ranges). Create == POST, Read == GET (include byte-range support), Update == PUT, Delete == DELETE.

例如

POST /videosrc/some/path/video1.mp4

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

would store the uploaded multipart video to /some/path/video.mp4.

Spring Content也可以与Spring Data结合使用,以存储和搜索有关这些视频的元数据.如果您有兴趣,请在此处此处.

Spring Content can also be combined with Spring Data to store and search metadata about these videos too. If that is of interest then take a look at the Getting Started guides here and here.

这篇关于春季启动+ HTML 5视频流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆