Skip to content

Commit

Permalink
#81: allow passing encoder options
Browse files Browse the repository at this point in the history
  • Loading branch information
russelltg committed Aug 23, 2024
1 parent bfa0ce5 commit 680d929
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ Options:
Options to pass to the muxer. Format looks like key=val,key2=val2
--ffmpeg-encoder <FFMPEG_ENCODER>
Use this to force a particular ffmpeg encoder. Generally, this is not necessary and the combo of --codec and --hw can get you to where you need to be
--ffmpeg-encoder-options <FFMPEG_ENCODER_OPTIONS>
Options to pass to the encoder. Format looks like key=val,key2=val2
--audio-codec <AUDIO_CODEC>
Which audio codec to use. Ignored if `--ffmpeg-audio-encoder` is supplied [default: auto] [possible values: auto, aac, mp3, flac, opus]
--ffmpeg-audio-encoder <FFMPEG_AUDIO_ENCODER>
Expand All @@ -179,6 +181,8 @@ Options:
copy every frame, not just unique frames. This can be helpful to get a non-variable framerate video, but is generally discouraged as it uses much more resources. Useful for testing
--gop-size <GOP_SIZE>
GOP (group of pictures) size
--generate-completions <COMPLETIONS_GENERATOR>
print completions for the specified shell to stdout [possible values: bash, elvish, fish, powershell, zsh]
-h, --help
Print help
-V, --version
Expand Down
38 changes: 27 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ pub struct Args {
)]
ffmpeg_encoder: Option<String>,

#[clap(
long,
value_enum,
help = "Options to pass to the encoder. Format looks like key=val,key2=val2"
)]
ffmpeg_encoder_options: Option<String>,

#[clap(
long,
value_enum,
Expand Down Expand Up @@ -1219,7 +1226,7 @@ struct EncState {
audio: Option<AudioHandle>,
}

#[derive(Copy, Clone)]
#[derive(Copy, Clone, Debug)]
enum EncodePixelFormat {
Vaapi(Pixel),
Sw(Pixel),
Expand Down Expand Up @@ -1389,6 +1396,7 @@ impl EncState {
Some(fmt) => bail!("Encoder does not support pixel format {fmt:?}"),
}
};
info!("encode pixel format is {enc_pixfmt:?}");

let codec_id = encoder.id();
if unsafe {
Expand Down Expand Up @@ -1455,17 +1463,24 @@ impl EncState {
&mut frames_yuv,
)?;

let passed_enc_options = match &args.ffmpeg_encoder_options {
Some(enc_options) => parse_dict(enc_options).unwrap(),
None => dict!(),
};

let enc_video = if args.hw {
let low_power_opts = dict! {
"low_power" => "1"
let low_power_opts = {
let mut d = passed_enc_options.clone();
d.set("low_power", "1");
d
};

let regular_opts = if codec_id == codec::Id::H264 {
dict! {
"level" => "30"
}
let mut d = passed_enc_options.clone();
d.set("level", "30");
d
} else {
dict! {}
passed_enc_options.clone()
};

match args.low_power {
Expand All @@ -1490,10 +1505,11 @@ impl EncState {
LowPowerMode::Off => enc.open_with(regular_opts)?,
}
} else {
enc.open_with(dict! {
"preset" => "ultrafast"
})
.unwrap()
let mut enc_options = passed_enc_options.clone();
if enc_options.get("preset").is_none() {
enc_options.set("preset", "ultrafast");
}
enc.open_with(enc_options).unwrap()
};

let mut ost_video = octx.add_stream(encoder).unwrap();
Expand Down

0 comments on commit 680d929

Please sign in to comment.