HTML5和PHP:播放base 64编码的视频文件 [英] HTML5 and PHP: Play base 64 encoded video file

查看:191
本文介绍了HTML5和PHP:播放base 64编码的视频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用HTML5数据URI播放以base 64编码的MP4.由于某些原因,该视频无法播放.这种方法对图像文件(jpeg和png)效果很好,但是由于某种原因,对媒体文件不起作用.

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.

在客户端,我有:

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

类似HTML的

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

我正在使用PHP提取文件并对其进行base64编码,然后再将其发送给用户:

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)));

我也尝试过:

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

有什么建议吗?

推荐答案

我想说,您首先需要使用JavaScript对Base 64文件进行解码.在此处中对此进行了解释,但是某些浏览器可以不支持.

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.

然后,我将根据创建一个 Blob对象解码的文件,然后从Blob创建 objectURL 并将该objectURL作为HTML5视频标签的src推送.

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.

我还没有这样做,但是如果需要的话,这就是我会做的.为什么首先需要对视频文件进行base64编码?这会增加开销并增加处理时间.如果您需要加密,则DRM解决方案会更好.

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.

这篇关于HTML5和PHP:播放base 64编码的视频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆