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|
+
def list_dirs(path: str):
|
| 6 |
+
if not os.path.isdir(path):
|
| 7 |
+
return []
|
| 8 |
+
return sorted([d for d in os.listdir(path) if os.path.isdir(os.path.join(path, d))])
|
| 9 |
+
|
| 10 |
+
def list_mp4s(path: str):
|
| 11 |
+
return sorted(glob.glob(os.path.join(path, "*.mp4")))
|
| 12 |
+
|
| 13 |
+
def refresh_chunks(root_dir: str):
|
| 14 |
+
root_dir = root_dir.strip()
|
| 15 |
+
chunks = [d for d in list_dirs(root_dir) if d.startswith("chunk_")]
|
| 16 |
+
# 初始化时顺带给出默认 chunk / id / shot
|
| 17 |
+
default_chunk = chunks[0] if chunks else None
|
| 18 |
+
return gr.Dropdown(choices=chunks, value=default_chunk)
|
| 19 |
+
|
| 20 |
+
def refresh_ids(root_dir: str, chunk: str):
|
| 21 |
+
root_dir = root_dir.strip()
|
| 22 |
+
chunk = (chunk or "").strip()
|
| 23 |
+
if not chunk:
|
| 24 |
+
return gr.Dropdown(choices=[], value=None)
|
| 25 |
+
|
| 26 |
+
chunk_path = os.path.join(root_dir, chunk)
|
| 27 |
+
ids = list_dirs(chunk_path)
|
| 28 |
+
default_id = ids[0] if ids else None
|
| 29 |
+
# filterable=True 允许搜索过滤
|
| 30 |
+
return gr.Dropdown(choices=ids, value=default_id, filterable=True)
|
| 31 |
+
|
| 32 |
+
def refresh_shots(root_dir: str, chunk: str, sample_id: str):
|
| 33 |
+
root_dir = root_dir.strip()
|
| 34 |
+
chunk = (chunk or "").strip()
|
| 35 |
+
sample_id = (sample_id or "").strip()
|
| 36 |
+
|
| 37 |
+
if not (chunk and sample_id):
|
| 38 |
+
return gr.Dropdown(choices=[], value=None)
|
| 39 |
+
|
| 40 |
+
id_path = os.path.join(root_dir, chunk, sample_id)
|
| 41 |
+
shots = list_dirs(id_path) # 你这里 shot 目录一般是 "0" 这样的
|
| 42 |
+
default_shot = shots[0] if shots else None
|
| 43 |
+
return gr.Dropdown(choices=shots, value=default_shot)
|
| 44 |
+
|
| 45 |
+
def load_videos(root_dir: str, chunk: str, sample_id: str, shot: str):
|
| 46 |
+
root_dir = root_dir.strip()
|
| 47 |
+
chunk = (chunk or "").strip()
|
| 48 |
+
sample_id = (sample_id or "").strip()
|
| 49 |
+
shot = (shot or "").strip().strip("/")
|
| 50 |
+
|
| 51 |
+
target_dir = os.path.join(root_dir, chunk, sample_id, shot)
|
| 52 |
+
|
| 53 |
+
if not os.path.isdir(target_dir):
|
| 54 |
+
msg = f"目录不存在:{target_dir}"
|
| 55 |
+
return (msg, None, None, None, None)
|
| 56 |
+
|
| 57 |
+
mp4s = list_mp4s(target_dir)
|
| 58 |
+
if not mp4s:
|
| 59 |
+
msg = f"目录下没有 mp4:{target_dir}"
|
| 60 |
+
return (msg, None, None, None, None)
|
| 61 |
+
|
| 62 |
+
mp4s = mp4s[:4]
|
| 63 |
+
msg = (
|
| 64 |
+
f"目录:{target_dir}\n"
|
| 65 |
+
f"找到 {len(mp4s)} 个视频:\n" +
|
| 66 |
+
"\n".join([os.path.basename(p) for p in mp4s])
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
vids = mp4s + [None] * (4 - len(mp4s))
|
| 70 |
+
return (msg, vids[0], vids[1], vids[2], vids[3])
|
| 71 |
+
|
| 72 |
+
with gr.Blocks(title="CI Video Viewer") as demo:
|
| 73 |
+
gr.Markdown("## CI 视频浏览器(自动遍历 chunk / id / shot,4宫格展示)")
|
| 74 |
+
|
| 75 |
+
with gr.Row():
|
| 76 |
+
root_dir = gr.Textbox(value="ci_vid_SC_test", label="Root 目录(解压后的根目录)", scale=2)
|
| 77 |
+
refresh_btn = gr.Button("重新扫描目录", scale=1)
|
| 78 |
+
|
| 79 |
+
with gr.Row():
|
| 80 |
+
chunk_dd = gr.Dropdown(label="chunk(下拉)", choices=[], value=None)
|
| 81 |
+
id_dd = gr.Dropdown(label="id(可搜索)", choices=[], value=None, filterable=True)
|
| 82 |
+
shot_dd = gr.Dropdown(label="shot(下拉)", choices=[], value=None)
|
| 83 |
+
|
| 84 |
+
load_btn = gr.Button("加载视频", variant="primary")
|
| 85 |
+
status = gr.Textbox(label="状态 / 路径 / 文件列表", lines=6)
|
| 86 |
+
|
| 87 |
+
with gr.Row():
|
| 88 |
+
v1 = gr.Video(label="video 1")
|
| 89 |
+
v2 = gr.Video(label="video 2")
|
| 90 |
+
with gr.Row():
|
| 91 |
+
v3 = gr.Video(label="video 3")
|
| 92 |
+
v4 = gr.Video(label="video 4")
|
| 93 |
+
|
| 94 |
+
# --- 初始化:扫描 chunks,并级联更新 id、shot ---
|
| 95 |
+
demo.load(
|
| 96 |
+
fn=refresh_chunks,
|
| 97 |
+
inputs=[root_dir],
|
| 98 |
+
outputs=[chunk_dd],
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
# 当 chunk 变化:刷新 id
|
| 102 |
+
chunk_dd.change(
|
| 103 |
+
fn=refresh_ids,
|
| 104 |
+
inputs=[root_dir, chunk_dd],
|
| 105 |
+
outputs=[id_dd],
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
# 当 id 变化:刷新 shot
|
| 109 |
+
id_dd.change(
|
| 110 |
+
fn=refresh_shots,
|
| 111 |
+
inputs=[root_dir, chunk_dd, id_dd],
|
| 112 |
+
outputs=[shot_dd],
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
# 也允许当 chunk 变化后,shot 也跟着刷新(因为默认 id 会变)
|
| 116 |
+
id_dd.change(
|
| 117 |
+
fn=refresh_shots,
|
| 118 |
+
inputs=[root_dir, chunk_dd, id_dd],
|
| 119 |
+
outputs=[shot_dd],
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
# 手动重新扫描(root_dir 变了也可点)
|
| 123 |
+
def full_refresh(root_dir_val: str):
|
| 124 |
+
# 返回三个下拉的更新(chunk、id、shot)
|
| 125 |
+
chunk_update = refresh_chunks(root_dir_val)
|
| 126 |
+
# 取 chunk_update 的 value 需要在运行时拿不到对象内部;所以在这里重新算一次默认值
|
| 127 |
+
chunks = [d for d in list_dirs(root_dir_val.strip()) if d.startswith("chunk_")]
|
| 128 |
+
default_chunk = chunks[0] if chunks else None
|
| 129 |
+
id_update = refresh_ids(root_dir_val, default_chunk) if default_chunk else gr.Dropdown(choices=[], value=None)
|
| 130 |
+
ids = list_dirs(os.path.join(root_dir_val.strip(), default_chunk)) if default_chunk else []
|
| 131 |
+
default_id = ids[0] if ids else None
|
| 132 |
+
shot_update = refresh_shots(root_dir_val, default_chunk, default_id) if (default_chunk and default_id) else gr.Dropdown(choices=[], value=None)
|
| 133 |
+
return chunk_update, id_update, shot_update
|
| 134 |
+
|
| 135 |
+
refresh_btn.click(
|
| 136 |
+
fn=full_refresh,
|
| 137 |
+
inputs=[root_dir],
|
| 138 |
+
outputs=[chunk_dd, id_dd, shot_dd],
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
# 加载视频
|
| 142 |
+
load_btn.click(
|
| 143 |
+
fn=load_videos,
|
| 144 |
+
inputs=[root_dir, chunk_dd, id_dd, shot_dd],
|
| 145 |
+
outputs=[status, v1, v2, v3, v4],
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
if __name__ == "__main__":
|
| 149 |
+
demo.launch(
|
| 150 |
+
server_name="0.0.0.0", # 允许局域网/远程访问
|
| 151 |
+
server_port=8384,
|
| 152 |
+
share=True
|
| 153 |
+
)
|