diff --git a/src/audio.rs b/src/audio.rs index a136314..ed42f77 100644 --- a/src/audio.rs +++ b/src/audio.rs @@ -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}, @@ -168,9 +169,23 @@ impl AudioHandle { args: &Args, octx: &mut format::context::Output, ) -> anyhow::Result { - 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!( @@ -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(); diff --git a/src/main.rs b/src/main.rs index 375860a..fb0291e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -165,6 +165,21 @@ pub struct Args { )] ffmpeg_encoder: Option, + #[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, + #[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" @@ -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]