1

I have a program called capture that reads the webcam data and output to the avconv program.

./capture | avconv -f mpegts udp://10.1.62.252:5050

Now I have to output to avconv inside my C program.

So, instead of output to the stoud:

fwrite(p, size, 1, stdout);

I need to do do something like that:

system("stdout | avconv -f mpegts udp://10.1.62.252:5050");

How can I do that?

user3068649
  • 411
  • 3
  • 13
  • You might want to learn about the [`popen`](http://man7.org/linux/man-pages/man3/popen.3.html) function. – Some programmer dude Jun 25 '15 at 19:03
  • What do you want to do with the output generated by `avconv`? – jxh Jun 25 '15 at 19:13
  • avconv encapsulate to h264. Best regards. There are missing flags that i removed to simplify. – user3068649 Jun 25 '15 at 19:15
  • Note that you're not actually asking out redirecting your `stdout`. Rather, you're asking how to redirect the `stdin` of an external program. To that question, there are already answers: [1](http://stackoverflow.com/questions/20187734), [2](http://stackoverflow.com/questions/43116) (stdout, but answers apply) – Jonathon Reinhart Jun 25 '15 at 19:18
  • Read [Advanced Linux Programming](http://advancedlinuxprogramming.com/); it has *several chapters* to answer your questions – Basile Starynkevitch Jun 25 '15 at 19:25
  • What I meant was if `avconv` sends any output to its `stdout` or `stderr`, what do you want to do with it? – jxh Jun 25 '15 at 19:41

1 Answers1

4

You can use popen() for this purpose.

FILE *f = popen("avconv -f mpegts udp://10.1.62.252:5050","w");

then use fwrite() to write.

PS: Actually, this method is not redirecting stdout of your c program but it is using pipe stream concept to provide input to avconv from your code.

Milan Patel
  • 422
  • 4
  • 12
  • 1
    Since this is linux, and the data is binary, this works out fine. But in the general case, `popen` does not support a binary mode. Also, note that you use `pclose` to close the file, not `fclose`. – jxh Jun 25 '15 at 19:21