from flask import Flask, request, render_template, redirect, url_for import os from moviepy.editor import VideoFileClip import whisper app = Flask(__name__) from flask import Flask, request, render_template, redirect, url_for import os from moviepy.editor import VideoFileClip import whisper app = Flask(__name__) # Set ffmpeg path for the Docker container environment ffmpeg_path = r'/usr/bin/ffmpeg' if not os.path.isdir(ffmpeg_path): raise FileNotFoundError(f"FFmpeg directory not found: {ffmpeg_path}") os.environ['PATH'] += os.pathsep + ffmpeg_path # Load the Whisper model model = whisper.load_model("medium") # Change to "large" for the most accurate model @app.route('/') def index(): return render_template('index.html') @app.route('/upload', methods=['POST']) def upload_video(): if 'video' not in request.files: return redirect(url_for('index')) video_file = request.files['video'] if video_file.filename == '': return redirect(url_for('index')) # Save the video file video_path = os.path.join('uploads', video_file.filename) video_file.save(video_path) print(f"Video saved to {video_path}") # Extract audio from the video try: audio_path = extract_audio(video_path) print(f"Audio extracted to {audio_path}") if not os.path.exists(audio_path): return f"Error: Audio file not found at {audio_path}" except Exception as e: return f"Error extracting audio: {e}" # Transcribe the audio try: transcript = transcribe_audio(audio_path) except Exception as e: return f"Error transcribing audio: {e}" return render_template('result.html', transcript=transcript) def extract_audio(video_path): audio_path = os.path.splitext(video_path)[0] + ".wav" video = VideoFileClip(video_path) video.audio.write_audiofile(audio_path) return audio_path def transcribe_audio(audio_path): print(f"Transcribing audio from: {audio_path}") if not os.path.exists(audio_path): raise FileNotFoundError(f"Audio file not found at {audio_path}") try: os.system("ffmpeg -version") print("FFmpeg is available") except Exception as e: print(f"FFmpeg is not available: {e}") raise try: result = model.transcribe(audio_path) print(f"Transcription result: {result}") return result["text"] except Exception as e: print(f"Error during transcription: {e}") raise if __name__ == '__main__': if not os.path.exists('uploads'): os.makedirs('uploads') app.run(debug=True) # Add ffmpeg to the PATH ffmpeg_path = r'/usr/bin/ffmpeg' # Path for ffmpeg in the Docker container # Load the Whisper model model = whisper.load_model("medium") # Change to "large" for the most accurate model @app.route('/') def index(): return render_template('index.html') @app.route('/upload', methods=['POST']) def upload_video(): if 'video' not in request.files: return redirect(url_for('index')) video_file = request.files['video'] if video_file.filename == '': return redirect(url_for('index')) # Save the video file video_path = os.path.join('uploads', video_file.filename) video_file.save(video_path) try: # Extract audio from the video audio_path = extract_audio(video_path) # Transcribe the audio transcript = transcribe_audio(audio_path) except Exception as e: return f"Error: {e}" return render_template('result.html', transcript=transcript, model_name="medium") # Adjust model name accordingly def extract_audio(video_path): audio_path = os.path.splitext(video_path)[0] + ".wav" try: video = VideoFileClip(video_path) video.audio.write_audiofile(audio_path) except Exception as e: raise RuntimeError(f"Error extracting audio: {e}") return audio_path def transcribe_audio(audio_path): if not os.path.exists(audio_path): raise FileNotFoundError(f"Audio file not found at {audio_path}") try: result = model.transcribe(audio_path) return result["text"] except Exception as e: raise RuntimeError(f"Error during transcription: {e}")