fix filename issue with downloader

This commit is contained in:
ThetaDev 2022-08-08 17:19:14 +02:00
parent 150f074131
commit 45bd435d34
4 changed files with 34 additions and 16 deletions

View file

@ -26,7 +26,7 @@ async-trait = "0.1.56"
chrono = {version = "0.4.19", features = ["serde"]} chrono = {version = "0.4.19", features = ["serde"]}
futures = "0.3.21" futures = "0.3.21"
indicatif = "0.17.0" indicatif = "0.17.0"
slug = "0.1.4" filenamify = "0.1.0"
[dev-dependencies] [dev-dependencies]
env_logger = "0.9.0" env_logger = "0.9.0"

View file

@ -11,4 +11,5 @@ tokio = {version = "1.20.0", features = ["rt-multi-thread"]}
indicatif = "0.17.0" indicatif = "0.17.0"
futures = "0.3.21" futures = "0.3.21"
anyhow = "1.0" anyhow = "1.0"
env_logger = "0.9.0"
rusty-tube = {path = "../"} rusty-tube = {path = "../"}

View file

@ -1,4 +1,4 @@
use anyhow::Result; use anyhow::{Context, Result};
use futures::stream::{self, StreamExt}; use futures::stream::{self, StreamExt};
use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
use reqwest::{Client, ClientBuilder}; use reqwest::{Client, ClientBuilder};
@ -7,7 +7,7 @@ use rusty_tube::client::{ClientType, RustyTube};
async fn download_video( async fn download_video(
video_id: String, video_id: String,
output_dir: &str, output_dir: &str,
output_fname: &str, output_fname: Option<String>,
resolution: Option<u32>, resolution: Option<u32>,
ffmpeg: &str, ffmpeg: &str,
rt: &RustyTube, rt: &RustyTube,
@ -23,18 +23,26 @@ async fn download_video(
let player_data = rt let player_data = rt
.get_player(video_id.as_str(), ClientType::Desktop) .get_player(video_id.as_str(), ClientType::Desktop)
.await?; .await
.context(format!(
"Failed to fetch player data for video {}",
video_id
))?;
rusty_tube::download::download_video( rusty_tube::download::download_video(
&player_data, &player_data,
output_dir, output_dir,
// output_fname, output_fname,
resolution, resolution,
ffmpeg, ffmpeg,
http, http,
pb, pb,
) )
.await?; .await
.context(format!(
"Failed to download video '{}' [{}]",
player_data.info.title, video_id
))?;
main.inc(1); main.inc(1);
Ok(()) Ok(())
@ -73,7 +81,7 @@ async fn main() {
download_video( download_video(
item.video_id.to_owned(), item.video_id.to_owned(),
"tmp", "tmp",
"xyz", None,
None, None,
"ffmpeg", "ffmpeg",
&rt, &rt,

View file

@ -1,9 +1,9 @@
use std::{cmp::Ordering, ffi::OsString, fmt::format, ops::Range, path::PathBuf}; use std::{cmp::Ordering, ffi::OsString, ops::Range, path::PathBuf};
use anyhow::{anyhow, bail, Result}; use anyhow::{anyhow, bail, Result};
use fancy_regex::Regex; use fancy_regex::Regex;
use futures::stream::{self, StreamExt}; use futures::stream::{self, StreamExt};
use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; use indicatif::ProgressBar;
use log::debug; use log::debug;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use rand::Rng; use rand::Rng;
@ -180,11 +180,18 @@ struct StreamDownload {
pub async fn download_video( pub async fn download_video(
player_data: &PlayerData, player_data: &PlayerData,
output_dir: &str, output_dir: &str,
output_fname: Option<String>,
resolution: Option<u32>, resolution: Option<u32>,
ffmpeg: &str, ffmpeg: &str,
http: Client, http: Client,
pb: ProgressBar, pb: ProgressBar,
) -> Result<()> { ) -> Result<()> {
// Download filepath
let download_dir = PathBuf::from(output_dir);
let title = player_data.info.title.to_owned();
let output_fname = output_fname
.unwrap_or_else(|| filenamify::filenamify(format!("{} [{}]", title, player_data.info.id)));
// Select streams to download // Select streams to download
let video = match resolution { let video = match resolution {
Some(r) => Some(some_or_bail!( Some(r) => Some(some_or_bail!(
@ -204,22 +211,22 @@ pub async fn download_video(
Err(anyhow!("no audio stream")) Err(anyhow!("no audio stream"))
); );
let download_dir = PathBuf::from(output_dir);
let title = player_data.info.title.to_owned();
let title_fname = slug::slugify(&title); // TODO: slugify
let mut downloads: Vec<StreamDownload> = Vec::new(); let mut downloads: Vec<StreamDownload> = Vec::new();
video.map(|v| { video.map(|v| {
downloads.push(StreamDownload { downloads.push(StreamDownload {
file: download_dir.join(format!("{}.video{}", title_fname, v.format.extension())), file: download_dir.join(format!("{}.video{}", output_fname, v.format.extension())),
url: v.url.to_owned(), url: v.url.to_owned(),
video_codec: Some(v.codec), video_codec: Some(v.codec),
audio_codec: None, audio_codec: None,
}); });
}); });
downloads.push(StreamDownload { downloads.push(StreamDownload {
file: download_dir.join(format!("{}.audio{}", title_fname, audio.format.extension())), file: download_dir.join(format!(
"{}.audio{}",
output_fname,
audio.format.extension()
)),
url: audio.url.to_owned(), url: audio.url.to_owned(),
video_codec: None, video_codec: None,
audio_codec: Some(audio.codec), audio_codec: Some(audio.codec),
@ -230,7 +237,7 @@ pub async fn download_video(
pb.set_message(format!("Converting {}", title)); pb.set_message(format!("Converting {}", title));
// let output_file = download_dir.join(format!("{}.mp4", title_fname)); // let output_file = download_dir.join(format!("{}.mp4", title_fname));
convert_streams(&downloads, download_dir.join(title_fname), ffmpeg).await?; convert_streams(&downloads, download_dir.join(output_fname), ffmpeg).await?;
// Delete original files // Delete original files
stream::iter(&downloads) stream::iter(&downloads)
@ -324,6 +331,7 @@ async fn convert_streams<P: Into<PathBuf>>(
Ok(()) Ok(())
} }
/*
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::client::RustyTube; use crate::client::RustyTube;
@ -358,3 +366,4 @@ mod tests {
// .unwrap(); // .unwrap();
} }
} }
*/