(self, filename=None, audio=None, block_size=4096, ffmpeg_args=None)
Start decoding an audio file.

        Provide either `filename` to read from the filesystem or
        `audio`, a file-like object, to read from an open stream.
Start decoding an audio file.
Start decoding an audio file .
def __init__(self, filename=None, audio=None, block_size=4096, ffmpeg_args=None):
        """Start decoding an audio file.

        Provide either `filename` to read from the filesystem or
        `audio`, a file-like object, to read from an open stream.
        """
        if filename:
            self.from_file = True
        elif audio:
            self.from_file = False
        else:
            raise ValueError('one of `filename` or `audio` must be provided')

        # On Windows, we need to disable the subprocess's crash dialog
        # in case it dies. Passing SEM_NOGPFAULTERRORBOX to SetErrorMode
        # disables this behavior.
        windows = sys.platform.startswith("win")
        if windows:
            windows_error_mode_lock.acquire()
            SEM_NOGPFAULTERRORBOX = 0x0002
            import ctypes
            # We call SetErrorMode in two steps to avoid overriding
            # existing error mode.
            previous_error_mode = \
                ctypes.windll.kernel32.SetErrorMode(SEM_NOGPFAULTERRORBOX)
            ctypes.windll.kernel32.SetErrorMode(
                previous_error_mode | SEM_NOGPFAULTERRORBOX
            )

        # Start the subprocess.
        try:
            in_arg = filename if self.from_file else '-'
            self.devnull = open(os.devnull)
            in_stream = self.devnull if self.from_file else subprocess.PIPE
            if type(ffmpeg_args) == str:
                ffmpeg_args = ffmpeg_args.split()
            if ffmpeg_args:
                args = ['-i', in_arg, '-f', 's16le'] + ffmpeg_args + ['-']
            else:
                args = ['-i', in_arg, '-f', 's16le', '-']
            self.proc = popen_multiple(
                COMMANDS,
                args,
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE,
                stdin=in_stream,
            )

        except OSError:
            raise NotInstalledError()

        finally:
            # Reset previous error mode on Windows. (We can change this
            # back now because the flag was inherited by the subprocess;
            # we don't need to keep it set in the parent process.)
            if windows:
                try:
                    import ctypes
                    ctypes.windll.kernel32.SetErrorMode(previous_error_mode)
                finally:
                    windows_error_mode_lock.release()

        # If the input data comes from a stream, start a thread to write
        # the compressed audio to the subprocess's standard input
        # stream.
        if not self.from_file:
            self.stdin_writer = WriterThread(self.proc.stdin, audio,
                                             block_size)
            self.stdin_writer.start()

        # Start another thread to consume the standard output of the
        # process, which contains raw audio data.
        self.stdout_reader = QueueReaderThread(self.proc.stdout, block_size)
        self.stdout_reader.start()

        # Read relevant information from stderr.
        self._get_info()

        # Start a separate thread to read the rest of the data from
        # stderr. This (a) avoids filling up the OS buffer and (b)
        # collects the error output for diagnosis.
        self.stderr_reader = QueueReaderThread(self.proc.stderr)
        self.stderr_reader.start()
def __init__ ( self , filename = None , audio = None , block_size = 4096 , ffmpeg_args = None ) : if filename : self . from_file = True elif audio : self . from_file = False else : raise ValueError ( 'one of `filename` or `audio` must be provided' ) # On Windows, we need to disable the subprocess's crash dialog # in case it dies. Passing SEM_NOGPFAULTERRORBOX to SetErrorMode # disables this behavior. windows = sys . platform . startswith ( "win" ) if windows : windows_error_mode_lock . acquire ( ) SEM_NOGPFAULTERRORBOX = 0x0002 import ctypes # We call SetErrorMode in two steps to avoid overriding # existing error mode. previous_error_mode = ctypes . windll . kernel32 . SetErrorMode ( SEM_NOGPFAULTERRORBOX ) ctypes . windll . kernel32 . SetErrorMode ( previous_error_mode | SEM_NOGPFAULTERRORBOX ) # Start the subprocess. try : in_arg = filename if self . from_file else '-' self . devnull = open ( os . devnull ) in_stream = self . devnull if self . from_file else subprocess . PIPE if type ( ffmpeg_args ) == str : ffmpeg_args = ffmpeg_args . split ( ) if ffmpeg_args : args = [ '-i' , in_arg , '-f' , 's16le' ] + ffmpeg_args + [ '-' ] else : args = [ '-i' , in_arg , '-f' , 's16le' , '-' ] self . proc = popen_multiple ( COMMANDS , args , stdout = subprocess . PIPE , stderr = subprocess . PIPE , stdin = in_stream , ) except OSError : raise NotInstalledError ( ) finally : # Reset previous error mode on Windows. (We can change this # back now because the flag was inherited by the subprocess; # we don't need to keep it set in the parent process.) if windows : try : import ctypes ctypes . windll . kernel32 . SetErrorMode ( previous_error_mode ) finally : windows_error_mode_lock . release ( ) # If the input data comes from a stream, start a thread to write # the compressed audio to the subprocess's standard input # stream. if not self . from_file : self . stdin_writer = WriterThread ( self . proc . stdin , audio , block_size ) self . stdin_writer . start ( ) # Start another thread to consume the standard output of the # process, which contains raw audio data. self . stdout_reader = QueueReaderThread ( self . proc . stdout , block_size ) self . stdout_reader . start ( ) # Read relevant information from stderr. self . _get_info ( ) # Start a separate thread to read the rest of the data from # stderr. This (a) avoids filling up the OS buffer and (b) # collects the error output for diagnosis. self . stderr_reader = QueueReaderThread ( self . proc . stderr ) self . stderr_reader . start ( )