feat(subs): translate from an image-only release
A release whose only subtitle is a PGS or VobSub track was stuck both ways: the track satisfied English so no English SRT was ever fetched, and bitmaps can never feed a translator. §15 is amended to separate satisfying viewing from providing a translation source, and to carve a fetch made to obtain a source out of the no-upgrade rule. Timings come from the disc rather than from alass guessing at the audio. At import, each non-forced image track's packet timestamps are paired show-to-clear into a cue skeleton and stored; the fetched source is then aligned against that skeleton, and the translation made from it skips the post-translation pass, which could only move disc-exact timings off. Pairing is validated before it is trusted — even packet count, plausible durations, sane density for the runtime — because PGS allows several composition segments per subtitle and a slipped pairing is quietly half a second out. A track that fails validation gets no skeleton and falls back to aligning against the video, as does any file imported before this: there is no backfill. Closes #268 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
015b90a458
commit
6cd2902341
@@ -23,7 +23,7 @@ use arr_core::policy::{evaluate, Candidate};
|
||||
use arr_core::{ProbedMedia, Rule, Source, Verdict};
|
||||
use arr_db::Db;
|
||||
use arr_dl::QbitClient;
|
||||
use arr_probe::Prober;
|
||||
use arr_probe::{Prober, Skeletons};
|
||||
|
||||
use crate::notify::Notifier;
|
||||
use crate::reconcile::{Action, ActionFuture, Outcome};
|
||||
@@ -75,6 +75,11 @@ enum ProbeOutcome {
|
||||
pub struct ImportAction {
|
||||
qbit: QbitClient,
|
||||
prober: Prober,
|
||||
/// §15, #268. Reads the packet timings of image-format subtitle tracks
|
||||
/// into a cue skeleton. Here rather than in the subtitle lane because
|
||||
/// deriving one costs a full demux, and import is where the file is
|
||||
/// being read and hardlinked anyway.
|
||||
skeletons: Skeletons,
|
||||
jellyfin: JellyfinClient,
|
||||
notifier: Notifier,
|
||||
/// The operator's ntfy topic (DESIGN.md §9.5), for the *broken*
|
||||
@@ -121,6 +126,7 @@ impl ImportAction {
|
||||
Self {
|
||||
qbit,
|
||||
prober,
|
||||
skeletons: Skeletons::new(),
|
||||
jellyfin,
|
||||
notifier,
|
||||
operator_topic,
|
||||
@@ -176,6 +182,94 @@ impl ImportAction {
|
||||
Ok(files)
|
||||
}
|
||||
|
||||
/// Derive the cue skeleton of every non-forced image-format subtitle
|
||||
/// track in a file that has just been placed (§15, #268).
|
||||
///
|
||||
/// Detached, like the probes above: one derivation is a full demux and
|
||||
/// the reconcile lane's tick budget is 25 s. Nothing downstream waits on
|
||||
/// it — a skeleton that never lands, because the process died or because
|
||||
/// the packets did not pair, reads exactly like a track that has none,
|
||||
/// and the subtitle lane aligns against the video instead.
|
||||
async fn spawn_skeletons(&self, database: &Db, placed: &Path, feature: &arr_probe::ProbedFile) {
|
||||
let tracks: Vec<(usize, String)> = feature
|
||||
.media
|
||||
.subtitle_tracks
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, track)| !track.forced && track.codec.is_image())
|
||||
.map(|(index, track)| (index, track.language.to_string()))
|
||||
.collect();
|
||||
if tracks.is_empty() {
|
||||
return;
|
||||
}
|
||||
|
||||
let path_text = placed.to_string_lossy().into_owned();
|
||||
let media_file_id = match sqlx::query_scalar!(
|
||||
r#"SELECT id AS "id!: i64" FROM media_files WHERE path = ?"#,
|
||||
path_text
|
||||
)
|
||||
.fetch_optional(database.pool())
|
||||
.await
|
||||
{
|
||||
Ok(Some(id)) => id,
|
||||
Ok(None) => return,
|
||||
Err(error) => {
|
||||
tracing::warn!(path = %placed.display(), %error, "no media file row to hang a cue skeleton on");
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let skeletons = self.skeletons.clone();
|
||||
let database = database.clone();
|
||||
let video = placed.to_path_buf();
|
||||
let runtime = feature.duration;
|
||||
tokio::spawn(async move {
|
||||
for (stream_index, language) in tracks {
|
||||
let outcome = skeletons.derive(&video, stream_index, Some(runtime)).await;
|
||||
let index = i64::try_from(stream_index).unwrap_or(i64::MAX);
|
||||
match outcome {
|
||||
Ok(arr_probe::SkeletonOutcome::Derived(skeleton)) => {
|
||||
let cues: Vec<(i64, i64)> = skeleton
|
||||
.cues
|
||||
.iter()
|
||||
.map(|cue| (millis(cue.start), millis(cue.end)))
|
||||
.collect();
|
||||
if let Err(error) = arr_db::skeletons::record(
|
||||
database.pool(),
|
||||
media_file_id,
|
||||
index,
|
||||
&language,
|
||||
&cues,
|
||||
)
|
||||
.await
|
||||
{
|
||||
tracing::warn!(%error, media_file_id, stream_index, "cue skeleton not recorded");
|
||||
} else {
|
||||
tracing::info!(
|
||||
media_file_id,
|
||||
stream_index,
|
||||
cues = cues.len(),
|
||||
"cue skeleton derived"
|
||||
);
|
||||
}
|
||||
}
|
||||
Ok(arr_probe::SkeletonOutcome::Implausible(reason)) => tracing::info!(
|
||||
media_file_id,
|
||||
stream_index,
|
||||
%reason,
|
||||
"cue skeleton refused, alignment falls back to the video"
|
||||
),
|
||||
Err(error) => tracing::warn!(
|
||||
media_file_id,
|
||||
stream_index,
|
||||
%error,
|
||||
"cue skeleton could not be read"
|
||||
),
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// Drop a settled grab's probe results — imported or blacklisted, they
|
||||
/// will not be needed again.
|
||||
async fn forget_probes(&self, paths: &[PathBuf]) {
|
||||
@@ -345,6 +439,7 @@ impl ImportAction {
|
||||
tokio::task::spawn_blocking(move || place(&source_path, &link_target)).await??;
|
||||
|
||||
record_import(database, pending, &feature, waiver.as_ref(), &destination).await?;
|
||||
self.spawn_skeletons(database, &destination, &feature).await;
|
||||
self.forget_probes(&paths).await;
|
||||
self.refresh_jellyfin().await;
|
||||
let path_text = destination.to_string_lossy().into_owned();
|
||||
@@ -591,6 +686,7 @@ impl ImportAction {
|
||||
|
||||
record_episode_import(database, episode.id, feature, waiver.as_ref(), &destination)
|
||||
.await?;
|
||||
self.spawn_skeletons(database, &destination, feature).await;
|
||||
imported += 1;
|
||||
tracing::info!(
|
||||
grab_id = pending.grab_id,
|
||||
@@ -1200,6 +1296,11 @@ async fn record_episode_import(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Milliseconds, saturating. A cue past 292 million years does not exist.
|
||||
fn millis(value: std::time::Duration) -> i64 {
|
||||
i64::try_from(value.as_millis()).unwrap_or(i64::MAX)
|
||||
}
|
||||
|
||||
/// The `probed` column (§4, §5.6): what `ffprobe` found, in the spellings the
|
||||
/// policy columns use.
|
||||
fn probed_json(media: &ProbedMedia) -> serde_json::Value {
|
||||
|
||||
@@ -151,6 +151,14 @@ impl EmbeddedTrack {
|
||||
fn is_text(&self) -> bool {
|
||||
matches!(self.codec.as_deref(), Some("subrip" | "ass" | "mov_text"))
|
||||
}
|
||||
|
||||
/// §15's bitmap tracks. They feed no translator, but their timings are
|
||||
/// exact for the release, which is what a cue skeleton is made of (#268).
|
||||
fn is_image(&self) -> bool {
|
||||
self.codec
|
||||
.as_deref()
|
||||
.is_some_and(|codec| arr_core::SubtitleCodec::from_probe_name(codec).is_image())
|
||||
}
|
||||
}
|
||||
|
||||
fn embedded_tracks(probed: &str) -> Vec<EmbeddedTrack> {
|
||||
@@ -403,6 +411,73 @@ struct Worker {
|
||||
in_flight: Arc<Mutex<BTreeSet<(i64, String)>>>,
|
||||
}
|
||||
|
||||
/// A finished translation on its way to disk.
|
||||
struct Translation<'a> {
|
||||
engine: String,
|
||||
cues: &'a [arr_subs::Cue],
|
||||
/// Its source came off a cue skeleton, so §15's post-translation `alass`
|
||||
/// pass is skipped (#268).
|
||||
skeleton_aligned: bool,
|
||||
}
|
||||
|
||||
/// A subtitle to translate from (§15).
|
||||
struct Source {
|
||||
path: String,
|
||||
language: Language,
|
||||
/// Its timings came off a cue skeleton, so they are exact for this
|
||||
/// release and no further `alass` pass should touch them (#268).
|
||||
skeleton_aligned: bool,
|
||||
}
|
||||
|
||||
/// Whether a translation source could be had, and at whose expense.
|
||||
///
|
||||
/// Distinct from `Option` because looking for one may now spend a provider
|
||||
/// download (#268), so "none" and "not today" are different answers and land
|
||||
/// the gap in different states.
|
||||
enum SourceOutcome {
|
||||
Found(Source),
|
||||
/// Nothing on this file can feed a translator.
|
||||
None,
|
||||
/// A source exists but the provider is at its daily allowance.
|
||||
Capped,
|
||||
/// The fetch was tried and broke.
|
||||
Failed(String),
|
||||
}
|
||||
|
||||
impl SourceOutcome {
|
||||
/// The source, or the state and sentence the attempt row records instead.
|
||||
fn settled(self, wanted_tag: &str) -> Result<Source, (SubtitleState, String)> {
|
||||
match self {
|
||||
Self::Found(source) => Ok(source),
|
||||
Self::None => Err((
|
||||
SubtitleState::Unavailable,
|
||||
format!(
|
||||
"no provider has {wanted_tag} and there is no text source to translate from"
|
||||
),
|
||||
)),
|
||||
Self::Capped => Err((
|
||||
SubtitleState::Capped,
|
||||
"provider daily budget exhausted fetching a translation source".to_owned(),
|
||||
)),
|
||||
Self::Failed(reason) => Err((SubtitleState::Failed, reason)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Read a translation source off disk. The error is the sentence the attempt
|
||||
/// row records.
|
||||
async fn read_cues(path: &str) -> Result<Vec<arr_subs::Cue>, String> {
|
||||
let raw = tokio::fs::read_to_string(path)
|
||||
.await
|
||||
.map_err(|error| format!("{path}: {error}"))?;
|
||||
arr_subs::srt::parse(&raw).map_err(|error| format!("{path}: not SRT: {error}"))
|
||||
}
|
||||
|
||||
/// Milliseconds as stored in a cue skeleton.
|
||||
fn duration_from_millis(value: i64) -> std::time::Duration {
|
||||
std::time::Duration::from_millis(u64::try_from(value).unwrap_or(0))
|
||||
}
|
||||
|
||||
/// What one close settled to, for the log.
|
||||
enum Closed {
|
||||
Fetched {
|
||||
@@ -756,17 +831,19 @@ impl Worker {
|
||||
Ok(Closed::Recorded { state, reason })
|
||||
};
|
||||
|
||||
let Some((source_path, source_language)) =
|
||||
self.translation_source(database, target).await?
|
||||
else {
|
||||
return record(
|
||||
SubtitleState::Unavailable,
|
||||
format!(
|
||||
"no provider has {wanted_tag} and there is no text source to translate from"
|
||||
),
|
||||
)
|
||||
.await;
|
||||
let source = match self
|
||||
.translation_source(database, target, wanted, settings)
|
||||
.await?
|
||||
.settled(wanted_tag)
|
||||
{
|
||||
Ok(source) => source,
|
||||
Err((state, reason)) => return record(state, reason).await,
|
||||
};
|
||||
let Source {
|
||||
path: source_path,
|
||||
language: source_language,
|
||||
skeleton_aligned,
|
||||
} = source;
|
||||
|
||||
let Some(engine) = settings.translation_engine.clone() else {
|
||||
return record(
|
||||
@@ -788,41 +865,14 @@ impl Worker {
|
||||
.await;
|
||||
};
|
||||
|
||||
let raw = match tokio::fs::read_to_string(&source_path).await {
|
||||
Ok(raw) => raw,
|
||||
Err(error) => {
|
||||
return record(SubtitleState::Failed, format!("{source_path}: {error}")).await
|
||||
}
|
||||
};
|
||||
let cues = match arr_subs::srt::parse(&raw) {
|
||||
let cues = match read_cues(&source_path).await {
|
||||
Ok(cues) => cues,
|
||||
Err(error) => {
|
||||
return record(
|
||||
SubtitleState::Failed,
|
||||
format!("{source_path}: not SRT: {error}"),
|
||||
)
|
||||
.await
|
||||
}
|
||||
Err(reason) => return record(SubtitleState::Failed, reason).await,
|
||||
};
|
||||
|
||||
// Translators bill per character, not per call (§15), and the text
|
||||
// going out is known before any of it is sent — no need to ask the
|
||||
// backend afterwards (#197).
|
||||
let characters = i64::try_from(
|
||||
cues.iter()
|
||||
.map(|cue| cue.text.chars().count())
|
||||
.sum::<usize>(),
|
||||
)
|
||||
.unwrap_or(i64::MAX);
|
||||
let allowance = settings.translator_allowance(&engine);
|
||||
if !budget::try_spend(
|
||||
database.pool(),
|
||||
BudgetKind::Translator,
|
||||
&engine,
|
||||
characters,
|
||||
allowance,
|
||||
)
|
||||
.await?
|
||||
if !self
|
||||
.claim_translator_budget(database, &engine, &cues, settings)
|
||||
.await?
|
||||
{
|
||||
return record(
|
||||
SubtitleState::Capped,
|
||||
@@ -846,8 +896,45 @@ impl Worker {
|
||||
};
|
||||
self.clear_broken(&broken_name).await;
|
||||
|
||||
self.write_translation(database, target, wanted, wanted_tag, engine, &translated)
|
||||
.await
|
||||
self.write_translation(
|
||||
database,
|
||||
target,
|
||||
wanted,
|
||||
wanted_tag,
|
||||
Translation {
|
||||
engine,
|
||||
cues: &translated,
|
||||
skeleton_aligned,
|
||||
},
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
/// Claim the translator's daily character allowance for `cues` (§15,
|
||||
/// #197). Translators bill per character, not per call, and the text
|
||||
/// going out is known before any of it is sent — no need to ask the
|
||||
/// backend afterwards.
|
||||
async fn claim_translator_budget(
|
||||
&self,
|
||||
database: &Db,
|
||||
engine: &str,
|
||||
cues: &[arr_subs::Cue],
|
||||
settings: &Settings,
|
||||
) -> Result<bool, SubtitleError> {
|
||||
let characters = i64::try_from(
|
||||
cues.iter()
|
||||
.map(|cue| cue.text.chars().count())
|
||||
.sum::<usize>(),
|
||||
)
|
||||
.unwrap_or(i64::MAX);
|
||||
Ok(budget::try_spend(
|
||||
database.pool(),
|
||||
BudgetKind::Translator,
|
||||
engine,
|
||||
characters,
|
||||
settings.translator_allowance(engine),
|
||||
)
|
||||
.await?)
|
||||
}
|
||||
|
||||
/// Sync and write a finished translation, record it, settle the want.
|
||||
@@ -857,9 +944,13 @@ impl Worker {
|
||||
target: &Target,
|
||||
wanted: &Language,
|
||||
wanted_tag: &str,
|
||||
engine: String,
|
||||
translated: &[arr_subs::Cue],
|
||||
translation: Translation<'_>,
|
||||
) -> Result<Closed, SubtitleError> {
|
||||
let Translation {
|
||||
engine,
|
||||
cues: translated,
|
||||
skeleton_aligned,
|
||||
} = translation;
|
||||
let media_file_id = target.media_file_id;
|
||||
let record = |state: SubtitleState, reason: String| async move {
|
||||
db::record_attempt(
|
||||
@@ -884,37 +975,58 @@ impl Worker {
|
||||
return record(SubtitleState::Failed, error).await;
|
||||
}
|
||||
|
||||
let sync = self.syncer.settle(&target.path, &destination).await;
|
||||
// §15 as amended by #268: a source aligned against a cue skeleton
|
||||
// already carries the disc's own timings, and translation copies them
|
||||
// over unchanged. A second `alass` pass has nothing left to find and
|
||||
// can only move them off, so it is skipped.
|
||||
let sync = if skeleton_aligned {
|
||||
arr_subs::Settled {
|
||||
content: None,
|
||||
state: arr_subs::SyncState::NotRun,
|
||||
}
|
||||
} else {
|
||||
self.syncer.settle(&target.path, &destination).await
|
||||
};
|
||||
if let Some(synced) = &sync.content {
|
||||
if let Err(error) = write_sidecar(&destination, synced).await {
|
||||
return record(SubtitleState::Failed, error).await;
|
||||
}
|
||||
}
|
||||
|
||||
db::record_file(
|
||||
database.pool(),
|
||||
&arr_db::NewSubtitleFile::translated(
|
||||
media_file_id,
|
||||
wanted_tag,
|
||||
&engine,
|
||||
&destination.to_string_lossy(),
|
||||
)
|
||||
.sync(db_sync_state(sync.state)),
|
||||
let mut record_row = arr_db::NewSubtitleFile::translated(
|
||||
media_file_id,
|
||||
wanted_tag,
|
||||
&engine,
|
||||
&destination.to_string_lossy(),
|
||||
)
|
||||
.await?;
|
||||
.sync(db_sync_state(sync.state));
|
||||
if skeleton_aligned {
|
||||
record_row = record_row.skeleton_aligned();
|
||||
}
|
||||
db::record_file(database.pool(), &record_row).await?;
|
||||
db::mark_satisfied(database.pool(), media_file_id, wanted_tag).await?;
|
||||
self.refresh_jellyfin().await;
|
||||
Ok(Closed::Translated { engine })
|
||||
}
|
||||
|
||||
/// The subtitle to translate from: any non-forced sidecar arr wrote — a
|
||||
/// real one before a machine translation — or, failing that, a
|
||||
/// text-format embedded track extracted now (§15).
|
||||
/// real one before a machine translation — then a text-format embedded
|
||||
/// track extracted now, and failing both a text subtitle downloaded for
|
||||
/// the purpose (§15, #268).
|
||||
///
|
||||
/// That last step is the one §15 had to be amended for. A release whose
|
||||
/// only subtitle is an image track reads as *satisfied* in that language
|
||||
/// and so is never fetched again, while the track itself can never feed a
|
||||
/// translator — the file is stuck both ways. The carve-out is narrow: a
|
||||
/// fetch made to obtain a source, in a language the file already
|
||||
/// satisfies, which settles no want of its own.
|
||||
async fn translation_source(
|
||||
&self,
|
||||
database: &Db,
|
||||
target: &Target,
|
||||
) -> Result<Option<(String, Language)>, SubtitleError> {
|
||||
wanted: &Language,
|
||||
settings: &Settings,
|
||||
) -> Result<SourceOutcome, SubtitleError> {
|
||||
let files = db::files_for(database.pool(), target.media_file_id).await?;
|
||||
let sidecar = files
|
||||
.iter()
|
||||
@@ -922,7 +1034,11 @@ impl Worker {
|
||||
.min_by_key(|file| matches!(file.origin, SubtitleOrigin::Translated));
|
||||
if let Some(file) = sidecar {
|
||||
if let Some(path) = file.path.clone() {
|
||||
return Ok(Some((path, arr_db::policy::language(&file.language))));
|
||||
return Ok(SourceOutcome::Found(Source {
|
||||
path,
|
||||
language: arr_db::policy::language(&file.language),
|
||||
skeleton_aligned: file.skeleton_aligned,
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -934,42 +1050,209 @@ impl Worker {
|
||||
.await?
|
||||
.flatten();
|
||||
let Some(probed) = probed else {
|
||||
return Ok(None);
|
||||
return Ok(SourceOutcome::None);
|
||||
};
|
||||
let Some(track) = embedded_tracks(&probed)
|
||||
.into_iter()
|
||||
.find(|track| !track.forced && track.is_text())
|
||||
else {
|
||||
return Ok(None);
|
||||
let tracks = embedded_tracks(&probed);
|
||||
|
||||
if let Some(track) = tracks.iter().find(|track| !track.forced && track.is_text()) {
|
||||
let language = arr_db::policy::language(&track.language);
|
||||
let Some(destination) = target.sidecar(&language, false) else {
|
||||
return Ok(SourceOutcome::None);
|
||||
};
|
||||
if let Err(error) = self
|
||||
.extractor
|
||||
.extract_srt(&target.path, track.index, &destination)
|
||||
.await
|
||||
{
|
||||
tracing::warn!(
|
||||
path = %target.path.display(),
|
||||
stream = track.index,
|
||||
%error,
|
||||
"embedded track extraction failed"
|
||||
);
|
||||
return Ok(SourceOutcome::None);
|
||||
}
|
||||
let mut record = arr_db::NewSubtitleFile::extracted(
|
||||
target.media_file_id,
|
||||
&track.language,
|
||||
&destination.to_string_lossy(),
|
||||
);
|
||||
if track.sdh {
|
||||
record = record.sdh();
|
||||
}
|
||||
db::record_file(database.pool(), &record).await?;
|
||||
return Ok(SourceOutcome::Found(Source {
|
||||
path: destination.to_string_lossy().into_owned(),
|
||||
language,
|
||||
skeleton_aligned: false,
|
||||
}));
|
||||
}
|
||||
|
||||
self.fetch_source(database, target, wanted, &tracks, settings)
|
||||
.await
|
||||
}
|
||||
|
||||
/// Every candidate the enabled providers offer in `languages`.
|
||||
///
|
||||
/// Best-effort, unlike the search a gap opens with: this one runs after
|
||||
/// that one has already reported, so a provider that errors here is
|
||||
/// logged and skipped rather than backing the gap off a second time.
|
||||
async fn offered_in(
|
||||
&self,
|
||||
target: &Target,
|
||||
languages: &[Language],
|
||||
settings: &Settings,
|
||||
) -> Vec<Candidate> {
|
||||
let request = target.search_request(languages.to_vec());
|
||||
let mut offered: Vec<Candidate> = Vec::new();
|
||||
for provider in self
|
||||
.providers
|
||||
.iter()
|
||||
.filter(|provider| settings.providers_enabled.contains(provider.id().as_str()))
|
||||
{
|
||||
match provider.search(&request).await {
|
||||
Ok(candidates) => offered.extend(
|
||||
candidates
|
||||
.into_iter()
|
||||
.filter(|candidate| languages.contains(&candidate.language)),
|
||||
),
|
||||
Err(error) => tracing::debug!(
|
||||
%error,
|
||||
provider = %provider.id(),
|
||||
"no translation source from this provider"
|
||||
),
|
||||
}
|
||||
}
|
||||
offered
|
||||
}
|
||||
|
||||
/// Download a text subtitle to translate from, in a language an image
|
||||
/// track already satisfies (§15 as amended, #268).
|
||||
///
|
||||
/// The image track decides the language: a skeleton derived from it is
|
||||
/// the alignment reference, so the source has to be the subtitle that
|
||||
/// track is a bitmap rendering of. Where no skeleton was derived — the
|
||||
/// packets did not pair, or the file predates #268 — the fetch still
|
||||
/// happens and `alass` aligns against the video as it always has.
|
||||
async fn fetch_source(
|
||||
&self,
|
||||
database: &Db,
|
||||
target: &Target,
|
||||
wanted: &Language,
|
||||
tracks: &[EmbeddedTrack],
|
||||
settings: &Settings,
|
||||
) -> Result<SourceOutcome, SubtitleError> {
|
||||
let skeletons =
|
||||
arr_db::skeletons::for_media_file(database.pool(), target.media_file_id).await?;
|
||||
|
||||
// A forced track covers signs alone, and one whose language already
|
||||
// answers the want is the wrong direction to translate in.
|
||||
let wanted_tag = wanted.to_string();
|
||||
let Some(track) = tracks.iter().find(|track| {
|
||||
!track.forced && track.is_image() && !satisfies(&wanted_tag, &track.language)
|
||||
}) else {
|
||||
return Ok(SourceOutcome::None);
|
||||
};
|
||||
|
||||
let language = arr_db::policy::language(&track.language);
|
||||
let languages = vec![language.clone()];
|
||||
let Some(destination) = target.sidecar(&language, false) else {
|
||||
return Ok(None);
|
||||
return Ok(SourceOutcome::None);
|
||||
};
|
||||
if let Err(error) = self
|
||||
.extractor
|
||||
.extract_srt(&target.path, track.index, &destination)
|
||||
.await
|
||||
{
|
||||
tracing::warn!(
|
||||
path = %target.path.display(),
|
||||
stream = track.index,
|
||||
%error,
|
||||
"embedded track extraction failed"
|
||||
);
|
||||
return Ok(None);
|
||||
|
||||
let offered = self.offered_in(target, &languages, settings).await;
|
||||
let hash = moviehash(&target.path, target.size).await;
|
||||
let (winner, budget_capped) = self
|
||||
.claim_within_budget(
|
||||
database,
|
||||
target,
|
||||
hash.as_deref(),
|
||||
offered,
|
||||
&languages,
|
||||
settings,
|
||||
)
|
||||
.await?;
|
||||
let Some(winner) = winner else {
|
||||
return Ok(if budget_capped {
|
||||
SourceOutcome::Capped
|
||||
} else {
|
||||
SourceOutcome::None
|
||||
});
|
||||
};
|
||||
|
||||
let provider_name = winner.provider.to_string();
|
||||
let Some(provider) = self.provider(&provider_name) else {
|
||||
return Ok(SourceOutcome::Failed(format!(
|
||||
"provider {provider_name} vanished mid-close"
|
||||
)));
|
||||
};
|
||||
let fetched = match provider.download(&winner.id).await {
|
||||
Ok(fetched) => fetched,
|
||||
Err(error) => return Ok(SourceOutcome::Failed(error.to_string())),
|
||||
};
|
||||
let text = match fetched.to_srt() {
|
||||
Ok(text) => text,
|
||||
Err(error) => return Ok(SourceOutcome::Failed(error.to_string())),
|
||||
};
|
||||
if let Err(error) = write_sidecar(&destination, &text).await {
|
||||
return Ok(SourceOutcome::Failed(error));
|
||||
}
|
||||
let mut record = arr_db::NewSubtitleFile::extracted(
|
||||
|
||||
let skeleton = skeletons
|
||||
.iter()
|
||||
.find(|skeleton| usize::try_from(skeleton.stream_index) == Ok(track.index));
|
||||
let sync = match skeleton {
|
||||
Some(skeleton) => {
|
||||
let spans: Vec<(std::time::Duration, std::time::Duration)> = skeleton
|
||||
.cues
|
||||
.iter()
|
||||
.map(|(start, end)| (duration_from_millis(*start), duration_from_millis(*end)))
|
||||
.collect();
|
||||
self.syncer.settle_against_cues(&spans, &destination).await
|
||||
}
|
||||
None => self.syncer.settle(&target.path, &destination).await,
|
||||
};
|
||||
if let Some(synced) = &sync.content {
|
||||
if let Err(error) = write_sidecar(&destination, synced).await {
|
||||
return Ok(SourceOutcome::Failed(error));
|
||||
}
|
||||
}
|
||||
// Only an accepted alignment against the skeleton leaves the file on
|
||||
// disc-exact timings. A rejected or unrun one leaves the provider's
|
||||
// own, which the post-translation pass should still get a look at.
|
||||
let skeleton_aligned =
|
||||
skeleton.is_some() && matches!(sync.state, arr_subs::SyncState::Synced);
|
||||
|
||||
let mut record = arr_db::NewSubtitleFile::fetched(
|
||||
target.media_file_id,
|
||||
&track.language,
|
||||
&winner.language.to_string(),
|
||||
&provider_name,
|
||||
winner.id.as_str(),
|
||||
&destination.to_string_lossy(),
|
||||
);
|
||||
if track.sdh {
|
||||
)
|
||||
.sync(db_sync_state(sync.state));
|
||||
if winner.sdh {
|
||||
record = record.sdh();
|
||||
}
|
||||
if skeleton_aligned {
|
||||
record = record.skeleton_aligned();
|
||||
}
|
||||
db::record_file(database.pool(), &record).await?;
|
||||
Ok(Some((destination.to_string_lossy().into_owned(), language)))
|
||||
self.refresh_jellyfin().await;
|
||||
|
||||
tracing::info!(
|
||||
media_file_id = target.media_file_id,
|
||||
provider = %provider_name,
|
||||
language = %winner.language,
|
||||
skeleton_aligned,
|
||||
"translation source fetched for a language an image track satisfies"
|
||||
);
|
||||
|
||||
Ok(SourceOutcome::Found(Source {
|
||||
path: destination.to_string_lossy().into_owned(),
|
||||
language: winner.language,
|
||||
skeleton_aligned,
|
||||
}))
|
||||
}
|
||||
|
||||
/// The same single rescan import makes (§7.5): Jellyfin's watcher misses
|
||||
@@ -1439,10 +1722,228 @@ mod tests {
|
||||
.inline()
|
||||
}
|
||||
|
||||
fn action_with_syncer(
|
||||
providers: Vec<Arc<dyn Provider>>,
|
||||
backends: Vec<Arc<dyn Backend>>,
|
||||
syncer: Syncer,
|
||||
) -> SubtitleAction {
|
||||
SubtitleAction::new(
|
||||
providers,
|
||||
backends,
|
||||
syncer,
|
||||
Extractor::new().with_binary("ffmpeg-not-installed"),
|
||||
JellyfinClient::new("http://127.0.0.1:1", None).unwrap(),
|
||||
)
|
||||
.inline()
|
||||
}
|
||||
|
||||
fn no_tracks() -> serde_json::Value {
|
||||
serde_json::json!({ "sub_tracks": [] })
|
||||
}
|
||||
|
||||
/// One non-forced PGS track, the shape #268 was found on: it satisfies
|
||||
/// English for viewing and can never feed a translator.
|
||||
fn one_image_track() -> serde_json::Value {
|
||||
serde_json::json!({ "sub_tracks": [
|
||||
{ "language": "en", "codec": "hdmv_pgs_subtitle", "forced": false, "sdh": false },
|
||||
] })
|
||||
}
|
||||
|
||||
/// A fake `alass` that copies its input to its output — every run
|
||||
/// accepted — and appends a line to `log` so the runs can be counted.
|
||||
async fn counting_alass(directory: &std::path::Path, log: &std::path::Path) -> PathBuf {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
|
||||
let binary = directory.join("alass");
|
||||
tokio::fs::write(
|
||||
&binary,
|
||||
format!(
|
||||
"#!/bin/sh\necho run >> {}\ncp \"$2\" \"$3\"\n",
|
||||
log.display()
|
||||
),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
tokio::fs::set_permissions(&binary, std::fs::Permissions::from_mode(0o755))
|
||||
.await
|
||||
.unwrap();
|
||||
binary
|
||||
}
|
||||
|
||||
async fn alass_runs(log: &std::path::Path) -> usize {
|
||||
tokio::fs::read_to_string(log)
|
||||
.await
|
||||
.map_or(0, |text| text.lines().count())
|
||||
}
|
||||
|
||||
/// #268's deadlock. The release carries one English PGS track, so English
|
||||
/// reads as satisfied and is never fetched, while the track itself is
|
||||
/// bitmaps and can never be translated from. §15 as amended lets the
|
||||
/// translation lane fetch a text English subtitle anyway.
|
||||
#[tokio::test]
|
||||
async fn an_image_track_does_not_block_the_language_it_satisfies_from_being_fetched() {
|
||||
let fixture = Fixture::new(&one_image_track()).await;
|
||||
fixture.configure(r#"["pt-PT"]"#, Some("openai")).await;
|
||||
let provider = StubProvider::new(
|
||||
"opensubtitles",
|
||||
vec![candidate(
|
||||
"opensubtitles",
|
||||
"en-1",
|
||||
Language::Other("en".to_owned()),
|
||||
)],
|
||||
);
|
||||
let action = action(vec![Arc::new(provider)], vec![Arc::new(StubBackend)]);
|
||||
|
||||
action.run(&fixture.database).await.unwrap();
|
||||
|
||||
let translated = fixture
|
||||
.directory
|
||||
.path()
|
||||
.join("Movie (2024) - [1080p].pt-PT.mt.srt");
|
||||
assert!(
|
||||
tokio::fs::read_to_string(&translated)
|
||||
.await
|
||||
.unwrap()
|
||||
.contains("HELLO THERE"),
|
||||
"the gap closed by translating the fetched English source"
|
||||
);
|
||||
|
||||
let files = fixture.files().await;
|
||||
let source = files
|
||||
.iter()
|
||||
.find(|file| file.origin == SubtitleOrigin::Provider)
|
||||
.expect("the English source was fetched despite the image track satisfying English");
|
||||
assert_eq!(source.language, "en");
|
||||
assert_eq!(
|
||||
fixture.attempt("pt-PT").await.unwrap().state,
|
||||
SubtitleState::Satisfied
|
||||
);
|
||||
}
|
||||
|
||||
/// The point of a skeleton: the fetched source is aligned against the
|
||||
/// disc's own cue structure, and the translation made from it is then
|
||||
/// left alone. A second `alass` pass can only move disc-exact timings
|
||||
/// off.
|
||||
#[tokio::test]
|
||||
async fn a_source_aligned_to_a_skeleton_skips_the_post_translation_pass() {
|
||||
let fixture = Fixture::new(&one_image_track()).await;
|
||||
fixture.configure(r#"["pt-PT"]"#, Some("openai")).await;
|
||||
arr_db::skeletons::record(
|
||||
fixture.database.pool(),
|
||||
fixture.media_file_id,
|
||||
0,
|
||||
"en",
|
||||
&[(1_000, 2_500), (3_000, 4_000)],
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let log = fixture.directory.path().join("alass.log");
|
||||
let binary = counting_alass(fixture.directory.path(), &log).await;
|
||||
let action = action_with_syncer(
|
||||
vec![Arc::new(StubProvider::new(
|
||||
"opensubtitles",
|
||||
vec![candidate(
|
||||
"opensubtitles",
|
||||
"en-1",
|
||||
Language::Other("en".to_owned()),
|
||||
)],
|
||||
))],
|
||||
vec![Arc::new(StubBackend)],
|
||||
Syncer::new().with_binary(&binary),
|
||||
);
|
||||
|
||||
action.run(&fixture.database).await.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
alass_runs(&log).await,
|
||||
1,
|
||||
"alass aligned the source against the skeleton and was not run again"
|
||||
);
|
||||
let files = fixture.files().await;
|
||||
assert!(
|
||||
files
|
||||
.iter()
|
||||
.find(|file| file.origin == SubtitleOrigin::Provider)
|
||||
.unwrap()
|
||||
.skeleton_aligned,
|
||||
"the fetched source is recorded as disc-exact"
|
||||
);
|
||||
assert!(
|
||||
files
|
||||
.iter()
|
||||
.find(|file| file.origin == SubtitleOrigin::Translated)
|
||||
.unwrap()
|
||||
.skeleton_aligned,
|
||||
"and so is what was translated from it"
|
||||
);
|
||||
}
|
||||
|
||||
/// Without a skeleton — the packets did not pair, or the file predates
|
||||
/// #268 — the fetch still happens and both passes align against the
|
||||
/// video, exactly as they always did.
|
||||
#[tokio::test]
|
||||
async fn without_a_skeleton_both_passes_still_align_against_the_video() {
|
||||
let fixture = Fixture::new(&one_image_track()).await;
|
||||
fixture.configure(r#"["pt-PT"]"#, Some("openai")).await;
|
||||
|
||||
let log = fixture.directory.path().join("alass.log");
|
||||
let binary = counting_alass(fixture.directory.path(), &log).await;
|
||||
let action = action_with_syncer(
|
||||
vec![Arc::new(StubProvider::new(
|
||||
"opensubtitles",
|
||||
vec![candidate(
|
||||
"opensubtitles",
|
||||
"en-1",
|
||||
Language::Other("en".to_owned()),
|
||||
)],
|
||||
))],
|
||||
vec![Arc::new(StubBackend)],
|
||||
Syncer::new().with_binary(&binary),
|
||||
);
|
||||
|
||||
action.run(&fixture.database).await.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
alass_runs(&log).await,
|
||||
2,
|
||||
"source and translation both synced"
|
||||
);
|
||||
assert!(fixture
|
||||
.files()
|
||||
.await
|
||||
.iter()
|
||||
.all(|file| !file.skeleton_aligned));
|
||||
}
|
||||
|
||||
/// A forced image track covers signs alone (§15), so it is not a reason
|
||||
/// to go looking for a source subtitle either.
|
||||
#[tokio::test]
|
||||
async fn a_forced_image_track_is_not_a_translation_source_to_fetch() {
|
||||
let fixture = Fixture::new(&serde_json::json!({ "sub_tracks": [
|
||||
{ "language": "en", "codec": "hdmv_pgs_subtitle", "forced": true, "sdh": false },
|
||||
] }))
|
||||
.await;
|
||||
fixture.configure(r#"["pt-PT"]"#, Some("openai")).await;
|
||||
let action = action(
|
||||
vec![Arc::new(StubProvider::new(
|
||||
"opensubtitles",
|
||||
vec![candidate(
|
||||
"opensubtitles",
|
||||
"en-1",
|
||||
Language::Other("en".to_owned()),
|
||||
)],
|
||||
))],
|
||||
vec![Arc::new(StubBackend)],
|
||||
);
|
||||
|
||||
action.run(&fixture.database).await.unwrap();
|
||||
|
||||
let attempt = fixture.attempt("pt-PT").await.unwrap();
|
||||
assert_eq!(attempt.state, SubtitleState::Unavailable);
|
||||
assert!(fixture.files().await.iter().all(|file| file.path.is_none()));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn an_embedded_track_satisfies_without_asking_any_provider() {
|
||||
let fixture = Fixture::new(&serde_json::json!({ "sub_tracks": [
|
||||
|
||||
Reference in New Issue
Block a user