This repository contains a Python script to compress video files using moviepy
and Pillow
libraries. The script resizes video frames to a specified resolution and adjusts the bitrate dynamically based on the input video's properties to achieve efficient compression while maintaining quality.
- Video Resizing: Resizes video frames to a target resolution using high-quality downsampling with the
Pillow
library. - Dynamic Bitrate Adjustment: Calculates and applies a dynamic bitrate based on the input video’s original bitrate and resolution.
- Simple and Efficient: Easy to use script that handles video compression efficiently, suitable for videos of various sizes.
- Python 3.x
moviepy
libraryPillow
library
Install the required libraries using pip:
pip install moviepy pillow numpy
- Place your input video file in the same directory as the script or provide the full path to the video file.
- Update the
input_video_path
andoutput_video_path
variables in the script with your input and desired output file paths. - Run the script:
python compress_video.py
from moviepy.editor import VideoFileClip
import os
def get_vid_dynamic_bitrate(video_path, resolution_target) -> str:
vid_clip = VideoFileClip(video_path)
current_bitrate = vid_clip.reader.bitrate
current_resolution = vid_clip.size
scaling_factor = ( (resolution_target[0] * resolution_target[1]) / (current_resolution[0] * current_resolution[1]) )
return f"{int(current_bitrate * scaling_factor)}k"
def video_compressor( input_path, output_path, target_resolution=(1280, 720) ) -> None:
# Get video
vid_clip = VideoFileClip(input_path)
# Resizing the video
resized_clip = vid_clip.resize(height=target_resolution[1])
# Getting dynamic bitrate based on the current video properties
dynamic_bitrate = get_vid_dynamic_bitrate(input_path, target_resolution)
# Saving the resized video
resized_clip.write_videofile(output_path, bitrate=dynamic_bitrate, codec="libx264")
return None
if __name__ == "__main__":
video_path = "transformer.mp4"
output_video_path = "compressed.mp4"
# Compress Video
video_compressor(video_path, output_video_path)
print(f"{video_path} was successfuly compressed and saved as {output_video_path}")
- Ensure you have enough memory and processing power to handle large video files.
- Adjust the
target_resolution
anddynamic_bitrate
as needed to achieve the desired compression quality and file size.
This project is licensed under the MIT License - see the LICENSE file for details.
Feel free to customize this description further to match your repository structure and additional details you might want to include.