Support the ongoing development of Laravel.io →
posted 10 years ago
Views
Last updated 2 years ago.
0

Have you got a media player in the mix? It will probably work better in desktop browsers. I'm not sure so much about iOS, But I don't think any browser has a video encoder in place to handle this type of media. So your browser is just seeing the file and handles it by downloading it for you. So a player will probably be your best bet.

Take a look at jPlayer: http://jplayer.org/

Its fully customizable and will deal with streaming your file.

Another option, which might be better for you depending on the project is to use a CDN to deliver the videos. You'll still need a player to handle the video. But it will save your server a crap load. I'm sure you are aware that its pretty intensive to stream videos. There are a few out there that specialise in video. We've used one in the past - Can't remember the company of the top of my head.

Hope it helps.

Last updated 2 years ago.
0

Got this working with angular (videogular). This class I found (author and link in class comments) works for partial content and seeking (ranges) when streaming.

Just place the VideoStream.php class below in app/helpers, and call it from your controller function as follows :

public function streamVideo() {
  $video_path = 'somedirectory/somefile.mp4';
  $stream = new VideoStream($video_path);
  $stream->start(); 
}

The stream helper class (VideoStream.php)

<?php
/**
 *  * Description of VideoStream
 *   *
 *    * @author Rana
 *     * @link http://codesamplez.com/programming/php-html5-video-streaming-tutorial
 *      */
class VideoStream
{
	private $path = "";
	private $stream = "";
	private $buffer = 102400;
	private $start  = -1;
	private $end    = -1;
	private $size   = 0;

	function __construct($filePath) 
	{
		$this->path = $filePath;
	}

	/**
	 *      * Open stream
	 *           */
	private function open()
	{
		if (!($this->stream = fopen($this->path, 'rb'))) {
			die('Could not open stream for reading');
		}

	}

	/**
	 *      * Set proper header to serve the video content
	 *           */
	private function setHeader()
	{
		ob_get_clean();
		header("Content-Type: video/mp4");
		header("Cache-Control: max-age=2592000, public");
		header("Expires: ".gmdate('D, d M Y H:i:s', time()+2592000) . ' GMT');
		header("Last-Modified: ".gmdate('D, d M Y H:i:s', @filemtime($this->path)) . ' GMT' );
		$this->start = 0;
		$this->size  = filesize($this->path);
		$this->end   = $this->size - 1;
		header("Accept-Ranges: 0-".$this->end);

		if (isset($_SERVER['HTTP_RANGE'])) {

			$c_start = $this->start;
			$c_end = $this->end;

			list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
			if (strpos($range, ',') !== false) {
				header('HTTP/1.1 416 Requested Range Not Satisfiable');
				header("Content-Range: bytes $this->start-$this->end/$this->size");
				exit;
			}
			if ($range == '-') {
				$c_start = $this->size - substr($range, 1);
			}else{
				$range = explode('-', $range);
				$c_start = $range[0];

				$c_end = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $c_end;
			}
			$c_end = ($c_end > $this->end) ? $this->end : $c_end;
			if ($c_start > $c_end || $c_start > $this->size - 1 || $c_end >= $this->size) {
				header('HTTP/1.1 416 Requested Range Not Satisfiable');
				header("Content-Range: bytes $this->start-$this->end/$this->size");
				exit;
			}
			$this->start = $c_start;
			$this->end = $c_end;
			$length = $this->end - $this->start + 1;
			fseek($this->stream, $this->start);
			header('HTTP/1.1 206 Partial Content');
			header("Content-Length: ".$length);
			header("Content-Range: bytes $this->start-$this->end/".$this->size);
		}
		else
		{
			header("Content-Length: ".$this->size);
		}  

	}

	/**
	 *      * close curretly opened stream
	 *           */
	private function end()
	{
		fclose($this->stream);
		exit;
	}

	/**
	 *      * perform the streaming of calculated range
	 *           */
	private function stream()
	{
		$i = $this->start;
		set_time_limit(0);
		while(!feof($this->stream) && $i <= $this->end) {
			$bytesToRead = $this->buffer;
			if(($i+$bytesToRead) > $this->end) {
				$bytesToRead = $this->end - $i + 1;
			}
			$data = fread($this->stream, $bytesToRead);
			echo $data;
			flush();
			$i += $bytesToRead;
		}
	}

	/**
	 *      * Start streaming video content
	 *           */
	function start()
	{
		$this->open();
		$this->setHeader();
		$this->stream();
		$this->end();
	}
}
Last updated 9 years ago.
0

Thanks @jolleyjoe - this got my videos working in Safari.

0

HI @jolleyjoe, @jvv and all :). Please help sigh! How is the videogular script written to display list of videos

0

saved post

0

Sign in to participate in this thread!

Eventy

Your banner here too?

gfarrell gfarrell Joined 13 Apr 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.