Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 782
Lyle Pratt Avatar asked May 01 '12 16:05

Lyle Pratt


2 Answers

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

like image 109
mata Avatar answered Sep 18 '22 19:09

mata


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)
like image 21
metacollision Avatar answered Sep 16 '22 19:09

metacollision