wip: actually set codec_opts to encoder

This commit is contained in:
Pavel 2024-05-26 22:13:30 +03:00
parent 75600a85de
commit 6adf7401a5
4 changed files with 23 additions and 5 deletions

View File

@ -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' \

View File

@ -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<P: AsRef<Path>>(
}
}
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);

View File

@ -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 = {

View File

@ -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<String>,
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)?;