I am trying to read binary data from sys.stdin using Python 2.7 on Windows XP. Binary data is a WAV file decoded by foobar2000. Typically, this data is sent to a command-line encoder, such as lame.exe, to stdin, where it is processed and written to the output file whose name is specified in the command-line arguments. I am trying to intercept the WAV data that is being output and send it to another file. However, I can only get a few kilobytes from stdin before the pipeline seems to crash, and so I only have a very short (about 75 KB) WAV file, and not the few tens of megabytes that I expect. What could be the reason for this? I tried to open both sys.stdin and the output file as binary files.
from __future__ import print_function
import os
import os.path
import sys
sys.stdin = os.fdopen(sys.stdin.fileno(), 'rb', 0)
wave_fname = os.path.join(os.environ['USERPROFILE'], 'Desktop',
'foobar_test.wav')
try:
os.remove(wave_fname)
except Exception:
pass
CHUNKSIZE = 8192
wave_f = open(wave_fname, 'wb')
try:
bytes_read = sys.stdin.read(CHUNKSIZE)
while bytes_read:
for b in bytes_read:
wave_f.write(b)
bytes_read = sys.stdin.read(CHUNKSIZE)
finally:
pass
wave_f.close()