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

Lyle Pratt picture Lyle Pratt · May 1, 2012 · Viewed 10.7k times · Source

I'm trying to use ffmpeg with Python's subprocess module to convert some audio files. I grab the audio files from a URL and would like to just be able to pass the Python File Objects to ffmpeg, instead of first saving them to disk. It would also be very nice if I could just get back a file stream instead of having ffmpeg save the output to a file.

For reference, this is what I'm doing now:

tmp = "/dev/shm"
audio_wav_file = requests.get(audio_url)
##              ##                         ##
## This is what I don't want to have to do ##
wavfile = open(tmp+filename, 'wrb')   
wavfile.write(audio_wav_file.content)
wavfile.close()
##              ##                         ##
conversion = subprocess.Popen('ffmpeg -i "'+tmp+filename+'" -y "'+tmp+filename_noext+'.flac" 2>&1', shell = True, stdout = subprocess.PIPE).stdout.read()

Does anyone know how to do this?

Thanks!

Answer

mata picture mata · May 1, 2012

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...