How do I use ffmpeg with Python by passing File Objects (instead of locations to files on disk)

with ffmpeg you can use - as input/output file name to indicate that it should read the data from stdin / write to stdout.

Then you can use the stdin/stdout arguments of Popen to read/write your data.

an example:

from subprocess import Popen, PIPE

with open("test.avi", "rb") as infile:
    p=Popen(["ffmpeg", "-i", "-", "-f", "matroska", "-vcodec", "mpeg4",
        "-acodec", "aac", "-strict", "experimental", "-"],
           stdin=infile, stdout=PIPE)
    while True:
        data = p.stdout.read(1024)
        if len(data) == 0:
            break
        # do something with data...
        print(data)
    print p.wait() # should have finisted anyway

instead you supplying a file for stdin you could also use a PIPE and write directly to the processes input stream (p.stdin). or in your case you would simply use wavfile...

note that you have to specify the output format and codecs explicitly, as ffmpeg can't guess them from the file extension as it usually does.
and it will only work for muxers that don't need seekable output streams, but flac should work...


For anyone still reading this: This can be done without subprocesses by using FFMPEG's pipe protocol instead. If FFMPEG is called using the package ffmpeg-python, the stdout, stderr output of the FFMPEG command can be fed into Python variables as seen here:

out, err = inpstream.output('pipe:', ... ).run(capture_stdout=True)