Skip to content

subprocess PIPE error #141

@SiggiGue

Description

@SiggiGue

Using PySoundFile with a subprocess PIPE file-like-object leads to an error:
Example raising the error:

sp = subprocess.Popen(
    ['ffmpeg', '-i', 'test.mp3', '-f', 'wav', '-'],
    stdout=subprocess.PIPE)

sp.stdout.read(8)
Out[69]: b'RIFF\xff\xff\xff\xff'

sp.stdout.seek(0)
Out[70]: 0

dir(sp.stdout)
Out[74]: 
['__class__',
 '__del__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__enter__',
 '__eq__',
 '__exit__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getstate__',
 '__gt__',
 '__hash__',
 '__init__',
 '__iter__',
 '__le__',
 '__lt__',
 '__ne__',
 '__new__',
 '__next__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '_checkClosed',
 '_checkReadable',
 '_checkSeekable',
 '_checkWritable',
 '_dealloc_warn',
 '_finalizing',
 'close',
 'closed',
 'detach',
 'fileno',
 'flush',
 'isatty',
 'mode',
 'name',
 'peek',
 'raw',
 'read',
 'read1',
 'readable',
 'readinto',
 'readline',
 'readlines',
 'seek',
 'seekable',
 'tell',
 'truncate',
 'writable',
 'write',
 'writelines']

# Try reading stdout with soundfile
sf = soundfile.SoundFile(sp.stdout)
From cffi callback <function SoundFile._init_virtual_io.<locals>.vio_get_filelen at 0x0000000005DF3730>:
Traceback (most recent call last):
  File "C:\Miniconda3\envs\anaconda\lib\site-packages\soundfile.py", line 1027, in vio_get_filelen
    file.seek(curr, SEEK_SET)
OSError: [Errno 22] Invalid argument
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-73-38dc580aa6b1> in <module>()
----> 1 sf = soundfile.SoundFile(sp.stdout)

C:\Miniconda3\envs\anaconda\lib\site-packages\soundfile.py in __init__(self, file, mode, samplerate, channels, subtype, endian, format, closefd)
    654         self._info = _create_info_struct(file, mode, samplerate, channels,
    655                                          format, subtype, endian)
--> 656         self._file = self._open(file, mode_int, closefd)
    657         if set(mode).issuperset('r+') and self.seekable():
    658             # Move write position to 0 (like in Python file objects)

C:\Miniconda3\envs\anaconda\lib\site-packages\soundfile.py in _open(self, file, mode_int, closefd)
   1008             raise TypeError("Invalid file: {0!r}".format(self.name))
   1009         _error_check(_snd.sf_error(file_ptr),
-> 1010                      "Error opening {0!r}: ".format(self.name))
   1011         if mode_int == _snd.SFM_WRITE:
   1012             # Due to a bug in libsndfile version <= 1.0.25, frames != 0

C:\Miniconda3\envs\anaconda\lib\site-packages\soundfile.py in _error_check(err, prefix)
   1167     if err != 0:
   1168         err_str = _snd.sf_error_number(err)
-> 1169         raise RuntimeError(prefix + _ffi.string(err_str).decode())
   1170 
   1171 

RuntimeError: Error opening <_io.BufferedReader name=7>: File contains data in an unknown format.

I'd be very happy getting some advice to fix it without reading the whole stdout in cache befor loading a SoundFile instance.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions