Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play local (hard-drive) video file with HTML5 video tag?

I want to achieve the following.

<video src="file:///Users/username/folder/video.webm"> </video> 

The intent is that the user will be able to select a file from his/her hard drive.

And the reason for not uploading is of course transmission costs and storage quota. There will be no reason to save the file.

Is it possible?

like image 358
Chris Avatar asked Jan 16 '12 20:01

Chris


People also ask

What is the correct HTML5 element for playing video files?

<video>: The Video Embed element. The <video> HTML element embeds a media player which supports video playback into the document.

Can HTML play video files?

HTML allows playing video in the web browser by using <video> tag. To embed the video in the webpage, we use src element for mentioning the file address and width and height attributes are used to define its size. Example: In this example, we are using <video> tag to to add video into the web page.

Why video is not playing in HTML5?

An 'HTML5: Video file not found' error indicates either the browser you are using doesn't support HTML5 or the webpage doesn't have the proper video codec. You may contact the website's developer to install HTML5 supporting codecs for all the three WebM, MP4, and OGG formats.


2 Answers

It is possible to play a local video file.

<input type="file" accept="video/*"/> <video controls autoplay></video> 

When a file is selected via the input element:

  1. 'change' event is fired
  2. Get the first File object from the input.files FileList
  3. Make an object URL that points to the File object
  4. Set the object URL to the video.src property
  5. Lean back and watch :)

http://jsfiddle.net/dsbonev/cCCZ2/embedded/result,js,html,css/

(function localFileVideoPlayer() {    'use strict'    var URL = window.URL || window.webkitURL    var displayMessage = function(message, isError) {      var element = document.querySelector('#message')      element.innerHTML = message      element.className = isError ? 'error' : 'info'    }    var playSelectedFile = function(event) {      var file = this.files[0]      var type = file.type      var videoNode = document.querySelector('video')      var canPlay = videoNode.canPlayType(type)      if (canPlay === '') canPlay = 'no'      var message = 'Can play type "' + type + '": ' + canPlay      var isError = canPlay === 'no'      displayMessage(message, isError)        if (isError) {        return      }        var fileURL = URL.createObjectURL(file)      videoNode.src = fileURL    }    var inputNode = document.querySelector('input')    inputNode.addEventListener('change', playSelectedFile, false)  })()
video,  input {    display: block;  }    input {    width: 100%;  }    .info {    background-color: aqua;  }    .error {    background-color: red;    color: white;  }
<h1>HTML5 local video file player example</h1>  <div id="message"></div>  <input type="file" accept="video/*" />  <video controls autoplay></video>
like image 73
Dimitar Bonev Avatar answered Sep 22 '22 08:09

Dimitar Bonev


That will be possible only if the HTML file is also loaded with the file protocol from the local user's harddisk.

If the HTML page is served by HTTP from a server, you can't access any local files by specifying them in a src attribute with the file:// protocol as that would mean you could access any file on the users computer without the user knowing which would be a huge security risk.

As Dimitar Bonev said, you can access a file if the user selects it using a file selector on their own. Without that step, it's forbidden by all browsers for good reasons. Thus, while his answer might prove useful for many people, it loosens the requirement from the code in the original question.

like image 37
Holger Just Avatar answered Sep 20 '22 08:09

Holger Just