File size: 14,947 Bytes
25ed4c6
2725c6b
25ed4c6
 
 
034aa20
4ab551f
 
 
 
 
 
 
 
 
 
919660f
4ab551f
 
 
034aa20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ab551f
919660f
 
 
4ab551f
 
 
034aa20
 
 
 
 
4ab551f
 
034aa20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ab551f
 
034aa20
 
 
4ab551f
034aa20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ab551f
 
 
 
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import os
os.environ["HOME"] = "/tmp"
os.environ["STREAMLIT_CONFIG_DIR"] = "/tmp/.streamlit"
os.makedirs("/tmp/.streamlit", exist_ok=True)


import shutil
import tempfile
from pathlib import Path

import streamlit as st

from sorawm.core import SoraWM


def main():
    st.set_page_config(
        page_title="Sora Watermark Cleaner", page_icon="🎬", layout="centered"
    )

    # Header section with improved layout
    st.markdown(
        """
        <div style='text-align: center; padding: 1rem 0;'>
            <h1 style='margin-bottom: 0.5rem;'>
                🎬 Sora Watermark Cleaner
            </h1>
            <p style='font-size: 1.2rem; color: #666; margin-bottom: 1rem;'>
                Remove watermarks from Sora-generated videos with AI-powered precision
            </p>
        </div>
        """,
        unsafe_allow_html=True,
    )
    
    # # Feature badges
    # col1, col2, col3 = st.columns(3)
    # with col1:
    #     st.markdown(
    #         """
    #         <div style='text-align: center; padding: 0.8rem; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); 
    #                     border-radius: 10px; color: white;'>
    #             <div style='font-size: 1.5rem;'>⚑</div>
    #             <div style='font-weight: bold;'>Fast Processing</div>
    #             <div style='font-size: 0.85rem; opacity: 0.9;'>GPU Accelerated</div>
    #         </div>
    #         """,
    #         unsafe_allow_html=True,
    #     )
    # with col2:
    #     st.markdown(
    #         """
    #         <div style='text-align: center; padding: 0.8rem; background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%); 
    #                     border-radius: 10px; color: white;'>
    #             <div style='font-size: 1.5rem;'>🎯</div>
    #             <div style='font-weight: bold;'>High Precision</div>
    #             <div style='font-size: 0.85rem; opacity: 0.9;'>AI-Powered</div>
    #         </div>
    #         """,
    #         unsafe_allow_html=True,
    #     )
    # with col3:
    #     st.markdown(
    #         """
    #         <div style='text-align: center; padding: 0.8rem; background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%); 
    #                     border-radius: 10px; color: white;'>
    #             <div style='font-size: 1.5rem;'>πŸ“¦</div>
    #             <div style='font-weight: bold;'>Batch Support</div>
    #             <div style='font-size: 0.85rem; opacity: 0.9;'>Process Multiple</div>
    #         </div>
    #         """,
    #         unsafe_allow_html=True,
    #     )
    
    # Footer info
    st.markdown(
        """
        <div style='text-align: center; padding: 1rem 0; margin-top: 1rem;'>
            <p style='color: #888; font-size: 0.9rem;'>
                Built with ❀️ using Streamlit and AI | 
                <a href='https://github.com/linkedlist771/SoraWatermarkCleaner' 
                   target='_blank' style='color: #667eea; text-decoration: none;'>
                    ⭐ Star on GitHub
                </a>
            </p>
        </div>
        """,
        unsafe_allow_html=True,
    )
    # Initialize SoraWM
    if "sora_wm" not in st.session_state:
        with st.spinner("Loading AI models..."):
            st.session_state.sora_wm = SoraWM()

    st.markdown("---")

    # Mode selection
    mode = st.radio(
        "Select input mode:",
        ["πŸ“ Upload Video File", "πŸ—‚οΈ Process Folder"],
        horizontal=True,
    )

    if mode == "πŸ“ Upload Video File":
        # File uploader
        uploaded_file = st.file_uploader(
            "Upload your video",
            type=["mp4", "avi", "mov", "mkv"],
            accept_multiple_files=False,
            help="Select a video file to remove watermark",
        )

        if uploaded_file:
            # Clear previous processed video if a new file is uploaded
            if "current_file_name" not in st.session_state or st.session_state.current_file_name != uploaded_file.name:
                st.session_state.current_file_name = uploaded_file.name
                if "processed_video_data" in st.session_state:
                    del st.session_state.processed_video_data
                if "processed_video_path" in st.session_state:
                    del st.session_state.processed_video_path
                if "processed_video_name" in st.session_state:
                    del st.session_state.processed_video_name
            
            # Display video info
            st.success(f"βœ… Uploaded: {uploaded_file.name}")
            
            # Create two columns for before/after comparison
            col_left, col_right = st.columns(2)
            
            with col_left:
                st.markdown("### πŸ“₯ Original Video")
                st.video(uploaded_file)
            
            with col_right:
                st.markdown("### 🎬 Processed Video")
                # Placeholder for processed video
                if "processed_video_data" not in st.session_state:
                    st.info("Click 'Remove Watermark' to process the video")
                else:
                    st.video(st.session_state.processed_video_data)

            # Process button
            if st.button("πŸš€ Remove Watermark", type="primary", use_container_width=True):
                with tempfile.TemporaryDirectory() as tmp_dir:
                    tmp_path = Path(tmp_dir)

                    try:
                        # Create progress bar and status text
                        progress_bar = st.progress(0)
                        status_text = st.empty()

                        def update_progress(progress: int):
                            progress_bar.progress(progress / 100)
                            if progress < 50:
                                status_text.text(f"πŸ” Detecting watermarks... {progress}%")
                            elif progress < 95:
                                status_text.text(f"🧹 Removing watermarks... {progress}%")
                            else:
                                status_text.text(f"🎡 Merging audio... {progress}%")

                        # Single file processing
                        input_path = tmp_path / uploaded_file.name
                        with open(input_path, "wb") as f:
                            f.write(uploaded_file.read())

                        output_path = tmp_path / f"cleaned_{uploaded_file.name}"

                        st.session_state.sora_wm.run(
                            input_path, output_path, progress_callback=update_progress
                        )

                        progress_bar.progress(100)
                        status_text.text("βœ… Processing complete!")
                        st.success("βœ… Watermark removed successfully!")

                        # Store processed video path and read video data
                        with open(output_path, "rb") as f:
                            video_data = f.read()
                        
                        st.session_state.processed_video_path = output_path
                        st.session_state.processed_video_data = video_data
                        st.session_state.processed_video_name = f"cleaned_{uploaded_file.name}"
                        
                        # Rerun to show the video in the right column
                        st.rerun()

                    except Exception as e:
                        st.error(f"❌ Error processing video: {str(e)}")
            
            # Download button (show only if video is processed)
            if "processed_video_data" in st.session_state:
                st.download_button(
                    label="⬇️ Download Cleaned Video",
                    data=st.session_state.processed_video_data,
                    file_name=st.session_state.processed_video_name,
                    mime="video/mp4",
                    use_container_width=True,
                )

    else:  # Folder mode
        st.info("πŸ’‘ Drag and drop your video folder here, or click to browse and select multiple video files")
        
        # File uploader for multiple files (supports folder drag & drop)
        uploaded_files = st.file_uploader(
            "Upload videos from folder",
            type=["mp4", "avi", "mov", "mkv"],
            accept_multiple_files=True,
            help="You can drag & drop an entire folder here, or select multiple video files",
            key="folder_uploader"
        )
        
        if uploaded_files:
            # Display uploaded files info
            video_count = len(uploaded_files)
            st.success(f"βœ… {video_count} video file(s) uploaded")
            
            # Show file list in an expander
            with st.expander("πŸ“‹ View uploaded files", expanded=False):
                for i, file in enumerate(uploaded_files, 1):
                    file_size_mb = file.size / (1024 * 1024)
                    st.text(f"{i}. {file.name} ({file_size_mb:.2f} MB)")
            
            # Process button
            if st.button("πŸš€ Process All Videos", type="primary", use_container_width=True):
                with tempfile.TemporaryDirectory() as tmp_dir:
                    tmp_path = Path(tmp_dir)
                    input_folder = tmp_path / "input"
                    output_folder = tmp_path / "output"
                    input_folder.mkdir(exist_ok=True)
                    output_folder.mkdir(exist_ok=True)
                    
                    try:
                        # Save all uploaded files to temp folder
                        status_text = st.empty()
                        status_text.text("πŸ“₯ Saving uploaded files...")
                        
                        for uploaded_file in uploaded_files:
                            # Preserve folder structure if file.name contains subdirectories
                            file_path = input_folder / uploaded_file.name
                            file_path.parent.mkdir(parents=True, exist_ok=True)
                            with open(file_path, "wb") as f:
                                f.write(uploaded_file.read())
                        
                        # Create progress tracking
                        progress_bar = st.progress(0)
                        current_file_text = st.empty()
                        processed_count = 0
                        
                        def update_progress(progress: int):
                            # Calculate overall progress
                            overall_progress = (processed_count * 100 + progress) / video_count / 100
                            progress_bar.progress(overall_progress)
                            
                            if progress < 50:
                                current_file_text.text(f"πŸ” Processing file {processed_count + 1}/{video_count}: Detecting watermarks... {progress}%")
                            elif progress < 95:
                                current_file_text.text(f"🧹 Processing file {processed_count + 1}/{video_count}: Removing watermarks... {progress}%")
                            else:
                                current_file_text.text(f"🎡 Processing file {processed_count + 1}/{video_count}: Merging audio... {progress}%")
                        
                        # Process each video file
                        for video_file in input_folder.rglob("*"):
                            if video_file.is_file() and video_file.suffix.lower() in [".mp4", ".avi", ".mov", ".mkv"]:
                                # Determine output path maintaining folder structure
                                rel_path = video_file.relative_to(input_folder)
                                output_path = output_folder / rel_path.parent / f"cleaned_{rel_path.name}"
                                output_path.parent.mkdir(parents=True, exist_ok=True)
                                
                                # Process the video
                                st.session_state.sora_wm.run(
                                    video_file, output_path, progress_callback=update_progress
                                )
                                processed_count += 1
                        
                        progress_bar.progress(100)
                        current_file_text.text("βœ… All videos processed!")
                        st.success(f"βœ… {video_count} video(s) processed successfully!")
                        
                        # Create download option for processed videos
                        st.markdown("### πŸ“¦ Download Processed Videos")
                        
                        # Store processed files info in session state
                        if "batch_processed_files" not in st.session_state:
                            st.session_state.batch_processed_files = []
                        
                        st.session_state.batch_processed_files.clear()
                        
                        for processed_file in output_folder.rglob("*"):
                            if processed_file.is_file():
                                with open(processed_file, "rb") as f:
                                    video_data = f.read()
                                rel_path = processed_file.relative_to(output_folder)
                                st.session_state.batch_processed_files.append({
                                    "name": str(rel_path),
                                    "data": video_data
                                })
                        
                        st.rerun()
                        
                    except Exception as e:
                        st.error(f"❌ Error processing videos: {str(e)}")
                        import traceback
                        st.error(f"Details: {traceback.format_exc()}")
            
            # Show download buttons for processed files
            if "batch_processed_files" in st.session_state and st.session_state.batch_processed_files:
                st.markdown("---")
                st.markdown("### ⬇️ Download Processed Videos")
                
                for file_info in st.session_state.batch_processed_files:
                    col1, col2 = st.columns([3, 1])
                    with col1:
                        st.text(f"πŸ“Ή {file_info['name']}")
                    with col2:
                        st.download_button(
                            label="⬇️ Download",
                            data=file_info['data'],
                            file_name=file_info['name'],
                            mime="video/mp4",
                            key=f"download_{file_info['name']}",
                            use_container_width=True
                        )


if __name__ == "__main__":
    main()