-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamlit_app.py
More file actions
168 lines (146 loc) · 6.06 KB
/
Copy pathstreamlit_app.py
File metadata and controls
168 lines (146 loc) · 6.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# streamlit_app.py
import streamlit as st
from pathlib import Path
import tempfile
import gc
import time
import pandas as pd
from typing import Optional, Tuple
from scene_slicer import SceneSlicer
st.set_page_config(page_title="Scene Slicer", page_icon="🎬", layout="wide")
st.title("🎬 Scene Slicer")
st.caption("Download a YouTube video (or use a local file) and split it into scenes.")
slicer = SceneSlicer()
# --- Sidebar options ---
st.sidebar.header("Options")
input_mode = st.sidebar.radio("Input Source", ["YouTube URL", "Local File"], index=0)
cookies_from_browser = None
url = ""
uploaded_file = None
if input_mode == "YouTube URL":
url = st.sidebar.text_input("YouTube URL", placeholder="https://www.youtube.com/watch?v=...")
cookies_help = "Optionally use cookies from your browser (e.g., 'chrome', 'edge', 'firefox' or 'chrome:Profile 1')."
cookies_from_browser = st.sidebar.text_input("Cookies from browser (optional)", help=cookies_help, placeholder="chrome")
else:
uploaded_file = st.sidebar.file_uploader("Upload a local video", type=["mp4", "mkv", "webm", "mov"])
detector = st.sidebar.selectbox("Detector", options=["content", "adaptive", "histogram"], index=0)
threshold = st.sidebar.slider("Threshold (higher = fewer cuts)", min_value=5, max_value=60, value=27, step=1)
start = st.sidebar.number_input("Start offset (seconds)", min_value=0.0, value=0.0, step=0.5)
max_duration = st.sidebar.number_input("Max analyze duration (seconds, 0 = full)", min_value=0.0, value=0.0, step=1.0)
do_dry_run = st.sidebar.checkbox("Dry run (no clip export)", value=False)
min_export_len = st.sidebar.number_input(
"Drop clips shorter than (seconds)",
min_value=0.0,
value=30.0,
step=0.5,
help="Any detected scene shorter than this will not be exported.",
)
out_root = Path("outputs")
st.sidebar.write("---")
run_btn = st.sidebar.button("▶️ Run Scene Slicing", type="primary")
# Session state
if "last_output_dir" not in st.session_state:
st.session_state.last_output_dir = None
if "manifest_df" not in st.session_state:
st.session_state.manifest_df = None
if "video_id" not in st.session_state:
st.session_state.video_id = None
if "clip_paths" not in st.session_state:
st.session_state.clip_paths = []
def process_video():
try:
slicer.ensure_ffmpeg()
except Exception as e:
st.error(f"ffmpeg check failed: {e}")
return
with tempfile.TemporaryDirectory(prefix="scene_slicer_ui_") as tmp:
tmpdir = Path(tmp)
video = None # ensure defined in outer scope
try:
# Source
if input_mode == "Local File":
if not uploaded_file:
st.error("Please upload a local video file.")
return
tmp_video = tmpdir / uploaded_file.name
with open(tmp_video, "wb") as f:
f.write(uploaded_file.read())
video_path = tmp_video
video_id = "localfile"
else:
if not url.strip():
st.error("Please enter a YouTube URL.")
return
st.write("**[1/3] Downloading…**")
video_path, video_id = slicer.download_youtube(url.strip(), tmpdir, cookies_from_browser=cookies_from_browser)
st.success(f"Downloaded: {video_path.name}")
st.write("**[2/3] Detecting scenes…**")
scenes, video = slicer.detect_scenes(
video_path=video_path,
detector=detector,
threshold=threshold,
start=start,
max_duration=max_duration,
min_scene_len=min_export_len
)
st.info(f"Detected **{len(scenes)}** raw scenes")
base_out = out_root / video_id
scenes_dir = base_out / "scenes"
base_out.mkdir(parents=True, exist_ok=True)
st.write("**[3/3] Exporting clips…**")
outputs, kept_scenes = slicer.export_scenes_ffmpeg(
video_path,
scenes,
scenes_dir,
prefix="scene_",
dry_run=do_dry_run,
min_export_len=min_export_len,
)
if not do_dry_run:
manifest_csv = slicer.write_manifest(kept_scenes, base_out, base_prefix="scene_")
# Load manifest via pandas
df = pd.read_csv(manifest_csv)
st.session_state.manifest_df = df
st.session_state.clip_paths = outputs if not do_dry_run else []
st.session_state.last_output_dir = base_out
st.session_state.video_id = video_id
st.success(f"Done! Exported {len(outputs)} scenes ≥ {min_export_len:.2f}s.")
finally:
# Best-effort: release scenedetect handles, delete reference, run GC and allow a short delay
try:
slicer.close_video(video)
except Exception as e:
st.error(f"Error closing video: {e}")
return
try:
# remove Python reference and collect so OS handles get closed
del video
except Exception as e:
st.error(f"Error deleting video reference: {e}")
gc.collect()
time.sleep(0.25)
if run_btn:
with st.spinner("Processing…"):
process_video()
# --- Results
if (
st.session_state.manifest_df is not None
and not do_dry_run
and st.session_state.get("clip_paths")
):
st.subheader("Download scenes")
import zipfile, io
mem = io.BytesIO()
with zipfile.ZipFile(mem, "w", zipfile.ZIP_DEFLATED) as z:
for full_path in st.session_state.clip_paths:
p = Path(full_path)
if p.is_file():
z.write(p, arcname=p.name)
zip_name = f"{st.session_state.video_id}_scenes.zip"
st.download_button(
"⬇️ Download Scenes ZIP",
data=mem.getvalue(),
file_name=zip_name,
mime="application/zip",
)
st.caption(f"Output folder: `{st.session_state.last_output_dir}`")