Switch to unified view

a b/youtube_video-checkpoint.py
1
import yt_dlp
2
import os
3
import imageio
4
5
# Set the YouTube video URL and directories
6
video_url = 'https://www.youtube.com/watch?v=rfscVS0vtbw'  # Replace with another URL
7
data_root = 'C:/Users/Vishal R/OneDrive/Desktop/esophageal/data/'  # Replace with the correct path
8
video_dir = 'surgical_video_src'
9
output_dir = 'surgical_video_frames'
10
11
# Check if the root path exists
12
if not os.path.exists(data_root):
13
    print(f"The path {data_root} does not exist. Please check the path.")
14
    exit()
15
16
# Create directories for video and output frames if they don't exist
17
video_path = os.path.join(data_root, video_dir)
18
output_path = os.path.join(data_root, output_dir)
19
20
if not os.path.exists(video_path):
21
    os.makedirs(video_path)
22
23
if not os.path.exists(output_path):
24
    os.makedirs(output_path)
25
26
# Download the video using yt-dlp
27
ydl_opts = {
28
    'outtmpl': os.path.join(video_path, '%(id)s.%(ext)s'),  # Set the output file name
29
}
30
31
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
32
    info_dict = ydl.extract_info(video_url, download=True)
33
    filename = ydl.prepare_filename(info_dict)
34
35
# Read the video using imageio
36
vid = imageio.get_reader(filename, 'ffmpeg')
37
38
# Define the start and end frames for extraction
39
start_frame = 340
40
end_frame = 500
41
42
# Loop through the frames and save them as images
43
for frame in range(start_frame, end_frame):
44
    image = vid.get_data(frame)
45
    output_filename = os.path.join(output_path, f"frame_{frame - start_frame:04d}.png")
46
    imageio.imwrite(output_filename, image)
47
    print(f"Saved: {output_filename}")
48
49
print("Video frames extracted successfully.")
50
51
52