python - 如何从 Windows 上的 Gstreamer 子进程读取二进制数据?

标签 python windows binary pipe gstreamer

f = os.popen("gst-launch -q whateversrc ! ... ! fdsink")
f.read(1024);

在 GNU/Linux 上工作得很好,但导致\x0d\x0a 而不是 Windows 的每个\x0a。如何修复?

我也试过 gst-launch -q .....! matroskamux streamable=true ! fdsink > qqq3.mkv in console 和 qqq3.mkv 也是乱码。 gst-launch -q filesrc 位置=文件! fdsink > file2 也转换为 CRLF。

要做什么?适用于 Windows 的 Gstreamer 构建没有 tcp{server,client}sink...

可能有一种方法可以在 Windows 中全局关闭 LF->CRLF 转换(或对于给定的应用程序)?

最佳答案

我想也许你不应该使用 shell=Truesubprocess.Popen() .

使用 Windows 7 64 位,Python 在 Cygwin 中运行,我编写了这个简单的测试程序:

import subprocess as sp

p = sp.Popen(['cat', 'junk.bin'], stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.
PIPE)
stdin, stdout = p.communicate()

with open('junk1.bin', "wb") as f:
    f.write(stdin)

对我来说,junk1.binjunk.bin 的字节完美副本,所以我没有看到 "\n" -> "\r\n"您正在发生的转变。

编辑:我找到了一个看起来很有用的网页:

http://blog.rubypdf.com/2009/11/03/how-to-let-python-send-binary-data-to-stdout-under-windows/

import sys

if sys.platform == "win32":
    import os, msvcrt
    msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)

如果子进程继承了父进程的 stdin 和 stdout 文件句柄的二进制模式设置,那么也许在您的 Python 父进程中运行上述代码会有帮助?

如果没有,则尝试相同的技巧,但在 stdin 上和 stdout subprocess.Popen() 返回的对象中的句柄.在我上面的代码中,我将该对象绑定(bind)到名称 p所以他们会是p.stdinp.stdout :

msvcrt.setmode(p.stdin.fileno(), os.O_BINARY)
msvcrt.setmode(p.stdout.fileno(), os.O_BINARY)

希望对您有所帮助。

关于python - 如何从 Windows 上的 Gstreamer 子进程读取二进制数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10219759/

相关文章:

C:索引整数中数字的最佳方法

php - 是否可以从PHP中的十六进制/二进制(原始数据)值生成音频文件?

windows - 在 Windows 8 WinRT 中访问振动功能?

python - 关于网站停机更新的问题

python - 保存 matplotlib 动画会导致错误

python - 使用 Python 将文件流式传输到 Azure Blob 存储中的 Zip 文件?

c - C 中的 OpenMP 在 Windows 中使用 GCC 创建 DLL

windows - 在 Program Files 与 Appdata 中安装

c++ - 保证 C++ 库、C 链接的二进制兼容性的简单方法?

python - GitLab API - 如何获取存储库/项目文件和元数据?