import React, {useEffect, useRef, useState} from 'react';
import {cancelJob, fetchVideoDownloadDefaults, getJob, transcodeVideo} from '../api';
import {useWROLMode} from '../hooks/customHooks';
import {Button, Group, Icon, Menu, Modal, Progress, Select, Stack, Status, Text, toast} from './ui';
import {
TRANSCODE_COPY,
transcodeAudioCodecOptions,
transcodeContainerOptions,
transcodeVideoCodecOptions,
} from './Vars';
// How often the modal asks the API about the running Job.
const JOB_POLL_MS = 2000;
const FINISHED_STATUSES = ['complete', 'failed', 'cancelled'];
// Embedded thumbnails are "video" streams to ffprobe, but not codecs a user chooses.
const THUMBNAIL_CODECS = ['mjpeg', 'png', 'bmp', 'gif'];
/** The codec names of a Video's real streams, from the `codec_names`/`codec_types` the API sends. */
export function currentCodecs(video) {
const names = video?.codec_names || [];
const types = video?.codec_types || [];
const video_ = names.filter((name, i) => types[i] === 'video' && !THUMBNAIL_CODECS.includes(name));
const audio = names.filter((name, i) => types[i] === 'audio');
return {video: video_, audio};
}
/**
* The transcode target implied by the user's ordered codec preferences: the first preferred codec
* that ffmpeg can produce (the same rule the downloader uses). `copy` when none can be.
*/
export function preferredTarget(preferences, options) {
const producible = options.map(i => i.value).filter(i => i !== TRANSCODE_COPY);
for (const codec of preferences || []) {
if (producible.includes(codec)) {
return codec;
}
}
return TRANSCODE_COPY;
}
const statusKind = (status) => {
if (status === 'complete') return 'complete';
if (status === 'running') return 'active';
if (status === 'failed' || status === 'cancelled') return 'failed';
return 'pending';
}
/** Progress, status and log tail of one Job, polled until it finishes. */
export function JobProgress({jobId, onFinished}) {
const [job, setJob] = useState(null);
const [cancelling, setCancelling] = useState(false);
const timer = useRef(null);
const finishedRef = useRef(false);
useEffect(() => {
finishedRef.current = false;
let cancelled = false;
const poll = async () => {
let latest;
try {
latest = await getJob(jobId);
} catch (e) {
console.error(e);
return;
}
if (cancelled) return;
setJob(latest);
if (FINISHED_STATUSES.includes(latest.status)) {
if (!finishedRef.current) {
finishedRef.current = true;
if (onFinished) onFinished(latest);
}
return;
}
timer.current = window.setTimeout(poll, JOB_POLL_MS);
};
poll();
return () => {
cancelled = true;
clearTimeout(timer.current);
};
// Only a new Job restarts polling; `onFinished` may be a fresh closure on every render.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [jobId]);
const handleCancel = async () => {
setCancelling(true);
try {
const latest = await cancelJob(jobId);
setJob(latest);
} catch (e) {
console.error(e);
} finally {
setCancelling(false);
}
};
if (!job) {
return ;
}
const finished = FINISHED_STATUSES.includes(job.status);
const percent = job.progress ?? 0;
const logTail = (job.log || []).slice(-8);
return {logTail.join('\n')}}