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.
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.
Most helpful comment
Yes - see the following resources:
Then when you run ffmpeg using ffmpeg-python, you can use e.g.
subprocess.Popento pass the data to stdin and read the output from stdout: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.