import gradio as gr import pathlib import os import re import shutil from huggingface_hub import snapshot_download #manga_video_dir = "reorganized_manga" #manga_video_dir = "reorganized_manga_0_5" manga_video_dir = "reorganized_manga_0_3" # 从 Hugging Face Hub 下载数据集到当前路径 def download_dataset(): # 定义本地目标路径 local_path = os.path.join(os.getcwd(), manga_video_dir) # 如果目标路径已存在,先删除 if os.path.exists(local_path): shutil.rmtree(local_path) # 下载数据集到缓存目录 cache_path = snapshot_download( repo_id="svjack/Genshin-Impact-3d-Kenburns-Manga", repo_type="dataset", allow_patterns="{}/*".format(manga_video_dir) ) # 将缓存目录中的 reorganized_manga 复制到当前路径 shutil.copytree(os.path.join(cache_path, manga_video_dir), local_path) # 返回本地路径 return local_path # 检查本地路径是否存在且内容完整 def is_dataset_valid(local_path): if not os.path.exists(local_path): return False # 检查文件夹是否为空 if not os.listdir(local_path): return False # 检查是否存在至少一个子目录 subdirs = [d for d in os.listdir(local_path) if os.path.isdir(os.path.join(local_path, d))] if not subdirs: return False return True # 初始路径 local_path = os.path.join(os.getcwd(), manga_video_dir) if not is_dataset_valid(local_path): base_path = download_dataset() else: base_path = local_path # 中文数字到阿拉伯数字的映射 chinese_num_map = { "零": 0, "一": 1, "二": 2, "三": 3, "四": 4, "五": 5, "六": 6, "七": 7, "八": 8, "九": 9, "十": 10 } # 将中文数字转换为阿拉伯数字 def chinese_to_arabic(chinese_num): if chinese_num in chinese_num_map: return chinese_num_map[chinese_num] # 处理“十一”到“十九”的情况 if chinese_num.startswith("十"): return 10 + chinese_num_map.get(chinese_num[1:], 0) # 处理“二十”以上的情况(简单实现,仅支持两位数) if chinese_num.startswith("二") and chinese_num.endswith("十"): return 20 return 0 # 默认值 # 加载视频文件并排序 def load_videos(path): # 获取所有 .mp4 文件 video_files = list(pathlib.Path(path).rglob("*.mp4")) # 排序逻辑 def sort_key(file): filename = file.name.replace(".mp4", "") # 无数字的文件(如“第四话扉页.mp4”)排在最前面 if not re.search(r"\d+", filename): return (0, filename) # 提取文件名中的数值并排序 match = re.search(r"(\d+)", filename) if match: return (1, int(match.group(1))) return (1, filename) # 按自定义规则排序 video_files.sort(key=sort_key) print([str(video) for video in video_files]) return [str(video) for video in video_files] # 获取 reorganized_manga 的子目录并排序 def get_subdirectories(): subdirs = [d.name for d in pathlib.Path(base_path).iterdir() if d.is_dir()] # 排序逻辑:序章在前,之后按“第几话”排序,再按“上”和“下”排序 def sort_key(name): if "序章" in name: return (0, 0, name, 2) # 序章优先级最高,第四列默认 2 # 匹配中文数字(如“第一話”) match = re.search(r"第([零一二三四五六七八九十]+)話", name) if match: chinese_num = match.group(1) arabic_num = chinese_to_arabic(chinese_num) # 匹配“上”和“下” if "上" in name: return (1, arabic_num, name, 0) # “上”优先级为 0 elif "下" in name: return (1, arabic_num, name, 1) # “下”优先级为 1 else: return (1, arabic_num, name, 2) # 无“上”或“下”优先级为 2 return (2, 0, name, 2) # 其他目录优先级最低 subdirs.sort(key=sort_key) return subdirs # 初始页码 current_page = 0 subdirectories = get_subdirectories() content = load_videos(os.path.join(base_path, subdirectories[0])) if subdirectories else [] # 更新页面内容 def update_page(action): global current_page if action == "Previous Page" and current_page > 0: current_page -= 1 elif action == "Next Page" and current_page < len(content) - 1: current_page += 1 return content[current_page], f"Page {current_page + 1} / {len(content)}" # 切换路径逻辑 def switch_path(new_path): global content, current_page content = load_videos(os.path.join(base_path, new_path)) current_page = 0 return content[current_page], f"Page {current_page + 1} / {len(content)}" # Gradio 界面 with gr.Blocks(title="Genshin Impact 3D Kenburns Manga Viewer") as demo: # HTML 标题 gr.Markdown(""" # Genshin Impact 3D Kenburns Manga Viewer Explore the manga pages in a 3D Kenburns style. """) # 路径切换下拉菜单 path_selector = gr.Dropdown( choices=get_subdirectories(), value=get_subdirectories()[0] if get_subdirectories() else None, label="Select Subdirectory" ) # 显示当前页面内容 with gr.Row(): with gr.Column(scale=2): # 控制宽度 page_display = gr.Video( value=content[current_page] if content else None, label="Current Page", autoplay=True, # 自动播放 loop=True, # 循环播放 interactive=True, # 允许用户交互(如调节播放速度) height=1024, width=768 ) page_info = gr.Textbox(value=f"Page {current_page + 1} / {len(content)}" if content else "No Pages", label="Page Info", interactive=False) # 翻页按钮 with gr.Row(): prev_button = gr.Button("Previous Page") next_button = gr.Button("Next Page") # 绑定按钮事件 prev_button.click(fn=lambda: update_page("Previous Page"), outputs=[page_display, page_info]) next_button.click(fn=lambda: update_page("Next Page"), outputs=[page_display, page_info]) # 监听路径切换事件 path_selector.change( fn=switch_path, inputs=path_selector, outputs=[page_display, page_info] ) # 启动应用 demo.launch(share=True)