File size: 6,299 Bytes
a0b503f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
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:**

```markdown
# 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.

```python
!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`.

```python
!python3 scripts/process_videos.py
```

### 4. Save Final Videos
- Save all processed videos in the `videos/final_videos/` directory.

```python
!python3 scripts/save_videos.py
```

### 5. Run Streamlit App
- Start the Streamlit app to interact with the entire process.

```python
!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`**

```python
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`**

```python
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`**

```python
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.