博客列表 >js 转视频第一帧为base64图片,用php转为图片文件

js 转视频第一帧为base64图片,用php转为图片文件

Time
Time原创
2022年10月22日 14:45:43657浏览

js代码:视频文件和js代码最好为同一服务器下,不然容易出错

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title></title>
  6. </head>
  7. <body>
  8. <video id="video" controls="controls" width="400" height="300" preload="auto">
  9. <source src="./upload/63534e2688f81.mp4">
  10. </video>
  11. <br>
  12. <br> 视频第一帧图片:
  13. <div id="output"></div>
  14. <script type="text/javascript">
  15. (function() {
  16. var video, output;
  17. var scale = 0.8;
  18. var initialize = function() {
  19. output = document.getElementById("output");
  20. video = document.getElementById("video");
  21. video.addEventListener('loadeddata', captureImage);
  22. };
  23. var captureImage = function() {
  24. var canvas = document.createElement("canvas");
  25. canvas.width = video.videoWidth * scale;
  26. canvas.height = video.videoHeight * scale;
  27. canvas.getContext('2d').drawImage(video, 0, 0, canvas.width,canvas.height);
  28. var img = document.createElement("img");
  29. img.src = canvas.toDataURL("image/png");
  30. img.width = 400;
  31. img.height = 300;
  32. console.log(img);
  33. output.appendChild(img);
  34. };
  35. initialize();
  36. })();
  37. </script>
  38. </body>
  39. </html>

php代码:将js获取到的base64数据转为文件

  1. file_put_contents('./aa.jpg',base64_decode(explode(',',$base64)[1]));
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议