1919from IPython .display import display
2020from PIL import Image , ImageDraw , ImageFont
2121from pillow_heif import register_heif_opener # type: ignore
22- from pytube import YouTube # type: ignore
22+ import yt_dlp # type: ignore
2323import pymupdf # type: ignore
2424from google import genai # type: ignore
2525from google .genai import types # type: ignore
@@ -3174,7 +3174,6 @@ def extract_frames_and_timestamps(
31743174 [{"frame": np.ndarray, "timestamp": 0.0}, ...]
31753175 """
31763176 if isinstance (fps , str ):
3177- # fps could be a string when it's passed in from a web endpoint deployment
31783177 fps = float (fps )
31793178
31803179 def reformat (
@@ -3194,23 +3193,20 @@ def reformat(
31943193 )
31953194 ):
31963195 with tempfile .TemporaryDirectory () as temp_dir :
3197- yt = YouTube (str (video_uri ))
3198- # Download the highest resolution video
3199- video = (
3200- yt .streams .filter (progressive = True , file_extension = "mp4" )
3201- .order_by ("resolution" )
3202- .desc ()
3203- .first ()
3204- )
3205- if not video :
3206- raise Exception ("No suitable video stream found" )
3207- video_file_path = video .download (output_path = temp_dir )
3196+ ydl_opts = {
3197+ "outtmpl" : os .path .join (temp_dir , "%(title)s.%(ext)s" ),
3198+ "format" : "bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best" ,
3199+ "quiet" : True ,
3200+ }
3201+ with yt_dlp .YoutubeDL (ydl_opts ) as ydl :
3202+ info = ydl .extract_info (str (video_uri ), download = True )
3203+ video_file_path = ydl .prepare_filename (info )
32083204
32093205 return reformat (extract_frames_from_video (video_file_path , fps ))
3206+
32103207 elif str (video_uri ).startswith (("http" , "https" )):
32113208 _ , image_suffix = os .path .splitext (video_uri )
32123209 with tempfile .NamedTemporaryFile (delete = False , suffix = image_suffix ) as tmp_file :
3213- # Download the video and save it to the temporary file
32143210 with urllib .request .urlopen (str (video_uri )) as response :
32153211 tmp_file .write (response .read ())
32163212 return reformat (extract_frames_from_video (tmp_file .name , fps ))
0 commit comments