A simple program that accepts RTMP streams and then restreams them directly to other services. I use it to record at a higher rate but stream to target services at the rate they want. It's also useful for streaming to multiple sites.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
distributary/main.cpp

45 lines
1.3 KiB

#include <fmt/core.h>
#include <nlohmann/json.hpp>
#include <fstream>
#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<string>();
const string bitrate = config["bitrate"].template get<string>();
const string send_to = config["send_to"].template get<string>();
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");
}