tsaarni / cpp-subprocess

popen() -like C++ library with iostream support for stdio forwarding
MIT License
88 stars 21 forks source link

Redirecting output of process to a pipe #7

Closed azertyalex closed 2 years ago

azertyalex commented 2 years ago

I create an ffmpeg process which needs to write to /tmp/virtmic, how can I redirect the output?

Bash equivalent:

ffmpeg -re -i test.webm -f s16le -ar 16000 -ac 1 - > /tmp/virtmic
azertyalex commented 2 years ago

I managed to do this with following code


void start() {
    virt_mic = new std::ofstream("/tmp/virtmic", std::ios::out | std::ios::binary);

    std::thread t([=, this]() {
      cmd = new subprocess::popen(
          "ffmpeg",
          {"-re", "-i", "-", "-f", "s16le", "-ar", "48000", "-ac", "1", "-"},
          *virt_mic);

      while (true) {
        if (!packets.empty()) {
          cmd->stdin().write(packets.front().data, packets.front().size);
          delete packets.front().data;
          packets.pop();
        }
      }
    });
    t.detach();
  };
`
tsaarni commented 2 years ago

Cool, great that you sorted it out 👍