-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
259 lines (223 loc) · 9.66 KB
/
app.py
File metadata and controls
259 lines (223 loc) · 9.66 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
from flask import Flask, render_template, jsonify, send_file, request
import subprocess
import os
import threading
import glob
import logging
app = Flask(__name__, static_folder='static')
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
VERSION = "0.3.1"
CAPTURE_DIR = "/captures"
capture_process = None
capture_status = {"running": False, "filename": None, "pid": None, "format": None, "autosplit": False, "last_error": None}
capture_stderr = []
def check_device_ready(device_path):
"""Check if device is fully initialized and ready for capture"""
config_rom_path = os.path.join(device_path, "config_rom")
units_path = os.path.join(device_path, "units")
try:
if os.path.exists(config_rom_path):
config_rom = open(config_rom_path, "rb").read()
if not config_rom or len(config_rom) < 8:
return False
else:
return False
if os.path.exists(units_path):
units = open(units_path, "rb").read()
if not units:
return False
else:
return False
return True
except (IOError, OSError):
return False
def detect_camcorder():
"""Check if a DV camcorder is connected via FireWire/IEEE 1394"""
devices_path = "/sys/bus/firewire/devices/"
result = {"connected": False, "ready": False, "vendor": None, "model": None, "guid": None}
if not os.path.exists(devices_path):
return result
try:
for device in os.listdir(devices_path):
device_path = os.path.join(devices_path, device)
is_local_path = os.path.join(device_path, "is_local")
if os.path.exists(is_local_path):
with open(is_local_path) as f:
if f.read().strip() == "0":
vendor_path = os.path.join(device_path, "vendor_name")
model_path = os.path.join(device_path, "model_name")
guid_path = os.path.join(device_path, "guid")
try:
if os.path.exists(vendor_path):
result["vendor"] = open(vendor_path).read().strip()
except:
pass
try:
if os.path.exists(model_path):
result["model"] = open(model_path).read().strip()
except:
pass
try:
if os.path.exists(guid_path):
result["guid"] = open(guid_path).read().strip()
except:
pass
result["connected"] = True
result["ready"] = check_device_ready(device_path)
return result
except (IOError, OSError):
pass
return result
def get_files():
files = []
for ext in ["*.dv", "*.avi", "*.mov", "*.mkv"]:
files.extend(glob.glob(os.path.join(CAPTURE_DIR, ext)))
files.sort(key=os.path.getmtime, reverse=True)
return [{"name": os.path.basename(f), "size": os.path.getsize(f)} for f in files]
def monitor_capture():
global capture_process, capture_status, capture_stderr
if capture_process:
stdout, stderr = capture_process.communicate()
returncode = capture_process.returncode
if returncode != 0:
logger.error(f"dvgrab exited with code {returncode}: {stderr}")
capture_status["last_error"] = f"Exit code {returncode}: {stderr.strip()}" if stderr.strip() else f"Exit code {returncode}"
else:
logger.info(f"dvgrab exited normally")
capture_process = None
capture_status["running"] = False
capture_status["pid"] = None
@app.route("/")
def index():
return render_template("index.html", files=get_files(), status=capture_status)
@app.route("/api/start", methods=["POST"])
def start_capture():
global capture_process, capture_status, capture_stderr
if capture_status["running"]:
return jsonify({"error": "Capture already running"}), 400
data = request.get_json() or {}
prefix = data.get("prefix", "DV-")
fmt = data.get("format", "dv2")
autosplit = data.get("autosplit", False)
device = detect_camcorder()
if not device.get("connected"):
return jsonify({"error": "No camera connected"}), 400
if not device.get("ready"):
return jsonify({"error": "Camera not ready - wait a moment and try again"}), 400
cmd = ["dvgrab", "-f", fmt]
if device.get("guid"):
cmd.extend(["-guid", device["guid"]])
if autosplit:
cmd.append("-autosplit")
cmd.append(os.path.join(CAPTURE_DIR, prefix))
def try_capture(retry_count=0):
global capture_process, capture_status, capture_stderr
try:
capture_process = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
capture_status = {
"running": True,
"filename": prefix,
"pid": capture_process.pid,
"format": fmt,
"autosplit": autosplit,
"last_error": None
}
capture_stderr = []
def monitor_with_retry():
global capture_process, capture_status
import time
start_time = time.time()
while capture_process and time.time() - start_time < 3:
poll_result = capture_process.poll()
if poll_result is not None:
_, stderr = capture_process.communicate()
if poll_result != 0 and retry_count < 1:
logger.warning(f"dvgrab failed quickly (code {poll_result}), retrying after 2s...")
time.sleep(2)
capture_process = None
try_capture(retry_count + 1)
return
else:
monitor_capture()
return
time.sleep(0.1)
if capture_process:
monitor_thread = threading.Thread(target=monitor_capture, daemon=True)
monitor_thread.start()
monitor_thread = threading.Thread(target=monitor_with_retry, daemon=True)
monitor_thread.start()
logger.info(f"Started dvgrab with PID {capture_process.pid}: {' '.join(cmd)}")
return None
except Exception as e:
logger.error(f"Failed to start dvgrab: {e}")
return str(e)
error = try_capture()
if error:
return jsonify({"error": error}), 500
return jsonify({"status": "started", "pid": capture_status["pid"]})
@app.route("/api/stop", methods=["POST"])
def stop_capture():
global capture_process, capture_status
if not capture_status["running"] or not capture_process:
return jsonify({"error": "No capture running"}), 400
try:
capture_process.terminate()
capture_process.wait(timeout=5)
logger.info(f"dvgrab terminated normally (PID {capture_status['pid']})")
except subprocess.TimeoutExpired:
capture_process.kill()
capture_process.wait()
logger.warning(f"dvgrab killed after timeout (PID {capture_status['pid']})")
except Exception as e:
logger.error(f"Error stopping dvgrab: {e}")
return jsonify({"error": str(e)}), 500
finally:
capture_process = None
capture_status = {"running": False, "filename": None, "pid": None, "format": None, "autosplit": False, "last_error": None}
return jsonify({"status": "stopped"})
@app.route("/api/status")
def status():
global capture_process, capture_status
if capture_status["running"] and capture_process:
poll_result = capture_process.poll()
if poll_result is not None:
_, stderr = capture_process.communicate()
if poll_result != 0:
capture_status["last_error"] = f"Process died (code {poll_result}): {stderr.strip()}" if stderr.strip() else f"Process died (code {poll_result})"
capture_status["running"] = False
capture_status["pid"] = None
capture_process = None
logger.warning(f"dvgrab process died unexpectedly: {capture_status.get('last_error')}")
return jsonify(capture_status)
@app.route("/api/version")
def get_version():
return jsonify({"version": VERSION})
@app.route("/api/device")
def device_status():
"""Return DV camcorder connection status and device info"""
return jsonify(detect_camcorder())
@app.route("/api/files")
def list_files():
return jsonify(get_files())
@app.route("/api/download/<filename>")
def download_file(filename):
filepath = os.path.join(CAPTURE_DIR, filename)
if os.path.exists(filepath):
return send_file(filepath, as_attachment=True)
return jsonify({"error": "File not found"}), 404
@app.route("/api/delete/<filename>", methods=["DELETE"])
def delete_file(filename):
filepath = os.path.join(CAPTURE_DIR, filename)
if os.path.exists(filepath):
os.remove(filepath)
return jsonify({"status": "deleted"})
return jsonify({"error": "File not found"}), 404
if __name__ == "__main__":
os.makedirs(CAPTURE_DIR, exist_ok=True)
app.run(host="0.0.0.0", port=5000)