aws_stream_s3_file_directly_to_ffmpeg_running_on_lambda

How to stream media file from s3 directly to AWS lambda FFMPEG Python example

There is no need to download a file and put it inside your lambda’s /tmp folder to process it with FFMPEG. I will show you how to do it “on the fly”. Just stream a damn file directly into ffmpeg!

def execute_ffmpeg_command(input_file: str):
"""
Execute the given ffmpeg command against the input file.
Args:
input_file (str): Path to the input video file (e.g. file.mp4).
"""
# Define the ffmpeg command as a list of arguments
s3_stream = None
ffmpeg_process = None
try:
output_path = "/tmp"
s3_client = boto3.client("s3")
# Get the S3 object as a stream
s3_object = s3_client.get_object(Bucket="bucket_name", Key=input_file) #key is the name of a file in your S3 bucket
s3_stream = s3_object["Body"]
# Prepare ffmpeg process with input from the S3 object stream
# s3_object_buffer = io.BytesIO(s3_object.read()) <- can try with this as well
ffmpeg_process = subprocess.Popen(
[
f"{bin_path}/bin/ffmpeg",
"-i",
"-", # '-' for stdin
#rest of yours ffmpeg command
],
stdin=subprocess.PIPE, # Actually, here you can try to use S3 object stream as stdin for ffmpeg
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
bufsize=-1,
)
# Read from S3 stream and write to FFmpeg stdin
chunk_size = 1024 * 1024 # Define chunk size (1 MB)
while True:
chunk = s3_stream.read(chunk_size)
if not chunk: # Break if no more data to read
break
ffmpeg_process.stdin.write(chunk)
# Close FFmpeg stdin
ffmpeg_process.stdin.close()
# Collect process output and error
output, error = ffmpeg_process.communicate()
# Return the FFmpeg output and error
return output, error
except Exception as e:
print(f"Error accessing S3 object: {e}")
return None, str(e)
finally:
# Ensure proper cleanup of resources
if s3_stream and not s3_stream.closed:
s3_stream.close()
if hasattr(ffmpeg_process, "stdin"):
ffmpeg_process.stdin.close()
# Example usage:
if __name__ == "__main__":
file_name = "new.MOV"
execute_ffmpeg_command(file_name, is_local=False)
def execute_ffmpeg_command(input_file: str): """ Execute the given ffmpeg command against the input file. Args: input_file (str): Path to the input video file (e.g. file.mp4). """ # Define the ffmpeg command as a list of arguments s3_stream = None ffmpeg_process = None try: output_path = "/tmp" s3_client = boto3.client("s3") # Get the S3 object as a stream s3_object = s3_client.get_object(Bucket="bucket_name", Key=input_file) #key is the name of a file in your S3 bucket s3_stream = s3_object["Body"] # Prepare ffmpeg process with input from the S3 object stream # s3_object_buffer = io.BytesIO(s3_object.read()) <- can try with this as well ffmpeg_process = subprocess.Popen( [ f"{bin_path}/bin/ffmpeg", "-i", "-", # '-' for stdin #rest of yours ffmpeg command ], stdin=subprocess.PIPE, # Actually, here you can try to use S3 object stream as stdin for ffmpeg stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=-1, ) # Read from S3 stream and write to FFmpeg stdin chunk_size = 1024 * 1024 # Define chunk size (1 MB) while True: chunk = s3_stream.read(chunk_size) if not chunk: # Break if no more data to read break ffmpeg_process.stdin.write(chunk) # Close FFmpeg stdin ffmpeg_process.stdin.close() # Collect process output and error output, error = ffmpeg_process.communicate() # Return the FFmpeg output and error return output, error except Exception as e: print(f"Error accessing S3 object: {e}") return None, str(e) finally: # Ensure proper cleanup of resources if s3_stream and not s3_stream.closed: s3_stream.close() if hasattr(ffmpeg_process, "stdin"): ffmpeg_process.stdin.close() # Example usage: if __name__ == "__main__": file_name = "new.MOV" execute_ffmpeg_command(file_name, is_local=False)
def execute_ffmpeg_command(input_file: str):
    """
    Execute the given ffmpeg command against the input file.

    Args:
        input_file (str): Path to the input video file (e.g. file.mp4).
    """
    # Define the ffmpeg command as a list of arguments
    s3_stream = None
    ffmpeg_process = None
 
        try:
            output_path = "/tmp"
            s3_client = boto3.client("s3")
            # Get the S3 object as a stream
            s3_object = s3_client.get_object(Bucket="bucket_name", Key=input_file) #key is the name of a file in your S3 bucket

            s3_stream = s3_object["Body"]
            # Prepare ffmpeg process with input from the S3 object stream
            # s3_object_buffer = io.BytesIO(s3_object.read()) <- can try with this as well

            ffmpeg_process = subprocess.Popen(
                [
                    f"{bin_path}/bin/ffmpeg",
                    "-i",
                    "-",  # '-' for stdin
                    #rest of yours ffmpeg command
                ],
                stdin=subprocess.PIPE,  # Actually, here you can try to use S3 object stream as stdin for ffmpeg
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE,
                bufsize=-1,
            )
            # Read from S3 stream and write to FFmpeg stdin
            chunk_size = 1024 * 1024  # Define chunk size (1 MB)
            while True:
                chunk = s3_stream.read(chunk_size)
                if not chunk:  # Break if no more data to read
                    break
                ffmpeg_process.stdin.write(chunk)

            # Close FFmpeg stdin
            ffmpeg_process.stdin.close()

            # Collect process output and error
            output, error = ffmpeg_process.communicate()

            # Return the FFmpeg output and error

            return output, error
        except Exception as e:
            print(f"Error accessing S3 object: {e}")
            return None, str(e)

        finally:
            # Ensure proper cleanup of resources

            if s3_stream and not s3_stream.closed:
                s3_stream.close()

            if hasattr(ffmpeg_process, "stdin"):
                ffmpeg_process.stdin.close()

# Example usage:
if __name__ == "__main__":
    file_name = "new.MOV"
    execute_ffmpeg_command(file_name, is_local=False)

 


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *