-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_video_processing.py
More file actions
57 lines (48 loc) · 1.68 KB
/
test_video_processing.py
File metadata and controls
57 lines (48 loc) · 1.68 KB
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
import os
import urllib.request
import cv2
# Define directories
videos_dir = 'videos'
if not os.path.exists(videos_dir):
os.makedirs(videos_dir)
# Function to download video
def download_video(url, output_path):
urllib.request.urlretrieve(url, output_path)
print(f'Downloaded {output_path}')
# URL of a sample video
sample_url = 'https://www.youtube.com/watch?v=K4TOrB7at0Y'
video_path = os.path.join(videos_dir, 'sample.mp4')
converted_video_path = os.path.join(videos_dir, 'sample_converted.avi')
# Download the sample video
download_video(sample_url, video_path)
# Check if the video file exists and has a non-zero size
if os.path.exists(video_path) and os.path.getsize(video_path) > 0:
print(f'Video file exists: {video_path}, size: {os.path.getsize(video_path)} bytes')
else:
print(f'Failed to download the video or video file is empty: {video_path}')
# Convert video to a more compatible format using ffmpeg
os.system(f'ffmpeg -i {video_path} {converted_video_path}')
# Function to extract frames from video
def extract_frames(video_path):
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
print(f'Error: Could not open video {video_path}')
return []
frames = []
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
frames.append(frame)
cap.release()
return frames
# Extract frames from the converted video
frames = extract_frames(converted_video_path)
print(f'Extracted {len(frames)} frames from {converted_video_path}')
# Display the first frame
if frames:
cv2.imshow('First Frame', frames[0])
cv2.waitKey(0)
cv2.destroyAllWindows()
else:
print('No frames extracted')