Coder / Plan.md
Abu1998's picture
Update Plan.md
a0b503f verified

A newer version of the Streamlit SDK is available: 1.53.0

Upgrade

Here’s a detailed plan and file structure for your YouTube Shorts creation system using Hugging Face and Streamlit, with credentials stored in a .env file:

File Structure:

youtube_shorts/
β”‚
β”œβ”€β”€ streamlit_app/
β”‚   └── app.py
β”‚
β”œβ”€β”€ scripts/
β”‚   β”œβ”€β”€ fetch_content.py
β”‚   β”œβ”€β”€ fetch_videos.py
β”‚   β”œβ”€β”€ process_videos.py
β”‚   └── color_correction.py
β”‚
β”œβ”€β”€ reference/
β”‚   └── reference_image.jpg
β”‚
β”œβ”€β”€ videos/
β”‚   └── final_videos/
β”‚
β”œβ”€β”€ .env
β”œβ”€β”€ requirements.txt
└── README.md

Scripts Overview:

  1. fetch_content.py: Uses the ChatGPT API from Hugging Face to generate trending content and extract keywords.
  2. fetch_videos.py: Uses the Pexels API to fetch stock videos based on the keywords.
  3. process_videos.py: Trims videos to 3 seconds and applies color correction from the reference image.
  4. color_correction.py: Handles the color correction based on the reference image.

Streamlit App:

  • app.py: Streamlit app to interact with the entire process, including fetching content, videos, processing, and displaying the results.

Credentials File:

  • .env: Stores API keys and sensitive information.

Google Colab Notebook Prompt:

# YouTube Shorts Automation with Streamlit

## Overview
This notebook automates the creation of YouTube Shorts by leveraging trending content generated via the Hugging Face API, fetching stock videos from the Pexels API, and applying color correction based on a reference image. The final videos are trimmed to 3 seconds and saved in a specified directory. The Streamlit app facilitates interaction with this system.

## Setup
1. **Install Required Libraries**
    - Install required Python packages including `requests`, `Pillow`, `moviepy`, `opencv-python`, `python-dotenv`, and `streamlit`.

2. **Import Necessary Modules**
    - Import modules from `fetch_content`, `fetch_videos`, `process_videos`, and `color_correction`.

3. **Configure Environment Variables**
    - Ensure the `.env` file contains your API keys for Hugging Face and Pexels.

## Steps

### 1. Fetch Trending Content
- Use the `fetch_content.py` script to interact with the Hugging Face API.
- Generate trending content and extract relevant keywords.

```python
!python3 scripts/fetch_content.py

2. Fetch Stock Videos

  • Use the fetch_videos.py script to fetch videos from Pexels based on the keywords.
  • Ensure that no video exceeds 3 seconds. If necessary, trim longer videos.
!python3 scripts/fetch_videos.py

3. Process Videos

  • Use the process_videos.py script to trim videos to 3 seconds.
  • Apply color correction based on the reference/ directory’s reference_image.jpg.
!python3 scripts/process_videos.py

4. Save Final Videos

  • Save all processed videos in the videos/final_videos/ directory.
!python3 scripts/save_videos.py

5. Run Streamlit App

  • Start the Streamlit app to interact with the entire process.
!streamlit run streamlit_app/app.py

Notes

  • Ensure API keys are correctly set in the .env file.
  • Check the requirements.txt for necessary package installations.

Conclusion

Run each script in the specified order to automate the creation of engaging YouTube Shorts content. Use the Streamlit app to interact with the process and view results in the videos/final_videos/ directory.


### **Scripts Details:**

#### **`fetch_content.py`**

```python
import requests
import os
from dotenv import load_dotenv

load_dotenv()

def fetch_trending_content():
    api_key = os.getenv('HUGGINGFACE_API_KEY')
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    }
    data = {
        "model": "gpt-4",
        "prompt": "Generate trending content and keywords for YouTube Shorts.",
        "max_tokens": 150
    }
    response = requests.post("https://api.huggingface.co/v1/models/gpt-4/completions", headers=headers, json=data)
    content = response.json()
    return content["choices"][0]["text"].strip()

fetch_videos.py

import requests
import os
from dotenv import load_dotenv

load_dotenv()

def fetch_videos(keywords):
    api_key = os.getenv('PEXELS_API_KEY')
    url = "https://api.pexels.com/v1/search"
    headers = {
        "Authorization": api_key
    }
    params = {
        "query": keywords,
        "per_page": 20
    }
    response = requests.get(url, headers=headers, params=params)
    videos = response.json()
    return videos["videos"]

process_videos.py

from moviepy.editor import VideoFileClip
import os
from color_correction import apply_color_correction

def trim_and_correct_videos(video_folder, output_folder, reference_image):
    for video_file in os.listdir(video_folder):
        if video_file.endswith(".mp4"):
            video_path = os.path.join(video_folder, video_file)
            output_path = os.path.join(output_folder, video_file)

            clip = VideoFileClip(video_path)
            if clip.duration > 3:
                clip = clip.subclip(0, 3)
            clip = apply_color_correction(clip, reference_image)
            clip.write_videofile(output_path, codec="libx264")

color_correction.py

from PIL import Image
import numpy as np
import cv2

def apply_color_correction(video_clip, reference_image_path):
    reference_image = Image.open(reference_image_path)
    reference_array = np.array(reference_image)
    
    def process_frame(frame):
        frame_array = np.array(frame)
        # Apply color correction here (simple example: adjust brightness)
        corrected_frame = cv2.add(frame_array, np.array([10, 10, 10]))
        return Image.fromarray(corrected_frame)

    return video_clip.fl_image(process_frame)

requirements.txt

requests
Pillow
moviepy
opencv-python
python-dotenv
streamlit

README.md

Include instructions on how to set up the environment, obtain API keys, and run each script and the Streamlit app.

This setup will automate the process of creating engaging YouTube Shorts with trending content and processed videos.