popen方法进程之间传递的二进制数据 [英] popen to pass binary data between processes

查看:516
本文介绍了popen方法进程之间传递的二进制数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在面临的传递进程之间的二进制数据的问题。我的程序打开一个管道用popen()来FFMPEG并试图捕获输出,然后流作为HTTP服务器。

I am facing issue in passing binary data between processes. My program opens a pipe to ffmpeg using popen() and tries to capture the output and then stream it as HTTP server.

我在做这样的事情。

ffmpeg -i "input_video.avi" -ab 56 -ar 44100 -b 1500000 -r 25 -s 800x600 -f flv - 

(输出文件名 - 转移输出到标准输出)

(Output filename "-" diverts the output to stdout)

我使用的fread()开盘后读取管道。

After opening I am using fread() to read the pipe.

我可以读它和我的节目流的内容,当我下载我的浏览器中的文件,它完成,但输出文件不能播放!

I can read it and my program streams content, when I downloaded the file on my browser, it completed, but the output file is not playable!!!

我怀疑管道开辟为我的popen(,R)打开它的fopen是非二进制处理,为R是文本文件打开。

I am suspecting pipe opened to be "non-binary" handle as I opened it with popen("", "r"), as "r" in fopen is for text file opening.

但我不能与RB打开它就像我的fopen()函数做的,为RB是不接受的popen()。

But I am not able to open it with "rb" like I do for fopen(), as "rb" is not acceptable by popen().

我怎样才能解决这个问题呢?

How can I solve this issue?

更新:

#define FFMPEG_COMMAND "ffmpeg -i %s -ab 56 -ar 44100 -b 1500000 -r 25 -s 800x600 -f flv -"

code开幕管

Opening_pipe(filename)
{
    STREAMING_HANDLE *vfp = NULL;
    char command[512] = {  0 };


    vfp = (STREAMING_HANDLE *)malloc(sizeof(STREAMING_HANDLE));
    if(NULL != vfp)
    {
        sprintf(command, FFMPEG_COMMAND, filename);
        vfp->ffmpeg_pipe = popen( command, 
                                  "r" );

        if( NULL == vfp->ffmpeg_pipe )
        {
            free(vfp);
            vfp = NULL;
        }
    }    
    printf("Virt_dir_fopen : vfp => 0x%X\n", vfp);
    return vfp;
}

code从管道读取

Reading_data_from_pipe(fileHnd, buff, buflen)
{
    STREAMING_HANDLE *vfp = (STREAMING_HANDLE *)fileHnd;
    int ret_code;
    printf("Virt_dir_fread : Asking for %d bytes \n", buflen);
    ret_code =  fread(buf, 1, buflen, vfp->ffmpeg_pipe );
    printf("Virt_dir_fread : Returning %d bytes \n", ret_code);

    return ret_code;
}

code用于封闭管道(为了完整:))

Closing_pipe(fileHnd)
{
    STREAMING_HANDLE *vfp = (STREAMING_HANDLE *)fileHnd;
    pclose(vfp->ffmpeg_pipe);
    free(vfp);
    return 0;
}

更新-2:

相比,文件

1)文件-1 =>通过管道将数据移出了ffmpeg的获得

1) File-1 => Obtained by piping the data out off ffmpeg

2)文件-2 =>直接从使用相同的配置的ffmpeg获得

2) File-2 => Obtained directly from ffmpeg using the same configuration

有,我可以看到的差异,
1)在File_Duration_field文件-1是0,但文件-2具有一定的价值。
2)在File_size_field文件-1是0,但文件-2具有一定的价值
3)文件-1具有在其不处于文件-2- present结束一些额外的18个字节。貌似约FILE_SIZE和file_duration缺少详细。

There are differences that I can see, 1) File_Duration_field in File-1 is 0 but File-2 has some value. 2) File_size_field in File-1 is 0 but File-2 has some value 3) File-1 has some extra 18 bytes at the end which is not present in File-2. Looks like the missing detailed about file_size and file_duration.

这看起来很自然,不是吗?因为ffmpeg的不知道输出的精确FILE_SIZE它可能已经把它作为0文件-1的报头(但仍然不知道为什么它把0持续时间在文件1)。

This looks natural isn't it? as ffmpeg doesn't know exact file_size of output it might have put it as 0 in the header of file-1 (But still wonder why it has to put 0 for duration in file-1).

我的目标是跨code我的视频文件转换成FLV,同时串流播放,所以我不必等待转换或有转换的一切提前。但完成这个看上去不可能这样。有没有办法为我做到这一点?

My goal is to transcode my videos files into flv and stream them simultaneously so I don't have to wait for the conversion or have to convert everything in advance. But accomplishing this looks not possible this way. Is there any way for me to do this???

任何帮助/建议将高度有益和AP preciated。

Any help/suggestion will be highly helpful and appreciated.

问候,

微内核

推荐答案

输出可以是原始数据没有容器。那么,有没有办法为一个应用程序知道如何跨preT的文件中。

The output may be raw data without a container. So, there is no way for an application to know how to interpret the binary data in the file.

您可能还需要使用的ffmpeg管选项,查看试用该对输出的效果。

You may also want to try using the ffmpeg pipe option to see if that has an effect on the output.

请注意,有些格式(一般
  MOV),要求输出协议
  是可搜索的,所以他们会失败,
  管道输出协议。

Note that some formats (typically MOV), require the output protocol to be seekable, so they will fail with the pipe output protocol.

这篇关于popen方法进程之间传递的二进制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆