在嵌入式码头中使用HTML页面以进行视频流
我有一个嵌入式的码头应用程序,具有各种端点,其中一个是/stream/fileid
,另一个是/streamWithHtml/fileid
。
第一个是第一个获得的文件具有来自文件系统的指定ID,并开始将其内容写入响应,利用范围标题跳到视频的不同部分。
第二个端点旨在执行同一件事,除非默认情况除外浏览器播放器,我想要自己的自定义播放器使用HTML构建。
到目前为止,这是我试图制定的代码。这返回一个网页,但我不知道如何处理视频流:
response.setContentType("text/html");
try {
PrintWriter writer = response.getWriter();
String filePath1 = "path/to/file";
String encodedFilename = URLEncoder.encode(filePath1, StandardCharsets.UTF_8);
// Get an html file and then print its contents after replacing a couple of parameters
InputStream stream = getClass().getClassLoader().getResourceAsStream("HTML/VideoPlayer/index_template.html");
String htmlPage = IOUtils.toString(stream, StandardCharsets.UTF_8);
htmlPage = htmlPage.replace("$title", fileEntity1.fileName);
htmlPage = htmlPage.replace("$video_source", encodedFilename);
writer.println(htmlPage);
writer.flush();
writer.close();
} catch (IOException | NullPointerException e) {
e.printStackTrace();
}
这是html文件的内容:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>$title</title>
</head>
<body style="background-color: #303030">
<video style="max-width: 100%; max-height: 100%;" src="$video_source"></video>
</body>
</html>
请注意,$ $ title
和$ video_source
只是弦乐我使用页面时要正确地放置一些值。
直到现在我尝试过或已经看到的每种解决方案都是为了使用静态视频,但是我不可能使用,因为视频保存在一个数据库。
其他注意:视频必须只能从外部到一个端点,这将是具有HTML页面的一个端点,因此将视频放在一个端点上,然后在另一个请求的html中使用i对我来说不是一个选择。
I have an embedded jetty application that has various endpoints, one of them is /stream/fileId
and another one is /streamWithHTML/fileId
.
The first one gets a file with the specified id from filesystem and starts writing its contents to the response, making use of the range headers to skip to different parts of the video.
The second endpoint is intended to do the same thing, except instead of the default browser player, i want my own custom player built with html.
This is the code i attempted to put together until now. This returns a web page but i dont know how to handle the video stream:
response.setContentType("text/html");
try {
PrintWriter writer = response.getWriter();
String filePath1 = "path/to/file";
String encodedFilename = URLEncoder.encode(filePath1, StandardCharsets.UTF_8);
// Get an html file and then print its contents after replacing a couple of parameters
InputStream stream = getClass().getClassLoader().getResourceAsStream("HTML/VideoPlayer/index_template.html");
String htmlPage = IOUtils.toString(stream, StandardCharsets.UTF_8);
htmlPage = htmlPage.replace("$title", fileEntity1.fileName);
htmlPage = htmlPage.replace("$video_source", encodedFilename);
writer.println(htmlPage);
writer.flush();
writer.close();
} catch (IOException | NullPointerException e) {
e.printStackTrace();
}
And this is the contents of the html file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>$title</title>
</head>
<body style="background-color: #303030">
<video style="max-width: 100%; max-height: 100%;" src="$video_source"></video>
</body>
</html>
Note that $title
and $video_source
are just strings i use to correctly place some values when i serve the page.
Every solution i have tried or have seen up until now, is to serve a static video, but it is not possible for me, since the video is saved on a database.
Additional note: the video must be accessible from outside through only one endpoint, which will be the one with the html page, so putting the video on one endpoint and then serving i in the html of another request is not an option for me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
$ video_source
只是网络服务器上特定资源的HTTP请求。 (它可以是/videos/67328148734529
)由您决定在Web服务器上实现该特定资源端点。通常为httpservlet。 (在上面的示例中,您的终点将在URL-Pattern
/videos/*
上,您将在servlet上使用request.getPathinfo()
以获取视频浏览器请求的ID)您将要处理一些事情。
接受
标题对您的响应的限制。content-type
响应。您应该考虑不将常规数据库用于视频内容本身,这会带来极低效率的体验,因为大多数数据库BLOB请求都不有效地支持范围请求(尤其是HTTP范围请求的细微差别以及Multipart/byteranges响应的细微差别)。
一种常见的技术是将视频以原始形式放在带有UUID样式名称的存储设备(HDD,SSD,CDN等)上,并将数据库参考引用UUID名称和其他元数据。这样,您可以有效地从存储到网络。
The
$video_source
is just an HTTP request to a specific resource on your webserver. (it could be/videos/67328148734529
)It will be up to you to implement that specific resource endpoint on your webserver. Typically as an HttpServlet. (in the above example, your endpoint would be on url-pattern
/videos/*
, and you would use therequest.getPathInfo()
on your servlet to obtain the video id that is being requested by the browser)Some things you'll be on the hook to handle.
Accept
headers on the request.Content-Type
response.You should consider not using a general database for the video content itself, it makes for a terribly inefficient experience, as most database blob requests don't support range requests efficiently (especially with the nuances of HTTP range requests and multipart/byteranges responses).
A common technique is to put the video in it's raw form on a storage device (hdd, ssd, cdn, etc) with a UUID style name, and have the database reference the UUID name, and other metadata. That way you serve from storage to network efficiently.