-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathapp.py
More file actions
211 lines (171 loc) · 6.96 KB
/
app.py
File metadata and controls
211 lines (171 loc) · 6.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
from flask import Flask, render_template, request, redirect, url_for, flash, send_from_directory
import os
from werkzeug.utils import secure_filename
import sqlite3
from datetime import datetime
from video_processor import VideoProcessor
import threading
from dotenv import load_dotenv
def adapt_datetime(ts):
return ts.isoformat()
def convert_datetime(val):
return datetime.fromisoformat(val)
# Register the adapter and converter
sqlite3.register_adapter(datetime, adapt_datetime)
sqlite3.register_converter("datetime", convert_datetime)
load_dotenv()
app = Flask(__name__)
app.secret_key = os.getenv('FLASK_SECRET_KEY', 'dev') # Change this in production
app.config['UPLOAD_FOLDER'] = os.path.abspath('uploads')
app.config['CLIPS_FOLDER'] = os.path.abspath('clips')
app.config['TRANSCRIPTS_FOLDER'] = os.path.abspath('transcripts')
app.config['DATABASE'] = os.path.abspath('videos.db')
app.config['MAX_CONTENT_LENGTH'] = 500 * 1024 * 1024 # 500MB max file size
app.config['ALLOWED_EXTENSIONS'] = {'mp4', 'avi', 'mov', 'mkv'}
# Create required directories if they don't exist
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
os.makedirs(app.config['CLIPS_FOLDER'], exist_ok=True)
os.makedirs(app.config['TRANSCRIPTS_FOLDER'], exist_ok=True)
# Database initialization
def init_db():
conn = sqlite3.connect(app.config['DATABASE'], detect_types=sqlite3.PARSE_DECLTYPES)
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS videos
(id INTEGER PRIMARY KEY AUTOINCREMENT,
filename TEXT,
upload_date DATETIME,
status TEXT)''')
c.execute('''CREATE TABLE IF NOT EXISTS clips
(id INTEGER PRIMARY KEY AUTOINCREMENT,
video_id INTEGER,
clip_path TEXT,
start_time REAL,
end_time REAL,
description TEXT,
FOREIGN KEY(video_id) REFERENCES videos(id))''')
conn.commit()
conn.close()
init_db()
@app.route('/')
def index():
return render_template('upload.html')
# Add after init_db()
@app.route('/upload', methods=['POST'])
def upload_video():
if 'video' not in request.files:
return redirect(url_for('index'))
file = request.files['video']
if file.filename == '':
return redirect(url_for('index'))
if not allowed_file(file.filename):
flash('File type not allowed')
return redirect(url_for('index'))
filename = secure_filename(file.filename)
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(filepath)
# Store in database
conn = sqlite3.connect(app.config['DATABASE'], detect_types=sqlite3.PARSE_DECLTYPES)
c = conn.cursor()
c.execute('INSERT INTO videos (filename, upload_date, status) VALUES (?, ?, ?)',
(filename, datetime.now(), 'uploaded'))
video_id = c.lastrowid
conn.commit()
conn.close()
# Start background processing
processor = VideoProcessor()
threading.Thread(target=process_video_background, args=(video_id, filepath)).start()
return redirect(url_for('status', video_id=video_id))
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS']
def process_video_background(video_id, filepath):
conn = sqlite3.connect(app.config['DATABASE'], detect_types=sqlite3.PARSE_DECLTYPES)
c = conn.cursor()
def update_status(status):
c.execute('UPDATE videos SET status=? WHERE id=?', (status, video_id))
conn.commit()
try:
update_status('initializing')
try:
processor = VideoProcessor()
except Exception as e:
update_status(f'error: Failed to initialize video processor: {str(e)}')
return
update_status('transcribing')
try:
clips = processor.process_video(filepath)
except Exception as e:
update_status(f'error: Failed to process video: {str(e)}')
return
update_status('saving_clips')
# Store clips in database
for clip in clips:
try:
c.execute('''INSERT INTO clips
(video_id, clip_path, start_time, end_time, description)
VALUES (?, ?, ?, ?, ?)''',
(video_id, clip['path'], clip['start'],
clip['end'], clip['description']))
except Exception as e:
update_status(f'error: Failed to save clip: {str(e)}')
return
update_status('processed')
conn.commit()
except Exception as e:
update_status(f'error: Unexpected error: {str(e)}')
finally:
conn.close()
@app.route('/status/<int:video_id>')
def status(video_id):
conn = sqlite3.connect(app.config['DATABASE'])
c = conn.cursor()
c.execute('SELECT v.status, v.filename FROM videos v WHERE v.id=?', (video_id,))
result = c.fetchone()
if not result:
return {'status': 'unknown'}
status, filename = result
if status == 'processed':
c.execute('SELECT COUNT(*) FROM clips WHERE video_id=?', (video_id,))
clip_count = c.fetchone()[0]
return {
'status': status,
'filename': filename,
'clips': clip_count,
'redirect': url_for('dashboard', video_id=video_id)
}
return {'status': status, 'filename': filename}
@app.route('/clips/<path:filename>')
def serve_clip(filename):
return send_from_directory(app.config['CLIPS_FOLDER'], filename)
# Add after status route
@app.route('/dashboard/<int:video_id>')
def dashboard(video_id):
conn = sqlite3.connect(app.config['DATABASE'])
c = conn.cursor()
# Get video info
c.execute('SELECT * FROM videos WHERE id=?', (video_id,))
video = c.fetchone()
if not video:
flash('Video not found', 'error')
return redirect(url_for('index'))
# Get clips
c.execute('SELECT * FROM clips WHERE video_id=?', (video_id,))
clips = c.fetchall()
# Convert clip paths to URLs
formatted_clips = []
for clip in clips:
clip_filename = os.path.basename(clip[2])
clip_url = url_for('serve_clip', filename=clip_filename)
formatted_clips.append({
'id': clip[0],
'video_id': clip[1],
'url': clip_url,
'start_time': clip[3],
'end_time': clip[4],
'description': clip[5]
})
conn.close()
return render_template('dashboard.html',
video=video,
clips=formatted_clips)
if __name__ == '__main__':
app.run(debug=True)