vue使用gitshot生成gif問題
問題背景
本文將介紹vue中使用gitshot生成gif。
問題分析
解決思路
使用input組件上傳一個(gè)視頻,獲取視頻文件后用一個(gè)video組件進(jìn)行播放,播放過程進(jìn)行截圖生成圖片數(shù)組。
demo演示上傳一個(gè)視頻,然后生成對應(yīng)的gif。
注意事項(xiàng)
html中使用video標(biāo)簽調(diào)用本地視頻結(jié)果只有音頻沒有視頻畫面問題?
解決思路
html中 video 標(biāo)簽支持的視頻格式,一共支持三種格式:
- Ogg
- MPEG4
- WebM
但這三種格式對于瀏覽器的兼容性卻各不同。
比如MP4格式,MP4只是一個(gè)容器,里面還有一個(gè)叫編碼器的東西。
格式雖然都是MP4但是html中只支持H.264的編碼格式。
所以要用軟件來轉(zhuǎn)碼。
- MP4 = MPEG 4文件使用 H264 視頻編解碼器和AAC音頻編解碼器
- WebM = WebM 文件使用 VP8 視頻編解碼器和 Vorbis 音頻編解碼器
- Ogg = Ogg 文件使用 Theora 視頻編解碼器和 Vorbis音頻編解碼器。
可使用格式工廠軟件對本地視頻格式進(jìn)行處理。
問題解決
(1)安裝所需的npm包
npm install gifshot --save
(2)完整代碼如下:
<template>
<div style="display: flex; flex-direction: column;">
<!-- 選擇要上傳的視頻 -->
<div style="display: flex;">
<input type="file" ref="videoInput" accept="video/mp4" style="margin: 15px;">
<button @click="handleFileChange" style="height: 30px; width: 50px; margin: 15px;">確定</button>
</div>
<video style="width: 400px;height: 400px; margin: 15px;" ref="videoRef" :src="videoUrl" controls>
</video>
<div style="margin: 15px;">輸出gif如下:</div>
</div>
</template><script>
import gifshot from 'gifshot';
export default {
data() {
return {
videoUrl: null,
interval: null,
clipImgs: [],
delay: 200,
}
},
methods: {
handleFileChange() {
// 獲取選擇的視頻文件
this.selectedVideo = this.$refs.videoInput.files[0];
console.log(this.selectedVideo);
if (!this.selectedVideo) {
console.log('請選擇文件');
return;
}
this.videoUrl = '';
this.convertFileToBase64(this.selectedVideo)
.then(base64Data => {
this.videoUrl = base64Data;
console.log('this videoUrl', this.videoUrl);
this.start() //視頻截圖并轉(zhuǎn)成gif
})
.catch(error => {
console.error('Error:', error);
});
},
// 文件轉(zhuǎn)成base64
convertFileToBase64(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => resolve(reader.result);
reader.onerror = error => reject(error);
});
},
// base64轉(zhuǎn)image
getImageFromBase64(base64Image) {
return new Promise((resolve, reject) => {
const img = new Image();
img.src = base64Image;
img.onload = () => resolve(img);
img.onerror = reject;
});
},
start() {
console.log('start');
// 每次開始重置以下值
clearInterval(this.interval);
let video = this.$refs.videoRef; //獲取video元素
console.log('start', video);
video.addEventListener('canplay', function () {
console.log('canplay', video);
video.play();
});
let that = this;
video.addEventListener('play', function () {
var canvas = document.createElement('canvas');
// 根據(jù)視頻大小調(diào)整畫布尺寸
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
console.log('play', video);
// 獲取上下文對象
var context = canvas.getContext('2d');
this.interval = setInterval(() => {
context.drawImage(video, 0, 0); // 將視頻內(nèi)容繪制到畫布上
let screenshotDataURL = canvas.toDataURL(); // 轉(zhuǎn)成Base64
console.log(screenshotDataURL, that.clipImgs);
that.clipImgs.push(screenshotDataURL) //把所有截圖放到一個(gè)數(shù)組里
}, that.delay)
});
// 監(jiān)聽視頻播放結(jié)束后,說明截圖完成。定時(shí)器停止,清除定時(shí)器緩存,開始轉(zhuǎn)換。
video.addEventListener('ended', function (e) {
console.log('stop');
clearInterval(this.interval)
this.interval = null
that.makeGIF()
})
},
async makeGIF() {
// 使用gifshot,將圖片數(shù)組轉(zhuǎn)成gif
gifshot.createGIF({
gifWidth: 200,
gifHeight: 200,
images: this.clipImgs,
interval: 0.1,
numFrames: 10,
frameDuration: 1,
fontWeight: 'normal',
fontSize: '16px',
fontFamily: 'sans-serif',
fontColor: '#ffffff',
textAlign: 'center',
textBaseline: 'bottom',
sampleInterval: 10,
numWorkers: 2
}, function (obj) {
console.log(obj, " obj");
if (!obj.error) {
let image = obj.image;
let animatedImage = document.createElement('img');
animatedImage.src = image;
animatedImage.style = 'margin: 15px';
document.body.appendChild(animatedImage);
}
});
}
}
}
</script>
運(yùn)行結(jié)果如下:

選擇本地視頻文件,確定選擇會(huì)播放該視頻,播放完成生成對應(yīng)的gif文件。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue3使用vue-office插件實(shí)現(xiàn)word預(yù)覽功能
vue-office是一個(gè)支持多種文件(docx、.xlsx、pdf)預(yù)覽的vue組件庫,支持vue2和vue3,這篇文章主要介紹了Vue3使用vue-office插件實(shí)現(xiàn)word預(yù)覽功能,需要的朋友可以參考下2024-04-04
關(guān)于console.log打印結(jié)果是?[object?Object]的解決
這篇文章主要介紹了關(guān)于console.log打印結(jié)果是?[object?Object]的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
vue-json-viewer展示JSON內(nèi)容實(shí)踐
這篇文章主要介紹了vue-json-viewer展示JSON內(nèi)容實(shí)踐,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2026-03-03
vue使用vue-i18n實(shí)現(xiàn)國際化的實(shí)現(xiàn)代碼
本篇文章主要介紹了vue使用vue-i18n實(shí)現(xiàn)國際化的實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04
解決vue3報(bào)錯(cuò):找不到模塊或其相應(yīng)的類型聲明
這篇文章主要給大家介紹了關(guān)于如何解決vue3報(bào)錯(cuò):找不到模塊或其相應(yīng)的類型聲明的相關(guān)資料,這個(gè)錯(cuò)誤提示是指在代碼中引用了Vue模塊,但是系統(tǒng)找不到該模塊或者缺少相應(yīng)的類型聲明文件,需要的朋友可以參考下2023-07-07
Vue3響應(yīng)式原理分析(Proxy(obj,{get(){},set(){}}))
文章詳細(xì)分析了Vue3的響應(yīng)式原理,包括Proxy對象的get和set方法,以及雙向數(shù)據(jù)綁定,通過代碼示例展示了Vue3和Vue2在set方法上的區(qū)別,說明了Vue3能夠正確修改值而Vue2不能2025-10-10

