使用管道将文件 I/O 传输到另一个进程
刚刚开始学习/使用管道,想知道如何将应用程序的文件输出路由到管道中,以便另一个应用程序可以使用它。
确切地说,我想将 ffmpeg 的输出(转码后的视频数据)通过管道传输到我的应用程序中以使用它。如果我创建一个像 /tmp/out.mp4
这样的命名管道并将其作为输出文件名提供给 ffmpeg,ffmpeg 将尝试再次创建此文件,可能会覆盖我的管道(或类似的东西) 。遇到这种情况该如何处理呢?
有没有通用的方法可以透明地转移应用程序的文件 IO?
(我正在尝试编写一个视频流服务器(只是为了学习和娱乐),在流媒体期间将 avi 等格式转码为流友好格式(如 mpeg4),我发现 ffmpeg 对于此目的太慢,转码大约需要 2 秒1 秒视频 :(
是我的设置/PC 的问题还是 ffmpeg 速度缓慢? )
PS:顺便说一下,我是用C写的。
Just started learning/using pipes and was wondering how to route file output of an application into a pipe so that another application can use it.
To be exact, I want to pipe ffmpeg's output (transcoded video data) into my application to use it. If I create a named pipe like /tmp/out.mp4
and give it to ffmpeg as output filename, ffmpeg is going to try to create this file again, probably overwriting my pipe (Or something like that). How to deal with this kind of situation?
is there any general way to divert File IO of an application transparently?
(I am trying to write a video streaming server (Just for learning and fun) which transcodes formats like avi into streaming friendly format like mpeg4 during streaming, I found ffmpeg to be too slow for this purpose, it was taking like 2 secs to transcode 1 sec video :(
Is it the problem with my setup/PC or ffmpeg is known for sluggishness?
)
PS : I am writing this in C by the way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可以说服 ffmpeg 输出到管道:
“-”告诉它输出到 stdout 而不是文件,“-f”告诉它输出文件应该是什么输出。
当然,您可以将其重定向到命名管道,但使用 popen 调用它以直接将输出作为文件描述符获取似乎是适合我的方法。
ffmpeg can be persuaded to output to a pipe:
The "-" tells it to output to stdout instead of to a file, and the "-f" tells it what output the output file should be.
You could redirect that to a named pipe, of course, but calling it with
popen
to get the output as a file descriptor directly seems the way to go to me.ffmpeg 还可以从 stdin 读取并写入 stdout,如下所示:
其中 0 和 1 是标准 POSIX 文件描述符。
ffmpeg can also read from stdin and write to stdout like this:
where 0 and 1 are the standard POSIX file descriptors.