我正在尝试遵循C++在Windows中的这些示例。 Python 示例 C# 示例
我有一个应用程序,它生成原始帧,应该用FFmpeg编码。原始帧通过IPC管道传输到FFmpegs STDIN。这正常工作,FFmpeg甚至显示当前可用的帧数。
问题出现在我们发送完帧后。当我关闭管道的写端口时,我希望FFmpeg能检测到并完成输出视频。但事实并非如此。FFmpeg保持打开状态,似乎在等待更多数据。
我在VisualStudio中制作了一个小测试项目。
问题出现在我们发送完帧后。当我关闭管道的写端口时,我希望FFmpeg能检测到并完成输出视频。但事实并非如此。FFmpeg保持打开状态,似乎在等待更多数据。
我在VisualStudio中制作了一个小测试项目。
#include "stdafx.h"
//// stdafx.h
//#include "targetver.h"
//#include <stdio.h>
//#include <tchar.h>
//#include <iostream>
#include "Windows.h"
#include <cstdlib>
using namespace std;
bool WritePipe(void* WritePipe, const UINT8 *const Buffer, const UINT32 Length)
{
if (WritePipe == nullptr || Buffer == nullptr || Length == 0)
{
cout << __FUNCTION__ << ": Some input is useless";
return false;
}
// Write to pipe
UINT32 BytesWritten = 0;
UINT8 newline = '\n';
bool bIsWritten = WriteFile(WritePipe, Buffer, Length, (::DWORD*)&BytesWritten, nullptr);
cout << __FUNCTION__ << " Bytes written to pipe " << BytesWritten << endl;
//bIsWritten = WriteFile(WritePipe, &newline, 1, (::DWORD*)&BytesWritten, nullptr); // Do we need this? Actually this should destroy the image.
FlushFileBuffers(WritePipe); // Do we need this?
return bIsWritten;
}
#define PIXEL 80 // must be multiple of 8. Otherwise we get warning: Bytes are not aligned
int main()
{
HANDLE PipeWriteEnd = nullptr;
HANDLE PipeReadEnd = nullptr;
{
// create us a pipe for inter process communication
SECURITY_ATTRIBUTES Attr = { sizeof(SECURITY_ATTRIBUTES), NULL, true };
if (!CreatePipe(&PipeReadEnd, &PipeWriteEnd, &Attr, 0))
{
cout << "Could not create pipes" << ::GetLastError() << endl;
system("Pause");
return 0;
}
}
// Setup the variables needed for CreateProcess
// initialize process attributes
SECURITY_ATTRIBUTES Attr;
Attr.nLength = sizeof(SECURITY_ATTRIBUTES);
Attr.lpSecurityDescriptor = NULL;
Attr.bInheritHandle = true;
// initialize process creation flags
UINT32 CreateFlags = NORMAL_PRIORITY_CLASS;
CreateFlags |= CREATE_NEW_CONSOLE;
// initialize window flags
UINT32 dwFlags = 0;
UINT16 ShowWindowFlags = SW_HIDE;
if (PipeWriteEnd != nullptr || PipeReadEnd != nullptr)
{
dwFlags |= STARTF_USESTDHANDLES;
}
// initialize startup info
STARTUPINFOA StartupInfo = {
sizeof(STARTUPINFO),
NULL, NULL, NULL,
(::DWORD)CW_USEDEFAULT,
(::DWORD)CW_USEDEFAULT,
(::DWORD)CW_USEDEFAULT,
(::DWORD)CW_USEDEFAULT,
(::DWORD)0, (::DWORD)0, (::DWORD)0,
(::DWORD)dwFlags,
ShowWindowFlags,
0, NULL,
HANDLE(PipeReadEnd),
HANDLE(nullptr),
HANDLE(nullptr)
};
LPSTR ffmpegURL = "\"PATHTOFFMPEGEXE\" -y -loglevel verbose -f rawvideo -vcodec rawvideo -framerate 1 -video_size 80x80 -pixel_format rgb24 -i - -vcodec mjpeg -framerate 1/4 -an \"OUTPUTDIRECTORY\"";
// Finally create the process
PROCESS_INFORMATION ProcInfo;
if (!CreateProcessA(NULL, ffmpegURL, &Attr, &Attr, true, (::DWORD)CreateFlags, NULL, NULL, &StartupInfo, &ProcInfo))
{
cout << "CreateProcess failed " << ::GetLastError() << endl;
}
//CloseHandle(ProcInfo.hThread);
// Create images and write to pipe
#define MYARRAYSIZE (PIXEL*PIXEL*3) // each pixel has 3 bytes
UINT8* Bitmap = new UINT8[MYARRAYSIZE];
for (INT32 outerLoopIndex = 9; outerLoopIndex >= 0; --outerLoopIndex) // frame loop
{
for (INT32 innerLoopIndex = MYARRAYSIZE - 1; innerLoopIndex >= 0; --innerLoopIndex) // create the pixels for each frame
{
Bitmap[innerLoopIndex] = (UINT8)(outerLoopIndex * 20); // some gray color
}
system("pause");
if (!WritePipe(PipeWriteEnd, Bitmap, MYARRAYSIZE))
{
cout << "Failed writing to pipe" << endl;
}
}
// Done sending images. Tell the other process. IS THIS NEEDED? HOW TO TELL FFmpeg WE ARE DONE?
//UINT8 endOfFile = 0xFF; // EOF = -1 == 1111 1111 for uint8
//if (!WritePipe(PipeWriteEnd, &endOfFile, 1))
//{
// cout << "Failed writing to pipe" << endl;
//}
//FlushFileBuffers(PipeReadEnd); // Do we need this?
delete Bitmap;
system("pause");
// clean stuff up
FlushFileBuffers(PipeWriteEnd); // Do we need this?
if (PipeWriteEnd != NULL && PipeWriteEnd != INVALID_HANDLE_VALUE)
{
CloseHandle(PipeWriteEnd);
}
// We do not want to destroy the read end of the pipe? Should not as that belongs to FFmpeg
//if (PipeReadEnd != NULL && PipeReadEnd != INVALID_HANDLE_VALUE)
//{
// ::CloseHandle(PipeReadEnd);
//}
return 0;
}
以下是FFmpeg的输出结果:
ffmpeg version 3.4.1 Copyright (c) 2000-2017 the FFmpeg developers
built with gcc 7.2.0 (GCC)
configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-bzlib --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-cuda --enable-cuvid --enable-d3d11va --enable-nvenc --enable-dxva2 --enable-avisynth --enable-libmfx
libavutil 55. 78.100 / 55. 78.100
libavcodec 57.107.100 / 57.107.100
libavformat 57. 83.100 / 57. 83.100
libavdevice 57. 10.100 / 57. 10.100
libavfilter 6.107.100 / 6.107.100
libswscale 4. 8.100 / 4. 8.100
libswresample 2. 9.100 / 2. 9.100
libpostproc 54. 7.100 / 54. 7.100
[rawvideo @ 00000221ff992120] max_analyze_duration 5000000 reached at 5000000 microseconds st:0
Input #0, rawvideo, from 'pipe:':
Duration: N/A, start: 0.000000, bitrate: 153 kb/s
Stream #0:0: Video: rawvideo, 1 reference frame (RGB[24] / 0x18424752), rgb24, 80x80, 153 kb/s, 1 fps, 1 tbr, 1 tbn, 1 tbc
Stream mapping:
Stream #0:0 -> #0:0 (rawvideo (native) -> mjpeg (native))
[graph 0 input from stream 0:0 @ 00000221ff999c20] w:80 h:80 pixfmt:rgb24 tb:1/1 fr:1/1 sar:0/1 sws_param:flags=2
[auto_scaler_0 @ 00000221ffa071a0] w:iw h:ih flags:'bicubic' interl:0
[format @ 00000221ffa04e20] auto-inserting filter 'auto_scaler_0' between the filter 'Parsed_null_0' and the filter 'format'
[swscaler @ 00000221ffa0a780] deprecated pixel format used, make sure you did set range correctly
[auto_scaler_0 @ 00000221ffa071a0] w:80 h:80 fmt:rgb24 sar:0/1 -> w:80 h:80 fmt:yuvj444p sar:0/1 flags:0x4
Output #0, mp4, to 'c:/users/vr3/Documents/Guenni/sometest.mp4':
Metadata:
encoder : Lavf57.83.100
Stream #0:0: Video: mjpeg, 1 reference frame (mp4v / 0x7634706D), yuvj444p(pc), 80x80, q=2-31, 200 kb/s, 1 fps, 16384 tbn, 1 tbc
Metadata:
encoder : Lavc57.107.100 mjpeg
Side data:
cpb: bitrate max/min/avg: 0/0/200000 buffer size: 0 vbv_delay: -1
frame= 10 fps=6.3 q=1.6 size= 0kB time=00:00:09.00 bitrate= 0.0kbits/s speed=5.63x
正如您在FFmpeg输出的最后一行中所看到的,图像已经通过了。共有10帧可用。但是在关闭管道后,FFmpeg并没有关闭,仍然需要输入。
正如链接示例所示,这应该是一种有效的方法。
已经尝试了一个星期...