Skip to content

Commit

Permalink
Added FFmpegController class
Browse files Browse the repository at this point in the history
  • Loading branch information
andcscott committed Oct 6, 2024
1 parent bf8dcb7 commit 78be6e6
Show file tree
Hide file tree
Showing 2 changed files with 269 additions and 0 deletions.
124 changes: 124 additions & 0 deletions MediaProcessor/src/FFmpegController.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#include "FFmpegController.h"

namespace MediaProcessor {

FFmpegController::FFmpegController()
: m_ffmpegGlobalConfig(), m_ffmpegAudioConfig(), m_ffmpegVideoConfig() {}

FFmpegController::FFmpegController(FFmpegGlobalConfig &globalConfig, FFmpegAudioConfig &audioConfig,
FFmpegVideoConfig &videoConfig)
: m_ffmpegGlobalConfig(globalConfig),
m_ffmpegAudioConfig(audioConfig),
m_ffmpegVideoConfig(videoConfig) {}

/* FFmpegGlobalConfig getters & setters */

void FFmpegController::setFFmpegPath(fs::path path) {
m_ffmpegGlobalConfig.m_binaryPath = path;
}

void FFmpegController::setOverwrite(bool overwrite) {
m_ffmpegGlobalConfig.m_overwrite = overwrite;
}

void FFmpegController::setShortest(bool useShortest) {
m_ffmpegGlobalConfig.m_useShortest = useShortest;
}

void FFmpegController::setStrictness(std::string &mode) {
m_ffmpegGlobalConfig.m_strictness = mode;
}

fs::path FFmpegController::getFFmpegPath() {
return m_ffmpegGlobalConfig.m_binaryPath;
}

bool FFmpegController::getOverwrite() {
return m_ffmpegGlobalConfig.m_overwrite;
}

bool FFmpegController::getShortest() {
return m_ffmpegGlobalConfig.m_useShortest;
}

std::string FFmpegController::getStrictness() {
return m_ffmpegGlobalConfig.m_strictness;
}

/* FFmpegAudioConfig getters & setters */

void FFmpegController::setAudioSampleRate(unsigned rate) {
m_ffmpegAudioConfig.m_sampleRate = rate;
}

void FFmpegController::setNumAudioChannels(unsigned channels) {
m_ffmpegAudioConfig.m_numChannels = channels;
}

void FFmpegController::setAudioStartTime(double time) {
m_ffmpegAudioConfig.m_startTime = time;
}

void FFmpegController::setAudioDuration(double duration) {
m_ffmpegAudioConfig.m_duration = duration;
}

void FFmpegController::setAudioMapping(std::string &map) {
m_ffmpegAudioConfig.m_mapping = map;
}

void FFmpegController::setAudioCodec(std::string &codec) {
m_ffmpegAudioConfig.m_codec = codec;
}

void FFmpegController::setFilterComplex(std::string &filtergraph) {
m_ffmpegAudioConfig.m_filtergraph = filtergraph;
}

unsigned FFmpegController::getAudioSampleRate() {
return m_ffmpegAudioConfig.m_sampleRate;
}

unsigned FFmpegController::getNumAudioChannels() {
return m_ffmpegAudioConfig.m_numChannels;
}

double FFmpegController::getAudioStartTime() {
return m_ffmpegAudioConfig.m_startTime;
}

double FFmpegController::getAudioDuration() {
return m_ffmpegAudioConfig.m_duration;
}

std::string FFmpegController::getAudioMapping() {
return m_ffmpegAudioConfig.m_mapping;
}

std::string FFmpegController::getAudioCodec() {
return m_ffmpegAudioConfig.m_codec;
}

std::string FFmpegController::getFilterComplex() {
return m_ffmpegAudioConfig.m_filtergraph;
}

/* FFmpegVideoConfig getters & setters */

void FFmpegController::setVideoCodec(std::string &codec) {
m_ffmpegVideoConfig.m_codec = codec;
}

void FFmpegController::setVideoMap(std::string &map) {
m_ffmpegVideoConfig.m_mapping = map;
}

std::string FFmpegController::getVideoCodec() {
return m_ffmpegVideoConfig.m_codec;
}

std::string FFmpegController::getVideoMap() {
return m_ffmpegVideoConfig.m_mapping;
}

} // namespace MediaProcessor
145 changes: 145 additions & 0 deletions MediaProcessor/src/FFmpegController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
#ifndef FFMPEGCONTROLLER_H
#define FFMPEGCONTROLLER_H

#include <filesystem>
#include <string>
#include <vector>

#include "ConfigManager.h"

namespace fs = std::filesystem;

namespace MediaProcessor {

struct FFmpegGlobalConfig {
fs::path m_binaryPath;
bool m_overwrite;
bool m_useShortest;
std::string m_strictness;

FFmpegGlobalConfig()
: m_binaryPath(ConfigManager::getInstance().getFFmpegPath()),
m_overwrite(true),
m_useShortest(true),
m_strictness("experimental") {}
};

struct FFmpegAudioConfig {
unsigned m_sampleRate;
unsigned m_numChannels;
double m_startTime;
double m_duration;
std::string m_mapping;
std::string m_codec;
std::string m_filtergraph;

FFmpegAudioConfig()
: m_sampleRate(48000),
m_numChannels(1),
m_startTime(0.0),
m_duration(0.0),
m_mapping("1:a:0"),
m_codec("pcm_s16le") {}
};

struct FFmpegVideoConfig {
std::string m_codec;
std::string m_mapping;

FFmpegVideoConfig() : m_codec("copy"), m_mapping("0:v:0") {}
};

class FFmpegController {
public:
FFmpegController();

FFmpegController(FFmpegGlobalConfig &globalConfig, FFmpegAudioConfig &audioConfig,
FFmpegVideoConfig &videoConfig);

// Set new path for the FFmpeg binary
void setFFmpegPath(fs::path path);

// Allow overwriting destination files
void setOverwrite(bool overwrite);

// Set FFmpeg to finish encoding when the shortest stream ends
void setShortest(bool shortest);

// Set strictness to standards adherence - ex. "experimental"
void setStrictness(std::string &mode);

// Get path for FFmpeg binary
fs::path getFFmpegPath();

// Get whether overwrite is allowed
bool getOverwrite();

// Get whether "-shortest" flag is set
bool getShortest();

// Get current strictness
std::string getStrictness();

// Set audio sample rate - ex. 48000
void setAudioSampleRate(unsigned rate);

// Set number of audio channels - ex. 2
void setNumAudioChannels(unsigned channels);

// Set time in seconds to start chunking the audio
void setAudioStartTime(double time);

// Set duration in seconds for audio chunking
void setAudioDuration(double duration);

// Set audio mapping
void setAudioMapping(std::string &map);

// Set audio codec - ex. "aac"
void setAudioCodec(std::string &audioCodec);

// Set a complex filtergraph
void setFilterComplex(std::string &filtergraph);

// Get audio sample rate
unsigned getAudioSampleRate();

// Get number of audio channels
unsigned getNumAudioChannels();

// Get audio start time in seconds
double getAudioStartTime();

// Get audio duration in seconds
double getAudioDuration();

// Get audio mapping
std::string getAudioMapping();

// Get audio codec
std::string getAudioCodec();

// Set a complex filtergraph
std::string getFilterComplex();

// Set video codec - ex. "copy"
void setVideoCodec(std::string &videoCodec);

// Set video mapping
void setVideoMap(std::string &map);

// Set video codec - ex. "copy"
std::string getVideoCodec();

// Set video mapping
std::string getVideoMap();

private:
FFmpegGlobalConfig m_ffmpegGlobalConfig;
FFmpegAudioConfig m_ffmpegAudioConfig;
FFmpegVideoConfig m_ffmpegVideoConfig;
};

} // namespace MediaProcessor

#endif /* FFMPEGCONTROLLER_H */

0 comments on commit 78be6e6

Please sign in to comment.