-
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?
Conversation
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 comment
The 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 self.audio
and writing it to self.fh
, one block at a time, in a loop.
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.
I'll add block size as an argument similar to QueueReaderThread
and read a block at a time. Perhaps in a while True:
loop.
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. The loop should probably exit when the file has no more data to read (i.e., when read
returns an empty string).
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.
I just added these changes to the most recent commit.
class WriterThread(threading.Thread): | ||
"""A thread that writes data to a filehandle | ||
""" | ||
def __init__(self, fh, audio=None): |
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
and infile
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!
Cool! This is looking great. Do you think we should ship this with just one backend enabled (and document that fact), or should we endeavor to add the streaming mode to more backends? (I don't have a strong feeling either way.) If the consensus is to merge, then all that's left is documentation. |
I was going to suggest working on other backends, just so that there's parity in backend support for decoding a file and compressed audio. I only have Linux and Mac OS X environments for testing. And so far, I've only been testing with Linux/Python 2.7. |
That sounds good. Some backends will probably more challenging than others to adapt—for example, the Core Audio backend will require fiddling with callbacks to read data from the file. |
Processing audio from pipe doesn't work for me with python3 on Ubuntu 16.04.1
With python2 it works, but print out wrong length:
With filename as input both python's versions works correct (same file and ffmpeg as backend):
|
Good point; it looks like we'll need to read from stdin in binary mode instead of text mode. If you're interested, any chance you could give that a shot? This SO answer might help: http://stackoverflow.com/a/32282458 |
Just adding
|
After changing
to
in |
Cool! Thanks for sorting this out. I think the right fix here is to use an |
Does this stem from decode.py calling audioread.decode(sys.stdin)? |
Yes
I didn't found PY2 variable (just ugly
so, it can be rewritten as:
|
Also reproduced bug with different sizes. It seems like ffmpeg bug:
|
Yeah, seems like ffmpeg might work slightly differently depending on where its data comes from. Thanks for looking into this! |
Adding this to decode.py, accounts for the correct sys.stdin input:
I tried catching the |
As I wrote above
is more pythonic ways than Also if we adding this check to |
I don't have a strong opinion about feature detection ( But I do think this check should go in |
Status? |
This is @jksinton's work on making the FFmpeg backend work on data from a stream instead of from disk. Addresses #34 and #35.
I changed around the top-level interface a bit. There's a new
audioread.decode
function that works on streams instead of filenames. The included demo script,decode.py
, can now work on data from stdin. So this works, for example:and produces a WAV file in
out.wav
. (Eventually, it would be nice to write that to stdout too to demonstrate that we're actually streaming!)