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

I know that one difference between ogg video and mp4 is that ogg video does not contain metadata describing the file size, so when ogg video is loaded, the controls cannot display the remaining time until the file is fully loaded. This may be a problem if the ogg video is more than a few minutes long. Is there a way to get this file size when requesting a page?

(A client developer asking a question that I think has an answer on the server side. I welcome the proposed changes to this question if you can think about how to ask it more specifically.)

+3
source share
1 answer

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

First configure .htaccess to transparently capture all .ogv videos and process them using 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 how does this system work. Just bind the video in .ogv format, as usual, but the .htaccess file first captures the request and sends it to ogv.php. Then the PHP file sends a file size header if the server is not working automatically. Alright, that still doesn't do you much good, right? So, you can make an Ajax request for the video and extract the file from the HTTP headers.

Hope this helps.

+2
source

Source: https://habr.com/ru/post/1759300/


All Articles