begin implementing ffmpeg interface
This commit is contained in:
parent
e35f84cdc3
commit
64e08e29f8
3 changed files with 55 additions and 1 deletions
5
.gitignore
vendored
5
.gitignore
vendored
|
|
@ -1,4 +1,7 @@
|
|||
/target
|
||||
.env
|
||||
*.json
|
||||
*.txt
|
||||
*.txt
|
||||
*.m4a
|
||||
*.mp3
|
||||
*.mp4
|
||||
|
|
@ -1 +1,2 @@
|
|||
pub mod yt_dlp;
|
||||
pub mod ffmpeg;
|
||||
50
src/dl/ffmpeg.rs
Normal file
50
src/dl/ffmpeg.rs
Normal file
|
|
@ -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<std::io::Error> for FFMpegError {
|
||||
fn from(value: std::io::Error) -> Self {
|
||||
Self::CommandError(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Utf8Error> 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(())
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue