Add pre-check function

This commit is contained in:
ByteDream 2022-12-03 00:39:52 +01:00
parent 9d45995e86
commit 135d59ce8b
3 changed files with 24 additions and 18 deletions

View file

@ -3,7 +3,7 @@ use crate::cli::utils::{download_segments, find_resolution};
use crate::utils::context::Context;
use crate::utils::format::{format_string, Format};
use crate::utils::log::progress;
use crate::utils::os::{free_file, tempfile};
use crate::utils::os::{free_file, has_ffmpeg, tempfile};
use crate::utils::parse::{parse_url, UrlFilter};
use crate::utils::sort::{sort_formats_after_seasons, sort_seasons_after_number};
use crate::Execute;
@ -110,6 +110,16 @@ pub struct Archive {
#[async_trait::async_trait(?Send)]
impl Execute for Archive {
fn pre_check(&self) -> Result<()> {
if !has_ffmpeg() {
bail!("FFmpeg is needed to run this command")
} else if PathBuf::from(&self.output).extension().unwrap_or_default().to_string_lossy() != "mkv" {
bail!("File extension is not '.mkv'. Currently only matroska / '.mkv' files are supported")
}
Ok(())
}
async fn execute(self, ctx: Context) -> Result<()> {
let mut parsed_urls = vec![];

View file

@ -65,6 +65,16 @@ pub struct Download {
#[async_trait::async_trait(?Send)]
impl Execute for Download {
fn pre_check(&self) -> Result<()> {
if has_ffmpeg() {
debug!("FFmpeg detected")
} else if PathBuf::from(&self.output).extension().unwrap_or_default().to_string_lossy() != "ts" {
bail!("File extension is not '.ts'. If you want to use a custom file format, please install ffmpeg")
}
Ok(())
}
async fn execute(self, ctx: Context) -> Result<()> {
let mut parsed_urls = vec![];
@ -180,21 +190,6 @@ impl Execute for Download {
)),
);
let use_ffmpeg = if let Some(extension) = path.extension() {
if extension != "ts" {
if !has_ffmpeg() {
bail!(
"File ending is not `.ts`, ffmpeg is required to convert the video"
)
}
true
} else {
false
}
} else {
false
};
info!(
"Downloading {} to '{}'",
format.title,
@ -211,7 +206,7 @@ impl Execute for Download {
tab_info!("Resolution: {}", format.stream.resolution);
tab_info!("FPS: {:.2}", format.stream.fps);
if use_ffmpeg {
if path.extension().unwrap_or_default().to_string_lossy() != "ts" {
download_ffmpeg(&ctx, format.stream, path.as_path()).await?;
} else if path.to_str().unwrap() == "-" {
let mut stdout = std::io::stdout().lock();

View file

@ -16,6 +16,7 @@ pub use cli::{archive::Archive, download::Download, login::Login};
#[async_trait::async_trait(?Send)]
trait Execute {
fn pre_check(&self) -> Result<()> { Ok(()) }
async fn execute(self, ctx: Context) -> Result<()>;
}
@ -163,7 +164,7 @@ pub async fn cli_entrypoint() {
}
};
if let Err(err) = result {
error!("{}", err);
error!("a unexpected error occurred: {}", err);
std::process::exit(1)
}
}