Skip to content

Commit 54879a6

Browse files
authored
add: video processing tool
1 parent 64e4528 commit 54879a6

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

Video Processor/README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Video Processing Tool
2+
3+
In this Python tool processes a video file by extracting short clips, applying filters and effects, and generating subtitles for each clip. The processed clips are saved as new video files along with corresponding subtitle files.
4+
5+
## Features
6+
7+
- Extracts clips of a specified duration from the input video.
8+
- Applies image processing effects including brightness, contrast, saturation adjustments, and blurring.
9+
- Generates simple subtitle files for each clip.
10+
11+
## Requirements
12+
13+
To run this script, you'll need the following Python packages:
14+
15+
- `opencv-python`
16+
- `pysrt`
17+
18+
You can install these using pip:
19+
20+
```bash
21+
pip install opencv-python pysrt
22+
```
23+
24+
## Usage
25+
26+
1. **Input Video**: The script prompts you to enter the link to a YouTube video. Make sure to use a video URL that is accessible and can be downloaded.
27+
28+
2. **Clip Duration**: The default duration for each extracted clip is set to 10 seconds. You can modify this value in the script as needed.
29+
30+
3. **Run the Script**: Execute the script using Python:
31+
32+
```bash
33+
python video_processor.py
34+
```
35+
36+
4. **Output**:
37+
- The processed clips will be saved as `clip0_out.mp4`, `clip1_out.mp4`, etc.
38+
- Corresponding subtitle files will be saved as `clip0_subtitle.srt`, `clip1_subtitle.srt`, etc.
39+
40+
## Code Explanation
41+
42+
- **Video Capture**: The script uses OpenCV to read frames from the video.
43+
- **Image Processing**: Each frame within the clip duration is processed using filters and effects:
44+
- **Brightness and Contrast**: Adjusts the brightness and contrast of the frame.
45+
- **Saturation**: Modifies the saturation of the frame.
46+
- **Blurring**: Applies Gaussian blur and combines with a weighted blend for a smoother look.
47+
48+
- **Subtitle Generation**: For each clip, a simple subtitle text ("This is a sample subtitle") is generated and saved in a `.srt` file format.
49+
50+
## Limitations
51+
52+
- The script assumes the video file can be read directly via OpenCV, which may not be applicable for all YouTube links without prior downloading.
53+
- Subtitle text is static; you may want to customize it based on the content of each clip.
54+

Video Processor/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
opencv-python==4.8.0.76
2+
pysrt==1.1.2

Video Processor/video_processor.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import cv2
2+
import pysrt
3+
4+
# Load the input video
5+
input_file = input("Enter the YouTube video link: ")
6+
cap = cv2.VideoCapture(input_file)
7+
8+
# Set the start and end times for each short video clip
9+
clip_duration = 10.0
10+
clip_start_time = 0.0
11+
clip_end_time = clip_start_time + clip_duration
12+
13+
# Set up OpenCV for video processing
14+
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
15+
brightness = 30
16+
contrast = 1.5
17+
saturation = 1.5
18+
19+
# Process each short video clip
20+
i = 0
21+
while cap.isOpened():
22+
# Read the next frame from the input video
23+
ret, frame = cap.read()
24+
if not ret:
25+
break
26+
27+
# Get the current time in seconds
28+
current_time = cap.get(cv2.CAP_PROP_POS_MSEC) / 1000.0
29+
30+
# If the current time is within the current clip, process the frame
31+
if current_time >= clip_start_time and current_time <= clip_end_time:
32+
# Apply the filters and effects
33+
frame = cv2.filter2D(frame, -1, kernel)
34+
frame = cv2.convertScaleAbs(frame, alpha=contrast, beta=brightness)
35+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
36+
h, s, v = cv2.split(frame)
37+
s = cv2.convertScaleAbs(s, alpha=saturation, beta=0)
38+
frame = cv2.merge((h, s, v))
39+
frame = cv2.cvtColor(frame, cv2.COLOR_HSV2BGR)
40+
frame = cv2.GaussianBlur(frame, (5,5), 0)
41+
frame = cv2.addWeighted(frame, 1.5, cv2.blur(frame, (10,10)), -0.5, 0)
42+
43+
# Write the modified frame to a new video file
44+
out = cv2.VideoWriter('clip' + str(i) + '_out.mp4', cv2.VideoWriter_fourcc(*'mp4v'), cap.get(cv2.CAP_PROP_FPS), (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))))
45+
out.write(frame)
46+
out.release()
47+
48+
# Generate subtitles for the clip
49+
subtitle_text = "This is a sample subtitle"
50+
subtitle_duration = clip_duration
51+
subtitle_file = pysrt.SubRipFile()
52+
subtitle = pysrt.SubRipItem(index=1, start=0, end=subtitle_duration, text=subtitle_text)
53+
subtitle_file.append(subtitle)
54+
subtitle_file.save('clip' + str(i) + '_subtitle.srt')
55+
56+
# Move to the next clip
57+
i += 1
58+
clip_start_time += clip_duration
59+
clip_end_time += clip_duration
60+
61+
# If the current time is past the end of the current clip, move to the next clip
62+
elif current_time > clip_end_time:
63+
clip_start_time += clip_duration
64+
clip_end_time += clip_duration
65+
66+
# Release the resources
67+
cap.release()
68+
cv2.destroyAllWindows()

0 commit comments

Comments
 (0)