Skip to content
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

Can't read data from file with unknown number of frames #14

Open
njsmith opened this issue Jan 3, 2013 · 2 comments
Open

Can't read data from file with unknown number of frames #14

njsmith opened this issue Jan 3, 2013 · 2 comments

Comments

@njsmith
Copy link

njsmith commented Jan 3, 2013

I want to read the audio out of a .mov file, so I'm doing

child = subprocess.Popen(["avconv", "-i", "myfile.mov", "-f", "au", "-"], stdout=subprocess.PIPE)
my_sndfile = Sndfile(child.stdout.fileno())

This calls avconv (from ffmpeg) to feed the data directly into my process (no need for temporary files), and that's great. But:

  • my_sndfile.nframes contains nonsense: Okay, so I don't know how many frames to read, not a surprise given it's coming from a pipe. (It would be nice if nframes in this situation were None or -1 or something; instead I'm getting 2305843009213693945L.)
  • If I just call read_frames with a large value, hoping to get all the frames, I get:
In [76]: d = s.read_frames(10000000)
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-76-cb9a61676b12> in <module>()
----> 1 d = s.read_frames(10000000)

/home/njs/.user-python2.7-64bit/local/lib/python2.7/site-packages/scikits.audiolab-0.11.0-py2.7-linux-x86_64.egg/scikits/audiolab/pysndfile/_sndfile.so in scikits.audiolab.pysndfile._sndfile.Sndfile.read_frames (scikits/audiolab/pysndfile/_sndfile.c:5361)()

/home/njs/.user-python2.7-64bit/local/lib/python2.7/site-packages/scikits.audiolab-0.11.0-py2.7-linux-x86_64.egg/scikits/audiolab/pysndfile/_sndfile.so in scikits.audiolab.pysndfile._sndfile.Sndfile.read_frames_double (scikits/audiolab/pysndfile/_sndfile.c:5749)()

RuntimeError: Asked 10000000 frames, read 573440

Sort of frustrating... it's telling me it managed to read the frames, but... then it threw them away and raised an exception. So the frames are there, but it's impossible for me to get at them. I expected this to allow for a shorter return value, like POSIX read or Python file.read.

The only solution I can see is to call read_frames(1) in a loop, which is silly, but there you go.

@cournape
Copy link
Owner

libsndfile has some facility to read from a PIPE (http://www.mega-nerd.com/libsndfile/FAQ.html#Q017), but I have never tried it. There may be a way to make this more friendly in python.

@njsmith
Copy link
Author

njsmith commented Jan 17, 2013

Right, the libsndfile part is fine, the problem is that the python wrapper
api has no way to say " please give me N frames... or if the file ends
before you've read N frames, then just give me what you got". The wrapper
considers it a hard error if the file ends before all N frames are read.

(Compare the length argument to file.read, which is just an upper bound and
you may get fewer bytes than requested.)
On 16 Jan 2013 07:55, "David Cournapeau" [email protected] wrote:

libsndfile has some facility to read from a PIPE (
http://www.mega-nerd.com/libsndfile/FAQ.html#Q017), but I have never
tried it. There may be a way to make this more friendly in python.


Reply to this email directly or view it on GitHubhttps://github.com//issues/14#issuecomment-12324696.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants