Can I convert videos with javascript?

Cokegod picture Cokegod · Sep 16, 2011 · Viewed 8.3k times · Source

I am making an application in HTML5 that lets you choose a local video file, then play it using the video tag. The problem is that some of the codecs of the videos are not playable by current browsers (XviD in general, H.264 for Firefox, etc). Is it possible to locally convert the videos to the right codec using JavaScript?

Edit: I probably didn't explain myself correctly, I am making a kind of "Media Player" using HTML5: The user chooses a file, the script reads it using the HTML5 File API and then adds it to a video tag, no server involved. This is why I need JavaScript for this.

Answer

user321531 picture user321531 · Sep 16, 2011

I don't believe you can easily do anything like what you want - especially if you expect users to select a video file and then within seconds it's being shared online.

There are a lot of variables and a ton of processing involved in video conversion/compression and there's a reason why even gigantic sites like YouTube take quite awhile to have your videos appear.

Plus, you want people to use a variety of video file types and somehow output the same type of video file, correct? Geez. Now you have to find open source video conversion apps that you can perhaps get to work behind the scenes just to get it to play nice with whatever you final video file type will be.

I don't see how this can be done with JavaScript alone. I think you could probably build an uploader and then maybe use another app to check a directory for new files and convert the files with a script to something else....but JS alone? And from your browser? And in a halfway decent amount of time? No way.

I would suggest only allowing certain types of video files to be uploaded and reject all others. Don't even mess with converting them.