如何在视频弹簧启动中滚动? [英] How to scroll in video spring boot?
问题描述
我在 spring boot 中构建了一个虚拟的视频流应用程序,我注意到我无法跳到另一个帧或视频中的某个时间.这是我读取 videoFile 的代码
I have built a dummy video streaming application in spring boot,I have noticed that i cant skip to another frame or to a certain time in a video. Here is my code to read videoFile
public byte[] getVideo() throws IOException {
byte[] bytes = new byte[(int) file.length()];
FileInputStream inputStream = new FileInputStream(file);
inputStream.read(bytes);
return bytes;
}
这是我的视频控制器返回的内容
and this is my what my video controller returns
return ResponseEntity.status(HttpStatus.OK)
.header("Content-Type","video/mp4")
.header("Content-length",String.valueOf(streamingService.file.length()))
.body(streamingService.getVideo());
注意我没有使用任何前端
note i am not using any frontend
推荐答案
所以经过一点点的消除,我发现它的原因是浏览器(chrome)向某些字节范围发出了另一个 get 请求.我的 getVideo() 是没有很好地接受字节范围并返回所需的字节范围.
So after little bit of extermination i found the reason for it was the browser(chrome) made another get request to certain byte range.and my getVideo() was not well written to accept the byte range and give back the required byte range.
重写我的 videoStreamingService 后,chrome 的问题就解决了
After rewritting my videoStreamingService the problem was solved for chrome
这是我重写的 getVideo() 代码
here is my rewitten code for getVideo()
byte[] data;
Long fileSize = file.length();
String[] ranges = range.split("-");
rangeStart = Long.parseLong(ranges[0].substring(6));
if (ranges.length > 1) {
rangeEnd = Long.parseLong(ranges[1]);
} else {
rangeEnd = fileSize - 1;
}
if (fileSize < rangeEnd) {
rangeEnd = fileSize - 1;
}
contentLength = String.valueOf((rangeEnd - rangeStart) + 1);
data = readByteRange( rangeStart, rangeEnd);
return data;
readRangeByte() 方法,以便我可以适当地读取数据
readRangeByte() method so i can read data appropriately
public byte[] readByteRange(long start, long end) throws IOException {
try (InputStream inputStream = (Files.newInputStream(Path.of(videoFileName)));
ByteArrayOutputStream bufferedOutputStream = new ByteArrayOutputStream()) {
byte[] data = new byte[128];
int nRead;
while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
bufferedOutputStream.write(data, 0, nRead);
}
bufferedOutputStream.flush();
byte[] result = new byte[(int) (end - start) + 1];
System.arraycopy(bufferedOutputStream.toByteArray(), (int) start, result, 0, result.length);
return result;
}
}
和我的控制器类代码
public ResponseEntity<byte[]> video(@RequestHeader(value = "Range",required = false)String range) throws IOException {
if (range == null) {
return ResponseEntity.status(HttpStatus.OK)
.header("Content-Type", "video/mp4")
.header("Content-Length", String.valueOf(streamingService.file.length()))
.body(streamingService.readByteRange(streamingService.getRangeStart(),streamingService.file.length()-1));
}
byte[] data = streamingService.getVideo(range);
return ResponseEntity.status(HttpStatus.PARTIAL_CONTENT)
.header("Content-Type", "video/mp4")
.header("Accept-Ranges", "bytes")
.header("Content-Length", streamingService.getContentLength())
.header("Content-Range", "bytes" + " " + streamingService.getRangeStart() + "-" + streamingService.getRangeEnd() + "/" + streamingService.file.length())
.body(data);
在@Andreas 建议之后,代码在两种浏览器上都运行良好
After @Andreas suggestion the code works very well with both the browsers
这篇关于如何在视频弹簧启动中滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!