有没有办法在完全加载之前告诉.ogg视频的文件大小? [英] Is there a way to tell the file size of an .ogg video before it is fully loaded?

查看:227
本文介绍了有没有办法在完全加载之前告诉.ogg视频的文件大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道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.)

推荐答案

尽管不是最多直接,你可以尝试这个。

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

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

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

.htaccess

.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标头中提取文件大小。

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.

希望这会有所帮助。

这篇关于有没有办法在完全加载之前告诉.ogg视频的文件大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆