From 78be6e6a76ce5675ad4d327e177e8b9117a313e7 Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Sun, 6 Oct 2024 17:46:10 -0400 Subject: [PATCH] Added FFmpegController class --- MediaProcessor/src/FFmpegController.cpp | 124 ++++++++++++++++++++ MediaProcessor/src/FFmpegController.h | 145 ++++++++++++++++++++++++ 2 files changed, 269 insertions(+) create mode 100644 MediaProcessor/src/FFmpegController.cpp create mode 100644 MediaProcessor/src/FFmpegController.h diff --git a/MediaProcessor/src/FFmpegController.cpp b/MediaProcessor/src/FFmpegController.cpp new file mode 100644 index 0000000..baee8cb --- /dev/null +++ b/MediaProcessor/src/FFmpegController.cpp @@ -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 diff --git a/MediaProcessor/src/FFmpegController.h b/MediaProcessor/src/FFmpegController.h new file mode 100644 index 0000000..fd24dcd --- /dev/null +++ b/MediaProcessor/src/FFmpegController.h @@ -0,0 +1,145 @@ +#ifndef FFMPEGCONTROLLER_H +#define FFMPEGCONTROLLER_H + +#include +#include +#include + +#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 */