在嵌入式码头中使用HTML页面以进行视频流

发布于 2025-01-25 03:03:35 字数 1802 浏览 3 评论 0原文

我有一个嵌入式的码头应用程序,具有各种端点,其中一个是/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

耳根太软 2025-02-01 03:03:35

$ video_source只是网络服务器上特定资源的HTTP请求。 (它可以是/videos/67328148734529

由您决定在Web服务器上实现该特定资源端点。通常为httpservlet。 (在上面的示例中,您的终点将在URL-Pattern /videos/*上,您将在servlet上使用request.getPathinfo()以获取视频浏览器请求的ID)

您将要处理一些事情。

  1. 兑现各种接受标题对您的响应的限制。
  2. 提供正确的content-type响应。
  3. 尊重各种HTTP范围请求标头,以了解您应该提供的视频内容中的位置。
  4. 产生正确的HTTP范围响应标头,以指示要响应的内容来自的浏览器。
  5. 将此视频内容从数据库流式传输到对请求的响应。

您应该考虑不将常规数据库用于视频内容本身,这会带来极低效率的体验,因为大多数数据库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 the request.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.

  1. Honor the limitations put on your response by the various Accept headers on the request.
  2. Provide the correct Content-Type response.
  3. Honor the various HTTP range request headers to know where in the video content you should serve.
  4. Produce the correct HTTP range response headers to indicate to the browser where the content being responded to is coming from.
  5. Stream this video content from your database to your response to the request.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文