Spaces:
No application file
No application file
File size: 2,433 Bytes
668bf5d |
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
from pytube import YouTube
import os
import traceback
from youtube_list import youtube_list
"""
frames/
βββ frame_1/
β βββ frame_00000.jpg
β βββ frame_00030.jpg
β βββ ...
βββ frame_2/
β βββ frame_00000.jpg
β βββ ...
"""
def download_youtube_videos(youtube_list, save_dir="content/sample_data/youtube_mp4"):
os.makedirs(save_dir, exist_ok=True)
for idx, youtube in enumerate(youtube_list, start=1):
try:
yt = YouTube(youtube)
filename = f"frame_{idx}.mp4"
stream = yt.streams.filter(progressive=True, file_extension='mp4')\
.order_by('resolution').desc().first()
if stream:
print(f"[β] λ€μ΄λ‘λ: {filename}")
stream.download(output_path=save_dir, filename=filename)
else:
print(f"[β] 건λλ (progressive μ€νΈλ¦Ό μμ): {filename}")
except Exception as e:
print(f"[!] μ€λ₯ λ°μ: {youtube} β {e}")
traceback.print_exc()
def extract_frames_from_videos(video_dir="content/sample_data/youtube_mp4",
frame_root_dir="frames",
every_n_seconds=1):
os.makedirs(frame_root_dir, exist_ok=True)
video_files = sorted([
f for f in os.listdir(video_dir)
if f.endswith(".mp4") and f.startswith("frame_")
])
for video_file in video_files:
video_path = os.path.join(video_dir, video_file)
video_title = os.path.splitext(video_file)[0]
out_dir = os.path.join(frame_root_dir, video_title)
os.makedirs(out_dir, exist_ok=True)
cap = cv2.VideoCapture(video_path)
fps = int(cap.get(cv2.CAP_PROP_FPS))
frame_interval = fps * every_n_seconds
frame_num = 0
print(f"[β] νλ μ μΆμΆ μ€: {video_file} (FPS={fps})")
while True:
ret, frame = cap.read()
if not ret:
break
if frame_num % frame_interval == 0:
frame_filename = f"frame_{frame_num:05d}.jpg"
frame_path = os.path.join(out_dir, frame_filename)
cv2.imwrite(frame_path, frame)
frame_num += 1
cap.release()
print(f"[β] μλ£: {out_dir}")
|