gpt4 book ai didi

c - popen 在进程之间传递二进制数据

转载 作者:太空宇宙 更新时间:2023-11-04 06:43:01 24 4
gpt4 key购买 nike

我在进程之间传递二进制数据时遇到问题。我的程序使用 popen() 打开一个到 ffmpeg 的管道,并 try catch 输出,然后将其作为 HTTP 服务器进行流式传输。

我正在做这样的事情

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

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

打开后,我使用 fread() 读取管道。

我可以读取它并且我的程序流式传输内容,当我在浏览器上下载文件时,它完成了,但输出文件无法播放!!!

我怀疑打开的管道是“非二进制”句柄,因为我用 popen("", "r") 打开它,因为 fopen 中的 "r"用于打开文本文件。

但我无法像打开 fopen() 那样用“rb”打开它,因为“rb”不能被 popen() 接受。

我该如何解决这个问题?

更新:

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

代码打开管道

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;
}

从管道读取代码

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;
}

关闭管道的代码(为了完整性:))

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

更新 2:

比较文件

1) File-1 => 通过将数据从 ffmpeg 输出管道获得

2) File-2 => 使用相同配置直接从 ffmpeg 获取

我能看到差异,1) File-1 中的 File_Duration_field 为 0 但 File-2 有一些值。2) File-1 中的 File_size_field 为 0 但 File-2 有一些值3) File-1 末尾有一些额外的 18 个字节,这在 File-2 中不存在。看起来缺少有关 file_size 和 file_duration 的详细信息。

这看起来很自然,不是吗?由于 ffmpeg 不知道输出的确切 file_size,它可能将其作为 0 放在 file-1 的 header 中(但仍然想知道为什么它必须将 0 的持续时间放在 file-1 中)。

我的目标是将我的视频文件转码为 flv 并同时流式传输它们,这样我就不必等待转换或必须提前转换所有内容。但是以这种方式完成这看起来是不可能的。我有什么办法可以做到这一点吗???

任何帮助/建议都将非常有帮助和感激。

问候,

微内核

最佳答案

我猜您使用的是 linux 或其他一些 unix。在那种情况下,以二进制模式或文本模式打开某些内容没有区别。您根本不需要“b”标志。你的问题出在别处。

来自fopen手册页:

The mode string can also include the letter ''b'' either as a last character or as a character between the characters in any of the two-character strings described above. This is strictly for compatibility with C89 and has no effect; the ''b'' is ignored on all POSIX conforming systems, including Linux. (Other systems may treat text files and binary files differently, and adding the ''b'' may be a good idea if you do I/O to a binary file and expect that your program may be ported to non-Unix environments.)

关于c - popen 在进程之间传递二进制数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5302196/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com