An open API service indexing awesome lists of open source software.

https://github.com/ibrahimjspy/ai-speed-ramping

A Flask-based web app that applies AI-powered speed ramping to video clips by analyzing motion using optical flow and dynamically adjusting playback speed with FFmpeg. Upload or link a video to get cinematic speed transitions automatically.
https://github.com/ibrahimjspy/ai-speed-ramping

ai-video computer-vision editing editing-videos ffmpeg flask machine-learning opencv python speed-ramp video-processing

Last synced: 17 days ago
JSON representation

A Flask-based web app that applies AI-powered speed ramping to video clips by analyzing motion using optical flow and dynamically adjusting playback speed with FFmpeg. Upload or link a video to get cinematic speed transitions automatically.

Awesome Lists containing this project

README

        

# AI Speed Ramping Flask App

A Flask-based web application that applies **motion-intensityโ€“driven speed ramping** to video clips using **OpenCV**, **NumPy**, and **FFmpeg**. This app allows users to upload a short video or provide a remote video URL, analyze motion across frames using optical flow, and generate a dynamic playback effectโ€”speeding up static moments and slowing down action-packed ones.

---

## ๐Ÿง  What It Does

This app intelligently alters video playback speed by analyzing how much movement occurs in each segment of the video:

- **Low-motion segments** (e.g., still frames or minimal camera movement) are played faster (up to 2ร—).
- **High-motion segments** (e.g., fast action or transitions) are slowed down (down to 0.5ร—).
- The result is a cinematic โ€œspeed rampโ€ effectโ€”commonly used in sports replays, music videos, and stylistic transitions.

---

## ๐Ÿš€ Features

- **๐ŸŽฅ Optical Flow Motion Analysis**
Uses OpenCV to compute pixel-level motion between frames.

- **๐Ÿงฎ Dynamic Speed Mapping**
Maps each segment to a speed multiplier based on detected motion.

- **๐ŸŽž๏ธ FFmpeg Segment Processing**
Applies speed changes per segment and stitches them back into a final video.

- **๐Ÿ–ฅ๏ธ Simple Web UI**
Upload your clip via a browser, watch it get transformed, and download the result.

- **โš™๏ธ Configurable Parameters**
Customize segment duration, minimum/maximum speeds, and file upload limits via `config.py`.

- **๐Ÿงฉ API Methods**
Two powerful endpoints to programmatically retrieve speed mapping:
- Upload a file and get back the motion-based speed list.
- Send a remote video URL (e.g. S3) and retrieve speed data.

---

## ๐Ÿงพ Requirements

- Python 3.8+
- [FFmpeg](https://ffmpeg.org/) installed and available in your system PATH
- Virtualenv (recommended)

### Python dependencies (`requirements.txt`):

```text
Flask>=2.0
Werkzeug>=2.0
opencv-python>=4.5
numpy>=1.19
moviepy>=1.0
requests>=2.25
````

---

## ๐Ÿ—‚ Project Structure

```
ai-speed-ramping/
โ”œโ”€โ”€ app.py # Flask application entrypoint
โ”œโ”€โ”€ config.py # Global configuration settings
โ”œโ”€โ”€ static/
โ”‚ โ””โ”€โ”€ styles.css # Basic form styling
โ”œโ”€โ”€ templates/
โ”‚ โ”œโ”€โ”€ index.html # Upload interface
โ”‚ โ””โ”€โ”€ result.html # Download page
โ”œโ”€โ”€ processing/
โ”‚ โ”œโ”€โ”€ analyzer.py # Optical flow motion detection and speed mapping
โ”‚ โ””โ”€โ”€ ffmpeg_utils.py # FFmpeg segment speed changes and merging
โ”œโ”€โ”€ uploads/ # Temporary input files
โ”œโ”€โ”€ output/ # Final processed files
โ”œโ”€โ”€ requirements.txt # Python dependencies
โ””โ”€โ”€ README.md # This file
```

---

## โš™๏ธ Configuration

You can configure the app through `config.py`:

```python
UPLOAD_FOLDER = "uploads/"
OUTPUT_FOLDER = "output/"
ALLOWED_EXTENSIONS = {"mp4", "mov", "avi"}
MAX_CONTENT_LENGTH = 50 * 1024 * 1024 # 50 MB

MIN_SPEED = 0.5 # Minimum playback speed
MAX_SPEED = 2.0 # Maximum playback speed
SEGMENT_DURATION = 0.5 # Seconds per speed-analyzed segment
```

---

## ๐Ÿงช Usage

1. **Start the server**

```bash
flask run
```

2. **Open your browser**

Navigate to:
`http://127.0.0.1:5000/`

3. **Upload a clip**
Choose a video and submit. Wait for processing.

4. **Download result**
After processing, download the ramped video with dynamic speed changes.

---

## ๐Ÿ“ก API Endpoints

#### `GET /`

Returns the upload form UI.

#### `POST /process`

Uploads a video, applies speed mapping, and returns a download link for the ramped result.

#### `POST /get-speeds`

**Purpose**: Upload a video and receive the speed mapping JSON.

**Form Fields**:

* `file`: Video file
* `segment_duration`: Optional (defaults to `config.py`)
* `min_speed`: Optional
* `max_speed`: Optional

**Response**:

```json
{
"speeds": [1.0, 0.6, 2.0, ...],
"segment_duration": 0.5,
"min_speed": 0.5,
"max_speed": 2.0
}
```

---

#### `POST /get-speeds-from-url`

**Purpose**: Provide a video URL (e.g., S3 link), download it, and return the motion-based speed map.

**JSON Payload**:

```json
{
"video_url": "https://s3.amazonaws.com/your-bucket/video.mp4",
"segment_duration": 0.5,
"min_speed": 0.5,
"max_speed": 2.0
}
```

**Response**:

```json
{
"speeds": [...],
"segment_duration": 0.5,
"min_speed": 0.5,
"max_speed": 2.0
}
```

---

## ๐Ÿงน Maintenance Tips

* Clean out `uploads/` and `output/` regularly or use a cron job to prevent disk fill-up.
* Make use of the cleanup logic built into new API methods.

---

## ๐Ÿงช Development

* **Debug Logging**: Adjust print/debug statements in `app.py` or refactor with logging module.
* **Docker Support** (Optional): Add Dockerfile + volume mounting for portability and scalability.

---

## ๐Ÿค Contributing

1. Fork this repository
2. Create a feature branch
`git checkout -b feature/YourFeature`
3. Commit your changes
`git commit -am "Add your feature"`
4. Push and create a PR

Include tests or usage examples if adding new logic.

---

## ๐Ÿชช License

MIT License. See [LICENSE](LICENSE) for details.