EmbedVideo.vue 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <template>
  2. <b-embed
  3. :type="item.type"
  4. aspect="16by9"
  5. :src="item.src"
  6. v-bind="item.attrs"
  7. />
  8. </template>
  9. <script>
  10. export default {
  11. name: 'EmbedVideo',
  12. props: {
  13. src: { type: String, required: true }
  14. },
  15. data () {
  16. return {
  17. types: [
  18. {
  19. reg: /^((?:https?:)?\/\/)?((?:www|m)\.)?((?:youtube\.com|youtu.be))(\/(?:[\w-]+\?v=|embed\/|v\/)?)([\w-]+)(\S+)?$/i,
  20. url: 'https://www.youtube.com/embed/$5',
  21. params: {
  22. 'picture-in-picture': 1,
  23. accelerometer: 1,
  24. gyroscope: 1
  25. }
  26. },
  27. {
  28. reg: /^.*vimeo.com\/(\d+)($|\/|\b)/i,
  29. url: 'https://player.vimeo.com/video/$1',
  30. params: {
  31. title: 1,
  32. byline: 1,
  33. portrait: 0
  34. }
  35. },
  36. {
  37. reg: /^.*(?:\/video|dai.ly)\/([A-Za-z0-9]+)([^#&?]*).*/i,
  38. url: 'https://www.dailymotion.com/embed/video/$1',
  39. params: {
  40. autoplay: 0
  41. }
  42. }
  43. ]
  44. }
  45. },
  46. computed: {
  47. embed () {
  48. return this.types.find(type => type.reg.exec(this.src))
  49. },
  50. item () {
  51. const embed = this.embed
  52. if (embed) {
  53. const query = Object.entries(embed.params).map(entry => entry.join('=')).join('&')
  54. const and = embed.url.indexOf('?') >= 0 ? '&' : '?'
  55. return {
  56. type: 'iframe',
  57. src: this.src.replace(embed.reg, embed.url) + and + query,
  58. attrs: { allowfullscreen: true }
  59. }
  60. }
  61. return {
  62. type: 'video',
  63. src: this.src,
  64. attrs: { controls: true }
  65. }
  66. }
  67. }
  68. }
  69. </script>