-
Notifications
You must be signed in to change notification settings - Fork 108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Compressed audio (continuation of #36) #37
base: main
Are you sure you want to change the base?
Changes from 5 commits
a5c08ad
8ca3f7a
1ec95c7
2b1c581
da0491a
ff7c15c
0153f6b
dd5a1d0
f83a9b2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,6 +51,8 @@ class NotInstalledError(FFmpegError): | |
class ReadTimeoutError(FFmpegError): | ||
"""Reading from the ffmpeg command-line tool timed out.""" | ||
|
||
class NoInputError(FFmpegError): | ||
"""Reading from the ffmpeg command-line tool timed out.""" | ||
|
||
class QueueReaderThread(threading.Thread): | ||
"""A thread that consumes data from a filehandle and sends the data | ||
|
@@ -73,6 +75,18 @@ def run(self): | |
# Stream closed (EOF). | ||
break | ||
|
||
class WriterThread(threading.Thread): | ||
"""A thread that writes data to a filehandle | ||
""" | ||
def __init__(self, fh, audio=None): | ||
super(WriterThread, self).__init__() | ||
self.fh = fh | ||
self.audio = audio | ||
self.daemon = True | ||
|
||
def run(self): | ||
self.fh.write(self.audio.read()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This currently reads the entire input stream into memory and then writes into FFmpeg. We should do this in a more streaming way by reading a block from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll add block size as an argument similar to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call. The loop should probably exit when the file has no more data to read (i.e., when There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just added these changes to the most recent commit. |
||
self.fh.close() | ||
|
||
def popen_multiple(commands, command_args, *args, **kwargs): | ||
"""Like `subprocess.Popen`, but can try multiple commands in case | ||
|
@@ -100,7 +114,10 @@ def popen_multiple(commands, command_args, *args, **kwargs): | |
|
||
class FFmpegAudioFile(object): | ||
"""An audio file decoded by the ffmpeg command-line utility.""" | ||
def __init__(self, filename, block_size=4096): | ||
def __init__(self, filename=None, audio=None, block_size=4096): | ||
self.openFile = True if filename is not None else False | ||
self.readAudio = True if audio is not None else False | ||
|
||
# On Windows, we need to disable the subprocess's crash dialog | ||
# in case it dies. Passing SEM_NOGPFAULTERRORBOX to SetErrorMode | ||
# disables this behavior. | ||
|
@@ -118,14 +135,26 @@ def __init__(self, filename, block_size=4096): | |
) | ||
|
||
try: | ||
self.devnull = open(os.devnull) | ||
self.proc = popen_multiple( | ||
COMMANDS, | ||
['-i', filename, '-f', 's16le', '-'], | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
stdin=self.devnull, | ||
) | ||
if self.openFile: | ||
self.devnull = open(os.devnull) | ||
self.proc = popen_multiple( | ||
COMMANDS, | ||
['-i', filename, '-f', 's16le', '-'], | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
stdin=self.devnull, | ||
) | ||
elif self.readAudio: | ||
self.devnull = open(os.devnull) | ||
self.proc = popen_multiple( | ||
COMMANDS, | ||
['-i', '-', '-f', 's16le', '-'], | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
stdin=subprocess.PIPE, | ||
) | ||
else: | ||
raise NoInputError() | ||
|
||
except OSError: | ||
raise NotInstalledError() | ||
|
@@ -141,6 +170,11 @@ def __init__(self, filename, block_size=4096): | |
finally: | ||
windows_error_mode_lock.release() | ||
|
||
# Start a thread to write the compressed audio to Popen.stdin | ||
if self.readAudio: | ||
self.stdin_writer = WriterThread(self.proc.stdin,audio) | ||
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) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The parameter names here could be a little more descriptive. Both parameters are actually file-like objects—so maybe we should just call them
outfile
andinfile
or something?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps writefile and readfile?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call; that's better!