3

我在 C++ 中有以下代码:

#include <iostream>
#include <windows.h>
#include <iostream>     // std::cout
#include <fstream>      // std::ifstream
#include <vector> 
#include <stdlib.h>

using namespace std;

int main(int argc, const char **argv)
{
    wcout << "Creating an instance of a named pipe..." << endl;

    // Create a pipe to send data
    HANDLE pipe = CreateNamedPipe(
        L"\\\\.\\pipe\\my_pipe", // name of the pipe
        PIPE_ACCESS_OUTBOUND, // 1-way pipe -- send only
        PIPE_TYPE_BYTE, // send data as a byte stream
        1, // only allow 1 instance of this pipe
        0, // no outbound buffer
        0, // no inbound buffer
        0, // use default wait time
        NULL // use default security attributes
    );

    if (pipe == NULL || pipe == INVALID_HANDLE_VALUE) {
        wcout << "Failed to create outbound pipe instance.";
        // look up error code here using GetLastError()
        system("pause");
        return 1;
    }

    wcout << "Waiting for a client to connect to the pipe..." << endl;

    // This call blocks until a client process connects to the pipe
    BOOL result = ConnectNamedPipe(pipe, NULL);
    if (!result) {
        wcout << "Failed to make connection on named pipe." << endl;
        // look up error code here using GetLastError()
        CloseHandle(pipe); // close the pipe
        system("pause");
        return 1;
    }

    wcout << "Sending data to pipe..." << endl;

    //opening file
    ifstream infile;
    infile.open("E:/xmen.jpg",std::ios::binary);
    ofstream out("E:/lelel.jpg",std::ios::binary);

    infile.seekg(0,std::ios::end);
    size_t file_size_in_byte = infile.tellg();
    vector<char> file_vec;

    file_vec.resize(file_size_in_byte);

    infile.seekg(0,std::ios::beg);
    infile.read(&file_vec[0],file_size_in_byte);

    out.write(&file_vec[0],file_vec.size());

    wcout<<file_size_in_byte<<endl;



    // This call blocks until a client process reads all the data
    DWORD numBytesWritten = 0;
    result = WriteFile(
        pipe, // handle to our outbound pipe
        &file_vec[0], // data to send
        61026, // length of data to send (bytes)
        &numBytesWritten, // will store actual amount of data sent
        NULL // not using overlapped IO
    );


    if (result) {
        wcout << "Number of bytes sent: " << numBytesWritten << endl;
    } else {
        wcout << "Failed to send data." << endl;
        // look up error code here using GetLastError()
    }

    // Close the pipe (automatically disconnects client too)
    CloseHandle(pipe);

    wcout << "Done." << endl;

    system("pause");
    return 0;
}

我使用以下命令创建一个命名管道 \\.\pipe\my_pipe,FFMPEG 连接到该管道:

64-static\bin\Video>ffmpeg.exe -loop 1 -s 4cif -f image2 -y -i \\.\pipe\\my_pipe

-r 25 -vframes 250 -vcodec rawvideo -an eaeew.mov

输出:

ffmpeg version N-54233-g86190af Copyright (c) 2000-2013 the FFmpeg developers
  built on Jun 27 2013 16:49:12 with gcc 4.7.3 (GCC)
  configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libcaca --enable-libfreetype --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxavs --enable-libxvid --enable-zlib  libavutil      52. 37.101 / 52. 37.101
  libavcodec     55. 17.100 / 55. 17.100
  libavformat    55. 10.100 / 55. 10.100
  libavdevice    55.  2.100 / 55.  2.100
  libavfilter     3. 77.101 /  3. 77.101
  libswscale      2.  3.100 /  2.  3.100
  libswresample   0. 17.102 /  0. 17.102
  libpostproc    52.  3.100 / 52.  3.100
[image2 @ 0000000003ee04a0] Could find no file with with path '\\.\pipe\\my_pipe
' and index in the range 0-4
\\.\pipe\\my_pipe: No such file or directory

我可以在我的控制台上看到我的 C++ 应用程序收到了一个连接,但是我在 FFMPEG 中收到了上述错误。有人可以建议吗?

编辑 1 使用下面的命令

ffmpeg.exe -s 4cif -i \\.\pipe\my_pipe -r 25 -vframes 250 -vcodec rawvideo -an tess.mov

我得到以下输出

ffmpeg version N-54233-g86190af Copyright (c) 2000-2013 the FFmpeg developers
  built on Jun 27 2013 16:49:12 with gcc 4.7.3 (GCC)
  configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libcaca --enable-libfreetype --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxavs --enable-libxvid --enable-zlib
  libavutil      52. 37.101 / 52. 37.101
  libavcodec     55. 17.100 / 55. 17.100
  libavformat    55. 10.100 / 55. 10.100
  libavdevice    55.  2.100 / 55.  2.100
  libavfilter     3. 77.101 /  3. 77.101
  libswscale      2.  3.100 /  2.  3.100
  libswresample   0. 17.102 /  0. 17.102
  libpostproc    52.  3.100 / 52.  3.100
\\.\pipe\my_pipe: Invalid data found when processing input

所以,现在它似乎能够连接到管道但无法处理输入。

4

1 回答 1

6

我正在做同样的事情,但使用普通管道,而不是命名管道。我的代码运行良好。希望能帮助到你

#include <stdio.h>
#include <iostream>
#include <iomanip>

using namespace std;


int main(int argc, char **argv) {
    FILE *pPipe;
    long lSize;
    char * imgdata;
    int imgcols = 640, imgrows = 480, elemSize = 3;

    imgdata = ...;

    stringstream sstm;
    sstm << "/usr/local/bin/ffmpeg -y -f rawvideo -vcodec rawvideo -s " << imgcols << "x" << imgrows  <<" -pix_fmt rgb24 -i - -c:v libx264 -shortest my_output.mp4";

    if ( !(pPipe = popen(sstm.str().c_str(), "w")) ) {
        cout << "popen error" << endl;
        exit(1);
    }

    // open a pipe to FFmpeg
    lSize = imgrows * imgcols * elemSize;

    // write to pipe
    fwrite(imgdata, 1, lSize, pPipe);
    fflush(pPipe);
    fclose(pPipe);

    return 0;

}
于 2015-08-28T20:39:38.163 回答