#include #include #include #include "dbc.hpp" #define BUF_MAX 1024 * 10 using namespace fmt; using namespace nlohmann; FILE *run_ffmpeg(const string listen_at, const string bitrate, const string send_to) { string ffmpeg_cmd = format("ffmpeg -listen 1 -i {} -bufsize 3000k -maxrate {} -flags +global_header -c:v libx264 -preset veryfast -tune zerolatency -g:v 60 -vb {} -c:a copy -f flv {}", listen_at, bitrate, bitrate, send_to); println("RUNNING: {}", ffmpeg_cmd); FILE *handle = popen(ffmpeg_cmd.c_str(), "r"); dbc::check(handle != nullptr, "failed to start ffmpeg"); return handle; } json load_config(const string file_name) { std::ifstream infile(file_name); return json::parse(infile); } int main() { FILE *handle = nullptr; char buffer[BUF_MAX]; char *res = nullptr; json config = load_config("config.json"); const string listen_at = config["listen_at"].template get(); const string bitrate = config["bitrate"].template get(); const string send_to = config["send_to"].template get(); handle = run_ffmpeg(listen_at, bitrate, send_to); do { res = fgets(buffer, BUF_MAX, handle); print("OUT: {}", res); } while(res != nullptr); int rc = pclose(handle); dbc::check(rc == 0, "error closing ffmpeg"); }