From 0699795c6f2e81d7c1594f9815dde11468e9c791 Mon Sep 17 00:00:00 2001 From: itsmattkc Date: Fri, 4 Dec 2020 01:01:50 +1100 Subject: [PATCH] export: implemented audio bit rate setting The UI for this isn't perfect, I just wrote this in because I needed it. But the backend is solid. --- app/codec/encoder.cpp | 3 ++- app/codec/encoder.h | 11 +++++++++++ app/codec/ffmpeg/ffmpegencoder.cpp | 17 ++++++++++++----- app/dialog/export/export.cpp | 2 ++ app/dialog/export/exportaudiotab.cpp | 11 +++++++++++ app/dialog/export/exportaudiotab.h | 7 +++++++ 6 files changed, 45 insertions(+), 6 deletions(-) diff --git a/app/codec/encoder.cpp b/app/codec/encoder.cpp index fe7204c369..c4d3ebd108 100644 --- a/app/codec/encoder.cpp +++ b/app/codec/encoder.cpp @@ -48,7 +48,8 @@ EncodingParams::EncodingParams() : video_max_bit_rate_(0), video_buffer_size_(0), video_threads_(0), - audio_enabled_(false) + audio_enabled_(false), + audio_bit_rate_(0) { } diff --git a/app/codec/encoder.h b/app/codec/encoder.h index 24ac3a84c3..f34c618d16 100644 --- a/app/codec/encoder.h +++ b/app/codec/encoder.h @@ -69,6 +69,16 @@ class EncodingParams { const ExportCodec::Codec &audio_codec() const; const AudioParams& audio_params() const; + const int64_t& audio_bit_rate() const + { + return audio_bit_rate_; + } + + void set_audio_bit_rate(const int64_t& b) + { + audio_bit_rate_ = b; + } + const rational& GetExportLength() const; void SetExportLength(const rational& GetExportLength); @@ -90,6 +100,7 @@ class EncodingParams { bool audio_enabled_; ExportCodec::Codec audio_codec_; AudioParams audio_params_; + int64_t audio_bit_rate_; rational export_length_; diff --git a/app/codec/ffmpeg/ffmpegencoder.cpp b/app/codec/ffmpeg/ffmpegencoder.cpp index 7b28558e14..bc38958c61 100644 --- a/app/codec/ffmpeg/ffmpegencoder.cpp +++ b/app/codec/ffmpeg/ffmpegencoder.cpp @@ -500,7 +500,7 @@ bool FFmpegEncoder::InitializeStream(AVMediaType type, AVStream** stream_ptr, AV if (codec_id == AV_CODEC_ID_H264) { // For some reason, FFmpeg doesn't set libx264's bff flag so we have to do it ourselves - av_opt_set(video_codec_ctx_->priv_data, "x264opts", "bff=1", AV_OPT_SEARCH_CHILDREN); + av_opt_set(codec_ctx->priv_data, "x264opts", "bff=1", AV_OPT_SEARCH_CHILDREN); } } } @@ -510,28 +510,35 @@ bool FFmpegEncoder::InitializeStream(AVMediaType type, AVStream** stream_ptr, AV QHash::const_iterator i; for (i=params().video_opts().begin();i!=params().video_opts().end();i++) { - av_opt_set(video_codec_ctx_->priv_data, i.key().toUtf8(), i.value().toUtf8(), AV_OPT_SEARCH_CHILDREN); + av_opt_set(codec_ctx->priv_data, i.key().toUtf8(), i.value().toUtf8(), AV_OPT_SEARCH_CHILDREN); } if (params().video_bit_rate() > 0) { - video_codec_ctx_->bit_rate = params().video_bit_rate(); + codec_ctx->bit_rate = params().video_bit_rate(); } if (params().video_max_bit_rate() > 0) { - video_codec_ctx_->rc_max_rate = params().video_max_bit_rate(); + codec_ctx->rc_max_rate = params().video_max_bit_rate(); } if (params().video_buffer_size() > 0) { - video_codec_ctx_->rc_buffer_size = static_cast(params().video_buffer_size()); + codec_ctx->rc_buffer_size = static_cast(params().video_buffer_size()); } } } else { + + // Assume audio stream codec_ctx->sample_rate = params().audio_params().sample_rate(); codec_ctx->channel_layout = params().audio_params().channel_layout(); codec_ctx->channels = av_get_channel_layout_nb_channels(codec_ctx->channel_layout); codec_ctx->sample_fmt = encoder->sample_fmts[0]; codec_ctx->time_base = {1, codec_ctx->sample_rate}; + + if (params().audio_bit_rate() > 0) { + codec_ctx->bit_rate = params().audio_bit_rate(); + } + } if (!SetupCodecContext(stream, codec_ctx, encoder)) { diff --git a/app/dialog/export/export.cpp b/app/dialog/export/export.cpp index 54fece4e82..b122e8985f 100644 --- a/app/dialog/export/export.cpp +++ b/app/dialog/export/export.cpp @@ -490,6 +490,8 @@ ExportParams ExportDialog::GenerateParams() const if (audio_enabled_->isChecked()) { ExportCodec::Codec audio_codec = static_cast(audio_tab_->codec_combobox()->currentData().toInt()); params.EnableAudio(audio_render_params, audio_codec); + + params.set_audio_bit_rate(audio_tab_->bit_rate_slider()->GetValue() * 1000); } return params; diff --git a/app/dialog/export/exportaudiotab.cpp b/app/dialog/export/exportaudiotab.cpp index 781183c190..72896d55b8 100644 --- a/app/dialog/export/exportaudiotab.cpp +++ b/app/dialog/export/exportaudiotab.cpp @@ -61,6 +61,17 @@ ExportAudioTab::ExportAudioTab(QWidget* parent) : layout->addWidget(new QLabel(tr("Format:")), row, 0); layout->addWidget(new QComboBox(), row, 1); + row++; + + layout->addWidget(new QLabel(tr("Bit Rate:")), row, 0); + + bit_rate_slider_ = new IntegerSlider(); + bit_rate_slider_->SetMinimum(32); + bit_rate_slider_->SetMaximum(320); + bit_rate_slider_->SetValue(256); + bit_rate_slider_->SetFormat(tr("%1 kbps")); + layout->addWidget(bit_rate_slider_, row, 1); + outer_layout->addStretch(); } diff --git a/app/dialog/export/exportaudiotab.h b/app/dialog/export/exportaudiotab.h index 5ef06d2a28..142ea5e250 100644 --- a/app/dialog/export/exportaudiotab.h +++ b/app/dialog/export/exportaudiotab.h @@ -25,6 +25,7 @@ #include #include "common/define.h" +#include "widget/slider/integerslider.h" #include "widget/standardcombos/standardcombos.h" namespace olive { @@ -50,10 +51,16 @@ class ExportAudioTab : public QWidget return channel_layout_combobox_; } + IntegerSlider* bit_rate_slider() const + { + return bit_rate_slider_; + } + private: QComboBox* codec_combobox_; SampleRateComboBox* sample_rate_combobox_; ChannelLayoutComboBox* channel_layout_combobox_; + IntegerSlider* bit_rate_slider_; };