Vue2 使用 Knova Canvas 合成图片、多个视频、音频在一个画面中并播放,自定义 video control 控制条
本文转载https://blog.csdn.net/RosaChampagne/article/details/128020428?spm=1001.2014.3001.5502的文章
安装插件
1 | npm install vue-konva@2 konva --save |
在main.js中使用
1 2 3 4 | import Vue from 'vue' ; import VueKonva from 'vue-konva' ; Vue.use(VueKonva); |
相关实现代码
html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | <template> <div class = "video-preview-wrapper" > <div ref= "videoPreviewBox" class = "video-preview-box" > <div class = "video-box" > <v-stage ref= "stage" :config= "stageConfig" @click= "onControl" > <v-layer ref= "layer" > <v-image ref= "frame" :config= "imageConfig" /> </v-layer> <v-layer> <v-image v- for = "(cover, index) in videoCovers" :key= "index" :config= "cover" /> </v-layer> </v-stage> </div> <div class = "control-play" > <div class = "control-play-btn" @click= "onControl" > <i : class = "[{ 'el-icon-video-pause': isPlay }, { 'el-icon-video-play': !isPlay }]" /> </div> <div class = "control-progress common-progress" > <div> <el-slider v-model= "videoProgress" :show-tooltip= "false" :max= "canvas.duration" input-size= "small" @change= "onProgressChange" /> </div> </div> <div class = "current-time" >{{ currentTime }}</div> / <div class = "duration" >{{ duration }}</div> <div class = "video-speed-box" > <el-dropdown placement= "bottom" @command= "onCommand" > <div class = "video-speed-show" >{{ playbackRate }}x</div> <el-dropdown-menu slot= "dropdown" > <el-dropdown-item command= "1" >0.5x</el-dropdown-item> <el-dropdown-item command= "2" >1x</el-dropdown-item> <el-dropdown-item command= "3" >1.5x</el-dropdown-item> <el-dropdown-item command= "4" >2x</el-dropdown-item> <el-dropdown-item command= "5" >3x</el-dropdown-item> </el-dropdown-menu> </el-dropdown> </div> <div class = "control-voice common-progress" > <span class = "voice-icon" /> <div class = "voice-slider" > <el-slider v-model= "voiceProgress" input-size= "small" @change= "onVoiceChange" /> </div> </div> <div class = "fullscreen" title= "全屏" @click= "onFullScreen" > <i class = "el-icon-full-screen" /> </div> </div> </div> </div> </template> |
相关方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 | export default { name: 'VideoPreview' , data() { return { stageConfig: { width: window.innerWidth, height: (window.innerHeight - 64), }, imageConfig: { image: null , width: window.innerWidth, height: (window.innerHeight - 64), }, canvas: { duration: 10, volume: 1, playbackRate: 1, frames: [ { imageUrl: require( './bg1.jpg' ), duration: 3, videos: [ { x: 20, y: 100, width: 200, height: 200, cover: require( './VfE_html5.jpg' ), url: require( './VfE_html5.mp4' ), volume: 1, playbackRate: 1, }, { x: 420, y: 100, width: 200, height: 200, cover: require( './video_thumb.jpg' ), url: require( './video.mp4' ), volume: 1, playbackRate: 1, }, ], audios: [ { url: require( './dengnixiake.flac' ), volume: 1, }, ], }, { imageUrl: require( './bg2.jpg' ), duration: 3, videos: [ { x: 100, y: 100, width: 200, height: 200, cover: require( './VfE_html5.jpg' ), url: require( './flower.mp4' ), volume: 1, playbackRate: 1, }, ], audios: [], }, { imageUrl: require( './bg3.jpg' ), duration: 2, videos: [], audios: [ { url: require( './shuohaobuku.flac' ), volume: 1, }, ], }, { imageUrl: require( './bg4.jpg' ), duration: 2, videos: [], audios: [], }, ], }, videoCovers: [], videos: [], audios: [], isPlay: false , duration: 0, currentTime: '00:00:00' , videoProgress: 0, playbackRate: 1, voiceProgress: 100, videoTimeTimer: null , videoSceneTimer: null , videoTimers: [], audioTimers: [], }; }, watch: { videoProgress(value) { // 如果播放完成,则暂停播放,清除视频时间定时器 if (value === this .canvas.duration) { this .isPlay = false ; clearInterval( this .videoTimeTimer); } // 更换视频背景图 this .canvas.frames.forEach(({ imageUrl, startAt, endAt }) => { if (value >= startAt && value < endAt) { const img = new Image(); img.src = imageUrl; img.onload = () => { if (`http: //localhost:8080${imageUrl}` !== this.imageConfig.image.src) { this .imageConfig.image = img; } }; } }); // 暂停不在播放时间范围内的窗口视频 this .videos .filter(({ startAt, endAt }) => ( this .videoProgress < startAt || this .videoProgress > endAt)) .forEach(({ videoObj }) => { videoObj.pause(); videoObj.currentTime = 0; }); // 暂停不在播放时间范围内的音频 this .audios .filter(({ startAt, endAt }) => ( this .videoProgress < startAt || this .videoProgress > endAt)) .forEach(({ audioObj }) => { audioObj.pause(); audioObj.currentTime = 0; }); }, }, created() { this .duration = this .formatVideoTime( this .canvas.duration); // 计算出每一个场景的开始时间、结束时间 const durationList = this .canvas.frames.map(({ duration }) => duration); durationList.reduce((prev, current, idx) => { if (idx <= 1) { this .canvas.frames[0].startAt = 0; this .canvas.frames[0].endAt = prev; } this .canvas.frames[idx].startAt = prev; this .canvas.frames[idx].endAt = prev + current; return prev + current; }); // 将所有场景的窗口视频、音频初始化 this .canvas.frames.forEach(({ videos, audios, startAt, endAt }) => { videos.forEach((video) => { const videoObj = document.createElement( 'video' ); videoObj.src = video.url; videoObj.muted = true ; videoObj.addEventListener( 'play' , () => { this .videoCovers = []; this .timerCallback(); }); this .videos.push({ videoObj, x: video.x, y: video.y, width: video.width, height: video.height, startAt, endAt, }); }); audios.forEach((audio) => { const audioObj = document.createElement( 'audio' ); audioObj.src = audio.url; audioObj.volume = audio.volume; this .audios.push({ audioObj, startAt, endAt }); }); }); if ( this .canvas.frames?.[0]) { // 第一个场景的视频封面 this .videoCovers = this .canvas.frames[0].videos?.map(({ cover, x, y, width, height }) => { const image = new Image(); image.src = cover; return { x, y, width, height, image }; }); // 第一个场景的背景图 const img = new Image(); img.src = this .canvas.frames[0].imageUrl; img.onload = () => { this .imageConfig.image = img; }; } }, mounted() { this .ctx = this .$refs.frame.getNode().getContext( '2d' ); // 在没有cover的情况下,可设置视频首帧为封面 this .videos .filter(({ startAt, endAt }) => ( this .videoProgress >= startAt && this .videoProgress < endAt)) .forEach(({ videoObj, x, y, width, height }) => { videoObj.addEventListener( 'loadeddata' , () => { videoObj.play(); this .ctx.drawImage(videoObj, x, y, width, height); setTimeout(() => { videoObj.pause(); }, 100); }); }); }, methods: { timerCallback() { this .videos .filter(({ startAt, endAt }) => ( this .videoProgress >= startAt && this .videoProgress < endAt)) .forEach(({ videoObj, x, y, width, height }) => { if (videoObj.paused || videoObj.ended) { return ; } this .ctx.drawImage(videoObj, x, y, width, height); clearTimeout( this .videoSceneTimer); this .videoSceneTimer = setTimeout(() => { this .timerCallback(); }, 0); }); }, onControl() { this .isPlay = ! this .isPlay; if ( this .canvas.duration <= this .videoProgress) { this .videoProgress = 0; } this .controlPlay(); }, onProgressChange(val) { // 设置窗口小视频的播放进度 this .videos .filter(({ startAt, endAt }) => ( this .videoProgress >= startAt && this .videoProgress < endAt)) .forEach(({ videoObj, startAt }) => { videoObj.currentTime = val - startAt; }); // 设置音频的播放进度 this .audios .filter(({ startAt, endAt }) => ( this .videoProgress >= startAt && this .videoProgress < endAt)) .forEach(({ audioObj, startAt }) => { audioObj.currentTime = val - startAt; }); this .updateVideoProgress(); this .controlPlay(); // 显示在播放时间范围内的窗口视频 this .videos .filter(({ startAt, endAt }) => ( this .videoProgress > startAt && this .videoProgress < endAt)) .forEach(({ videoObj, x, y, width, height }) => { videoObj.play(); this .ctx.drawImage(videoObj, x, y, width, height); setTimeout(() => { videoObj.pause(); }, 100); }); }, controlPlay() { clearInterval( this .videoTimeTimer); if ( this .isPlay) { // 定时器定时更新视频时间 this .videoTimeTimer = setInterval(() => { this .updateVideoProgress(); }, 1000 / this .playbackRate); } this .videoTimers = []; this .audioTimers = []; this .videos.forEach(({ videoObj, startAt, endAt }) => { // 控制视频的播放、暂停 if ( this .videoProgress >= startAt && this .videoProgress < endAt) { if ( this .isPlay) { videoObj.play(); } else { videoObj.pause(); } } // 控制即将播放的视频的播放、暂停 if ( this .videoProgress < startAt) { const videoTimer = setTimeout(() => { if ( this .isPlay) { videoObj.play(); } else { videoObj.pause(); } }, (startAt - this .videoProgress + 1) * 1000); this .videoTimers.push(videoTimer); } }); this .audios.forEach(({ audioObj, startAt, endAt }) => { // 控制音频的播放、暂停 if ( this .videoProgress >= startAt && this .videoProgress < endAt) { if ( this .isPlay) { audioObj.play(); } else { audioObj.pause(); } } // 控制即将播放的音频的播放、暂停 if ( this .videoProgress < startAt) { const audioTimer = setTimeout(() => { if ( this .isPlay) { audioObj.play(); } else { audioObj.pause(); } }, (startAt - this .videoProgress + 1) * 1000); this .audioTimers.push(audioTimer); } }); }, updateVideoProgress() { if ( this .videoProgress >= this .canvas.duration) { this .videoProgress = this .canvas.duration; } else { this .videoProgress += 1; } this .currentTime = this .formatVideoTime( this .videoProgress); }, formatVideoTime(time) { const currentTime = time; let hour = parseInt(currentTime / 3600, 10); let minute = parseInt((currentTime % 3600) / 60, 10); let seconds = parseInt(currentTime % 60, 10); hour = hour < 10 ? `0${hour}` : hour; minute = minute < 10 ? `0${minute}` : minute; seconds = seconds < 10 ? `0${seconds}` : seconds; return `${hour}:${minute}:${seconds}`; }, onCommand(val) { let playbackRate = 0; switch (val) { case '1' : playbackRate = 0.5; break ; case '2' : playbackRate = 1; break ; case '3' : playbackRate = 1.5; break ; case '4' : playbackRate = 2; break ; case '5' : playbackRate = 3; break ; default : playbackRate = 1; break ; } this .playbackRate = playbackRate; this .videos .filter(({ startAt, endAt }) => ( this .videoProgress >= startAt && this .videoProgress < endAt)) .forEach(({ videoObj }) => { videoObj.playbackRate = playbackRate; }); this .audios .filter(({ startAt, endAt }) => ( this .videoProgress >= startAt && this .videoProgress < endAt)) .forEach(({ audioObj }) => { audioObj.playbackRate = playbackRate; }); }, onVoiceChange(val) { const newVolume = val / 100; this .audios .filter(({ startAt, endAt }) => ( this .videoProgress >= startAt && this .videoProgress < endAt)) .forEach(({ audioObj }) => { audioObj.volume = newVolume; }); }, onFullScreen() { const element = this .$refs.videoPreviewBox; const isFullScreen = document.fullscreen || document.mozFullScreen || document.webkitIsFullScreen || document.webkitFullScreen || document.msFullScreen; if (isFullScreen) { if (document.exitFullscreen) { document.exitFullscreen(); } else if (document.mozCancelFullScreen) { document.mozCancelFullScreen(); } else if (document.msExitFullscreen) { document.msExitFullscreen(); } else if (document.webkitExitFullscreen) { document.webkitExitFullscreen(); } return ; } if (element.requestFullscreen) { element.requestFullscreen(); } else if (element.mozRequestFullScreen) { element.mozRequestFullScreen(); } else if (element.msRequestFullscreen) { element.msRequestFullscreen(); } else if (element.webkitRequestFullscreen) { element.webkitRequestFullScreen(); } }, }, }; |
css样式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | .video-preview-wrapper { position: relative; height: 100%; .video-preview-box { .video-box { position: absolute; } .control-play { width: 100%; position: absolute; left: 0; bottom: 5%; display: flex; align-items: center; padding: 0 10px; color: #fff; .control-play-btn { margin-right: 20px; font-size: 24px; cursor: pointer; } .control-progress { width: 60%; } .current-time { margin: 0 10px 0 20px; } .duration { margin-left: 10px; } .video-speed-box { width: 40px; display: flex; justify-content: center; margin: 0 20px; background-color: aliceblue; cursor: pointer; .el-dropdown { width: 100%; text-align: center; } } .control-voice { width: 10%; } .fullscreen { margin-left: 20px; cursor: pointer; } } } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!