import { v4 as uuidv4 } from "uuid";
export const chooseVideo = async ({ count = 1 }) => {
const videoId = "chooseVideo";
let chooseVideoId = document.getElementById(videoId);
if (!chooseVideoId) {
const obj = document.createElement("input");
obj.setAttribute("type", "file");
obj.setAttribute("id", videoId);
if (count > 1) {
obj.setAttribute("multiple", "multiple");
}
obj.setAttribute("accept", "video/*");
obj.setAttribute(
"style",
"position: fixed; top: -4000px; left: -3000px; z-index: -300;"
);
document.body.appendChild(obj);
chooseVideoId = document.getElementById(videoId);
} else if (count > 1) {
chooseVideoId.setAttribute("multiple", "multiple");
} else {
chooseVideoId.removeAttribute("multiple");
}
const event = new MouseEvent("click");
const files = await new Promise((resolve) => {
chooseVideoId.onchange = (e) => {
resolve([...e.target.files]);
};
chooseVideoId.dispatchEvent(event);
});
const list = files.slice(0, count).map((file) => {
const blob = new Blob([file], { type: "video/mp4" });
const url = URL.createObjectURL(blob);
return { path: url, size: file.size, type: file.type, file, id: uuidv4() };
});
return list;
};