Restful-based video streaming

16,114

Spring Content supports video streaming out of the box. Using Spring Content for the file-system (FS) you would be able to create yourself a video store backed by the filesystem, put your video(s) in that store, and using the companion library Spring Content REST to serve them over HTTP to any front-end video player.

Create a new Spring Boot project via start.spring.io or via your IDE spring project wizard (Spring Boot 1.5.10 at time of writing). Add the following Spring Content dependencies so you end up with these:-

<dependencies>
    <!-- Standard Spring Boot -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.3</version>
    </dependency>

    <!-- Spring Content -->
    <dependency>
        <groupId>com.github.paulcwarren</groupId>
        <artifactId>spring-content-fs-boot-starter</artifactId>
        <version>0.0.9</version>
    </dependency>
    <dependency>
        <groupId>com.github.paulcwarren</groupId>
        <artifactId>spring-content-rest-boot-starter</artifactId>
        <version>0.0.9</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

In your Spring Boot Application class, create a VideoStore. Annotate it as a Store REST resource. This causes Spring Content to inject an implementation (of this interface for the file-system) as well add REST endpoints for this interface too saving you from having to write any of it yourself:-

    @SpringBootApplication 
    public class DemoApplication {

        public static void main(String[] args) {        
           SpringApplication.run(DemoApplication.class, args);  
        }

        @StoreRestResource(path="videos")   
        public interface VideoStore extends Store<String> {} 
    }

By default Spring Content will create a store under java.io.tmpdir. So you will also need to set the SPRING_CONTENT_FS_FILESYSTEM_ROOT environment variable to point to the root of your "store".

Copy your video into this "root" location. Start the application and your video(s) will be streamable from:-

/videos/MyVideo.mp4

Share:
16,114
Erdem Aydemir
Author by

Erdem Aydemir

Updated on June 04, 2022

Comments

  • Erdem Aydemir
    Erdem Aydemir almost 2 years

    Using spring boot, I want to make RESTful-based video player. I have my .mp4 extension videos in my file browser. How can I serve these videos on the frontend side by creating a rest endpoint?

    I've tried this method. The video can be started or stopped. But it can not be done backwards or forwards. Can not get it to the desired minute and start.

  • Marcello B.
    Marcello B. over 5 years
    Hello, can you please post a code snippet of how this user should use this resource? Generally, link-only answers are frowned upon by the SO community. For more information about this check out the how to ask article.
  • Yaroslav
    Yaroslav over 5 years
    I believe it's from this project - github.com/melgenek/spring-video-service/tree/mvc Also here is an article that reference it - melgenek.github.io/spring-video-service.