Update app.py
Browse files
app.py
CHANGED
@@ -62,8 +62,11 @@ def download_youtube_video(url, output_folder):
|
|
62 |
output_path_template = os.path.join(output_folder, os.path.splitext(unique_filename)[0])
|
63 |
ydl_opts = {
|
64 |
'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
|
65 |
-
'outtmpl': output_path_template,
|
66 |
-
'
|
|
|
|
|
|
|
67 |
}
|
68 |
try:
|
69 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
@@ -124,13 +127,14 @@ def replace_video_audio(video_path, new_audio_path, output_path):
|
|
124 |
@app.route('/', methods=['GET'])
|
125 |
def index():
|
126 |
"""Renders the main upload page."""
|
127 |
-
return render_template('index.html')
|
128 |
|
129 |
@app.route('/process', methods=['POST'])
|
130 |
def process_video():
|
131 |
"""Handles video input (upload or URL), processing, and renders the result."""
|
132 |
input_video_path, temp_audio_path = None, None
|
133 |
try:
|
|
|
134 |
youtube_url = request.form.get('youtube_url', '').strip()
|
135 |
if youtube_url:
|
136 |
input_video_path = download_youtube_video(youtube_url, app.config['UPLOAD_FOLDER'])
|
@@ -142,30 +146,38 @@ def process_video():
|
|
142 |
file.save(input_video_path)
|
143 |
else:
|
144 |
flash("Please either upload a video file or provide a YouTube URL.")
|
145 |
-
return render_template('index.html')
|
146 |
|
|
|
147 |
voice_choice = request.form['voice_choice']
|
148 |
is_cheerful = 'cheerful' in request.form
|
149 |
voice_name = VOICE_CHOICES[voice_choice]
|
|
|
|
|
150 |
script = generate_tamil_script(input_video_path)
|
151 |
|
|
|
152 |
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_audio:
|
153 |
temp_audio_path = temp_audio.name
|
154 |
|
155 |
generate_single_audio_track(script, voice_name, is_cheerful, temp_audio_path)
|
|
|
|
|
156 |
final_video_name = f"dubbed_{filename}"
|
157 |
final_video_path = os.path.join(app.config['DOWNLOAD_FOLDER'], final_video_name)
|
158 |
replace_video_audio(input_video_path, temp_audio_path, final_video_path)
|
159 |
|
160 |
flash("Video processing complete!", "success")
|
161 |
return render_template('index.html',
|
162 |
-
|
163 |
-
|
|
|
164 |
except Exception as e:
|
165 |
print(f"An error occurred during processing: {e}")
|
166 |
flash(f"An unexpected error occurred: {e}. Please check the console and try again.", "error")
|
167 |
-
return render_template('index.html')
|
168 |
finally:
|
|
|
169 |
if input_video_path and os.path.exists(input_video_path):
|
170 |
os.remove(input_video_path)
|
171 |
if temp_audio_path and os.path.exists(temp_audio_path):
|
@@ -178,4 +190,4 @@ def serve_video(filename):
|
|
178 |
|
179 |
# --- 5. APPLICATION ENTRY POINT ---
|
180 |
if __name__ == '__main__':
|
181 |
-
|
|
|
62 |
output_path_template = os.path.join(output_folder, os.path.splitext(unique_filename)[0])
|
63 |
ydl_opts = {
|
64 |
'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
|
65 |
+
'outtmpl': output_path_template,
|
66 |
+
'merge_output_format': 'mp4',
|
67 |
+
'quiet': False,
|
68 |
+
'noplaylist': True,
|
69 |
+
'progress': True,
|
70 |
}
|
71 |
try:
|
72 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
|
|
127 |
@app.route('/', methods=['GET'])
|
128 |
def index():
|
129 |
"""Renders the main upload page."""
|
130 |
+
return render_template('index.html', voice_choices=VOICE_CHOICES)
|
131 |
|
132 |
@app.route('/process', methods=['POST'])
|
133 |
def process_video():
|
134 |
"""Handles video input (upload or URL), processing, and renders the result."""
|
135 |
input_video_path, temp_audio_path = None, None
|
136 |
try:
|
137 |
+
# Handle YouTube URL or file upload
|
138 |
youtube_url = request.form.get('youtube_url', '').strip()
|
139 |
if youtube_url:
|
140 |
input_video_path = download_youtube_video(youtube_url, app.config['UPLOAD_FOLDER'])
|
|
|
146 |
file.save(input_video_path)
|
147 |
else:
|
148 |
flash("Please either upload a video file or provide a YouTube URL.")
|
149 |
+
return render_template('index.html', voice_choices=VOICE_CHOICES)
|
150 |
|
151 |
+
# Get user preferences
|
152 |
voice_choice = request.form['voice_choice']
|
153 |
is_cheerful = 'cheerful' in request.form
|
154 |
voice_name = VOICE_CHOICES[voice_choice]
|
155 |
+
|
156 |
+
# Generate Tamil script
|
157 |
script = generate_tamil_script(input_video_path)
|
158 |
|
159 |
+
# Generate audio track
|
160 |
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_audio:
|
161 |
temp_audio_path = temp_audio.name
|
162 |
|
163 |
generate_single_audio_track(script, voice_name, is_cheerful, temp_audio_path)
|
164 |
+
|
165 |
+
# Create final video
|
166 |
final_video_name = f"dubbed_{filename}"
|
167 |
final_video_path = os.path.join(app.config['DOWNLOAD_FOLDER'], final_video_name)
|
168 |
replace_video_audio(input_video_path, temp_audio_path, final_video_path)
|
169 |
|
170 |
flash("Video processing complete!", "success")
|
171 |
return render_template('index.html',
|
172 |
+
voice_choices=VOICE_CHOICES,
|
173 |
+
result_video=url_for('serve_video', filename=final_video_name),
|
174 |
+
script=script)
|
175 |
except Exception as e:
|
176 |
print(f"An error occurred during processing: {e}")
|
177 |
flash(f"An unexpected error occurred: {e}. Please check the console and try again.", "error")
|
178 |
+
return render_template('index.html', voice_choices=VOICE_CHOICES)
|
179 |
finally:
|
180 |
+
# Clean up temporary files
|
181 |
if input_video_path and os.path.exists(input_video_path):
|
182 |
os.remove(input_video_path)
|
183 |
if temp_audio_path and os.path.exists(temp_audio_path):
|
|
|
190 |
|
191 |
# --- 5. APPLICATION ENTRY POINT ---
|
192 |
if __name__ == '__main__':
|
193 |
+
app.run(host="0.0.0.0", port=7860)
|