在没有ffmpeg的情况下从视频创建缩略图 [英] Create thumbnail from video without ffmpeg in PHP
问题描述
我需要创建没有ffmpeg的缩略图,因为我必须在共享主机上部署站点,而ffmpeg在共享主机上不可用.
I need to create thumbnail without ffmpeg because I have to deploy site on shared hosting and ffmpeg is not available on shared hosting.
有人可以提出一些解决方案吗?
Can someone suggest some solution?
推荐答案
如果可以在浏览器中播放视频,则可以尝试使用html5的Canvas功能在画布中播放视频,然后从中发布静止图像视频使用javascript到服务器...也许您甚至可以将其自动化,或者如果您只有几十个视频可以手动处理...
If the video can be played back in the browser you could try using the Canvas feature of html5 to playback the video in a canvas and then post a still from that video to your server using javascript... Maybe you could even automate it, or if you only have a few dozen videos do it by hand...
以下是一些jquery风格的javascript,用于上载base64编码的jpg,以帮助您入门. (由几个不同的项目组成,因此未经测试,并且可能是安全噩梦.)
The following is some jquery flavored javascript to upload a base64-encoded jpg in order to get you started. (Mashed up from several different projects, so untested and probably a security nightmare.)
<script>
var vidDOM = $('article').children('video');
var vid = vidDOM.get(2);
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
vidDOM.bind({
'paused':function () {
vid.width = canvas.width = vid.offsetWidth;
vid.height = canvas.height = vid.offsetHeight;
var $this = this;
ctx.drawImage($this, 0, 0, vid.width, vid.height);
uploadbase64();
}
})
function uploadbase64(){
canvasData = canvas.toDataURL("image/jpeg");
var ajax = new XMLHttpRequest();
ajax.open("POST",'ImageUpload.php?filename='+vidDOM.id,false);
ajax.setRequestHeader('Content-Type', 'application/upload');
ajax.send(canvasData);
}
</script>
这里有一些php可以接受上传.
And here is a bit of php to accept the upload.
<?php
$filename =$_GET['filename'];
if (file_get_contents('php://input')){
// Remove the headers (data:,) part.
$filteredData=substr(file_get_contents('php://input'), strpos(file_get_contents('php://input'), ",")+1);
// Need to decode before saving since the data we received is already base64 encoded
$decodedData=base64_decode($filteredData);
//create the file
if($fp = fopen( $filename, 'wb' )){
fwrite( $fp, $decodedData);
fclose( $fp );
} else {
echo "Could not create file.";
}
}
echo "Created image ".$filename;
?>
这篇关于在没有ffmpeg的情况下从视频创建缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!