std::string get_command(int previewWidth, int previewHeight) {
std::stringstream ss;
ss << "ffmpeg -r 25 -f rawvideo -pix_fmt rgba -s ";
ss << previewWidth << 'x' << previewHeight;
ss << "-i - -threads 0 -preset fast -y -pix_fmt yuv420p -crf 21 -vf vflip output.mp4";
return ss.str();
}
then at point of use:
const std::string cmd = get_command(previewWidth, previewHeight);
and call cmd.c_str()
to get the raw const char*
to pass to whatever API.
Note that this is not the fastest solution, but you are about to call an external process to do audio file manipulation (probably writing to disk), so the relatively slowness of stringstream
shouldn't matter much.
stringstream
makes it really easy to build up strings, and is very safe. It is quite slow at it compared to alternatives, but ease and safety often trumps speed.
Advanced technique:
In C++11 I will sometimes use a lambda:
const std::string cmd = [&]{
std::stringstream ss;
ss << "ffmpeg -r 25 -f rawvideo -pix_fmt rgba -s ";
ss << previewWidth << 'x' << previewHeight;
ss << "-i - -threads 0 -preset fast -y -pix_fmt yuv420p -crf 21 -vf vflip output.mp4";
ss.str();
}();
and do my string building in there rather than create helper functions, if I don't think this task deserved a helper function. The above is a useful technique to create trace messages or the like, or for simple "scratch" work before I refactor the string building operation into its own function.