The browser does not allow the user to download mp4 or webm video file from a simple link. Instead, it opens a video player in a new card. JavaScript cannot open and download video from a relative path to a media file. JavaScript is a client-side language; it cannot download or fetch video from a server and cannot use tools like ffmpeg to combine media segments of an HLS or MPEG-DASH stream.
One of the methods to force file downloading in JavaScript is to use XMLHttpRequest and generate a blob file-like object. However, this is possible for files hosted on the same server only. Also, such a method is fine for relatively small files. It takes ages to create a blob object from a big video file.
In the example above, we send downloadVideo event parameters to a simple PHP file with a function to force video download immediately from our server.
To enable the downloadButton, simply enable the appropriate Nuevo plugin's option.
Code snippet
<script>const player = videojs('my_player',{license: "key"}});player.nuevo({ video_id:"v1234", downloadButton:true });</script>Code snippet
<script>player.on('downloadVideo', function(event,data){video_url = data.source;video_id = data.id;//send file to server-side file with download function });</script>Code snippet
<script>window.location(download.php?url=video_url);orwindow.location(download.php?video_id=video_id);</script>Code snippet
<?php$video_url = trim($_GET['url']);
header('Content-Description: File Transfer');header('Content-Type: application/octet-stream');header('Content-Disposition: attachment; filename='.basename($file));header('Content-Transfer-Encoding: chunked');header('Expires: 0');header('Cache-Control: must-revalidate, post-check=0, pre-check=0');header('Pragma: public');$stream = fopen('php://output', 'w');$ch = curl_init($file);curl_setopt($ch, CURLOPT_READFUNCTION, function($ch, $fd, $length) use ($stream) {return fwrite($stream, fread($fd, $length));});?>Code snippet
<?php$video_url = trim($_GET['url']);$video_id = trim($_GET['id']);
// your code to get relative file path of video, based on video url or based on video_id and information stored in database for example.// the result store in $filepath variable
if (file_exists($filepath) && is_file($filepath) && is_readable($filepath)) {ini_set('memory_limit', '-1');@ob_end_clean();if(ini_get('zlib.output_compression')) {ini_set('zlib.output_compression', 'Off');}header('Content-Type: application/force-download');header('Content-Disposition: attachment; filename="'.basename($file).'"');header('Content-Transfer-Encoding: binary');header('Accept-Ranges: bytes');header('Cache-control: private');header('Pragma: private');header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');header('Content-Length: ' .filesize($file)); readfile($file);exit();}die();?>The PHP code examples above are for illustrative purposes only. The User or User's company must adopt it or write its own download function according to server settings, file storage system, database, etc. PHP is not the only option to prepare the download function, though it's certainly the most popular server-side language.