Skip to main content

shift_subtitle_timing

Function shift_subtitle_timing 

Source
pub fn shift_subtitle_timing(
    subtitle: &mut Subtitle,
    offset_seconds: f32,
) -> Result<SyncResult, SubXError>
Expand description

Shift every subtitle entry’s start and end time by a manual offset.

This is the entire manual-offset timing transform, reachable without a SyncEngine: SyncEngine::new requires a VAD detector even for callers that only ever apply a manual offset, so a host that never performs detection would have to satisfy a precondition for a subsystem it does not use. SyncEngine::apply_manual_offset delegates here after enforcing sync.max_offset_seconds, so exactly one implementation of the shift exists.

A positive offset delays every entry via a checked addition; a negative offset advances every entry, clamping at Duration::ZERO rather than producing negative timestamps.

§Arguments

  • subtitle - Mutable subtitle data whose entries are shifted in place
  • offset_seconds - Offset in seconds (positive delays, negative advances)

§Returns

A SyncResult with the supplied offset, full confidence, method_used = SyncMethod::Manual, an additional_info object recording the applied offset and the number of entries modified, and the measured processing duration.

§Errors

Returns an crate::error::SubXError::AudioProcessing error if a positive offset would overflow any entry’s timing (Duration::MAX plus a positive offset).

sync.max_offset_seconds is not enforced here — this function has no configuration to read it from. SyncEngine::apply_manual_offset is the entry point that enforces the configured maximum; a caller reaching this function directly is responsible for its own bound.

§Examples

use std::time::Duration;
use subx_core::core::formats::{Subtitle, SubtitleEntry, SubtitleFormatType, SubtitleMetadata};
use subx_core::core::sync::{shift_subtitle_timing, SyncMethod};

let mut subtitle = Subtitle::new(
    SubtitleFormatType::Srt,
    SubtitleMetadata::default(),
);
subtitle.entries.push(SubtitleEntry::new(
    1,
    Duration::from_secs(10),
    Duration::from_secs(12),
    "Hello".to_string(),
));

let result = shift_subtitle_timing(&mut subtitle, 2.5).unwrap();
assert_eq!(subtitle.entries[0].start_time, Duration::from_secs_f32(12.5));
assert_eq!(result.method_used, SyncMethod::Manual);
assert_eq!(result.confidence, 1.0);