Skip to content

Commit

Permalink
#73: add --audio-codec and --ffmpeg-audio-encoder flags to control au…
Browse files Browse the repository at this point in the history
…dio encoder
  • Loading branch information
russelltg committed May 20, 2024
1 parent 3361522 commit dd9d3f4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
30 changes: 23 additions & 7 deletions src/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ use std::{
thread::spawn,
};

use anyhow::bail;
use anyhow::{anyhow, bail};
use ffmpeg::{
codec::{Context, Id},
decoder, encoder,
codec::{self, Context, Id},
decoder,
encoder::{self},
ffi::{av_channel_layout_describe, av_find_input_format},
filter,
format::{self, context::Input, Sample},
Expand Down Expand Up @@ -168,9 +169,23 @@ impl AudioHandle {
args: &Args,
octx: &mut format::context::Output,
) -> anyhow::Result<IncompleteAudioState> {
let audio_codec_id = octx
let audio_codec = if let Some(enc) = &args.ffmpeg_audio_encoder {
encoder::find_by_name(&enc)
.ok_or_else(|| {
anyhow!("codec {enc} specified by --ffmpeg-audio-encoder does not exist")
})?
.audio()
.unwrap()
} else {
let audio_codec_id = match args.audio_codec {
crate::AudioCodec::Auto => octx
.format()
.codec(&args.output, ffmpeg::media::Type::Audio);
.codec(&args.output, ffmpeg::media::Type::Audio),
crate::AudioCodec::Aac => Id::AAC,
crate::AudioCodec::Mp3 => Id::MP3,
crate::AudioCodec::Flac => Id::FLAC,
crate::AudioCodec::Opus => Id::OPUS,
};

if audio_codec_id == Id::None {
bail!(
Expand All @@ -179,10 +194,11 @@ impl AudioHandle {
);
}

let audio_codec = ffmpeg::encoder::find(audio_codec_id)
ffmpeg::encoder::find(audio_codec_id)
.unwrap()
.audio()
.unwrap();
.unwrap()
};

let mut ost_audio = octx.add_stream(audio_codec).unwrap();

Expand Down
25 changes: 25 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,21 @@ pub struct Args {
)]
ffmpeg_encoder: Option<String>,

#[clap(
long,
value_enum,
default_value_t,
help = "Which audio codec to use. Ignored if `--ffmpeg-audio-encoder` is supplied"
)]
audio_codec: AudioCodec,

#[clap(
long,
value_enum,
help = "Use this to force a particular audio ffmpeg encoder. By default, this is guessed from the muxer (which is guess by the file extension if --ffmpeg-muxer isn't passed)"
)]
ffmpeg_audio_encoder: Option<String>,

#[clap(
long,
help = "which pixel format to encode with. not all codecs will support all pixel formats. This should be a ffmpeg pixel format string, like nv12 or x2rgb10"
Expand Down Expand Up @@ -210,6 +225,16 @@ enum Codec {
AV1,
}

#[derive(clap::ValueEnum, Debug, Default, Clone)]
enum AudioCodec {
#[default]
Auto,
Aac,
Mp3,
Flac,
Opus,
}

#[derive(clap::ValueEnum, Debug, Clone, Default)]
enum LowPowerMode {
#[default]
Expand Down

0 comments on commit dd9d3f4

Please sign in to comment.