Skip to content

Commit

Permalink
add: video processing tool
Browse files Browse the repository at this point in the history
  • Loading branch information
king04aman authored Oct 25, 2024
1 parent 64e4528 commit 54879a6
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 0 deletions.
54 changes: 54 additions & 0 deletions Video Processor/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Video Processing Tool

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.

## Features

- Extracts clips of a specified duration from the input video.
- Applies image processing effects including brightness, contrast, saturation adjustments, and blurring.
- Generates simple subtitle files for each clip.

## Requirements

To run this script, you'll need the following Python packages:

- `opencv-python`
- `pysrt`

You can install these using pip:

```bash
pip install opencv-python pysrt
```

## Usage

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.

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.

3. **Run the Script**: Execute the script using Python:

```bash
python video_processor.py
```

4. **Output**:
- The processed clips will be saved as `clip0_out.mp4`, `clip1_out.mp4`, etc.
- Corresponding subtitle files will be saved as `clip0_subtitle.srt`, `clip1_subtitle.srt`, etc.

## Code Explanation

- **Video Capture**: The script uses OpenCV to read frames from the video.
- **Image Processing**: Each frame within the clip duration is processed using filters and effects:
- **Brightness and Contrast**: Adjusts the brightness and contrast of the frame.
- **Saturation**: Modifies the saturation of the frame.
- **Blurring**: Applies Gaussian blur and combines with a weighted blend for a smoother look.

- **Subtitle Generation**: For each clip, a simple subtitle text ("This is a sample subtitle") is generated and saved in a `.srt` file format.

## Limitations

- The script assumes the video file can be read directly via OpenCV, which may not be applicable for all YouTube links without prior downloading.
- Subtitle text is static; you may want to customize it based on the content of each clip.

2 changes: 2 additions & 0 deletions Video Processor/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
opencv-python==4.8.0.76
pysrt==1.1.2
68 changes: 68 additions & 0 deletions Video Processor/video_processor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import cv2
import pysrt

# Load the input video
input_file = input("Enter the YouTube video link: ")
cap = cv2.VideoCapture(input_file)

# Set the start and end times for each short video clip
clip_duration = 10.0
clip_start_time = 0.0
clip_end_time = clip_start_time + clip_duration

# Set up OpenCV for video processing
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
brightness = 30
contrast = 1.5
saturation = 1.5

# Process each short video clip
i = 0
while cap.isOpened():
# Read the next frame from the input video
ret, frame = cap.read()
if not ret:
break

# Get the current time in seconds
current_time = cap.get(cv2.CAP_PROP_POS_MSEC) / 1000.0

# If the current time is within the current clip, process the frame
if current_time >= clip_start_time and current_time <= clip_end_time:
# Apply the filters and effects
frame = cv2.filter2D(frame, -1, kernel)
frame = cv2.convertScaleAbs(frame, alpha=contrast, beta=brightness)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(frame)
s = cv2.convertScaleAbs(s, alpha=saturation, beta=0)
frame = cv2.merge((h, s, v))
frame = cv2.cvtColor(frame, cv2.COLOR_HSV2BGR)
frame = cv2.GaussianBlur(frame, (5,5), 0)
frame = cv2.addWeighted(frame, 1.5, cv2.blur(frame, (10,10)), -0.5, 0)

# Write the modified frame to a new video file
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))))
out.write(frame)
out.release()

# Generate subtitles for the clip
subtitle_text = "This is a sample subtitle"
subtitle_duration = clip_duration
subtitle_file = pysrt.SubRipFile()
subtitle = pysrt.SubRipItem(index=1, start=0, end=subtitle_duration, text=subtitle_text)
subtitle_file.append(subtitle)
subtitle_file.save('clip' + str(i) + '_subtitle.srt')

# Move to the next clip
i += 1
clip_start_time += clip_duration
clip_end_time += clip_duration

# If the current time is past the end of the current clip, move to the next clip
elif current_time > clip_end_time:
clip_start_time += clip_duration
clip_end_time += clip_duration

# Release the resources
cap.release()
cv2.destroyAllWindows()

0 comments on commit 54879a6

Please sign in to comment.