|
| 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