A tool to sync music with your favorite devices
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

ffmpeg: parse vbr and cbr values separately, make Codec return ffmpeg special flags if needed

Gee Sawra 94f35c7f 64dc9d46

+30 -20
+30 -20
src/ffmpeg.rs
··· 14 14 type Err = String; 15 15 16 16 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> { 17 - let br_regex = regex::Regex::new("([0-9]+)k").unwrap(); 18 17 let spl = s.split(":").collect::<Vec<&str>>(); 19 - 20 - if spl.len() != 2 { 21 - return Err("malformed bitrate format".to_owned()); 22 - } 23 - 24 18 let br_type = spl[0]; 25 19 let br_amt = spl[1]; 26 - if !br_regex.is_match(br_amt) { 27 - return Err("malformed bitrate format".to_owned()); 28 - } 29 20 30 21 match br_type { 31 - "cbr" => Ok(Self::CBR(br_amt.to_owned())), 32 - "vbr" => Ok(Self::VBR(br_amt.to_owned())), 22 + "cbr" => { 23 + let br_regex = regex::Regex::new("([0-9]+)k").unwrap(); 24 + let spl = s.split(":").collect::<Vec<&str>>(); 25 + 26 + if spl.len() != 2 { 27 + return Err("malformed bitrate format".to_owned()); 28 + } 29 + 30 + if !br_regex.is_match(br_amt) { 31 + return Err("malformed bitrate format".to_owned()); 32 + } 33 + Ok(Self::CBR(br_amt.to_owned())) 34 + } 35 + "vbr" => { 36 + // does the quality parameter make sense 37 + br_amt.parse::<usize>().map_err(|e| e.to_string())?; 38 + Ok(Self::VBR(br_amt.to_owned())) 39 + } 33 40 _ => Err("malformed bitrate format".to_owned()), 34 41 } 35 42 } ··· 108 115 Self::Opus => "opus", 109 116 } 110 117 .to_owned() 118 + } 119 + 120 + fn ffmpeg_flags(&self) -> Vec<&str> { 121 + match self { 122 + Self::AAC => vec!["-aac_pns", "0"], 123 + _ => vec![], 124 + } 111 125 } 112 126 } 113 127 ··· 223 237 224 238 pub fn convert(track: &Track, dest: PathBuf, quality: &Quality) -> Result<()> { 225 239 let mut cmd = std::process::Command::new("ffmpeg"); 226 - let cmd = cmd 240 + let res = cmd 227 241 .arg("-y") 228 242 .arg("-i") 229 243 .arg(track.file_path.clone()) ··· 249 263 .split(" ") 250 264 .collect::<Vec<&str>>(), 251 265 ) 252 - .arg(dest.display().to_string()); 253 - 254 - let res = match quality.codec { 255 - Codec::AAC => cmd.args(["-aac_pns", "0"]), 256 - _ => cmd, 257 - } 258 - .output()?; 266 + .arg(dest.display().to_string()) 267 + .args(quality.codec.ffmpeg_flags()) 268 + .output()?; 259 269 260 270 match res.status.success() { 261 271 true => Ok(()), 262 272 false => Err(anyhow!( 263 - "exit status {}: {}", 273 + "{}: {}", 264 274 res.status, 265 275 String::from_utf8(res.stderr).unwrap() 266 276 )),