Skip to content

Commit 0315e16

Browse files
authored
Add support for multiple files upload
2 parents 8b21c85 + bce8b2b commit 0315e16

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed

src/FilesEditor.tsx

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ import { Section } from "./components/Section";
55
import { computeChunks, FPS } from "./lib";
66
import { Settings, Vid } from "./types";
77

8+
const initialProgress = {
9+
processed: 0,
10+
total: 0
11+
};
12+
813
export const FilesEditor = ({
914
vids,
1015
setVids,
@@ -25,6 +30,7 @@ export const FilesEditor = ({
2530
setPreprocessSettings: Dispatch<SetStateAction<Settings>>;
2631
}) => {
2732
const [loading, setLoading] = useState(false);
33+
const [filesProgress, setFilesProgress] = useState(initialProgress);
2834

2935
return (
3036
<Section name="Files">
@@ -45,29 +51,36 @@ export const FilesEditor = ({
4551
<input
4652
type="file"
4753
accept="video/*,image/*"
54+
multiple
4855
onChange={async (evt) => {
4956
setLoading(true);
50-
const file = evt.target.files![0];
51-
const src = URL.createObjectURL(file);
52-
const withoutSpaces = file.name.replace(/\s/g, "_");
53-
let name = withoutSpaces;
54-
let i = 0;
55-
while (vids.map((vid) => vid.name).includes(name)) {
56-
name = `${withoutSpaces}_${i}`;
57-
i++;
57+
if (evt.target.files) {
58+
setFilesProgress({ processed: 0, total: evt.target.files.length });
59+
for (const file of evt.target.files) {
60+
const src = URL.createObjectURL(file);
61+
const withoutSpaces = file.name.replace(/\s/g, "_");
62+
let name = withoutSpaces;
63+
let i = 0;
64+
while (vids.map((vid) => vid.name).includes(name)) {
65+
name = `${withoutSpaces}_${i}`;
66+
i++;
67+
}
68+
const chunks = await computeChunks(
69+
ffmpeg,
70+
file,
71+
name,
72+
settings.width,
73+
settings.height,
74+
onConfig
75+
);
76+
setVids(prevVids => [...prevVids, { file, name, chunks, src }]);
77+
setFilesProgress(prev => ({ ...prev, processed: prev.processed + 1 }));
78+
}
5879
}
59-
const chunks = await computeChunks(
60-
ffmpeg,
61-
file,
62-
name,
63-
settings.width,
64-
settings.height,
65-
onConfig
66-
);
67-
setVids([...vids, { file, name, chunks, src }]);
6880
evt.target.value = "";
6981
setLoading(false);
7082
setPreprocessSettings(settings);
83+
setFilesProgress(initialProgress);
7184
}}
7285
disabled={loading}
7386
/>
@@ -79,6 +92,7 @@ export const FilesEditor = ({
7992
disabled={loading}
8093
onClick={async () => {
8194
setLoading(true);
95+
setFilesProgress({ processed: 0, total: vids.length });
8296
for (const vid of vids) {
8397
vid.chunks = await computeChunks(
8498
ffmpeg,
@@ -88,10 +102,12 @@ export const FilesEditor = ({
88102
settings.height,
89103
onConfig
90104
);
105+
setFilesProgress(prev => ({ ...prev, processed: prev.processed + 1 }));
91106
}
92107
setVids([...vids]);
93108
setPreprocessSettings(settings);
94109
setLoading(false);
110+
setFilesProgress(initialProgress);
95111
}}
96112
>
97113
Reprocess files
@@ -100,6 +116,8 @@ export const FilesEditor = ({
100116
)}
101117
{loading && (
102118
<p>
119+
<span>Processed {filesProgress.processed} of {filesProgress.total} files</span>
120+
<br />
103121
<progress value={progress} />
104122
</p>
105123
)}

0 commit comments

Comments
 (0)