Skip to content

Commit feed935

Browse files
committed
Implement a bitrate option for the video encoder class
You can just pass this option in a codec profile and it will be set up in the codec context correctly.
1 parent 054678c commit feed935

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/AvTranscoder/encoder/VideoEncoder.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ extern "C" {
99
#include <stdexcept>
1010
#include <cstdlib>
1111

12+
13+
template <class T>
14+
bool from_string(T &t,
15+
const std::string &s,
16+
std::ios_base & (*f)(std::ios_base&))
17+
{
18+
std::istringstream iss(s);
19+
return !(iss>>f>>t).fail();
20+
}
21+
1222
namespace avtranscoder
1323
{
1424

@@ -59,7 +69,7 @@ void VideoEncoder::setupEncoder(const ProfileLoader::Profile& profile)
5969
(*it).first == constants::avProfileType || (*it).first == constants::avProfileCodec ||
6070
(*it).first == constants::avProfileWidth || (*it).first == constants::avProfileHeight ||
6171
(*it).first == constants::avProfilePixelFormat || (*it).first == constants::avProfileFrameRate ||
62-
(*it).first == constants::avProfileThreads)
72+
(*it).first == constants::avProfileThreads || (*it).first == constants::avProfileBitRate)
6373
continue;
6474

6575
try
@@ -80,6 +90,17 @@ void VideoEncoder::setupEncoder(const ProfileLoader::Profile& profile)
8090
encoderFlags |= CODEC_FLAG_PSNR;
8191
}
8292
_codec.getAVCodecContext().flags |= encoderFlags;
93+
94+
if(profile.count(constants::avProfileBitRate)) {
95+
96+
int64_t bitRate = 0;
97+
bool bconvert = from_string<int64_t>(bitRate, profile.at(constants::avProfileBitRate), std::dec);
98+
99+
if (bconvert) {
100+
_codec.getAVCodecContext().bit_rate = bitRate;
101+
}
102+
}
103+
83104
_codec.openCodec();
84105

85106
// after open encoder, set specific encoder options
@@ -89,7 +110,7 @@ void VideoEncoder::setupEncoder(const ProfileLoader::Profile& profile)
89110
(*it).first == constants::avProfileType || (*it).first == constants::avProfileCodec ||
90111
(*it).first == constants::avProfileWidth || (*it).first == constants::avProfileHeight ||
91112
(*it).first == constants::avProfilePixelFormat || (*it).first == constants::avProfileFrameRate ||
92-
(*it).first == constants::avProfileThreads)
113+
(*it).first == constants::avProfileThreads || (*it).first == constants::avProfileBitRate)
93114
continue;
94115

95116
try

src/AvTranscoder/profile/ProfileLoader.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const std::string avProfileCodec = "codec";
2424
const std::string avProfilePixelFormat = "pix_fmt";
2525
const std::string avProfileSampleFormat = "sample_fmt";
2626
const std::string avProfileFrameRate = "r";
27+
const std::string avProfileBitRate = "bit_rate";
2728
const std::string avProfileWidth = "width";
2829
const std::string avProfileHeight = "height";
2930
const std::string avProfileSampleRate = "ar";

0 commit comments

Comments
 (0)