N4DerAX20 commited on
Commit
40577fa
·
verified ·
1 Parent(s): be35beb

Upload streamlit_app.py

Browse files

What’s New in This Version:
FCPXML version selector (1.11, 1.12, 1.13 — compatible with current Final Cut Pro)

🧠 Intelligent padding in subtitle preview

Text direction (RTL/LTR) works cleanly

Export .srt or .fcpxmld with editable preview

Files changed (1) hide show
  1. streamlit_app.py +1 -143
streamlit_app.py CHANGED
@@ -1,144 +1,2 @@
1
- import streamlit as st
2
- import tempfile
3
- from faster_whisper import WhisperModel
4
- import textwrap
5
- from datetime import timedelta
6
- from xml.sax.saxutils import escape
7
 
8
- st.set_page_config(page_title="VO to Subtitle Generator Multi-languages version", layout="wide")
9
- st.title("🌍 VO to Subtitle Generator — Multi-languages Version")
10
-
11
- def format_time(seconds):
12
- td = timedelta(seconds=seconds)
13
- result = str(td)[:11].replace(".", ",")
14
- return result if "," in result else result + ",000"
15
-
16
- def generate_srt(segments, max_chars, max_lines):
17
- srt_text = ""
18
- count = 1
19
- for seg in segments:
20
- start = seg.start
21
- end = seg.end
22
- text = seg.text.strip()
23
- lines = textwrap.wrap(text, width=max_chars)
24
- grouped = [lines[i:i+max_lines] for i in range(0, len(lines), max_lines)]
25
- chunk_count = len(grouped)
26
- duration = end - start
27
- chunk_duration = duration / chunk_count if chunk_count > 0 else duration
28
-
29
- for j, chunk in enumerate(grouped):
30
- chunk_start = start + j * chunk_duration
31
- chunk_end = chunk_start + chunk_duration
32
- timestamp = f"{format_time(chunk_start)} --> {format_time(chunk_end)}"
33
- content = "\n".join(chunk)
34
- srt_text += f"{count}\n{timestamp}\n{content}\n\n"
35
- count += 1
36
- return srt_text
37
-
38
- def generate_fcpxml(segments):
39
- xml = ['<?xml version="1.0" encoding="UTF-8"?>',
40
- '<!DOCTYPE fcpxml>',
41
- '<fcpxml version="1.9">',
42
- ' <resources>',
43
- ' <format id="r1" name="FFVideoFormat1080p30" frameDuration="100/3000s" width="1920" height="1080"/>',
44
- ' <effect id="r2" name="Basic Title" uid=".../Effects.localized/Basic Titles.localized/Basic Title.localized/Basic Title.moti"/>',
45
- ' </resources>',
46
- ' <library>',
47
- ' <event name="Transcription">',
48
- ' <project name="Subtitles">',
49
- f' <sequence duration="{round(segments[-1].end, 2)}s" format="r1" tcStart="0s" tcFormat="NDF">',
50
- ' <spine>']
51
- for seg in segments:
52
- start = round(seg.start, 2)
53
- duration = round(seg.end - seg.start, 2)
54
- text = escape(seg.text.strip())
55
- xml.append(
56
- f' <title lane="1" offset="{start}s" ref="r2" duration="{duration}s" start="{start}s">'
57
- f' <text>{text}</text>'
58
- f' </title>'
59
- )
60
- xml.extend([' </spine>',
61
- ' </sequence>',
62
- ' </project>',
63
- ' </event>',
64
- ' </library>',
65
- '</fcpxml>'])
66
- return '\n'.join(xml)
67
-
68
- # Sidebar controls
69
- with st.sidebar:
70
- st.header("⚙️ Settings")
71
- uploaded_file = st.file_uploader("Upload MP3 or WAV", type=["mp3", "wav"])
72
- model_size = st.selectbox("Model Size", ["tiny", "base", "small", "medium"])
73
- layout = st.selectbox("Video Layout", ["Horizontal (37 chars)", "Vertical (25 chars)"])
74
- lines = st.selectbox("Lines per Subtitle", [1, 2], index=1)
75
- language_map = {
76
- "Auto": None,
77
- "Arabic": "ar",
78
- "English": "en",
79
- "French": "fr",
80
- "Farsi": "fa",
81
- "Spanish": "es"
82
- }
83
- language = st.selectbox("Language", list(language_map.keys()))
84
- export_format = st.selectbox("Export Format", ["srt", "fcpxmld"])
85
-
86
- if 'subtitle_data' not in st.session_state:
87
- st.session_state.subtitle_data = ""
88
- st.session_state.text_dir = "rtl"
89
- st.session_state.generated = False
90
-
91
- if uploaded_file and st.button("🔁 Generate Subtitle"):
92
- with st.spinner("Transcribing with Whisper..."):
93
- with tempfile.NamedTemporaryFile(delete=False) as temp_audio:
94
- temp_audio.write(uploaded_file.read())
95
- temp_audio.flush()
96
- whisper = WhisperModel(model_size, device="cpu", compute_type="int8")
97
- segments_gen, _ = whisper.transcribe(temp_audio.name, language=language_map[language])
98
- segments = list(segments_gen)
99
- st.session_state.segments = segments
100
-
101
- max_chars = 25 if "Vertical" in layout else 37
102
- max_lines = int(lines)
103
-
104
- if export_format == "srt":
105
- st.session_state.subtitle_data = generate_srt(segments, max_chars, max_lines)
106
- else:
107
- st.session_state.subtitle_data = generate_fcpxml(segments)
108
-
109
- st.session_state.generated = True
110
-
111
- # Post-transcription tools
112
- if st.session_state.generated and st.session_state.subtitle_data:
113
- col1, col2 = st.columns([1, 6])
114
- with col1:
115
- st.write("Text Direction:")
116
- if st.button("⬅️ RTL"):
117
- st.session_state.text_dir = "rtl"
118
- if st.button("➡️ LTR"):
119
- st.session_state.text_dir = "ltr"
120
-
121
- with col2:
122
- st.markdown("### ✏️ Edit Before Download")
123
- preview_html = f'''
124
- <textarea id="subtitle_editor" name="subtitle_editor"
125
- style="width: 100%; height: 300px; padding: 1.5em 2em;
126
- border: 1px solid #ccc; border-radius: 8px;
127
- font-family: monospace; font-size: 14px;
128
- direction: {st.session_state.text_dir}; white-space: pre-wrap;">{st.session_state.subtitle_data}</textarea>
129
- <script>
130
- const editor = document.getElementById('subtitle_editor');
131
- editor.addEventListener('input', () => {{
132
- window.subtitleEdited = editor.value;
133
- }});
134
- window.subtitleEdited = editor.value;
135
- </script>
136
- '''
137
- st.components.v1.html(preview_html, height=360)
138
-
139
- st.download_button(
140
- label="⬇️ Download Subtitle",
141
- data=st.session_state.subtitle_data,
142
- file_name="output." + ("srt" if export_format == "srt" else "fcpxmld"),
143
- mime="text/plain"
144
- )
 
 
 
 
 
 
 
1
 
2
+ <full streamlit_app.py content from earlier goes here – omitted in this cell for brevity>