Initial commit

This commit is contained in:
D1G1T4L3CH0
2024-05-20 19:38:18 -04:00
commit a34ee4ac83
5 changed files with 145 additions and 0 deletions

107
src/main.rs Normal file
View File

@@ -0,0 +1,107 @@
use std::fs::{self, File};
use std::io::{self, BufRead, Write};
use std::path::{Path, PathBuf};
use std::process::{exit, Command};
fn main() -> io::Result<()> {
// Check for yt-dlp and ffmpeg dependencies
if !command_exists("yt-dlp") || !command_exists("ffmpeg") {
println!("The required dependencies yt-dlp and ffmpeg are not installed.");
println!("Please install them before running this program.");
println!("On Linux, you can use the following commands:");
println!("sudo curl -L https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp -o /usr/local/bin/yt-dlp");
println!("sudo chmod a+rx /usr/local/bin/yt-dlp");
println!("sudo apt-get install ffmpeg");
println!("On Windows, you can download the executables and add them to your PATH:");
println!("yt-dlp: https://github.com/yt-dlp/yt-dlp/releases/latest");
println!("ffmpeg: https://www.gyan.dev/ffmpeg/builds/");
return Ok(());
}
let dir_path = "urls";
let base_dir = "videos";
let archive_file = "downloaded.txt";
if !Path::new(dir_path).exists() {
fs::create_dir(dir_path)?;
println!("Created directory: {}. You can create your own .urls files in this directory. The name of the file will be used as the subdirectory for the downloaded videos.", dir_path);
let default_file = Path::new(dir_path).join("default.urls");
let mut file = File::create(&default_file)?;
writeln!(file, "# Add your URLs here, one per line. This is the default file, videos will be downloaded to the base directory.")?;
println!("Created file: {}. You can add URLs to this file for downloading videos. For different subdirectories, create a new .urls file with the name of the subdirectory.", default_file.display());
exit(0);
}
let default_file = Path::new(dir_path).join("default.urls");
if !default_file.exists() {
let mut file = File::create(&default_file)?;
writeln!(file, "# Add your URLs here, one per line. This is the default file, videos will be downloaded to the base directory.")?;
println!("Created file: {}. You can add URLs to this file for downloading videos. For different subdirectories, create a new .urls file with the name of the subdirectory.", default_file.display());
exit(0);
}
let mut urls_exist = false;
for entry in fs::read_dir(dir_path)? {
let entry = entry?;
let path = entry.path();
if path.is_file() {
let file_stem = path.file_stem().unwrap().to_str().unwrap();
let output_dir = if file_stem == "default" {
PathBuf::from(base_dir)
} else {
PathBuf::from(base_dir).join(file_stem)
};
fs::create_dir_all(&output_dir)?;
let file = File::open(&path)?;
let reader = io::BufReader::new(file);
for line in reader.lines() {
let url = line?;
if !url.starts_with('#') {
urls_exist = true;
break;
}
}
if urls_exist {
let status = Command::new("yt-dlp")
.arg("-a")
.arg(path.to_str().unwrap())
.arg("--download-archive")
.arg(archive_file)
.arg("-f")
.arg("bestvideo+bestaudio")
.arg("--prefer-ffmpeg")
.arg("--write-description")
.arg("--add-metadata")
.arg("--write-auto-sub")
.arg("--embed-subs")
.arg("-o")
.arg(output_dir.join("%(title)s.%(ext)s").to_str().unwrap())
.status()?;
println!("Download finished with exit status: {}", status);
}
}
}
if !urls_exist {
println!("No URLs found in the .urls files. Please add URLs to the .urls files for downloading videos. Each URL should be on a new line. Lines starting with '#' are considered comments and are ignored.");
}
fn command_exists(cmd: &str) -> bool {
let output = if cfg!(target_os = "windows") {
Command::new("where").arg("/Q").arg(cmd).output()
} else {
Command::new("which").arg(cmd).output()
};
output.map_or(false, |o| o.status.success())
}
Ok(())
}