From 64e08e29f80dcc9fa90bf9a397cd020a37259249 Mon Sep 17 00:00:00 2001 From: mykola2312 Date: Tue, 20 Feb 2024 20:26:45 +0200 Subject: [PATCH] begin implementing ffmpeg interface --- .gitignore | 5 ++++- src/dl.rs | 1 + src/dl/ffmpeg.rs | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 src/dl/ffmpeg.rs diff --git a/.gitignore b/.gitignore index fb3c5b0..4dc466c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,7 @@ /target .env *.json -*.txt \ No newline at end of file +*.txt +*.m4a +*.mp3 +*.mp4 \ No newline at end of file diff --git a/src/dl.rs b/src/dl.rs index e91fcfd..ac3aebe 100644 --- a/src/dl.rs +++ b/src/dl.rs @@ -1 +1,2 @@ pub mod yt_dlp; +pub mod ffmpeg; \ No newline at end of file diff --git a/src/dl/ffmpeg.rs b/src/dl/ffmpeg.rs new file mode 100644 index 0000000..a833dbf --- /dev/null +++ b/src/dl/ffmpeg.rs @@ -0,0 +1,50 @@ +use core::fmt; +use std::{path::Path, str::Utf8Error}; +use tokio::process::Command; + +pub enum FFMpegError { + CommandError(std::io::Error), + UtfError(Utf8Error), + ErrorMessage(String) +} + +impl From for FFMpegError { + fn from(value: std::io::Error) -> Self { + Self::CommandError(value) + } +} + +impl From for FFMpegError { + fn from(value: Utf8Error) -> Self { + Self::UtfError(value) + } +} + +impl fmt::Display for FFMpegError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + use FFMpegError as FE; + match self { + FE::CommandError(e) => write!(f, "Command::new - {}", e), + FE::UtfError(_) => write!(f, "Error while decoding UTF8"), + FE::ErrorMessage(msg) => write!(f, "ffmpeg error - {}", msg) + } + } +} + +pub struct FFMpeg {} + +impl FFMpeg { + pub async fn convert_to_mp3(input_path: &str, output_path: &str, bitrate: u16) -> Result<(), FFMpegError> { + let output = Command::new("ffmpeg") + .args(["-i", input_path, "-codec:a", "libmp3lame", "-b:a", "32k", output_path]) + .output() + .await?; + + if !output.status.success() { + let message = std::str::from_utf8(&output.stderr)?; + return Err(FFMpegError::ErrorMessage(message.to_string())); + } + + Ok(()) + } +} \ No newline at end of file