有没有办法在 .ogg 视频完全加载之前知道其文件大小?

发布于 2024-10-22 22:03:19 字数 199 浏览 3 评论 0原文

我知道 ogg 视频和 mp4 视频之间的一个区别是 ogg 视频没有描述文件大小的元数据,因此当加载 ogg 视频时,控件无法显示文件完全加载之前的剩余时间。如果 ogg 视频长度超过几分钟,这可能会成为问题。有没有办法在请求页面时获取此文件大小?

(客户端开发人员,提出一个我认为有服务器端答案的问题。如果您能想出一种更具体的方法,我欢迎对此问题进行建议编辑。)

I know one difference between ogg video and mp4 video is that ogg video doesn't have metadata describing the file size, so when an ogg video is loaded, the controls can't show the time remaining until the file has fully loaded. This can be a problem if the ogg video is more than a few minutes long. Is there a way to get this filesize when the page is requested?

(Client-side developer, asking a question that I think has a server-side answer. I welcome suggested edits to this question, if you can think of a way to ask it more specifically.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

携余温的黄昏 2024-10-29 22:03:19

好吧,虽然不是最直接的,但你可以尝试一下。

首先,设置 .htaccess 以透明地抓取所有 .ogv 视频并使用 PHP 处理它们

。 .htaccess

RewriteEngine On
RewriteRule ^(.*)\.ogv$ ogv.php?file=$1

ogv.php

<?php
$file = $_GET['file'] . '.ogv';

while ( strpos($file, '..') !== false )
{
    $file = str_replace('..', '', $file);
}

$filesize = filesize($file);

header("Content-Type: video/ogg");
header("Content-Transfer-Encoding: binary"); 
header("Content-Length: {$filesize}");
readfile($file);
exit()
?>

HTML:

<video src="video.ogv" id="video" controls></video>

<script>
var video_src = document.getElementById('video').src;
var xhr = new XMLHttpRequest();
xhr.open('GET', video_src, false);
xhr.send(null);
var size = xhr.getResponseHeader('Content-Length');
alert(size);
</script>

以下是该系统的工作原理。只需像平常一样链接 .ogv 视频,但 .htaccess 文件首先捕获请求并将其发送到 ogv.php。然后,PHP 文件会专门发送一个文件大小标头,以防服务器不自动发送。好吧,这仍然对你没有多大好处,对吧?那么,您可以对视频发出 Ajax 请求并从 HTTP 标头中提取文件大小。

希望这有帮助。

Well although not the most direct, you could try this.

First, set up a .htaccess to transparently grab all .ogv videos and process them with PHP

.htaccess

RewriteEngine On
RewriteRule ^(.*)\.ogv$ ogv.php?file=$1

ogv.php

<?php
$file = $_GET['file'] . '.ogv';

while ( strpos($file, '..') !== false )
{
    $file = str_replace('..', '', $file);
}

$filesize = filesize($file);

header("Content-Type: video/ogg");
header("Content-Transfer-Encoding: binary"); 
header("Content-Length: {$filesize}");
readfile($file);
exit()
?>

HTML:

<video src="video.ogv" id="video" controls></video>

<script>
var video_src = document.getElementById('video').src;
var xhr = new XMLHttpRequest();
xhr.open('GET', video_src, false);
xhr.send(null);
var size = xhr.getResponseHeader('Content-Length');
alert(size);
</script>

So here's how this system works. Just link a .ogv video like normal but the .htaccess file captures the request first and sends it to ogv.php. The PHP file then specifically sends out a file size header in case the server doesn't automatically. Okay, that still doesn't do you a whole lot of good, right? Well, you can then make an Ajax request for the video and extract the filesize from the HTTP headers.

Hope this helps.

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