2012-04-18 59 views
2
f = os.popen("gst-launch -q whateversrc ! ... ! fdsink") 
f.read(1024); 

在GNU/Linux上工作得很好,但是導致了\ x0d \ x0a而不是每個Windows \ x0a。怎麼修?如何從Windows上的Gstreamer子進程讀取二進制數據?

我也試過在控制檯中只有gst-launch -q ..... ! matroskamux streamable=true ! fdsink > qqq3.mkv而qqq3.mkv也是亂碼。 gst-launch -q filesrc location=file ! fdsink > file2也轉換爲CRLF。

怎麼辦? Gstreamer爲Windows生成沒有tcp {服務器,客戶端}接收器...

可能有辦法在Windows(或給定應用程序)中全局關閉LF-> CRLF轉換?

+0

你應該考慮使用[subprocess module](http://docs.python.org/library/subprocess.html#module-subprocess)而不是'os.popen'。另外,你使用的是什麼版本的Python? – srgerg 2012-04-19 00:01:46

+0

Cygwin中的「Python 2.6.5」 – 2012-04-19 00:20:28

+0

您是否嘗試過在'os.popen'調用中指定模式?例如,你可以嘗試'os.popen(「gst-launch ...」,「r」)或者os.popen(「gst-launch ...」,「rb」)'。前者應該是默認行爲,而後者則明確指定讀取二進制數據。 – srgerg 2012-04-19 01:04:30

回答

0

我想也許你不應該使用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) 

如果子進程繼承父進程的標準輸入和標準輸出文件句柄的二進制模式的設置,那麼可能在你的Python父進程中運行上面的代碼會有幫助嗎?

如果不是,則嘗試相同的技巧,但返回的對象中的stdinstdout句柄。在我上面的代碼中,我綁定該對象的名稱p,這樣他們就可以p.stdinp.stdout

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

我希望這有助於。

+0

'subprocess.Popen([「gst-launch」,「-q」,「whateversrc」,.........,「!」,「matroskamux」,「streamable = true」,「!」 「fdsink」],shell = False,bufsize = 1024,stdout = subprocess.PIPE).stdout' - 同樣的問題 – 2012-04-19 01:50:37

+0

「fdsink」Gstreamer模塊不會「_binmode(... O_BINARY)」(或其他Windows - 具體的東西)。如何強制其他進程默認處理二進制文件? – 2012-04-19 01:51:32

+0

我很想知道'msvcrt.setmode()'是否可以幫助你。請嘗試時發佈更新! – steveha 2012-04-19 18:25:35