import os from pathlib import Path import shutil import sys from loguru import logger from tqdm import tqdm from concurrent.futures import ThreadPoolExecutor, as_completed LINK_PREFIX = "https://www.youtube.com/watch?v=" def download_video(video_dir: Path, name: str, time_range): video_dir.mkdir(exist_ok=True) path_full_video = str((video_dir / f"{name}_full_video.mp4").absolute()) path_trimmed_video = video_dir / f"{name}.mp4" if path_trimmed_video.exists(): logger.info(f"{path_trimmed_video} exists.") return path_trimmed_video = str(path_trimmed_video.absolute()) video_link = LINK_PREFIX + name logger.info(f"Downloading full video to: {path_full_video}") command1 = f"yt-dlp {video_link} --js-runtime node --remote-components ejs:github --cookies cookies.txt " command1 += "-o " + path_full_video + " " command1 += "-f best " logger.debug(command1) os.system(command1) if not os.path.exists(path_full_video): logger.error("Full video not found. Aborting") return t_start, t_end = time_range t_dur = t_end - t_start logger.info("Trim the video to [%.1f-%.1f]" % (t_start, t_end)) command2 = "ffmpeg " command2 += "-ss " command2 += str(t_start) + " " command2 += "-i " command2 += path_full_video + " " command2 += "-t " command2 += str(t_dur) + " " command2 += "-vcodec libx264 " command2 += "-acodec aac -strict -2 " command2 += path_trimmed_video + " " command2 += "-y " # overwrite without asking command2 += "-loglevel info " # print no log os.system(command2) logger.info(f"Download complete for {path_trimmed_video}") def download_from_csv(video_dir, csv_file): video_dir = Path(video_dir) with open(csv_file, "r") as f: data = f.readlines()[1:] length = len(data) logger.info(f"Total number of videos to download {length}") names, segments = [], {} futures = [] with ThreadPoolExecutor(max_workers=1) as executor: for line in data: if not line: continue vid, _ = line.split("\t") name, time_range = vid[:11], vid[11:].split("_") t_start = float(time_range[1]) t_end = t_start + 10 segments[name] = (t_start, t_end) names.append(name) futures.append( executor.submit(download_video, video_dir, name, segments[name]) ) for future in tqdm(as_completed(futures), total=len(futures)): future.result() print(len(segments)) def check_unavailable(video_dir, csv_file): video_dir = Path(video_dir) with open(csv_file, "r") as f: data = f.readlines()[1:] missing = 0 for line in tqdm(data): vid, _ = line.split("\t") name = vid[:11] path = video_dir / f"{name}.mp4" if not path.exists(): missing += 1 logger.info(f"Total :{len(data)}, missing: {missing}") def move_raw(): videos = os.listdir("videos") os.makedirs("raw_videos", exist_ok=True) for video in videos: video_path = os.path.join("videos", video) if not video.endswith("full_video.mp4"): continue shutil.move(video_path, "raw_videos") try: cookie_path = "cookies.txt" if not os.path.exists(cookie_path): logger.warning( "Cookies not found. Please export your browser's cookie and paste it as `cookies.txt`." ) sys.exit(1) download_from_csv("videos", "AVVP_dataset_full.csv") move_raw() except KeyboardInterrupt: logger.error("Keyboard interrupt") sys.exit(1)