Skip to main content
update wording
Source Link

I am transcoding video to different quality using ffmpeg using subprocess in Python. I want to make my transcoding faster,. I tried multi-threading but it didn't help. Hardware acceleration is not possible as we are using an AWS EC2 t3.large instance at this time, which has 2 CPUs, 8GiB memory, and 30% baseline performance. SuggestCan you suggest something maybe regarding the ffmpeg command or update my pythonthe code.?

I am transcoding video to different quality using ffmpeg using subprocess in Python. I want to make my transcoding faster, I tried multi-threading but it didn't help. Hardware acceleration is not possible as we are using AWS EC2 t3.large at this time, which has 2 CPUs, 8GiB memory, and 30% baseline performance. Suggest something maybe regarding ffmpeg command or update my python code.

I am transcoding video to different quality using ffmpeg using subprocess in Python. I want to make my transcoding faster. I tried multi-threading but it didn't help. Hardware acceleration is not possible as we are using an AWS EC2 t3.large instance at this time, which has 2 CPUs, 8GiB memory, and 30% baseline performance. Can you suggest something maybe regarding the ffmpeg command or update the code?

Made title about what code does instead of concerns about the code.
Link
pacmaninbw
  • 26.1k
  • 13
  • 47
  • 114

how can I make ffmpeg Video transcoding fasterusing ffmpeg

Source Link

how can I make ffmpeg transcoding faster

I am transcoding video to different quality using ffmpeg using subprocess in Python. I want to make my transcoding faster, I tried multi-threading but it didn't help. Hardware acceleration is not possible as we are using AWS EC2 t3.large at this time, which has 2 CPUs, 8GiB memory, and 30% baseline performance. Suggest something maybe regarding ffmpeg command or update my python code.

def transcode_video(video, quality, file_name):
try:
    quality_mapping = {
        "360p": "640x360",
        "480p": "854x480",
        "720p": "1280x720",
        "1080p": "1920x1080"
    }

    if quality not in quality_mapping:
        raise ValueError(f"Quality '{quality}' not supported")

    video_name, video_ext = os.path.splitext(file_name)

    # Construct output path with quality suffix
    output_path = f"{video_name}_{quality}{video_ext}"

    # Create FFmpeg object with streaming
    ffmpeg_command = [
        'ffmpeg',
        '-i', 'pipe:0',
        '-vf', f'scale={quality_mapping[quality]}',
        '-c:a', 'aac',
        '-c:v', 'libx264',
        '-preset', 'ultrafast',
        '-crf', '23',
        '-strict', 'experimental',
        output_path
    ]

    # Use subprocess.PIPE for stdin and stdout
    with subprocess.Popen(ffmpeg_command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as process:
        # Write video content to stdin
        process.stdin.write(video.read())
        process.stdin.close()  # Close stdin to indicate the end of input

        # Wait for the process to finish
        process.wait()

    return output_path

except FileNotFoundError as e:
    # Clean up temporary file
    # if 'temp_file_path' in locals() and os.path.exists(temp_file_path):
    #     os.remove(temp_file_path)
    # Remove output file after returning
    if 'output_path' in locals() and os.path.exists(output_path):
        os.remove(output_path)

    raise e
except subprocess.CalledProcessError as e:
    if 'output_path' in locals() and os.path.exists(output_path):
        os.remove(output_path)

    raise e
except Exception as e:
    if 'output_path' in locals() and os.path.exists(output_path):
        os.remove(output_path)
    raise e