From 30611a63a7e339e63f05acb263a41307255040f1 Mon Sep 17 00:00:00 2001 From: Kostya Shishkov Date: Thu, 4 May 2023 18:22:39 +0200 Subject: [PATCH] parse bitrate in a more flexible way --- src/main.rs | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 409c677..a2889a8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -50,6 +50,38 @@ fn print_options(name: &str, options: &[NAOptionDefinition]) { } } +fn parse_bitrate(strval: &str) -> Result { + let mut val = 0; + let mut has_suffix = false; + for ch in strval.chars() { + match ch { + _ if has_suffix => return Err(()), + '0'..='9' => { + if val >= std::u32::MAX / 100 { + return Err(()); + } + val = val * 10 + ch.to_digit(10).unwrap_or(0); + }, + 'k' | 'K' => { + if val >= std::u32::MAX / 1000 { + return Err(()); + } + val *= 1000; + has_suffix = true; + }, + 'm' | 'M' => { + if val >= std::u32::MAX / 1000000 { + return Err(()); + } + val *= 1000000; + has_suffix = true; + }, + _ => return Err(()), + }; + } + Ok(val) +} + struct OptionArgs { name: String, value: Option, @@ -358,7 +390,7 @@ impl Transcoder { } },*/ "bitrate" => { - let ret = oval[1].parse::(); + let ret = parse_bitrate(oval[1]); if let Ok(val) = ret { ostr.enc_params.bitrate = val; } else { -- 2.39.5