Skip to content

Let's use AI to Earn!

feat: 视频截帧适配web

gaoSira3ed3368c83c
diff --git a/src/app/[lng]/publish/components/Choose/ImgChoose.tsx b/src/app/[lng]/publish/components/Choose/ImgChoose.tsx
index d98f73e0..7dc73f75 100644
--- a/src/app/[lng]/publish/components/Choose/ImgChoose.tsx
+++ b/src/app/[lng]/publish/components/Choose/ImgChoose.tsx
@@ -7,8 +7,6 @@
*/
import { Button } from "antd";
import { FC } from "react";
-import { generateUUID, getFilePathName } from "@/utils";
-// import { icpGetFileStream } from "@/icp/view";
interface ImgChooseProps {
// 单选就使用单选方法,多选就使用单选方法
diff --git a/src/app/[lng]/publish/components/Choose/ImgChoose.utils.ts b/src/app/[lng]/publish/components/Choose/ImgChoose.util.ts
similarity index 100%
rename from src/app/[lng]/publish/components/Choose/ImgChoose.utils.ts
rename to src/app/[lng]/publish/components/Choose/ImgChoose.util.ts
diff --git a/src/app/[lng]/publish/components/Choose/VideoChoose.tsx b/src/app/[lng]/publish/components/Choose/VideoChoose.tsx
index db1db09b..63ea2548 100644
--- a/src/app/[lng]/publish/components/Choose/VideoChoose.tsx
+++ b/src/app/[lng]/publish/components/Choose/VideoChoose.tsx
@@ -9,8 +9,7 @@ import { Button, message, Upload } from "antd";
import { FC, useRef } from "react";
import { IImgFile } from "@/app/[lng]/publish/components/Choose/ImgChoose";
import { RcFile } from "antd/es/upload";
-import { formatImg } from "@/app/[lng]/publish/components/Choose/ImgChoose.utils";
-// import { saveCropperImage } from "@/views/publish/children/videoPage/components/VideoCoverSeting";
+import { VideoGrabFrame } from "@/app/[lng]/publish/components/Choose/videoChoose.util";
interface VideoChooseProps {
// 单选就使用单选方法,多选就使用单选方法
@@ -43,10 +42,7 @@ export interface IVideoFile {
cover: IImgFile;
}
-function getVideoInfo(
- videoUrl: string,
- fileName: string,
-): Promise<{
+async function getVideoInfo(videoUrl: string): Promise<{
width: number;
height: number;
// 下取整的时长
@@ -54,50 +50,7 @@ function getVideoInfo(
// 视频首帧
cover: IImgFile;
}> {
- return new Promise((resolve) => {
- // 创建一个临时的 video 元素
- const video = document.createElement("video");
-
- // 设置 video 元素的 src 属性
- video.src = videoUrl;
-
- // 当视频元数据加载完毕时执行回调
- video.addEventListener("loadedmetadata", () => {
- video.currentTime = 0;
- });
-
- video.addEventListener("seeked", function () {
- // 获取视频的宽度和高度
- const width = video.videoWidth;
- const height = video.videoHeight;
- // 获取视频的时长
- const duration = video.duration;
-
- // 获取视频首帧
- const canvas = document.createElement("canvas");
- canvas.width = width;
- canvas.height = height;
- const context = canvas.getContext("2d")!;
- context.fillStyle = "white";
- context.fillRect(0, 0, width, height);
- context.drawImage(video, 0, 0);
- canvas.toBlob(async (blob) => {
- const cover = await formatImg({
- blob: blob!,
- path: fileName,
- });
- resolve({
- width,
- height,
- duration: Math.floor(duration),
- cover,
- });
- video.remove();
- });
- });
- // 加载视频
- video.load();
- });
+ return await VideoGrabFrame(videoUrl, 0);
}
// 根据视频的Uint8Array和路径获取文件
@@ -122,8 +75,7 @@ function getVideoInfo(
const formatVideo = async (file: RcFile): Promise<IVideoFile> => {
const videoUrl = URL.createObjectURL(file);
-
- const videoInfo = await getVideoInfo(URL.createObjectURL(file), file.name);
+ const videoInfo = await VideoGrabFrame(videoUrl, 0);
return {
filename: file.name,
diff --git a/src/app/[lng]/publish/components/Choose/videoChoose.util.ts b/src/app/[lng]/publish/components/Choose/videoChoose.util.ts
new file mode 100644
index 00000000..ee69333f
--- /dev/null
+++ b/src/app/[lng]/publish/components/Choose/videoChoose.util.ts
@@ -0,0 +1,58 @@
+// 视频截帧
+import { formatImg } from "@/app/[lng]/publish/components/Choose/ImgChoose.util";
+import { IImgFile } from "@/app/[lng]/publish/components/Choose/ImgChoose";
+
+export function VideoGrabFrame(
+ videoUrl: string,
+ currentTime: number,
+): Promise<{
+ width: number;
+ height: number;
+ // 下取整的时长
+ duration: number;
+ // 视频首帧
+ cover: IImgFile;
+}> {
+ return new Promise((resolve) => {
+ const video = document.createElement("video");
+ video.src = videoUrl;
+
+ // 当视频元数据加载完毕时执行回调
+ video.addEventListener("loadedmetadata", () => {
+ video.currentTime = currentTime;
+ });
+
+ video.addEventListener("seeked", function () {
+ // 获取视频的宽度和高度
+ const width = video.videoWidth;
+ const height = video.videoHeight;
+ // 获取视频的时长
+ const duration = video.duration;
+
+ // 获取视频首帧
+ const canvas = document.createElement("canvas");
+ canvas.width = width;
+ canvas.height = height;
+ const context = canvas.getContext("2d")!;
+ context.fillStyle = "white";
+ context.fillRect(0, 0, width, height);
+ context.drawImage(video, 0, 0);
+ canvas.toBlob(async (blob) => {
+ const cover = await formatImg({
+ blob: blob!,
+ path: `cover.${blob!.type.split("/")[1]}`,
+ });
+ resolve({
+ width,
+ height,
+ duration: Math.floor(duration),
+ cover,
+ });
+ video.remove();
+ });
+ });
+
+ // 加载视频
+ video.load();
+ });
+}
diff --git a/src/app/[lng]/publish/videoPage/components/VideoCoverSeting.tsx b/src/app/[lng]/publish/videoPage/components/VideoCoverSeting.tsx
index 4c829157..76974c48 100644
--- a/src/app/[lng]/publish/videoPage/components/VideoCoverSeting.tsx
+++ b/src/app/[lng]/publish/videoPage/components/VideoCoverSeting.tsx
@@ -15,7 +15,8 @@ import "cropperjs/dist/cropper.css";
import { useVideoPageStore } from "@/app/[lng]/publish/videoPage/useVideoPageStore";
import { useShallow } from "zustand/react/shallow";
import { CloseCircleOutlined } from "@ant-design/icons";
-import { formatImg } from "@/app/[lng]/publish/components/Choose/ImgChoose.utils";
+import { formatImg } from "@/app/[lng]/publish/components/Choose/ImgChoose.util";
+import { VideoGrabFrame } from "@/app/[lng]/publish/components/Choose/videoChoose.util";
export interface IVideoCoverSetingRef {}
@@ -32,26 +33,6 @@ export interface IVideoCoverSetingProps {
onClose?: () => void;
}
-// 保存裁剪的图片
-const saveCropperImage = (filename: string, file: Blob): Promise<string> => {
- return new Promise((resolve) => {
- const reader = new FileReader();
- reader.onload = async function (event) {
- const arrayBuffer = event.target!.result as ArrayBuffer;
- const uint8Array = new Uint8Array(arrayBuffer);
-
- // TODO 报错裁剪的图片
- // const imgPath = await icpSaveFile({
- // file: uint8Array,
- // saveDir: "/images/cropper",
- // filename: filename,
- // });
- // resolve(imgPath);
- };
- reader.readAsArrayBuffer(file);
- });
-};
-
// 视频封面设置
const VideoCoverSeting = memo(
forwardRef(
@@ -93,17 +74,10 @@ const VideoCoverSeting = memo(
/* 获取封面 */
const getVideoCover = async (n: number) => {
- // TODO 读取文件
- // if (!videoFile!.videoPath) return;
- // setSliderVal(n);
- // setVideoCoverLoading(true);
- // const pathVideo = await ipcGetVideoCover(
- // videoFile!.videoPath!,
- // formatSeconds(n),
- // );
- // const imgFile = await getImgFile(pathVideo);
- // setVideoCoverLoading(false);
- // setImgFile(imgFile);
+ setVideoCoverLoading(true);
+ const videoInfo = await VideoGrabFrame(videoFile!.videoUrl, n);
+ setImgFile(videoInfo.cover);
+ setVideoCoverLoading(false);
};
const close = () => {
@@ -171,13 +145,9 @@ const VideoCoverSeting = memo(
onOk={async () => {
const canvas = cropper.current!.getCroppedCanvas();
canvas.toBlob(async function (blob) {
- const imgPath = await saveCropperImage(
- `${operateId}_${saveImgId}.${imgFile?.file.type.split("/")[1]}`,
- blob!,
- );
const cover = await formatImg({
blob: blob!,
- path: imgPath,
+ path: `${operateId}_${saveImgId}.${imgFile?.file.type.split("/")[1]}`,
});
onChoosed(cover);
close();