I try to convert m3u8 file to WebM using ffmpeg and stream it onto browser.
I want it to play on browser without using any javascript.
Stream video method:
public void streamVideo(OutputStream os ){
String url = "m3u8file.m3u8";
byte[] bytes = new byte[BUFFER];
int bytesRead = -1;
ProcessBuilder pb = new ProcessBuilder(
"ffmpeg",
"-i", url,
"-c:v", "libvpx-vp9",
"-b:v", "1M",
"-c:a", "libopus",
"-b:a", "128k",
"-f", "webm",
"pipe:1"
);
pb.redirectErrorStream(true);
try {
Process process = pb.start();
try (
InputStream is = process.getInputStream()){
while((bytesRead = is.read(bytes)) != -1){
os.write(bytes, 0 , bytesRead);
os.flush();
}
}catch (IOException ex){
ex.printStackTrace();
}
}catch (IOException ex){
ex.printStackTrace();
}
}
}
Web Controller
@Autowired
private VideoService videoService;
@GetMapping("/stream")
public ResponseEntity<StreamingResponseBody> streamVideo(@RequestHeader(value = "Range", required = false) String rangeHeader) {
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "video/webm");
headers.add("Accept-Ranges", "bytes");
StreamingResponseBody responseStream = os ->{
videoService.streamVideo(os);
};
return new ResponseEntity<>(responseStream, headers, HttpStatus.PARTIAL_CONTENT);
}
When I open the /stream I get an error on spring application
“ServletOutputStream failed to flush: java.io.IOException: An established connection was aborted by the software in your host machine”
-
On firefox in console I got 2 errors
Cant decode multimedia resource:
And Cant decode multimedia resource:Error Code: NS_ERROR_DOM_MEDIA_METADATA_ERR (0x806e0006)
And the window which video will be showing says “No video with supported format and MIME type found” -
On Chrome just nothing happens
-
And on firefox when i open the view-source i see the stream information and then there is jibberish which i suppose are bytes from the video the. The jibberish stops when i got the ServletOutputStream failed to flush: in console in Spring
Sorry if error codes translation is hard to understand
2