![]() |
| |||||||
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 |
| Registered User Join Date: Jun 2009
Posts: 9
| The basic part worked fine initially, using system(), I would call ffmpeg, have it convert the file, then use system again to call mtp-sendtr. The problem is, I need to specify the length of the video in seconds to mtp-sendtr. I can check the length myself and input it manually, and that works, but since ffmpeg displays the length as it converts, it seems I could use this data somehow. First, I tried simply piping the output from ffmpeg to a text file like "ffmpeg blah blah blah > output.txt" but that doesn't work, the file ends up being blank. So I did this (I've replaced ffmpeg with ls just to simplify things): Code: #include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main(int argc, char *argv)
{
int fd; /*file descriptor to the file we will redirect ls's output*/
if((fd = open("dirlist.txt", O_RDWR | O_CREAT))==-1){ /*open the file */
perror("open");
return 1;
}
dup2(fd,STDOUT_FILENO); /*copy the file descriptor fd into standard output*/
dup2(fd,STDERR_FILENO); /* same, for the standard error */
close(fd); /* close the file descriptor as we don't need it more */
/*execl ls */
execl("/bin/ls", "ls", (char *) 0);
printf("All done.\n");
return 0;
}
I have a feeling I'm going about this in a convoluted way, and I'd LOVE to have someone explain to me what I need to do to make this work. |
| IanKoro is offline | |
| | #2 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,946
| Read up on popen(), it will be easier for what you are trying to do, vis tapping the output from ffmpeg. Yes, no matter how you slice it using C to do this is convoluted. It would be better done in a shell script, but if you are more comfortable programming in C than for bash you might as well try.
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS |
| MK27 is online now | |
| | #3 |
| Registered User Join Date: Dec 2006
Posts: 1,780
| You need to fork somewhere. execl() replaces the process image, so it will never return. You need to first create pipes (using pipe()) for I/O, and then fork. In the child process, dup2() stdin and stdout to 2 ends of the pipes, close the unneeded ends, and call exec(). In the parent, close the unneeded ends (should be the opposite ends to the ones closed by the child), and then you can read/write from/to the pipes. I have recently written a C++ wrapper for this. You may want to have a look. Code review for a class that spawns a new process and allows IPC (final code on the last post) The AppInstance::start() function in particular. OR, if you only need to read OR write to the child process (as opposed to both read AND write), you can just use popen(). As for shell redirection not working, maybe it writes to stderr? (People do that because stderr is usually not buffered, whereas stdout usually is) |
| cyberfish is offline | |
| | #4 | |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,946
| Quote:
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS | |
| MK27 is online now | |
| | #5 |
| Registered User Join Date: Dec 2006
Posts: 1,780
| Life would be so good if popen supports "rw" mode... But yes, for this application, popen() is easier, since you presumably don't need to write to ffmpeg. |
| cyberfish is offline | |
| | #6 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| > Life would be so good if popen supports "rw" mode... IIRC, some versions do (or at least I recall someone saying they do)
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
| | #7 |
| Registered User Join Date: Jun 2009
Posts: 9
| Ahh... well, I figured out how to fork it, thank you very much... I found an example that cleared things up tremendously. I know C is a convoluted way of doing this, but I'm mostly trying to teach myself stuff. |
| IanKoro is offline | |
![]() |
| Tags |
| dup2, execl, fork, redirect |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| fopen returning "Bad File Pointer" | jprukop | C Programming | 4 | 04-17-2009 03:01 PM |
| Problems passing a file pointer to functions | smitchell | C Programming | 4 | 09-30-2008 02:29 PM |
| Problems with returning a char* to fprint. | qubit67 | C Programming | 5 | 08-08-2007 11:28 PM |
| Why does this program not work on my laptop?! | Eddie K | C Programming | 1 | 03-11-2006 04:34 PM |
| Dikumud | maxorator | C++ Programming | 1 | 10-01-2005 06:39 AM |