HTML5 and PHP: Play base 64 encoded video file

混江龙づ霸主 提交于 2021-01-29 17:50:59

问题


I am trying to play a base 64 encoded MP4 using the HTML5 data URI. For some reason, the video won't play. The approach works fine for image files (jpeg and png), however for some reason it won't work for media files.

On the client side I have:

document.getElementById("video_holder").src = "data:video/mp4;base64,"  + xmlhttp.responseText;

HTML something like:

<video controls> <source type="video/webm" src="" id="video_holder" />...

I am fetching the file using PHP and base64 encode it before sending it to the user:

$file = file_get_contents($path);
echo chunk_split(base64_encode(trim($file)));

I also tried:

$file = file_get_contents($path);
echo base64_encode(trim($file));

Any suggestions?


回答1:


I would say you first need to decode in JavaScript your Base 64 file. This is explained here but some browsers do not support it.

I would then create a Blob object from the decoded file and then create an objectURL from the blob and push this objectURL as the src of the HTML5 video tag.

I have not done this yet but this is how I would do it if I had to. Why do you need to base64 encode your video file in the first place? This can create overhead and increase processing time. If you need encryption a DRM solution would be better.



来源:https://stackoverflow.com/questions/26766336/html5-and-php-play-base-64-encoded-video-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!