0
votes

In my main process, i create a ffmpeg child process using CreateProcess(...). I need to track the status of converting progress to update a progress bar. To do it, I read text from ffmpeg output and extract progress status from it.

I make a sample programm like this:

HANDLE rPipe, wPipe;
CreatePipe(&rPipe,&wPipe,&secattr,0);

STARTUPINFO sInfo; 
ZeroMemory(&sInfo,sizeof(sInfo));
PROCESS_INFORMATION pInfo; 
ZeroMemory(&pInfo,sizeof(pInfo));
sInfo.cb=sizeof(sInfo);
sInfo.dwFlags=STARTF_USESTDHANDLES;
sInfo.hStdInput=NULL; 
sInfo.hStdOutput=wPipe; 
sInfo.hStdError=wPipe;

// pStr contain ffmpeg command
CreateProcess(0,(LPTSTR)pStr,0,0,TRUE,NORMAL_PRIORITY_CLASS|CREATE_NO_WINDOW,0,0,&sInfo,&pInfo);
CloseHandle(wPipe);

BOOL ok;
do
{
    memset(buf,0,bufsize);
    ok=::ReadFile(rPipe,buf,100,&reDword,0);
    result += buf;            
}while(ok);

But I couldnt get "result" interactively updated. My app is held during conversion, and "result" string update only after ffmpeg's process finish.

How can I have my main process and ffmpeg's run simultaneously, and interactively read from/write to ffmpeg process's output/input?

Thanks for your time!

LRs

2

2 Answers

1
votes

If the ffmpeg just uses stdout without explicitly flushing the output then it may not get sent to the calling process until it ends

Child processes that use such C run-time functions as printf() and fprintf() can behave poorly when redirected. The C run-time functions maintain separate IO buffers. When redirected, these buffers might not be flushed immediately after each IO call. As a result, the output to the redirection pipe of a printf() call or the input from a getch() call is not flushed immediately and delays, sometimes-infinite delays occur. This problem is avoided if the child process flushes the IO buffers after each call to a C run-time IO function. Only the child process can flush its C run-time IO buffers. A process can flush its C run-time IO buffers by calling the fflush() function.

http://support.microsoft.com/kb/190351

0
votes

In order of tracking the progress of your child process while it is running (and after its completion), you need to check the status of this child process. After the process was launched, check the status periodically using the following code. pi is the PROCESS_INFORMATION:

PROCESS_INFORMATION pi;

and the code:

DWORD exitCode = 0;
success = [GetExitCodeProcess][2](pi.hProcess, &exitCode);

exitCode will hold the value STILL_ACTIVE if the process is still running.

If the function succeeds, the return value of success is nonzero.