From 6adf7401a5068c7eb752fec7cdb29f2bfc885fa1 Mon Sep 17 00:00:00 2001 From: Neur0toxine Date: Sun, 26 May 2024 22:13:30 +0300 Subject: [PATCH] wip: actually set codec_opts to encoder --- README.md | 2 +- src/task.rs | 5 +++-- src/thread_pool.rs | 9 ++++++++- src/transcoder.rs | 12 +++++++++++- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 1fd8c99..f52c661 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Audio transcoder with simple HTTP API. Work in progress. # How to Use Transcoding can be done like this: -1. Use `cargo run` or [`neur0toxine/atranscoder-rpc`](https://hub.docker.com/r/neur0toxine/atranscoder-rpc/) Docker image. +1. Use [`neur0toxine/atranscoder-rpc`](https://hub.docker.com/r/neur0toxine/atranscoder-rpc/) Docker image. 2. Upload file for transcoding: ```bash curl --location 'http://localhost:8090/enqueue' \ diff --git a/src/task.rs b/src/task.rs index 84d7c10..4e486a8 100644 --- a/src/task.rs +++ b/src/task.rs @@ -95,6 +95,7 @@ impl Task { &mut octx, TranscoderParams { codec: self.params.codec, + codec_opts: self.params.codec_opts, bit_rate: self.params.bit_rate, max_bit_rate: self.params.max_bit_rate, sample_rate: self.params.sample_rate, @@ -248,10 +249,10 @@ fn upload_file>( } } -fn params_to_avdictionary(input: &str) -> Dictionary { +pub fn params_to_avdictionary(input: &str) -> Dictionary { let mut dict: Dictionary = Dictionary::new(); for pair in input.split(';') { - let mut parts = pair.split(':'); + let mut parts = pair.split('='); if let (Some(key), Some(value)) = (parts.next(), parts.next()) { dict.set(key, value); diff --git a/src/thread_pool.rs b/src/thread_pool.rs index 8a0ed42..be0c497 100644 --- a/src/thread_pool.rs +++ b/src/thread_pool.rs @@ -43,7 +43,14 @@ impl Worker { let thread = thread::spawn(move || { ffmpeg_next::init() .unwrap_or_else(|err| tracing::error!("couldn't init FFmpeg: {:?}", err)); - ffmpeg_next::util::log::set_level(Level::Quiet); + + ffmpeg_next::util::log::set_level( + if std::env::var("FFMPEG_VERBOSE").unwrap_or_default() == "1" { + Level::Trace + } else { + Level::Quiet + }, + ); loop { let task = { diff --git a/src/transcoder.rs b/src/transcoder.rs index 0dd1b77..8d819d0 100644 --- a/src/transcoder.rs +++ b/src/transcoder.rs @@ -1,9 +1,14 @@ extern crate ffmpeg_next as ffmpeg; +use std::any::Any; use std::error::Error; +use crate::task::params_to_avdictionary; use ffmpeg::{codec, filter, format, frame, media}; +use ffmpeg_next::codec::Parameters; use ffmpeg_next::error::EAGAIN; +use ffmpeg_next::Dictionary; +use tracing::log::debug; pub struct Transcoder { pub(crate) stream: usize, @@ -16,6 +21,7 @@ pub struct Transcoder { pub struct TranscoderParams { pub codec: String, + pub codec_opts: Option, pub bit_rate: usize, pub max_bit_rate: usize, pub sample_rate: i32, @@ -94,7 +100,11 @@ impl Transcoder { output.set_time_base((1, sample_rate)); let in_time_base = decoder.time_base(); - let encoder = encoder.open_as(codec)?; + let encoder = if let Some(codec_opts) = params.codec_opts { + encoder.open_as_with(codec, params_to_avdictionary(codec_opts.as_str()))? + } else { + encoder.open_as(codec)? + }; output.set_parameters(&encoder); let filter = filter_graph("anull", &decoder, &encoder)?;