VideoPlayer.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <template>
  2. <div class="video-player-container">
  3. <div class="video-container" ref="videoContainer">
  4. <video
  5. ref="videoPlayer"
  6. class="video-js vjs-big-play-centered"
  7. preload="auto"
  8. crossOrigin="anonymous"
  9. ></video>
  10. <!-- :poster="poster" -->
  11. <!-- 自定义控件 -->
  12. <div class="lcfbox">
  13. <div class="lcbox">
  14. <div class="tit">下一次可能出现的设备:</div>
  15. <div class="txt">1#旁 2号摄像头</div>
  16. </div>
  17. <div class="flexcj">
  18. <div class="bigbox flexc">
  19. <img src="@/assets/images/search/biga.png" @click="zoomOut" style="margin-left: 0;"/>
  20. <div class="custom-dot-steps">
  21. <div
  22. v-for="i in 5"
  23. :key="i"
  24. class="step-dot"
  25. :class="{ 'active': i == stepsactive}"
  26. ></div>
  27. </div>
  28. <img src="@/assets/images/search/bigb.png" @click="zoomIn"/>
  29. </div>
  30. <div class="bigbox flexc" style="padding-left: 7px;">
  31. <el-select v-model="playbackRate" @change="changeSpeed" placeholder="1.0x" popper-class="slectbox">
  32. <el-option
  33. v-for="item in speeds"
  34. :key="item.value"
  35. :label="item.text"
  36. :value="item.value">
  37. </el-option>
  38. </el-select>
  39. <img src="@/assets/images/search/bficoa.png" @click="takeScreenshot" />
  40. <img src="@/assets/images/search/bficob.png" @click="replay"/>
  41. <img src="@/assets/images/search/bficoc.png" @click="toggleFullscreen"/>
  42. </div>
  43. </div>
  44. <!-- 进度条 -->
  45. <div class="vjs-progress-container" @click="seek">
  46. <div class="vjs-progress-bar">
  47. <div class="vjs-progress-filled" :style="{ width: progressPercentage + '%' }">
  48. <img src="@/assets/images/search/cir.png" v-if="progressPercentage"/>
  49. </div>
  50. </div>
  51. </div>
  52. <div class="playbox">
  53. <div class="pltit">
  54. <div class="tit">10:32:18</div>
  55. <div class="txt">2025.06.13</div>
  56. </div>
  57. <div class="playbtn flexcc flex1">
  58. <img src="@/assets/images/search/pre.png"/>
  59. <div @click="paly=!paly" class="playb">
  60. <img src="@/assets/images/search/videob.png" @click="handlePlay" v-if="paly"/>
  61. <img src="@/assets/images/search/videoa.png" @click="handlePause" v-else/>
  62. </div>
  63. <img src="@/assets/images/search/next.png"/>
  64. </div>
  65. <div class="pltit txr">
  66. <div class="tit">10:47:56</div>
  67. <div class="txt">2025.06.13</div>
  68. </div>
  69. </div>
  70. </div>
  71. <!-- <div v-if="!playerLoading" class="vjs-custom-controls">
  72. <button class="vjs-custom-button" @click="replay" title="重播">↻</button>
  73. <button class="vjs-custom-button" @click="zoomOut" title="缩小">-</button>
  74. <span class="vjs-zoom-level">{{ zoomPercentage }}%</span>
  75. <button class="vjs-custom-button" @click="zoomIn" title="放大">+</button>
  76. <div class="vjs-progress-container" @click="seek">
  77. <div class="vjs-progress-bar">
  78. <div class="vjs-progress-filled" :style="{ width: progressPercentage + '%' }"></div>
  79. </div>
  80. </div>
  81. <select class="vjs-speed-selector" v-model="playbackRate" @change="changeSpeed" title="播放速度">
  82. <option v-for="speed in speeds" :key="speed.value" :value="speed.value">{{ speed.text }}</option>
  83. </select>
  84. <button class="vjs-custom-button" @click="takeScreenshot" title="截图">📷</button>
  85. <button class="vjs-custom-button" @click="toggleFullscreen" title="全屏">⛶</button>
  86. </div> -->
  87. </div>
  88. </div>
  89. </template>
  90. <script>
  91. import videojs from 'video.js'
  92. import 'video.js/dist/video-js.css'
  93. import html2canvas from 'html2canvas'
  94. export default {
  95. name: 'VideoPlayer',
  96. props: {
  97. src: {
  98. type: String,
  99. required: true
  100. },
  101. poster: {
  102. type: String,
  103. default: ''
  104. },
  105. options: {
  106. type: Object,
  107. default: () => ({})
  108. }
  109. },
  110. data() {
  111. return {
  112. player: null,
  113. paly:false,
  114. playerLoading: true,
  115. zoomLevel: 1,
  116. minZoom: 0.5,
  117. maxZoom: 1.5,
  118. zoomStep: 0.25,
  119. playbackRate: 1,
  120. stepsactive:3,
  121. speeds: [
  122. { value: 0.5, text: '0.5x' },
  123. { value: 0.75, text: '0.75x' },
  124. { value: 1, text: '1x' },
  125. { value: 1.25, text: '1.25x' },
  126. { value: 1.5, text: '1.5x' },
  127. { value: 2, text: '2x' }
  128. ],
  129. progressPercentage: 0
  130. }
  131. },
  132. computed: {
  133. zoomPercentage() {
  134. return Math.round(this.zoomLevel * 100)
  135. }
  136. },
  137. mounted() {
  138. this.initPlayer()
  139. },
  140. beforeDestroy() {
  141. if (this.player) {
  142. this.player.dispose()
  143. }
  144. },
  145. methods: {
  146. togglePlay() {
  147. if (this.player.paused()) {
  148. this.player.play()
  149. } else {
  150. this.player.pause()
  151. }
  152. },
  153. handlePlay(){
  154. this.player.pause(); // 会触发videoPlay()函数
  155. },
  156. handlePause(){
  157. this.player.play(); // 会触发videoPlay()函数
  158. },
  159. initPlayer() {
  160. const videoElement = this.$refs.videoPlayer
  161. // 合并默认选项和传入的选项
  162. const defaultOptions = {
  163. controls: false,
  164. autoplay: false,
  165. fluid: true,
  166. sources: [{
  167. src: this.src,
  168. type: 'video/mp4'
  169. }]
  170. }
  171. const finalOptions = Object.assign({}, defaultOptions, this.options)
  172. // 初始化播放器
  173. this.player = videojs(videoElement, finalOptions, () => {
  174. this.playerLoading = false
  175. // 监听时间更新事件来更新进度条
  176. this.player.on('timeupdate', this.updateProgress)
  177. // 监听全屏变化
  178. this.player.on('fullscreenchange', () => {
  179. if (!this.player.isFullscreen()) {
  180. this.resetZoom()
  181. }
  182. })
  183. })
  184. },
  185. updateProgress() {
  186. if (this.player.duration()) {
  187. this.progressPercentage = (this.player.currentTime() / this.player.duration()) * 100
  188. }
  189. },
  190. seek(event) {
  191. if (!this.player) return
  192. const progressBar = event.currentTarget
  193. const percent = event.offsetX / progressBar.offsetWidth
  194. this.player.currentTime(this.player.duration() * percent)
  195. },
  196. replay() {
  197. this.player.currentTime(0)
  198. this.player.play()
  199. },
  200. zoomIn() {
  201. if (this.zoomLevel < this.maxZoom) {
  202. this.zoomLevel += this.zoomStep
  203. this.stepsactive++
  204. this.applyZoom()
  205. }
  206. },
  207. zoomOut() {
  208. if (this.zoomLevel > this.minZoom) {
  209. this.zoomLevel -= this.zoomStep
  210. this.stepsactive--
  211. this.applyZoom()
  212. }
  213. },
  214. applyZoom() {
  215. const videoElement = this.$refs.videoPlayer
  216. if (videoElement) {
  217. videoElement.style.transform = `scale(${this.zoomLevel})`
  218. videoElement.style.transformOrigin = 'center center'
  219. }
  220. },
  221. resetZoom() {
  222. this.zoomLevel = 1
  223. const videoElement = this.$refs.videoPlayer
  224. if (videoElement) {
  225. videoElement.style.transform = 'none'
  226. }
  227. },
  228. changeSpeed() {
  229. this.player.playbackRate(this.playbackRate)
  230. },
  231. takeScreenshot() {
  232. const videoElement = this.$refs.videoPlayer
  233. // videoElement.setAttribute("crossOrigin", "anonymous"); // 处理跨域
  234. // video.setAttribute("src", url);
  235. // // 静音操作,防止播放失败
  236. // video.setAttribute("muted", "muted");
  237. // 创建一个canvas来绘制视频帧
  238. const canvas = document.createElement('canvas')
  239. canvas.width = videoElement.videoWidth
  240. canvas.height = videoElement.videoHeight
  241. const ctx = canvas.getContext('2d')
  242. ctx.drawImage(videoElement, 0, 0, canvas.width, canvas.height)
  243. // 创建下载链接
  244. const link = document.createElement('a')
  245. link.download = `screenshot-${new Date().getTime()}.png`
  246. link.href = canvas.toDataURL('image/png')
  247. document.body.appendChild(link)
  248. link.click()
  249. document.body.removeChild(link)
  250. // 或者使用html2canvas捕获整个播放器
  251. // html2canvas(this.$refs.videoContainer).then(canvas => {
  252. // const link = document.createElement('a')
  253. // link.download = `screenshot-${new Date().getTime()}.png`
  254. // link.href = canvas.toDataURL('image/png')
  255. // link.click()
  256. // })
  257. },
  258. toggleFullscreen() {
  259. if (this.player.isFullscreen()) {
  260. this.player.exitFullscreen()
  261. } else {
  262. this.player.requestFullscreen()
  263. }
  264. }
  265. }
  266. }
  267. </script>
  268. <style lang="scss" scoped>
  269. .flex{display: flex;}
  270. .flex0{flex: 0 0 auto;}
  271. .flex1{flex: 1;}
  272. .flexc{display: flex;align-items: center;}
  273. .flexcc{display: flex;align-items: center;justify-content: center;}
  274. .flexcj{display: flex;justify-content: space-between;}
  275. .lcfbox{position: absolute;left: 17px;right: 17px;bottom: 15px;
  276. .lcbox{background: rgba(0, 0, 0, 0.5);border-radius: 10px;padding: 10px 12px;display: inline-block;margin-bottom: 13px;
  277. .tit{font-size: 14px;color: #FFFFFF;margin-bottom: 6px;}
  278. .txt{font-weight: bold;font-size: 14px;color: #FFFFFF;}
  279. }
  280. .bigbox{background: rgba(0, 0, 0, 0.5);border-radius: 10px;padding: 6px 19px;min-height: 30px;margin-bottom: 12px;
  281. img{width: 17px;height: 17px;margin-left: 11px;}
  282. }
  283. .playbox{min-height: 60px;padding: 9px 18px 9px 28px;
  284. background: rgba(255, 255, 255, 0.8);display: flex;align-items: center;
  285. .pltit{
  286. .tit{font-size: 18px;color: #3D455B;margin-bottom: 3px;}
  287. .txt{font-size: 12px;color: #666666;}
  288. }
  289. .playbtn{
  290. .pre{width: 18px;height: 20px;}
  291. .playb{width: 36px;height: 36px;margin: 0 29px;
  292. img{width: 100%;height: 100%;}
  293. }
  294. }
  295. }
  296. }
  297. .custom-dot-steps {margin-left: 13px;width: 82px;position: relative;display: flex;align-items: center;justify-content: space-between;}
  298. .custom-dot-steps::before {content: '';position: absolute;top: 50%;left: 0;right: 0;height: 2px;background: #FFFFFF;transform: translateY(-50%);z-index: 1;}
  299. .step-dot {width: 4px;height: 4px;border-radius: 50%;background: #FFFFFF;position: relative;
  300. &.active{width: 8px;height: 8px;}
  301. }
  302. .video-player-container {
  303. position: relative;
  304. width: 100%;height: 100%;
  305. /* max-width: 800px;
  306. margin: 0 auto; */
  307. }
  308. .video-container {
  309. width: 100%;height: 100%;
  310. position: relative;
  311. overflow: hidden;
  312. }
  313. .video-js {
  314. width: 100%;
  315. height: 100% !important;
  316. padding-top: 50% !important;
  317. box-sizing: border-box;
  318. background-color: #000;
  319. transition: transform 0.3s ease;
  320. }
  321. .vjs-custom-controls {
  322. display: flex;
  323. align-items: center;
  324. padding: 0 10px;
  325. background: rgba(0, 0, 0, 0.7);
  326. position: absolute;
  327. bottom: 150px;
  328. left: 0;
  329. right: 0;
  330. z-index: 1;
  331. }
  332. .vjs-custom-button {
  333. background: none;
  334. border: none;
  335. color: white;
  336. cursor: pointer;
  337. font-size: 16px;
  338. margin: 0 5px;
  339. padding: 5px 10px;
  340. }
  341. .vjs-custom-button:hover {
  342. color: #00a8ff;
  343. }
  344. .vjs-progress-container {
  345. flex-grow: 1;
  346. margin-bottom:14px;
  347. cursor: pointer;
  348. }
  349. .vjs-progress-bar {
  350. height: 8px;
  351. border-radius:4px;
  352. background: rgba(0, 0, 0, 0.5);
  353. position: relative;
  354. }
  355. .vjs-progress-filled {
  356. height: 100%;border-radius: 4px;
  357. background: red;position: relative;
  358. img{width: 24px;height: 24px;position: absolute;right: -6px;top: -8px;}
  359. }
  360. .vjs-speed-selector {
  361. background: rgba(0, 0, 0, 0.8);
  362. color: white;
  363. border: none;
  364. padding: 5px;
  365. margin: 0 5px;
  366. cursor: pointer;
  367. }
  368. .vjs-zoom-level {
  369. color: white;
  370. margin: 0 5px;
  371. min-width: 40px;
  372. text-align: center;
  373. }
  374. </style>