use crate::{
cli::BatchArgs,
commands::batch::{run_batch, BatchFuture, BatchTask, FileOutcome},
ffmpeg::{args, Ffmpeg, ProcessRunner},
workflow::{Workflow, WorkflowOptions},
};
use anyhow::Result;
use clap::Args;
use std::path::{Path, PathBuf};
use tokio_util::sync::CancellationToken;
#[derive(Debug, Args)]
#[command(after_help = "Examples:
toolkitrs ts2mp4 Scan and process all .ts files in the current directory
toolkitrs ts2mp4 video.ts Process one file; prompt if sibling .ts files exist
toolkitrs ts2mp4 --batch --input-dir /dir Process all .ts files in /dir
toolkitrs ts2mp4 --batch --on-error skip Continue past errors and report at the end")]
pub struct Ts2mp4Args {
#[command(flatten)]
pub batch: BatchArgs,
#[arg(value_name = "FILE")]
pub files: Vec<PathBuf>,
}
#[derive(Debug, Clone, Copy)]
struct Ts2Mp4Task {
force: bool,
}
impl Ts2Mp4Task {
fn workflow_options(self) -> WorkflowOptions {
WorkflowOptions {
force: self.force,
..WorkflowOptions::default()
}
}
}
impl BatchTask for Ts2Mp4Task {
fn input_extension(&self) -> &str {
Workflow::Ts2Mp4.input_extension()
}
fn output_extension(&self) -> &str {
Workflow::Ts2Mp4.output_extension()
}
fn file_type_name(&self) -> &str {
Workflow::Ts2Mp4.file_type_name()
}
fn process_file<R: ProcessRunner>(
&self,
input: &Path,
output: &Path,
ffmpeg: &Ffmpeg<R>,
) -> Result<FileOutcome> {
ffmpeg.run(args::remux_copy(input, output, self.force))?;
Ok(FileOutcome::Success)
}
fn process_file_async(
&self,
input: PathBuf,
output: PathBuf,
ffmpeg_binary: PathBuf,
cancel: CancellationToken,
) -> BatchFuture {
let options = self.workflow_options();
Box::pin(async move {
Workflow::Ts2Mp4
.run_async(input, output, &options, &ffmpeg_binary, cancel, None)
.await
})
}
}
pub fn run<R: ProcessRunner>(args_cli: Ts2mp4Args, ffmpeg: &Ffmpeg<R>) -> Result<()> {
let task = Ts2Mp4Task {
force: args_cli.batch.force,
};
run_batch(&task, &args_cli.batch, args_cli.files, ffmpeg)
}