Ffmpeg-python: edit memory video files

Created on 5 Jan 2018  路  2Comments  路  Source: kkroening/ffmpeg-python

Is it possible to read an in memory video file, process it, and then write back to memory.
I need to combine a video file and a audio file from aws s3 and write it back to s3. I am trying to avoid saving the video in disk first.

thanks.

question

Most helpful comment

Yes - see the following resources:

Then when you run ffmpeg using ffmpeg-python, you can use e.g. subprocess.Popen to pass the data to stdin and read the output from stdout:

import subprocess
import ffmpeg

input_data = download_file_from_s3(...)
args = (ffmpeg
    .input('pipe:')
    # ... extra processing here
    .output('pipe:')
    .get_args()
)
p = subprocess.Popen(['ffmpeg'] + args, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
output_data = p.communicate(input=input_data)[0]
upload_file_to_s3(output_data, ...)

This approach reads the entire file into memory from S3, passes it into ffmpeg, gets the entire output from ffmpeg, then uploads the output to S3.

It's also possible to do a streaming approach where you read a little bit of the file from S3, pump it into ffmpeg, and then stream the upload piece by piece, but it's a bit harder because you either have to use non-blocking IO or multiple threads/greenlets (e.g. using gevent). It can be done, but I'd start with the 'read-entire-file-to-memory' approach first.

All 2 comments

Yes - see the following resources:

Then when you run ffmpeg using ffmpeg-python, you can use e.g. subprocess.Popen to pass the data to stdin and read the output from stdout:

import subprocess
import ffmpeg

input_data = download_file_from_s3(...)
args = (ffmpeg
    .input('pipe:')
    # ... extra processing here
    .output('pipe:')
    .get_args()
)
p = subprocess.Popen(['ffmpeg'] + args, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
output_data = p.communicate(input=input_data)[0]
upload_file_to_s3(output_data, ...)

This approach reads the entire file into memory from S3, passes it into ffmpeg, gets the entire output from ffmpeg, then uploads the output to S3.

It's also possible to do a streaming approach where you read a little bit of the file from S3, pump it into ffmpeg, and then stream the upload piece by piece, but it's a bit harder because you either have to use non-blocking IO or multiple threads/greenlets (e.g. using gevent). It can be done, but I'd start with the 'read-entire-file-to-memory' approach first.

@jl-DaDar Let me know if you need more clarification.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

CNugteren picture CNugteren  路  5Comments

cbitterfield picture cbitterfield  路  4Comments

Sangkwun picture Sangkwun  路  4Comments

lalamax3d picture lalamax3d  路  3Comments

Baa14453 picture Baa14453  路  5Comments