reaee-video-cover-extractor.vue 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <template>
  2. <view :res="path" :change:res="reaeeVideoCoverExtractor.loadres" style="width: 0;height: 0;"/>
  3. </template>
  4. <script>
  5. export default {
  6. name:'reaeeVideoCoverExtractor',
  7. props:{
  8. path:{
  9. type: String,
  10. default:''
  11. }
  12. },
  13. methods: {
  14. success(o) {
  15. this.$emit('success',o)
  16. },
  17. error(e) {
  18. this.$emit('error',e)
  19. }
  20. }
  21. }
  22. </script>
  23. <script module="reaeeVideoCoverExtractor" lang="renderjs">
  24. export default {
  25. methods: {
  26. loadres(newValue, oldValue, ownerInstance, instance) {
  27. if(!newValue)return
  28. const video = document.createElement("video")
  29. video.addEventListener('loadeddata', async (e) => {
  30. try {
  31. const mediaStream = video.captureStream();
  32. const mediaStreamTracks = mediaStream.getVideoTracks();
  33. const imageCapture = new ImageCapture(mediaStreamTracks[0]);
  34. const imageBitmap = await imageCapture.grabFrame()
  35. const {width, height} = imageBitmap
  36. const canvas = document.createElement("canvas");
  37. canvas.setAttribute("width", width);
  38. canvas.setAttribute("height", height);
  39. const canvasCtx = canvas.getContext("2d");
  40. canvasCtx.drawImage(imageBitmap, 0, 0);
  41. canvas.toBlob(b => {
  42. const src = URL.createObjectURL(b);
  43. ownerInstance.callMethod('success', src)
  44. }, 'image/png', 1)
  45. } catch (e) {
  46. console.error(e);
  47. ownerInstance.callMethod('error', 'other error')
  48. } finally {
  49. instance.$el.removeChild(video)
  50. }
  51. })
  52. video.addEventListener('error', e => {
  53. console.error(e);
  54. instance.$el.removeChild(video)
  55. ownerInstance.callMethod('error', 'load error')
  56. })
  57. video.style.width = '0'
  58. video.style.height = '0'
  59. video.setAttribute('crossorigin', 'anonymous');
  60. video.setAttribute('src', newValue);
  61. instance.$el.appendChild(video)
  62. },
  63. }
  64. }
  65. </script>