Skip to content

Instantly share code, notes, and snippets.

@wett1988
Last active April 21, 2025 15:07
Show Gist options
  • Save wett1988/88d880ec412a67e7c50cf4c85abee14f to your computer and use it in GitHub Desktop.
Save wett1988/88d880ec412a67e7c50cf4c85abee14f to your computer and use it in GitHub Desktop.
Tool for download video with `yt-dlp` and convert it to mobile optimized format with `ffmpeg`
use std::process::{exit, Command, ExitStatus};
use std::fs::metadata;
// fn check_installed(command: &str) -> bool {
// match Command::new(command).arg("--version").output() {
// Ok(output) => output.status.success(),
// Err(_) => false,
// }
// }
fn main() {
// Проверим, установлены ли yt-dlp и ffmpeg
// if !check_installed("yt-dlp") {
// eprintln!("yt-dlp is not installed.");
// exit(1);
// }
// if !check_installed("ffmpeg") {
// eprintln!("ffmpeg is not installed.");
// exit(1);
// }
// Убедитесь, что URL передан в качестве аргумента
// let url = std::env::args().nth(1).expect("Please provide a URL");
let args: Vec<String> = std::env::args().collect();
let yt_dlp_args = &args[1..];
// // Устанавливаем путь для скачивания видео
let video_filename = "downloaded_video.mp4";
let mut results: Vec<ExitStatus> = vec![];
// Скачиваем видео с помощью yt-dlp
// let status = Command::new("yt-dlp")
// .arg("-o")
// .arg(video_filename)
// .arg(&url)
// .status()
// .expect("Failed to execute yt-dlp");
results.push(Command::new("yt-dlp")
.args(yt_dlp_args)
.arg("-o")
.arg(video_filename)
.status()
.expect("Failed to execute yt-dlp"));
if !results[0].success() {
eprintln!("Error downloading the video.");
exit(1);
}
println!("Video downloaded successfully!");
// Проверим, что файл скачан
if metadata(video_filename).is_err() {
eprintln!("Downloaded file not found.");
exit(1);
}
// Конвертируем видео с помощью ffmpeg
let output_filename = "converted_video.mp4"; // или другой формат по вашему выбору
let ffmpeg_args = vec![
"-c:v", "libx264",
"-preset", "faster",
"-pix_fmt","yuv420p",
"-c:a","aac",
"-b:a","128k",
"-vf","scale=720:-2",
"-crf","33",
];
results.push(Command::new("ffmpeg")
.arg("-i")
.arg(video_filename)
.args(ffmpeg_args)
.arg(output_filename)
.status()
.expect("Failed to execute ffmpeg"));
if !results[1].success() {
eprintln!("Error converting the video.");
exit(1);
}
println!("Video converted successfully!");
// Удаляем исходный видео файл
if std::path::Path::new(video_filename).exists() {
if let Err(e) = std::fs::remove_file(video_filename) {
eprintln!("Не удалось удалить исходный файл '{}': {}", video_filename, e);
} else {
println!("Исходный файл '{}' был удален.", video_filename);
}
} else {
eprintln!("Файл '{}' не существует, поэтому его нельзя удалить.", video_filename);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment