Skip to content

Commit

Permalink
nvm change codec again yay!!!
Browse files Browse the repository at this point in the history
  • Loading branch information
maxnut committed Oct 14, 2024
1 parent 530c5cd commit 9b13343
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
4 changes: 2 additions & 2 deletions include/recorder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ class FFMPEG_API_DLL Recorder {
* This function iterates through all available codecs in FFmpeg and
* returns a sorted vector of codec names.
*
* @return A map of representing the names and ids of available codecs.
* @return A vector representing the names of available codecs.
*/
std::unordered_map<std::string, int> getAvailableCodecs();
std::vector<std::string> getAvailableCodecs();

private:
AVFormatContext* m_formatContext = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion include/render_settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ enum class HardwareAccelerationType : int {
struct RenderSettings {
HardwareAccelerationType m_hardwareAccelerationType = HardwareAccelerationType::NONE;
PixelFormat m_pixelFormat = PixelFormat::RGB0;
int m_codecId = 27;
std::string m_codec = "h264";
int64_t m_bitrate = 30000000;
uint32_t m_width = 1920;
uint32_t m_height = 1080;
Expand Down
26 changes: 18 additions & 8 deletions src/recorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,28 @@ extern "C" {

namespace ffmpeg {

std::unordered_map<std::string, int> Recorder::getAvailableCodecs() {
std::unordered_map<std::string, int> map;
std::vector<std::string> Recorder::getAvailableCodecs() {
std::vector<std::string> vec;

void* iter = nullptr;
const AVCodec * codec;

while ((codec = av_codec_iterate(&iter))) {
if(codec->type == AVMEDIA_TYPE_VIDEO)
map.insert({codec->name, (int)codec->id});
if(codec->type == AVMEDIA_TYPE_VIDEO && avcodec_find_encoder_by_name(codec->name) != nullptr)
vec.push_back(codec->name);
}

return map;
return vec;
}

const AVCodec* getCodecByName(const std::string& name) {
void* iter = nullptr;
const AVCodec * codec;
while ((codec = av_codec_iterate(&iter))) {
if(codec->type == AVMEDIA_TYPE_VIDEO && std::string(codec->name) == name)
return codec;
}
return nullptr;
}

bool Recorder::init(const RenderSettings& settings) {
Expand All @@ -33,7 +43,7 @@ bool Recorder::init(const RenderSettings& settings) {
return false;
}

m_codec = avcodec_find_encoder((AVCodecID)settings.m_codecId);
m_codec = getCodecByName(settings.m_codec);
if (!m_codec) {
geode::log::error("Could not find encoder.");
return false;
Expand Down Expand Up @@ -77,11 +87,11 @@ bool Recorder::init(const RenderSettings& settings) {
}

if(m_codecContext->pix_fmt == AV_PIX_FMT_NONE) {
geode::log::info("Codec {} does not support pixel format, defaulting to codec's format", settings.m_codecId);
geode::log::info("Codec {} does not support pixel format, defaulting to codec's format", settings.m_codec);
m_codecContext->pix_fmt = m_codec->pix_fmts[0];
}
else
geode::log::info("Codec {} supports pixel format.", settings.m_codecId);
geode::log::info("Codec {} supports pixel format.", settings.m_codec);

if (avcodec_open2(m_codecContext, m_codec, NULL) < 0) {
geode::log::error("Could not open codec.");
Expand Down

0 comments on commit 9b13343

Please sign in to comment.