This commit is contained in:
ByteDream 2023-03-23 01:17:41 +01:00
parent 90212c4ec0
commit 0a40f3c40f
30 changed files with 3651 additions and 2982 deletions

View file

@ -0,0 +1,151 @@
use crate::download::filter::DownloadFilter;
use crate::utils::context::Context;
use crate::utils::ffmpeg::FFmpegPreset;
use crate::utils::filter::Filter;
use crate::utils::format::formats_visual_output;
use crate::utils::log::progress;
use crate::utils::os::{free_file, has_ffmpeg};
use crate::utils::parse::parse_url;
use crate::Execute;
use anyhow::bail;
use anyhow::Result;
use crunchyroll_rs::media::Resolution;
use crunchyroll_rs::Locale;
use log::{debug, warn};
use std::path::Path;
#[derive(Clone, Debug, clap::Parser)]
#[clap(about = "Download a video")]
#[command(arg_required_else_help(true))]
pub struct Download {
#[arg(help = format!("Audio language. Can only be used if the provided url(s) point to a series. \
Available languages are: {}", Locale::all().into_iter().map(|l| l.to_string()).collect::<Vec<String>>().join(", ")))]
#[arg(long_help = format!("Audio language. Can only be used if the provided url(s) point to a series. \
Available languages are:\n{}", Locale::all().into_iter().map(|l| format!("{:<6} {}", l.to_string(), l.to_human_readable())).collect::<Vec<String>>().join("\n ")))]
#[arg(short, long, default_value_t = crate::utils::locale::system_locale())]
pub(crate) audio: Locale,
#[arg(help = format!("Subtitle language. Available languages are: {}", Locale::all().into_iter().map(|l| l.to_string()).collect::<Vec<String>>().join(", ")))]
#[arg(long_help = format!("Subtitle language. If set, the subtitle will be burned into the video and cannot be disabled. \
Available languages are: {}", Locale::all().into_iter().map(|l| l.to_string()).collect::<Vec<String>>().join(", ")))]
#[arg(short, long)]
pub(crate) subtitle: Option<Locale>,
#[arg(help = "Name of the output file")]
#[arg(long_help = "Name of the output file.\
If you use one of the following pattern they will get replaced:\n \
{title} Title of the video\n \
{series_name} Name of the series\n \
{season_name} Name of the season\n \
{audio} Audio language of the video\n \
{resolution} Resolution of the video\n \
{season_number} Number of the season\n \
{episode_number} Number of the episode\n \
{relative_episode_number} Number of the episode relative to its season\
{series_id} ID of the series\n \
{season_id} ID of the season\n \
{episode_id} ID of the episode")]
#[arg(short, long, default_value = "{title}.mp4")]
pub(crate) output: String,
#[arg(help = "Video resolution")]
#[arg(long_help = "The video resolution.\
Can either be specified via the pixels (e.g. 1920x1080), the abbreviation for pixels (e.g. 1080p) or 'common-use' words (e.g. best). \
Specifying the exact pixels is not recommended, use one of the other options instead. \
Crunchyroll let you choose the quality with pixel abbreviation on their clients, so you might be already familiar with the available options. \
The available common-use words are 'best' (choose the best resolution available) and 'worst' (worst resolution available)")]
#[arg(short, long, default_value = "best")]
#[arg(value_parser = crate::utils::clap::clap_parse_resolution)]
pub(crate) resolution: Resolution,
#[arg(help = format!("Presets for video converting. Can be used multiple times. \
Available presets: \n {}", FFmpegPreset::available_matches_human_readable().join("\n ")))]
#[arg(long_help = format!("Presets for video converting. Can be used multiple times. \
Generally used to minify the file size with keeping (nearly) the same quality. \
It is recommended to only use this if you download videos with high resolutions since low resolution videos tend to result in a larger file with any of the provided presets. \
Available presets: \n {}", FFmpegPreset::available_matches_human_readable().join("\n ")))]
#[arg(long)]
#[arg(value_parser = FFmpegPreset::parse)]
pub(crate) ffmpeg_preset: Option<FFmpegPreset>,
#[arg(help = "Skip files which are already existing")]
#[arg(long, default_value_t = false)]
pub(crate) skip_existing: bool,
#[arg(help = "Url(s) to Crunchyroll episodes or series")]
pub(crate) urls: Vec<String>,
}
#[async_trait::async_trait(?Send)]
impl Execute for Download {
fn pre_check(&mut self) -> Result<()> {
if !has_ffmpeg() {
bail!("FFmpeg is needed to run this command")
} else if Path::new(&self.output)
.extension()
.unwrap_or_default()
.is_empty()
&& self.output != "-"
{
bail!("No file extension found. Please specify a file extension (via `-o`) for the output file")
}
if self.subtitle.is_some() {
if let Some(ext) = Path::new(&self.output).extension() {
if ext.to_string_lossy() != "mp4" {
warn!("Detected a non mp4 output container. Adding subtitles may take a while")
}
}
}
Ok(())
}
async fn execute(self, ctx: Context) -> Result<()> {
let mut parsed_urls = vec![];
for (i, url) in self.urls.clone().into_iter().enumerate() {
let progress_handler = progress!("Parsing url {}", i + 1);
match parse_url(&ctx.crunchy, url.clone(), true).await {
Ok((media_collection, url_filter)) => {
progress_handler.stop(format!("Parsed url {}", i + 1));
parsed_urls.push((media_collection, url_filter))
}
Err(e) => bail!("url {} could not be parsed: {}", url, e),
};
}
for (i, (media_collection, url_filter)) in parsed_urls.into_iter().enumerate() {
let progress_handler = progress!("Fetching series details");
let download_formats = DownloadFilter::new(url_filter, self.clone())
.visit(media_collection)
.await?;
if download_formats.is_empty() {
progress_handler.stop(format!("Skipping url {} (no matching videos found)", i + 1));
continue;
}
progress_handler.stop(format!("Loaded series information for url {}", i + 1));
formats_visual_output(download_formats.iter().map(|(_, f)| f).collect());
for (downloader, format) in download_formats {
let formatted_path = format.format_path((&self.output).into(), true);
let (path, changed) = free_file(formatted_path.clone());
if changed && self.skip_existing {
debug!(
"Skipping already existing file '{}'",
formatted_path.to_string_lossy()
);
continue;
}
format.visual_output(&path);
downloader.download(&ctx, &path).await?
}
}
Ok(())
}
}

View file

@ -0,0 +1,349 @@
use crate::download::Download;
use crate::utils::download::{DownloadBuilder, DownloadFormat, Downloader};
use crate::utils::filter::Filter;
use crate::utils::format::{Format, SingleFormat};
use crate::utils::parse::UrlFilter;
use crate::utils::video::variant_data_from_stream;
use anyhow::{bail, Result};
use crunchyroll_rs::media::{Subtitle, VariantData};
use crunchyroll_rs::{Concert, Episode, Movie, MovieListing, MusicVideo, Season, Series};
use log::{error, warn};
use std::collections::HashMap;
pub(crate) struct FilterResult {
format: SingleFormat,
video: VariantData,
audio: VariantData,
subtitle: Option<Subtitle>,
}
pub(crate) struct DownloadFilter {
url_filter: UrlFilter,
download: Download,
season_episode_count: HashMap<u32, Vec<String>>,
season_subtitles_missing: Vec<u32>,
}
impl DownloadFilter {
pub(crate) fn new(url_filter: UrlFilter, download: Download) -> Self {
Self {
url_filter,
download,
season_episode_count: HashMap::new(),
season_subtitles_missing: vec![],
}
}
}
#[async_trait::async_trait]
impl Filter for DownloadFilter {
type T = FilterResult;
type Output = (Downloader, Format);
async fn visit_series(&mut self, series: Series) -> Result<Vec<Season>> {
// `series.audio_locales` isn't always populated b/c of crunchyrolls api. so check if the
// audio is matching only if the field is populated
if !series.audio_locales.is_empty() {
if !series.audio_locales.contains(&self.download.audio) {
error!(
"Series {} is not available with {} audio",
series.title, self.download.audio
);
return Ok(vec![]);
}
}
let seasons = series.seasons().await?;
Ok(seasons)
}
async fn visit_season(&mut self, mut season: Season) -> Result<Vec<Episode>> {
if !self.url_filter.is_season_valid(season.season_number) {
return Ok(vec![]);
}
if !season
.audio_locales
.iter()
.any(|l| l == &self.download.audio)
{
if season
.available_versions()
.await?
.iter()
.any(|l| l == &self.download.audio)
{
season = season
.version(vec![self.download.audio.clone()])
.await?
.remove(0)
} else {
error!(
"Season {} - '{}' is not available with {} audio",
season.season_number,
season.title,
self.download.audio.clone(),
);
return Ok(vec![]);
}
}
let mut episodes = season.episodes().await?;
if Format::has_relative_episodes_fmt(&self.download.output) {
for episode in episodes.iter() {
self.season_episode_count
.entry(episode.season_number)
.or_insert(vec![])
.push(episode.id.clone())
}
}
episodes.retain(|e| {
self.url_filter
.is_episode_valid(e.episode_number, season.season_number)
});
Ok(episodes)
}
async fn visit_episode(&mut self, mut episode: Episode) -> Result<Option<Self::T>> {
if !self
.url_filter
.is_episode_valid(episode.episode_number, episode.season_number)
{
return Ok(None);
}
// check if the audio locale is correct.
// should only be incorrect if the console input was a episode url. otherwise
// `DownloadFilter::visit_season` returns the correct episodes with matching audio
if episode.audio_locale != self.download.audio {
// check if any other version (same episode, other language) of this episode is available
// with the requested audio. if not, return an error
if !episode
.available_versions()
.await?
.contains(&self.download.audio)
{
bail!(
"Episode {} ({}) of {} season {} is not available with {} audio",
episode.episode_number,
episode.title,
episode.series_title,
episode.season_number,
self.download.audio
)
}
// overwrite the current episode with the other version episode
episode = episode
.version(vec![self.download.audio.clone()])
.await?
.remove(0)
}
// check if the subtitles are supported
if let Some(subtitle_locale) = &self.download.subtitle {
if !episode.subtitle_locales.contains(subtitle_locale) {
// if the episode doesn't have the requested subtitles, print a error. to print this
// error only once per season, it's checked if an error got printed before by looking
// up if the season id is present in `self.season_subtitles_missing`. if not, print
// the error and add the season id to `self.season_subtitles_missing`. if it is
// present, skip the error printing
if !self
.season_subtitles_missing
.contains(&episode.season_number)
{
self.season_subtitles_missing.push(episode.season_number);
error!(
"{} season {} is not available with {} subtitles",
episode.series_title, episode.season_number, subtitle_locale
);
}
return Ok(None);
}
}
// get the correct video stream
let stream = episode.streams().await?;
let (video, audio) = if let Some((video, audio)) =
variant_data_from_stream(&stream, &self.download.resolution).await?
{
(video, audio)
} else {
bail!(
"Resolution ({}) is not available for episode {} ({}) of {} season {}",
self.download.resolution,
episode.episode_number,
episode.title,
episode.series_title,
episode.season_number,
)
};
// it is assumed that the subtitle, if requested, exists b/c the subtitle check above must
// be passed to reach this condition.
// the check isn't done in this if block to reduce unnecessary fetching of the stream
let subtitle = if let Some(subtitle_locale) = &self.download.subtitle {
stream.subtitles.get(subtitle_locale).map(|s| s.clone())
} else {
None
};
// get the relative episode number. only done if the output string has the pattern to include
// the relative episode number as this requires some extra fetching
let relative_episode_number = if Format::has_relative_episodes_fmt(&self.download.output) {
if self
.season_episode_count
.get(&episode.season_number)
.is_none()
{
let season_episodes = episode.season().await?.episodes().await?;
self.season_episode_count.insert(
episode.season_number,
season_episodes.into_iter().map(|e| e.id).collect(),
);
}
let relative_episode_number = self
.season_episode_count
.get(&episode.season_number)
.unwrap()
.iter()
.position(|id| id == &episode.id);
if relative_episode_number.is_none() {
warn!(
"Failed to get relative episode number for episode {} ({}) of {} season {}",
episode.episode_number,
episode.title,
episode.series_title,
episode.season_number,
)
}
relative_episode_number
} else {
None
};
Ok(Some(FilterResult {
format: SingleFormat::new_from_episode(
&episode,
&video,
subtitle.clone().map_or(vec![], |s| vec![s.locale]),
relative_episode_number.map(|n| n as u32),
),
video,
audio,
subtitle,
}))
}
async fn visit_movie_listing(&mut self, movie_listing: MovieListing) -> Result<Vec<Movie>> {
Ok(movie_listing.movies().await?)
}
async fn visit_movie(&mut self, movie: Movie) -> Result<Option<Self::T>> {
let stream = movie.streams().await?;
let (video, audio) = if let Some((video, audio)) =
variant_data_from_stream(&stream, &self.download.resolution).await?
{
(video, audio)
} else {
bail!(
"Resolution ({}) of movie '{}' is not available",
self.download.resolution,
movie.title
)
};
let subtitle = if let Some(subtitle_locale) = &self.download.subtitle {
let Some(subtitle) = stream.subtitles.get(subtitle_locale) else {
error!(
"Movie '{}' has no {} subtitles",
movie.title,
subtitle_locale
);
return Ok(None)
};
Some(subtitle.clone())
} else {
None
};
Ok(Some(FilterResult {
format: SingleFormat::new_from_movie(
&movie,
&video,
subtitle.clone().map_or(vec![], |s| vec![s.locale]),
),
video,
audio,
subtitle,
}))
}
async fn visit_music_video(&mut self, music_video: MusicVideo) -> Result<Option<Self::T>> {
let stream = music_video.streams().await?;
let (video, audio) = if let Some((video, audio)) =
variant_data_from_stream(&stream, &self.download.resolution).await?
{
(video, audio)
} else {
bail!(
"Resolution ({}) of music video {} is not available",
self.download.resolution,
music_video.title
)
};
Ok(Some(FilterResult {
format: SingleFormat::new_from_music_video(&music_video, &video),
video,
audio,
subtitle: None,
}))
}
async fn visit_concert(&mut self, concert: Concert) -> Result<Option<Self::T>> {
let stream = concert.streams().await?;
let (video, audio) = if let Some((video, audio)) =
variant_data_from_stream(&stream, &self.download.resolution).await?
{
(video, audio)
} else {
bail!(
"Resolution ({}) of music video {} is not available",
self.download.resolution,
concert.title
)
};
Ok(Some(FilterResult {
format: SingleFormat::new_from_concert(&concert, &video),
video,
audio,
subtitle: None,
}))
}
async fn finish(self, mut input: Vec<Self::T>) -> Result<Vec<Self::Output>> {
let mut result = vec![];
input.sort_by(|a, b| {
a.format
.sequence_number
.total_cmp(&b.format.sequence_number)
});
for data in input {
let mut downloader = DownloadBuilder::new()
.default_subtitle(self.download.subtitle.clone())
.build();
downloader.add_format(DownloadFormat {
video: (data.video, data.format.audio.clone()),
audios: vec![(data.audio, data.format.audio.clone())],
subtitles: data.subtitle.map_or(vec![], |s| vec![s]),
});
result.push((downloader, Format::from_single_formats(vec![data.format])))
}
Ok(result)
}
}

View file

@ -0,0 +1,4 @@
mod command;
mod filter;
pub use command::Download;