|
|
import os |
|
|
import yt_dlp |
|
|
from datasets import load_dataset |
|
|
from concurrent.futures import ThreadPoolExecutor, as_completed |
|
|
from tqdm import tqdm |
|
|
import time |
|
|
import random |
|
|
|
|
|
|
|
|
DATASET_PATH = "/data/shuimu.chen/videomarathon" |
|
|
SAVE_ROOT = "/data/shuimu.chen/videomarathon/downloaded_videos" |
|
|
|
|
|
|
|
|
MAX_WORKERS = 2 |
|
|
|
|
|
|
|
|
COOKIE_PATH = "/data/shuimu.chen/TimeSearch-R/data_prepare/cookies.txt" |
|
|
|
|
|
|
|
|
NODE_BIN_DIR = "/data/shuimu.chen/TimeSearch-R/data_prepare/node-v24.12.0-linux-x64/bin" |
|
|
os.environ["PATH"] = NODE_BIN_DIR + os.pathsep + os.environ["PATH"] |
|
|
|
|
|
|
|
|
def process_video(item): |
|
|
video_rel_path = item['rel_path'] |
|
|
url = item['url'] |
|
|
|
|
|
base_name = os.path.splitext(video_rel_path)[0] |
|
|
full_save_path_no_ext = os.path.join(SAVE_ROOT, base_name) |
|
|
final_file_path = full_save_path_no_ext + ".mp4" |
|
|
|
|
|
|
|
|
if os.path.exists(final_file_path): |
|
|
if os.path.getsize(final_file_path) > 1024: |
|
|
return "skipped" |
|
|
else: |
|
|
try: |
|
|
os.remove(final_file_path) |
|
|
except: |
|
|
pass |
|
|
|
|
|
os.makedirs(os.path.dirname(final_file_path), exist_ok=True) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ydl_opts = { |
|
|
'format': 'bestvideo[height<=480][ext=mp4]+bestaudio[ext=m4a]/best[height<=480]/best', |
|
|
'merge_output_format': 'mp4', |
|
|
'outtmpl': f"{full_save_path_no_ext}.%(ext)s", |
|
|
|
|
|
|
|
|
'external_downloader': 'aria2c', |
|
|
|
|
|
|
|
|
'external_downloader_args': [ |
|
|
'-c', '-j', '3', '-x', '3', |
|
|
'-s', '3', '-k', '1M', |
|
|
'--user-agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"' |
|
|
], |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'source_address': '0.0.0.0', |
|
|
|
|
|
|
|
|
|
|
|
'quiet': False, |
|
|
'no_warnings': False, |
|
|
'ignoreerrors': False, |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
with yt_dlp.YoutubeDL(ydl_opts) as ydl: |
|
|
ydl.download([url]) |
|
|
|
|
|
|
|
|
if os.path.exists(final_file_path): |
|
|
if os.path.getsize(final_file_path) > 1024: |
|
|
return "success" |
|
|
else: |
|
|
|
|
|
os.remove(final_file_path) |
|
|
return "failed_empty" |
|
|
else: |
|
|
return "failed_unavailable" |
|
|
|
|
|
except Exception as e: |
|
|
|
|
|
|
|
|
|
|
|
return "error" |
|
|
|
|
|
def main(): |
|
|
|
|
|
print(f"🛠️ 环境检查...") |
|
|
if os.system("node -v") != 0: |
|
|
print("❌ 错误: Node 环境未生效,请检查路径!") |
|
|
return |
|
|
if not os.path.exists(COOKIE_PATH): |
|
|
print("❌ 错误: Cookie 文件不存在!") |
|
|
return |
|
|
|
|
|
|
|
|
print(f"📂 正在加载数据集: {DATASET_PATH} ...") |
|
|
try: |
|
|
dataset = load_dataset(DATASET_PATH) |
|
|
except Exception as e: |
|
|
print(f"❌ 加载失败: {e}") |
|
|
return |
|
|
|
|
|
unique_videos = {} |
|
|
print("🔍 正在筛选 ActivityNet 数据...") |
|
|
|
|
|
for item in tqdm(dataset['train'], desc="Filtering"): |
|
|
source = item.get('data_source') |
|
|
q_type = item.get('question_type') |
|
|
video_rel_path = item.get('video') |
|
|
url = item.get('URL') |
|
|
|
|
|
if source == 'ActivityNet' and q_type and str(q_type).endswith('mc'): |
|
|
if video_rel_path and url: |
|
|
if video_rel_path not in unique_videos: |
|
|
unique_videos[video_rel_path] = url |
|
|
|
|
|
tasks = [{'rel_path': k, 'url': v} for k, v in unique_videos.items()] |
|
|
print(f"✅ 筛选完成!共 {len(tasks)} 个唯一视频任务。") |
|
|
|
|
|
|
|
|
print(f"🚀 启动下载 (Workers={MAX_WORKERS}, Cookie 已启用)...") |
|
|
results = {"success": 0, "skipped": 0, "failed_unavailable": 0, "failed_empty": 0, "error": 0} |
|
|
|
|
|
with ThreadPoolExecutor(max_workers=MAX_WORKERS) as executor: |
|
|
future_to_video = {executor.submit(process_video, task): task for task in tasks} |
|
|
|
|
|
progress = tqdm(as_completed(future_to_video), total=len(tasks), desc="Downloading") |
|
|
for future in progress: |
|
|
status = future.result() |
|
|
|
|
|
|
|
|
if status in results: |
|
|
results[status] += 1 |
|
|
else: |
|
|
results["error"] += 1 |
|
|
|
|
|
progress.set_postfix( |
|
|
ok=results['success'], |
|
|
skip=results['skipped'], |
|
|
fail=results['failed_unavailable'] + results['failed_empty'] + results['error'] |
|
|
) |
|
|
|
|
|
print("\n" + "=" * 30) |
|
|
print(f"🎉 处理完毕") |
|
|
print(f"✅ 成功下载: {results['success']}") |
|
|
print(f"⏩ 跳过存在: {results['skipped']}") |
|
|
print(f"❌ 视频失效(死链): {results['failed_unavailable']}") |
|
|
print(f"⚠️ 下载为空(网络/限流): {results['failed_empty']}") |
|
|
print(f"🚫 其他错误: {results['error']}") |
|
|
print(f"保存在: {SAVE_ROOT}") |
|
|
|
|
|
if __name__ == "__main__": |
|
|
main() |