1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
/* eslint-disable camelcase */
import { FunctionComponent } from "react";
import { Alert, Button, Checkbox, Divider, Stack, Text } from "@mantine/core";
import { useForm } from "@mantine/form";
import { faInfoCircle } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import {
useRefTracksByEpisodeId,
useRefTracksByMovieId,
useSubtitleAction,
} from "@/apis/hooks";
import {
GroupedSelector,
GroupedSelectorOptions,
Selector,
} from "@/components/inputs";
import { useModals, withModal } from "@/modules/modals";
import { task } from "@/modules/task";
import { syncMaxOffsetSecondsOptions } from "@/pages/Settings/Subtitles/options";
import { fromPython, toPython } from "@/utilities";
const TaskName = "Syncing Subtitle";
function useReferencedSubtitles(
mediaType: "episode" | "movie",
mediaId: number,
subtitlesPath: string,
) {
// We cannot call hooks conditionally, we rely on useQuery "enabled" option to do only the required API call
const episodeData = useRefTracksByEpisodeId(
subtitlesPath,
mediaId,
mediaType === "episode",
);
const movieData = useRefTracksByMovieId(
subtitlesPath,
mediaId,
mediaType === "movie",
);
const mediaData = mediaType === "episode" ? episodeData : movieData;
const subtitles: GroupedSelectorOptions<string>[] = [];
if (!mediaData.data) {
return [];
} else {
if (mediaData.data.audio_tracks.length > 0) {
const embeddedAudioGroup: GroupedSelectorOptions<string> = {
group: "Embedded audio tracks",
items: [],
};
subtitles.push(embeddedAudioGroup);
mediaData.data.audio_tracks.forEach((item) => {
embeddedAudioGroup.items.push({
value: item.stream,
label: `${item.name || item.language} (${item.stream})`,
});
});
}
if (mediaData.data.embedded_subtitles_tracks.length > 0) {
const embeddedSubtitlesTrackGroup: GroupedSelectorOptions<string> = {
group: "Embedded subtitles tracks",
items: [],
};
subtitles.push(embeddedSubtitlesTrackGroup);
mediaData.data.embedded_subtitles_tracks.forEach((item) => {
embeddedSubtitlesTrackGroup.items.push({
value: item.stream,
label: `${item.name || item.language} (${item.stream})`,
});
});
}
if (mediaData.data.external_subtitles_tracks.length > 0) {
const externalSubtitlesFilesGroup: GroupedSelectorOptions<string> = {
group: "External Subtitles files",
items: [],
};
subtitles.push(externalSubtitlesFilesGroup);
mediaData.data.external_subtitles_tracks.forEach((item) => {
if (item) {
externalSubtitlesFilesGroup.items.push({
value: item.path,
label: item.name,
});
}
});
}
return subtitles;
}
}
interface Props {
selections: FormType.ModifySubtitle[];
onSubmit?: VoidFunction;
}
interface FormValues {
reference?: string;
maxOffsetSeconds?: string;
noFixFramerate: boolean;
gss: boolean;
hi?: boolean;
forced?: boolean;
}
const SyncSubtitleForm: FunctionComponent<Props> = ({
selections,
onSubmit,
}) => {
if (selections.length === 0) {
throw new Error("You need to select at least 1 media to sync");
}
const { mutateAsync } = useSubtitleAction();
const modals = useModals();
const subtitle = selections[0];
const mediaType = subtitle.type;
const mediaId = subtitle.id;
const subtitlesPath = subtitle.path;
const subtitles = useReferencedSubtitles(mediaType, mediaId, subtitlesPath);
const form = useForm<FormValues>({
initialValues: {
noFixFramerate: false,
gss: false,
hi: fromPython(subtitle.hi),
forced: fromPython(subtitle.forced),
},
});
return (
<form
onSubmit={form.onSubmit((parameters) => {
selections.forEach((s) => {
const form: FormType.ModifySubtitle = {
...s,
reference: parameters.reference,
max_offset_seconds: parameters.maxOffsetSeconds,
no_fix_framerate: toPython(parameters.noFixFramerate),
gss: toPython(parameters.gss),
};
task.create(s.path, TaskName, mutateAsync, { action: "sync", form });
});
onSubmit?.();
modals.closeSelf();
})}
>
<Stack>
<Alert
title="Subtitles"
color="gray"
icon={<FontAwesomeIcon icon={faInfoCircle}></FontAwesomeIcon>}
>
<Text size="sm">{selections.length} subtitles selected</Text>
</Alert>
<GroupedSelector
clearable
disabled={subtitles.length === 0 || selections.length !== 1}
label="Reference"
placeholder="Default: choose automatically within video file"
options={subtitles}
{...form.getInputProps("reference")}
></GroupedSelector>
<Selector
clearable
label="Max Offset Seconds"
options={syncMaxOffsetSecondsOptions}
placeholder="Select..."
{...form.getInputProps("maxOffsetSeconds")}
></Selector>
<Checkbox
label="No Fix Framerate"
{...form.getInputProps("noFixFramerate")}
></Checkbox>
<Checkbox
label="Golden-Section Search"
{...form.getInputProps("gss")}
></Checkbox>
<Divider></Divider>
<Button type="submit">Sync</Button>
</Stack>
</form>
);
};
export const SyncSubtitleModal = withModal(SyncSubtitleForm, "sync-subtitle", {
title: "Sync Subtitle Options",
size: "lg",
});
export default SyncSubtitleForm;
|