Home > database >  Uploading and live video stream to a Spring Boot endpoint, by example
Uploading and live video stream to a Spring Boot endpoint, by example

Time:10-17

I am interested in building a Spring Boot (Java 11) web service that has a controller/resource that exposes an API endpoint for accepting streaming video. Particularly I am interested in accomplishing this over HTTP/HLS. The idea is that clients could livestream a video feed to this endpoint, and the endpoint would be doing realtime video processing on it.

When you Google "java spring streaming video" you get a bazillion hits for either:

  • Writing a Spring controller/resource (endpoint) that accepts a single (already recorded; a.k.a. not live) video file upload as a MultiPartFile; or
  • Writing a Spring controller/resource (endpoint) that downloads and streams a video by range of bytes, as a MultiPartFile; a.k.a. a streaming download to a video player on the client-side

But this isn't what I want. I am curious to see how a client would livestream a video (byte after byte) to a service endpoint so that the video's byte[] is being constantly read and processed server-side. Maybe I want to run AI/ML detections on the video, or do some type of image/graphics processing. The application doesn't really matter, all I am curious here is how to write a Spring Boot endpoint to handle a livestream video feed from client to server (livestream upload).

Would the client just send the livestream in "chunks" or batches of MultiPartFile payloads? Either way, there don't appear to be any guides on how to do this in Spring, and I'm sure its definitely something that Spring could handle. Any ideas?

CodePudding user response:

Current solution is to have the client buffer streaming A/V until 32K bytes are buffered, and then I defined a POST endpoint in Spring Boot that accepts a MultipartFile in the request body (the 32K bytes).

The Java code in that resource endpoint then invokes an @Async service method that uses ffmpeg-cli-wrapper to append the bytes in the MultipartFile to the end of an MP4 file I'm storing in S3.

This is working but I will continue to tweak the buffer size until I have a size/configuration that is a careful balance of POST requests (the client sending chunks of the livestream as MultipartFiles) and video processing efficiency.

  • Related