videoapp-compiler / api /extract.php
josephrw's picture
Upload folder using huggingface_hub
37ac66f verified
Raw
History Blame Contribute Delete
1.45 kB
<?php
require_once __DIR__ . '/config.php';
ensure_dirs();
$id = $_POST['id'] ?? $_GET['id'] ?? null;
if (!$id || !preg_match('/^[a-f0-9]{16}$/', $id)) {
fail_json('Invalid id');
}
$jobPath = RECEIPT_DIR . "/$id.job.json";
$job = read_json_file($jobPath);
if (!$job) {
fail_json('Job not found', 404);
}
$video = $job['upload_path'];
if (!file_exists($video)) {
fail_json('Uploaded video missing');
}
$outDir = RECEIPT_DIR . "/$id";
if (!is_dir($outDir)) {
mkdir($outDir, 0775, true);
}
$cmd = BIN_EXTRACT . ' extract ' . shell_arg($video) . ' ' . shell_arg($outDir) . ' 2>&1';
$output = [];
$code = 0;
exec($cmd, $output, $code);
$receiptPath = "$outDir/receipt.json";
$receipt = read_json_file($receiptPath);
// Inject videoId and videoName into receipt
if ($receipt) {
$receipt['videoId'] = $id;
$receipt['videoName'] = $job['original_name'];
write_json_file($receiptPath, $receipt);
}
$job['status'] = $code === 0 ? 'extracted' : 'extract_failed';
$job['extract_exit_code'] = $code;
$job['extract_log'] = implode("\n", array_slice($output, -80));
$job['receipt_path'] = $receiptPath;
$job['updated_at'] = time();
write_json_file($jobPath, $job);
if ($code !== 0 || !$receipt) {
fail_json('Extraction failed', 500, [
'exit_code' => $code,
'log' => $job['extract_log']
]);
}
json_response([
'ok' => true,
'id' => $id,
'receipt' => $receipt,
'log' => $job['extract_log']
]);