Is there a way to tell the file size of an .ogg video before it is fully loaded?

﹥>﹥吖頭↗ 提交于 2019-12-05 21:33:06

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!