Intelligent video reframing library that automatically crops videos to different aspect ratios while preserving important content
Project description
PyAutoFlip
A Python library for saliency-aware video cropping that automatically reframes videos to different aspect ratios while preserving important content.
Note: This is a Python implementation inspired by MediaPipe's AutoFlip. The original MediaPipe AutoFlip solution is no longer actively supported, so this project provides a maintained alternative using similar techniques.
What it does
PyAutoFlip analyzes videos to identify salient content (faces, objects, motion) and intelligently crops frames to fit target aspect ratios. This is useful for adapting content between different platforms (e.g., landscape videos for portrait social media formats).
Examples
Landscape to 9:16 — the saliency method automatically tracks the most important region:
When two people are too far apart for a single crop, it automatically creates a split-screen layout:
Handles multi-person scenes by focusing on the salient subject:
Installation
# From PyPI
pip install pyautoflip
Quick Start
Command Line
# Convert a landscape video to portrait (9:16)
pyautoflip reframe -i input.mp4 -o output.mp4
# Use saliency-based detection (UNISAL + face detection)
pyautoflip reframe -i input.mp4 -o output.mp4 --method saliency
# Convert to square format
pyautoflip reframe -i input.mp4 -o output.mp4 --aspect-ratio 1:1
# Enable debug visualizations
pyautoflip reframe -i input.mp4 -o output.mp4 --debug
Python API
from pyautoflip import reframe_video
# Basic usage (detection method - face/object detection)
reframe_video(
input_path="input.mp4",
output_path="output.mp4",
target_aspect_ratio="9:16"
)
# Saliency-based method (better for complex content)
reframe_video(
input_path="input.mp4",
output_path="output.mp4",
target_aspect_ratio="9:16",
detection_method="saliency"
)
# With options
reframe_video(
input_path="input.mp4",
output_path="output.mp4",
target_aspect_ratio="1:1",
motion_threshold=0.3, # Lower = more stable crops
padding_method="blur", # or "solid_color"
detection_method="saliency",
debug_mode=True
)
Detection Methods
PyAutoFlip supports two detection methods for determining what to keep in frame:
detection (default)
Uses InsightFace for face detection and MediaPipe for object detection. Fast and reliable for content with clear subjects (people, animals, text). Assigns priority weights to different object types (faces > people > animals > text).
saliency
Uses UNISAL saliency maps combined with InsightFace face detection. Better for complex scenes where important content isn't just faces/objects. Features:
- UNISAL saliency: Learns what draws human visual attention from data, via ONNX Runtime for fast CPU inference
- Face-aware: Combines saliency with face detection, filters out false faces (portraits, posters) by size
- Adaptive crop width: Uses narrow (exact AR) or wide (+30% with blur padding) crop per scene based on saliency spread
- Split-screen: Automatically detects when two faces are too far apart for one crop (e.g., podcast wide shots) and renders a 2-panel split layout
- Temporal stabilization: Per-scene camera motion classification (stationary/panning/tracking) with trajectory smoothing
How it works
- Scene Detection: Identifies scene boundaries using PySceneDetect
- Content Analysis: Samples key frames per scene and runs detection
- Detection method: InsightFace faces + MediaPipe objects with priority weights
- Saliency method: UNISAL saliency maps + InsightFace faces (size-filtered) on downscaled frames
- Crop Computation: Determines optimal crop regions per frame
- Fixed-width crop windows centered on the saliency center of mass
- Per-scene crop width decision (narrow vs wide with padding)
- Temporal Smoothing: Camera motion classification (STATIONARY/PANNING/TRACKING) with appropriate stabilization per scene
- Output: Applies crops with blur/solid padding and recombines with original audio
Options
| Option | Description | Default |
|---|---|---|
--aspect-ratio |
Target aspect ratio (e.g., "9:16", "1:1", "4:3") | 9:16 |
--method |
Detection method: detection or saliency |
detection |
--motion-threshold |
Camera motion sensitivity (0.0 = stable, 1.0 = allow motion) | 0.5 |
--padding-method |
Padding style: blur or solid_color |
blur |
--debug |
Enable debug mode with visualizations and logging | off |
Requirements
- Python 3.10+
- FFmpeg (for video processing)
System dependencies
Ubuntu/Debian:
sudo apt-get install ffmpeg libgl1-mesa-glx libglib2.0-0
macOS:
brew install ffmpeg
Development
git clone https://github.com/AhmedHisham1/pyautoflip.git
cd pyautoflip
uv sync
License
MIT License - see LICENSE file for details.
Acknowledgments
- MediaPipe AutoFlip for the original concept and methodology
- UNISAL for the saliency detection model
- InsightFace for face detection
- MediaPipe for object detection
- PySceneDetect for scene analysis
- ONNX Runtime for fast CPU inference
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyautoflip-0.2.1.tar.gz.
File metadata
- Download URL: pyautoflip-0.2.1.tar.gz
- Upload date:
- Size: 57.6 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
da2cfea93e62b9cf0265c596faa174d7aad65a9800851a8dac0732fd631ee96e
|
|
| MD5 |
8de799a2a1ec86ec4df8ce13f116cf36
|
|
| BLAKE2b-256 |
3b6f4fa9adc012ad95efaf2be65936f838d5c2ab5749d562c1f3fdf48a4e1f23
|
Provenance
The following attestation bundles were made for pyautoflip-0.2.1.tar.gz:
Publisher:
publish-pypi.yml on AhmedHisham1/pyautoflip
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyautoflip-0.2.1.tar.gz -
Subject digest:
da2cfea93e62b9cf0265c596faa174d7aad65a9800851a8dac0732fd631ee96e - Sigstore transparency entry: 1189555459
- Sigstore integration time:
-
Permalink:
AhmedHisham1/pyautoflip@886e29d0cb0d33f648c31e06f6b97bbdddacecee -
Branch / Tag:
refs/heads/main - Owner: https://github.com/AhmedHisham1
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@886e29d0cb0d33f648c31e06f6b97bbdddacecee -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file pyautoflip-0.2.1-py3-none-any.whl.
File metadata
- Download URL: pyautoflip-0.2.1-py3-none-any.whl
- Upload date:
- Size: 56.8 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
34dfcc6cfbf3c142c927345f18baf5f16219b017997c247ed69bb5205130b629
|
|
| MD5 |
d850f9dddb56cb261d1d3e52497e0743
|
|
| BLAKE2b-256 |
60f9b6e3fab800b1a02862c25d111e348117b6626773988f1b632868ec11d498
|
Provenance
The following attestation bundles were made for pyautoflip-0.2.1-py3-none-any.whl:
Publisher:
publish-pypi.yml on AhmedHisham1/pyautoflip
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyautoflip-0.2.1-py3-none-any.whl -
Subject digest:
34dfcc6cfbf3c142c927345f18baf5f16219b017997c247ed69bb5205130b629 - Sigstore transparency entry: 1189555472
- Sigstore integration time:
-
Permalink:
AhmedHisham1/pyautoflip@886e29d0cb0d33f648c31e06f6b97bbdddacecee -
Branch / Tag:
refs/heads/main - Owner: https://github.com/AhmedHisham1
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@886e29d0cb0d33f648c31e06f6b97bbdddacecee -
Trigger Event:
workflow_dispatch
-
Statement type: