Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- app.py +77 -0
- requirements.txt +4 -0
- templates/index.html +15 -0
- templates/result.html +13 -0
app.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, render_template, redirect, url_for
|
2 |
+
import os
|
3 |
+
from moviepy.editor import VideoFileClip
|
4 |
+
import whisper
|
5 |
+
|
6 |
+
app = Flask(__name__)
|
7 |
+
|
8 |
+
# Add ffmpeg to the PATH
|
9 |
+
ffmpeg_path = r'C:\Users\M&G\Downloads\ffmpeg-7.0.1-essentials_build\bin' # Replace with your actual path
|
10 |
+
|
11 |
+
if not os.path.isdir(ffmpeg_path):
|
12 |
+
raise FileNotFoundError(f"FFmpeg directory not found: {ffmpeg_path}")
|
13 |
+
os.environ['PATH'] += os.pathsep + ffmpeg_path
|
14 |
+
|
15 |
+
# Verify that ffmpeg is available
|
16 |
+
try:
|
17 |
+
from subprocess import check_output
|
18 |
+
ffmpeg_version = check_output(['ffmpeg', '-version'], text=True)
|
19 |
+
print("FFmpeg is available. Version details:\n", ffmpeg_version)
|
20 |
+
except FileNotFoundError:
|
21 |
+
raise RuntimeError("FFmpeg executable not found in the specified PATH.")
|
22 |
+
except Exception as e:
|
23 |
+
raise RuntimeError(f"Error checking FFmpeg version: {e}")
|
24 |
+
|
25 |
+
# Load the Whisper model
|
26 |
+
model = whisper.load_model("medium")
|
27 |
+
|
28 |
+
@app.route('/')
|
29 |
+
def index():
|
30 |
+
return render_template('index.html')
|
31 |
+
|
32 |
+
@app.route('/upload', methods=['POST'])
|
33 |
+
def upload_video():
|
34 |
+
if 'video' not in request.files:
|
35 |
+
return redirect(url_for('index'))
|
36 |
+
|
37 |
+
video_file = request.files['video']
|
38 |
+
if video_file.filename == '':
|
39 |
+
return redirect(url_for('index'))
|
40 |
+
|
41 |
+
# Save the video file
|
42 |
+
video_path = os.path.join('uploads', video_file.filename)
|
43 |
+
video_file.save(video_path)
|
44 |
+
|
45 |
+
try:
|
46 |
+
# Extract audio from the video
|
47 |
+
audio_path = extract_audio(video_path)
|
48 |
+
# Transcribe the audio
|
49 |
+
transcript = transcribe_audio(audio_path)
|
50 |
+
except Exception as e:
|
51 |
+
return f"Error: {e}"
|
52 |
+
|
53 |
+
return render_template('result.html', transcript=transcript)
|
54 |
+
|
55 |
+
def extract_audio(video_path):
|
56 |
+
audio_path = os.path.splitext(video_path)[0] + ".wav"
|
57 |
+
try:
|
58 |
+
video = VideoFileClip(video_path)
|
59 |
+
video.audio.write_audiofile(audio_path)
|
60 |
+
except Exception as e:
|
61 |
+
raise RuntimeError(f"Error extracting audio: {e}")
|
62 |
+
return audio_path
|
63 |
+
|
64 |
+
def transcribe_audio(audio_path):
|
65 |
+
if not os.path.exists(audio_path):
|
66 |
+
raise FileNotFoundError(f"Audio file not found at {audio_path}")
|
67 |
+
|
68 |
+
try:
|
69 |
+
result = model.transcribe(audio_path)
|
70 |
+
return result["text"]
|
71 |
+
except Exception as e:
|
72 |
+
raise RuntimeError(f"Error during transcription: {e}")
|
73 |
+
|
74 |
+
if __name__ == '__main__':
|
75 |
+
if not os.path.exists('uploads'):
|
76 |
+
os.makedirs('uploads')
|
77 |
+
app.run(debug=True)
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
flask
|
2 |
+
moviepy
|
3 |
+
whisper
|
4 |
+
gunicorn
|
templates/index.html
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html lang="en">
|
3 |
+
<head>
|
4 |
+
<meta charset="UTF-8">
|
5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
+
<title>Video Transcription</title>
|
7 |
+
</head>
|
8 |
+
<body>
|
9 |
+
<h1>Upload Video for Transcription</h1>
|
10 |
+
<form action="{{ url_for('upload_video') }}" method="post" enctype="multipart/form-data">
|
11 |
+
<input type="file" name="video" accept="video/*" required>
|
12 |
+
<button type="submit">Upload Video</button>
|
13 |
+
</form>
|
14 |
+
</body>
|
15 |
+
</html>
|
templates/result.html
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html lang="en">
|
3 |
+
<head>
|
4 |
+
<meta charset="UTF-8">
|
5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
+
<title>Transcription Result</title>
|
7 |
+
</head>
|
8 |
+
<body>
|
9 |
+
<h1>Transcription Result</h1>
|
10 |
+
<p>{{ transcript }}</p>
|
11 |
+
<a href="{{ url_for('index') }}">Upload another video</a>
|
12 |
+
</body>
|
13 |
+
</html>
|