diff --git a/README.md b/README.md index 529b3c2..5d1664d 100644 --- a/README.md +++ b/README.md @@ -1,107 +1,108 @@ -# AutoCrop-Vertical: A Smart Video Cropper for Social Media (Horizontal -> Vertical) +# OpenShorts 🚀🎬 -![Demo of AutoCrop-Vertical](https://github.com/kamilstanuch/Autocrop-vertical/blob/main/churchil_queen_vertical_short.gif?raw=true) +OpenShorts is an open-source tool designed to automate the creation of viral vertical content. It transforms long YouTube videos or local files into multiple short clips optimized for **TikTok**, **Instagram Reels**, and **YouTube Shorts**. -AutoCrop-Vertical is a Python script that automatically converts horizontal videos into a vertical format suitable for platforms like TikTok, Instagram Reels, and YouTube Shorts. - -Instead of a simple, static center crop, this script analyzes video content scene-by-scene. It uses object detection to locate people and decides whether to tightly crop the frame on the subjects or to apply letterboxing to preserve a wide shot's composition. +![OpenShorts Demo](https://github.com/kamilstanuch/Autocrop-vertical/blob/main/churchil_queen_vertical_short.gif?raw=true) +*(The demo shows the smart crop process, now powered by AI clip selection)* --- -### Key Features +## ✨ Key Features -* **Content-Aware Cropping:** Uses a YOLOv8 model to detect people and automatically centers the vertical frame on them. -* **Automatic Letterboxing:** If multiple people are too far apart for a vertical crop, the script automatically adds black bars (letterboxing) to show the full scene. -* **Scene-by-Scene Processing:** All decisions are made per-scene, ensuring a consistent and logical edit without jarring transitions. -* **Native Resolution:** The output resolution is dynamically calculated based on the source video's height to prevent quality loss from unnecessary upscaling. -* **High Performance:** The processing is offloaded to FFmpeg via a direct pipe, resulting in extremely fast encoding and low CPU usage. +OpenShorts goes beyond simple cropping. It combines the power of Artificial Intelligence to understand your video and extract pure gold: + +1. **🧠 AI Virality Detection (Gemini + Whisper):** + * Automatically transcribes video audio using **OpenAI Whisper**. + * Analyzes content with **Google Gemini 1.5 Flash** to identify moments with the highest viral potential (hooks, jokes, impactful conclusions). + * Generates SEO-optimized titles and descriptions for social media. + +2. **✂️ Smart Cropping (Auto-Crop):** + * Detects people and faces using **YOLOv8** and **OpenCV**. + * Keeps the subject always in the center of the vertical frame (9:16). + * Automatically applies *letterboxing* if multiple people are detected far apart to preserve context. + +3. **⚡ Fast Processing:** + * Downloads YouTube videos in the best compatible quality. + * Performs precise cuts based on exact timestamps dictated by AI. + * Uses FFmpeg for efficient rendering. --- -### Technical Details +## 🛠️ Requirements -This script is built on a pipeline that uses specialized libraries for each step: - -* **Core Libraries:** - * `PySceneDetect`: For accurate, content-aware scene cut detection. - * `Ultralytics (YOLOv8)`: For fast and reliable person detection. - * `OpenCV`: Used for frame manipulation, face detection (as a fallback), and reading video properties. - * `FFmpeg`: The backbone of the video encoding. The script pipes raw, processed frames directly to FFmpeg, which handles the final video encoding and audio merging. - * `tqdm`: For clean and informative progress bars in the console. - -* **Processing Pipeline:** - 1. The script first uses `PySceneDetect` to get a list of all scene timestamps. - 2. It then loops through each scene and uses `OpenCV` to extract a sample frame. - 3. This frame is passed to a pre-trained `yolov8n.pt` model to get bounding boxes for all detected people. - 4. A set of rules determines the strategy (`TRACK` or `LETTERBOX`) for each scene based on the number and position of the detected people. - 5. Finally, the script re-reads the input video, applies the planned transformation to every frame, and pipes the raw `bgr24` pixel data to an `FFmpeg` subprocess for efficient encoding. Audio is handled separately and merged at the end, also via FFmpeg. - -* **Performance & Optimizations:** - The main performance gain comes from avoiding slow, frame-by-frame processing within a pure Python loop for *writing* the video. By piping frames directly to FFmpeg's optimized C-based `libx264` encoder, we achieve significant speed. - - * **Example Benchmark:** On a test 5-minute, 640x360 source video, the entire analysis and conversion process completes in **~11 seconds** on an Apple M1 processor. The video encoding itself runs at over 70x real-time speed. +* **Docker** and **Docker Compose** installed on your machine. +* A **Google Gemini API Key** (Free at [Google AI Studio](https://aistudio.google.com/app/apikey)). --- -### Usage +## 🚀 Quick Start -#### Option A: Local Installation +### 1. Setup -1. **Clone the repository:** - ```bash - git clone https://github.com/kamilstanuch/AutoCrop-Vertical.git - cd AutoCrop-Vertical - ``` +Clone the repository and configure your API key: -2. **Set up the environment:** - A Python virtual environment is recommended. - ```bash - python3 -m venv .venv - source .venv/bin/activate - pip install -r requirements.txt - ``` - The `yolov8n.pt` model weights will be downloaded automatically on the first run. +```bash +# 1. Clone the repo +git clone https://github.com/your-username/OpenShorts.git +cd OpenShorts -3. **Run the script:** - You can provide a local file or a YouTube URL. +# 2. Configure environment variables +cp .env.example .env - **Local file:** - ```bash - python main.py --input path/to/video.mp4 --output path/to/output.mp4 - ``` +# 3. Edit .env and paste your GEMINI_API_KEY (must start with 'AIza...') +nano .env +``` - **YouTube URL:** - ```bash - # Auto-generates output name - python main.py --url "https://www.youtube.com/watch?v=VIDEO_ID" - - # Specific output name - python main.py --url "https://www.youtube.com/watch?v=VIDEO_ID" --output vertical_video.mp4 - ``` +### 2. Run with Docker -#### Option B: Running with Docker (Recommended) +No need to install Python or libraries on your system, everything runs in a container. -Docker is the easiest way to run the project as it includes all dependencies (FFmpeg, OpenCV, etc.) pre-configured. +**From a YouTube video:** +The system will download the video, search for viral clips, and deliver multiple vertical files ready for upload. -1. **Build the image:** - ```bash - docker compose build - ``` +```bash +docker compose run --rm autocrop -u "https://www.youtube.com/watch?v=YOUR_VIDEO_ID" +``` -2. **Process a local video:** - Place your video in a `videos/` folder (it will be mounted inside the container). - ```bash - docker compose run --rm autocrop -i /videos/input/video.mp4 -o /videos/output/video_vertical.mp4 - ``` +**From a local file:** +Place your video in the `videos/input/` folder. -3. **Process a YouTube video:** - ```bash - docker compose run --rm autocrop -u "https://www.youtube.com/watch?v=VIDEO_ID" -o /videos/output/vertical.mp4 - ``` +```bash +docker compose run --rm autocrop -i /videos/input/my_video.mp4 +``` + +### 3. Results + +Generated clips will appear in the `videos/` folder (or wherever you specified). Additionally, you'll find a `.json` file with the viral titles and descriptions suggested by AI for each clip. --- -### Prerequisites (for Local Installation) +## ⚙️ Advanced Options -* Python 3.8+ -* **FFmpeg:** This script requires `ffmpeg` to be installed and available in your system's PATH. It can be installed via a package manager (e.g., `brew install ffmpeg` on macOS, `sudo apt install ffmpeg` on Debian/Ubuntu). +| Flag | Description | +| :--- | :--- | +| `-u`, `--url` | YouTube URL to download and process. | +| `-i`, `--input` | Path to the local video file (inside the container). | +| `-o`, `--output` | Output directory or specific filename. | +| `--keep-original` | Keeps the original downloaded video after processing. | +| `--skip-analysis` | **Classic Mode:** Skips Gemini/Whisper AI and converts the ENTIRE video to flat vertical format. | + +--- + +## 🏗️ How It Works (Pipeline) + +1. **Download/Input**: Gets the source video in high quality. +2. **Transcription (Whisper)**: Converts all audio to text with word-level timestamps. +3. **Analysis (Gemini)**: AI reads the transcript and identifies the 3-15 most interesting segments, returning exact timestamps and metadata. +4. **Extraction**: Selected segments are cut from the original video. +5. **Vertical Conversion**: Each clip goes through the visual detection engine (YOLO) to dynamically reframe the action to 9:16 format. + +--- + +## 🤝 Contributions + +Contributions are welcome! If you have ideas to improve clip detection or cropping, feel free to open a PR. + +## 📄 License + +MIT License. Feel free to use it for your personal or commercial projects.