将ffmpeg管道重定向到子进程

Redirect ffmpeg pipe to child process

本文关键字:子进程 重定向 管道 ffmpeg      更新时间:2023-10-16

如何将ffmpeg管道从进程重定向到子进程stdin?

我想在cmd:中实现与管道相同的功能

ffmpeg -i test.mov pipe:1 | vlc -

我试过了:

avio_open("pipe:1"); // ffmpeg open pipe to STD_OUTPUT_HANDLE.
// lots of code
STARTUPINFO si;
PROCESS_INFORMATION pi;
SECURITY_ATTRIBUTES saAttr = {0}; 
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); 
saAttr.bInheritHandle = TRUE; 
saAttr.lpSecurityDescriptor = NULL; 
CreatePipe(&hReadPipe, &hWritePipe, &saAttr, 0);
SetStdHandle(STD_OUTPUT_HANDLE, hWritePipe);
ZeroMemory(&si, sizeof(si));
ZeroMemory(&pi, sizeof(pi));
si.cb = sizeof(si);
si.dwFlags   = STARTF_USESTDHANDLES;
si.hStdInput = hReadPipe;   
CreateProcess(NULL,   // No module name (use command line)
    L"C:\Program Files (x86)\VideoLAN\VLC\vlc -vv --demux ffmpeg -",        // Command line
    NULL,           // Process handle not inheritable
    NULL,           // Thread handle not inheritable
    TRUE,          // Set handle inheritance to FALSE
    0,              // No creation flags
    NULL,           // Use parent's environment block
    NULL,           // Use parent's starting directory 
    &si,            // Pointer to STARTUPINFO structure
    &pi )           // Pointer to PROCESS_INFORMATION structure
// start ffmpeg write to file.

但我真的不知道自己在做什么。

是否有任何GetStdHandle不能正常打印到控制台?

您可以尝试创建一个管道:

  1. 调用CreatePipe()创建一个读句柄和一个写句柄

  2. 调用SetStdHandle()使管道的写句柄成为新的stdout

  3. 将管道的读取句柄指定为CreateProcess()的hStdInput

更新:

如果您的应用程序使用printf()打印到STD输出控制台,您可能需要破解stdout结构并替换那里的句柄。在任何情况下,尝试进入一个没有正确重定向的printf()调用,看看它最终使用了哪个句柄。