chuanenlin commited on
Commit
1a29f57
·
1 Parent(s): 222de66

Handle yt-dlp bot check

Browse files
Files changed (1) hide show
  1. whichframe.py +22 -5
whichframe.py CHANGED
@@ -1,13 +1,10 @@
1
  import streamlit as st
2
- from pytube import YouTube
3
- from pytube import extract
4
  import cv2
5
  from PIL import Image
6
  import clip as openai_clip
7
  import torch
8
  import math
9
  from humanfriendly import format_timespan
10
- from moviepy.video.io.VideoFileClip import VideoFileClip
11
  import numpy as np
12
  import time
13
  import os
@@ -22,15 +19,35 @@ model, preprocess = openai_clip.load("ViT-B/32", device=device)
22
 
23
  def fetch_video(url):
24
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  ydl_opts = {
26
  'format': 'bestvideo[height<=360][ext=mp4]',
27
  'quiet': True,
28
- 'no_warnings': True
29
  }
30
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
31
  info = ydl.extract_info(url, download=False)
32
  duration = info.get('duration', 0)
33
- if duration >= 300: # 5 minutes
34
  st.error("Please find a YouTube video shorter than 5 minutes.")
35
  st.stop()
36
  video_url = info['url']
 
1
  import streamlit as st
 
 
2
  import cv2
3
  from PIL import Image
4
  import clip as openai_clip
5
  import torch
6
  import math
7
  from humanfriendly import format_timespan
 
8
  import numpy as np
9
  import time
10
  import os
 
19
 
20
  def fetch_video(url):
21
  try:
22
+ browsers = ['chrome', 'firefox', 'edge', 'safari', 'opera']
23
+ for browser in browsers:
24
+ try:
25
+ ydl_opts = {
26
+ 'format': 'bestvideo[height<=360][ext=mp4]',
27
+ 'quiet': True,
28
+ 'no_warnings': True,
29
+ 'cookiesfrombrowser': (browser,),
30
+ }
31
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
32
+ info = ydl.extract_info(url, download=False)
33
+ duration = info.get('duration', 0)
34
+ if duration >= 300: # 5 minutes
35
+ st.error("Please find a YouTube video shorter than 5 minutes.")
36
+ st.stop()
37
+ video_url = info['url']
38
+ return None, video_url
39
+ except Exception:
40
+ continue
41
+
42
  ydl_opts = {
43
  'format': 'bestvideo[height<=360][ext=mp4]',
44
  'quiet': True,
45
+ 'no_warnings': True,
46
  }
47
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
48
  info = ydl.extract_info(url, download=False)
49
  duration = info.get('duration', 0)
50
+ if duration >= 300:
51
  st.error("Please find a YouTube video shorter than 5 minutes.")
52
  st.stop()
53
  video_url = info['url']