-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
88d237b
commit ddb201b
Showing
14 changed files
with
241 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
# @pear-rec/web | ||
|
||
## 1.3.7 | ||
|
||
feat: 录制GIF | ||
|
||
## 1.3.6 | ||
|
||
perf: 优化录制全屏 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
packages/web/src/components/videoToGif/VideoToGifConverter.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import React, { useEffect, useRef, useState } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import GIF from 'gif.js'; | ||
import { Button, Modal, Progress } from 'antd'; | ||
import { saveAs } from 'file-saver'; | ||
import { useApi } from '../../api'; | ||
|
||
export default function VideoToGifConverter({ videoSrc, user }) { | ||
const { t } = useTranslation(); | ||
const api = useApi(); | ||
const gifRef = useRef(null); | ||
const gifBlobRef = useRef(null); | ||
const videoRef = useRef(null); | ||
const [isLoaded, setIsLoaded] = useState(false); | ||
const [percent, setPercent] = useState(0); | ||
|
||
const handleConvertClick = async () => { | ||
await videoRef.current.play(); | ||
convertToGif(); | ||
}; | ||
|
||
const handlePlayClick = async () => { | ||
await videoRef.current.play(); | ||
}; | ||
|
||
useEffect(() => { | ||
const handleLoadedMetadata = () => { | ||
setIsLoaded(true); | ||
}; | ||
|
||
videoRef.current.addEventListener('loadedmetadata', handleLoadedMetadata); | ||
|
||
return () => { | ||
videoRef.current.removeEventListener('loadedmetadata', handleLoadedMetadata); | ||
}; | ||
}, []); | ||
|
||
const convertToGif = async () => { | ||
if (!isLoaded) { | ||
return; | ||
} | ||
|
||
const canvas = document.createElement('canvas'); | ||
canvas.width = videoRef.current.videoWidth; | ||
canvas.height = videoRef.current.videoHeight; | ||
const context = canvas.getContext('2d', { willReadFrequently: true }); | ||
const worker = new URL('./gif.js/gif.worker.js', import.meta.url) as any; | ||
// const delay = 1000 / videoRef.current.playbackRate; | ||
const delay = 100; | ||
|
||
const gif = new GIF({ | ||
workers: 4, | ||
quality: 10, | ||
workerScript: worker, | ||
}); | ||
|
||
gif.on('finished', (blob) => { | ||
gifBlobRef.current = blob; | ||
const gifUrl = URL.createObjectURL(blob); | ||
gifRef.current.src = gifUrl; | ||
}); | ||
|
||
gif.on('progress', (progress) => { | ||
setPercent(Math.round(progress * 100)); | ||
}); | ||
|
||
gif.addFrame(canvas, { copy: true, delay: delay }); | ||
|
||
const renderFrame = () => { | ||
context.drawImage(videoRef.current, 0, 0); | ||
gif.addFrame(canvas, { copy: true, delay: delay }); | ||
if (videoRef.current.currentTime < videoRef.current.duration) { | ||
setTimeout(renderFrame, delay); | ||
} else { | ||
gif.render(); | ||
} | ||
}; | ||
setTimeout(renderFrame, delay); | ||
}; | ||
|
||
async function handleSaveClick() { | ||
const blob = gifBlobRef.current; | ||
try { | ||
const formData = new FormData(); | ||
formData.append('type', 'gif'); | ||
formData.append('userId', user.id); | ||
formData.append('file', blob); | ||
const res = (await api.saveFile(formData)) as any; | ||
if (res.code == 0) { | ||
if (window.isElectron) { | ||
window.electronAPI?.sendEiCloseWin(); | ||
window.electronAPI?.sendViOpenWin({ imgUrl: res.data.filePath }); | ||
} else { | ||
Modal.confirm({ | ||
title: '图片已保存,是否查看?', | ||
content: `${res.data.filePath}`, | ||
okText: t('modal.ok'), | ||
cancelText: t('modal.cancel'), | ||
onOk() { | ||
window.open(`/viewImage.html?imgUrl=${res.data.filePath}`); | ||
console.log('OK'); | ||
}, | ||
onCancel() { | ||
console.log('Cancel'); | ||
}, | ||
}); | ||
} | ||
} | ||
} catch (err) { | ||
saveAs(blob, `pear-rec_${+new Date()}.gif`); | ||
} | ||
} | ||
|
||
return ( | ||
<div> | ||
<video className="videoRef" ref={videoRef} src={videoSrc}></video> | ||
<div className="tool"> | ||
<Progress percent={percent} /> | ||
<Button className="playBtn" onClick={handlePlayClick} type="primary" danger> | ||
播放 | ||
</Button> | ||
<Button className="convertBtn" onClick={handleConvertClick} type="primary"> | ||
保存 | ||
</Button> | ||
<Button className="saveBtn" onClick={handleSaveClick}> | ||
下载 | ||
</Button> | ||
</div> | ||
<img ref={gifRef} className="hide" alt="GIF" /> | ||
</div> | ||
); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
packages/web/src/components/videoToGif/gif.js/gif.worker.js.map
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<link rel="icon" type="image/x-icon" href="/imgs/logo/favicon.ico" /> | ||
<title>pear-rec | GIF</title> | ||
</head> | ||
|
||
<body> | ||
<div id="root"></div> | ||
<script type="module" src="./videoToGif/index.tsx"></script> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
.videoToGif { | ||
background: #fff; | ||
max-width: 1000px; | ||
:global { | ||
.videoRef { | ||
width: 100%; | ||
} | ||
.tool { | ||
text-align: center; | ||
.playBtn { | ||
margin-right: 10px; | ||
} | ||
.convertBtn { | ||
margin-right: 10px; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.