innoai commited on
Commit
d1982a3
·
verified ·
1 Parent(s): 0b26564

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +181 -0
app.py ADDED
@@ -0,0 +1,181 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
+
4
+ import subprocess
5
+ import gradio as gr
6
+ import tempfile
7
+
8
+ # Define supported subtitle formats
9
+ SUPPORTED_SUBTITLE_FORMATS = ['srt', 'vtt', 'ass', 'ssa']
10
+ SUPPORTED_SUBTITLE_FORMATS2 = ['srt', 'vtt', 'ass', 'ssa']
11
+
12
+ def convert_subtitle_file(src_format, dest_format, input_file_path):
13
+ """
14
+ Convert the subtitle file from src_format to dest_format using ffmpeg.
15
+
16
+ Parameters:
17
+ src_format (str): The source subtitle format.
18
+ dest_format (str): The target subtitle format.
19
+ input_file_path (str): Path to the input subtitle file.
20
+
21
+ Returns:
22
+ tuple: (converted_file_path, error_message)
23
+ """
24
+ # Create a temporary file to store the converted subtitle
25
+ output_file = tempfile.NamedTemporaryFile(delete=False, suffix=f".{dest_format}")
26
+ output_file_path = output_file.name
27
+ output_file.close() # Close the file so that ffmpeg can write to it
28
+
29
+ # Construct the ffmpeg command; the -y flag overwrites the output file if it exists
30
+ cmd = [
31
+ "ffmpeg",
32
+ "-y",
33
+ "-i", input_file_path,
34
+ output_file_path
35
+ ]
36
+ try:
37
+ subprocess.run(cmd, check=True, capture_output=True, text=True, encoding='utf-8')
38
+ return output_file_path, ""
39
+ except subprocess.CalledProcessError as e:
40
+ return "", e.stderr
41
+
42
+ def convert_subtitles(uploaded_file_path, dest_format):
43
+ """
44
+ Convert the uploaded subtitle file to the specified format.
45
+
46
+ Parameters:
47
+ uploaded_file_path (str): Path to the uploaded subtitle file.
48
+ dest_format (str): The target subtitle format.
49
+
50
+ Returns:
51
+ tuple: (converted_file_path, error_message)
52
+ """
53
+ if uploaded_file_path is not None:
54
+ # Determine the source format from the file extension (converted to lowercase)
55
+ src_format = uploaded_file_path.split('.')[-1].lower()
56
+
57
+ if src_format not in SUPPORTED_SUBTITLE_FORMATS:
58
+ return "", "Unsupported file format. Please upload a valid subtitle file (supported: srt, vtt, ass, ssa)."
59
+
60
+ try:
61
+ converted_file_path, error_message = convert_subtitle_file(src_format, dest_format, uploaded_file_path)
62
+ if converted_file_path:
63
+ return converted_file_path, ""
64
+ else:
65
+ return "", f"Error during conversion: {error_message}"
66
+ except Exception as e:
67
+ return "", f"Error during conversion: {e}"
68
+ return "", "No file uploaded."
69
+
70
+ def main():
71
+ # Create the Gradio Blocks interface with a page title for SEO
72
+ with gr.Blocks(analytics_enabled=False, title="Free Online Subtitle Converter - Convert SRT, VTT, ASS, SSA Subtitles Easily") as demo:
73
+
74
+ # Load custom CSS to improve the UI appearance
75
+ demo.load_css("""
76
+ /* Global background and font settings */
77
+ body {
78
+ background-color: #f7f7f7;
79
+ font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
80
+ }
81
+ /* Main container styling */
82
+ .gradio-container {
83
+ max-width: 900px;
84
+ margin: auto;
85
+ background-color: #ffffff;
86
+ border-radius: 8px;
87
+ box-shadow: 0 2px 10px rgba(0,0,0,0.1);
88
+ padding: 20px;
89
+ }
90
+ /* Convert button styling */
91
+ #convert-btn {
92
+ background-color: #007bff;
93
+ color: #ffffff;
94
+ border: none;
95
+ }
96
+ #convert-btn:hover {
97
+ background-color: #0056b3;
98
+ }
99
+ """)
100
+
101
+ # Add SEO information, application description, and usage instructions
102
+ with gr.Row():
103
+ with gr.Column():
104
+ gr.Markdown(
105
+ """
106
+ # Free Online Subtitle Converter
107
+
108
+ **Subtitle Converter** is a simple and easy-to-use online tool that supports converting subtitle files between SRT, VTT, ASS, and SSA formats.
109
+ This application leverages [ffmpeg](https://ffmpeg.org/) for subtitle format conversion, making it ideal for various subtitle conversion needs.
110
+
111
+ ## Application Overview
112
+ - **Fast Conversion**: Upload your file and get the conversion done in seconds.
113
+ - **Multiple Formats Supported**: Supports srt, vtt, ass, ssa formats.
114
+ - **Online Tool**: No need to install any software; use it directly from your browser.
115
+
116
+ ## Usage Instructions
117
+ 1. Upload a subtitle file (supported formats: srt, vtt, ass, ssa) from the left side.
118
+ 2. Select the desired target format from the dropdown menu.
119
+ 3. Click the "Convert" button to initiate the conversion.
120
+ 4. Once the conversion is complete, click the "Download Converted File" button to download your file.
121
+ """
122
+ )
123
+
124
+ # Build the upload and conversion operation area
125
+ with gr.Row():
126
+ # Left column: file upload and format selection
127
+ with gr.Column(scale=1):
128
+ file_input = gr.File(label="Upload Subtitle File", file_types=SUPPORTED_SUBTITLE_FORMATS, type="filepath")
129
+ dest_format = gr.Dropdown(label='Convert To', choices=SUPPORTED_SUBTITLE_FORMATS2, value=SUPPORTED_SUBTITLE_FORMATS2[0])
130
+ convert_button = gr.Button("Convert", elem_id="convert-btn")
131
+ # Right column: display conversion result and error messages
132
+ with gr.Column(scale=1):
133
+ result_file = gr.File(label="Download Converted File", visible=True)
134
+ error_message = gr.Textbox(label="Error Message", visible=False, interactive=False)
135
+ # Download button for the converted file
136
+ download_button = gr.DownloadButton(
137
+ label="Download Converted File",
138
+ value=None,
139
+ visible=True,
140
+ variant="primary",
141
+ )
142
+
143
+ def convert_and_show(uploaded_file_path, dest_format):
144
+ """
145
+ Process the uploaded file conversion and return the conversion result or error message.
146
+
147
+ Parameters:
148
+ uploaded_file_path (str): Path to the uploaded subtitle file.
149
+ dest_format (str): Target format for conversion.
150
+
151
+ Returns:
152
+ tuple: (converted_file_path, updated error message, download button value)
153
+ """
154
+ if not uploaded_file_path:
155
+ return None, gr.update(value="Please upload a subtitle file.", visible=True), None
156
+ if not dest_format:
157
+ return None, gr.update(value="Please select a target format.", visible=True), None
158
+
159
+ file_path, error = convert_subtitles(uploaded_file_path, dest_format)
160
+ if file_path:
161
+ # Conversion successful, hide error message and update download link
162
+ return file_path, gr.update(visible=False), file_path
163
+ else:
164
+ return None, gr.update(value=error, visible=True), None
165
+
166
+ # Bind the click event to the convert function
167
+ convert_button.click(fn=convert_and_show, inputs=[file_input, dest_format], outputs=[result_file, error_message, download_button])
168
+
169
+ # Footer information
170
+ gr.Markdown(
171
+ """
172
+ ---
173
+ **Disclaimer**: This tool is intended for subtitle format conversion only. Please ensure you have the legal rights to use the uploaded files.
174
+ """
175
+ )
176
+
177
+ # Launch the app without specifying a fixed port, making it suitable for Linux and Hugging Face Spaces
178
+ demo.launch(share=True)
179
+
180
+ if __name__ == "__main__":
181
+ main()