From 135d59ce8b3ac8230c385c3a9469564bd460ce5b Mon Sep 17 00:00:00 2001 From: ByteDream Date: Sat, 3 Dec 2022 00:39:52 +0100 Subject: [PATCH] Add pre-check function --- crunchy-cli-core/src/cli/archive.rs | 12 +++++++++++- crunchy-cli-core/src/cli/download.rs | 27 +++++++++++---------------- crunchy-cli-core/src/lib.rs | 3 ++- 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/crunchy-cli-core/src/cli/archive.rs b/crunchy-cli-core/src/cli/archive.rs index e798e75..d4b0a5d 100644 --- a/crunchy-cli-core/src/cli/archive.rs +++ b/crunchy-cli-core/src/cli/archive.rs @@ -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![]; diff --git a/crunchy-cli-core/src/cli/download.rs b/crunchy-cli-core/src/cli/download.rs index acb086b..b0d0ffa 100644 --- a/crunchy-cli-core/src/cli/download.rs +++ b/crunchy-cli-core/src/cli/download.rs @@ -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(); diff --git a/crunchy-cli-core/src/lib.rs b/crunchy-cli-core/src/lib.rs index 083b401..c895d51 100644 --- a/crunchy-cli-core/src/lib.rs +++ b/crunchy-cli-core/src/lib.rs @@ -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) } }