import os import re import pdb import logging import subprocess def format_time(seconds): hours = seconds // 3600 minutes = (seconds // 60) % 60 seconds = seconds % 60 return f"{hours}:{minutes:02d}:{seconds:02d}" def extract_info_from_url(url): if 'bilibili.com/video/' in url: # 如果 URL 是 Bilibili 的视频链接,提取 BV 号 match = re.search(r'/video/(BV\w+)', url) if match: return match.group(1) else: return "BV ID not found!" else: # 如果 URL 是博客链接等,提取最后一段路径 match = re.search(r'/([^/]+)/?$', url) if match: return match.group(1) else: return "URL address is wired!" def download_video(url, save_dir='./examples', size=768): filename = extract_info_from_url(url) save_path = f'{save_dir}/{filename}.mp4' #cmd = f'yt-dlp -S ext:mp4:m4a --throttled-rate 5M -f "best[width<={size}][height<={size}]" --output {save_path} --merge-output-format mp4 https://www.youtube.com/embed/{url}' # $ you-get -o ~/Videos -O zoo.webm 'https://www.youtube.com/watch?v=jNQXAC9IVRw' cmd = f'you-get -o {save_dir} -O {filename} {url}' if not os.path.exists(save_path): try: subprocess.call(cmd, shell=True) except: return None return save_path def logger_creator(video_id): # set up logger logger = logging.getLogger(__name__) logger.setLevel(logging.INFO) handler = logging.FileHandler(f'./examples/{video_id}.log', mode='w') handler.setLevel(logging.INFO) formatter = logging.Formatter('%(message)s') handler.setFormatter(formatter) logger.addHandler(handler) return logger if __name__ == "__main__": download_video('outcGtbnMuQ', save_dir='./examples', size=768)