![]() Server : Apache/2 System : Linux server-15-235-50-60 5.15.0-164-generic #174-Ubuntu SMP Fri Nov 14 20:25:16 UTC 2025 x86_64 User : gositeme ( 1004) PHP Version : 8.2.29 Disable Function : exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname Directory : /home/gositeme/domains/soundstudiopro.com/public_html/ |
<?php
/**
* Backfill Waveform Data for All Existing Tracks
*
* This script extracts waveform data from:
* 1. task_results JSON files (if available)
* 2. API.Box status endpoint (if task_id is available)
*
* It updates the waveform_data column in the music_tracks table.
*/
require_once 'config/database.php';
// API key for API.Box (if needed for re-syncing)
$api_key = '63edba40620216c5aa2c04240ac41dbd';
echo "<!DOCTYPE html>
<html>
<head>
<title>Backfill Waveform Data</title>
<style>
body { font-family: Arial, sans-serif; padding: 20px; background: #1a1a2e; color: #fff; }
.success { color: #4ade80; }
.error { color: #f87171; }
.warning { color: #fbbf24; }
.info { color: #60a5fa; }
.track-card { background: rgba(255,255,255,0.1); padding: 15px; margin: 10px 0; border-radius: 8px; }
.stats { background: rgba(102, 126, 234, 0.2); padding: 20px; border-radius: 8px; margin: 20px 0; }
h1 { color: #667eea; }
h2 { color: #764ba2; }
</style>
</head>
<body>";
echo "<h1>đ Backfill Waveform Data for All Tracks</h1>";
echo "<p><strong>This script extracts and updates waveform data for all existing tracks.</strong></p>";
try {
$pdo = getDBConnection();
if (!$pdo) {
echo "<p class='error'>â Database connection failed</p>";
exit;
}
// Get all complete tracks
$stmt = $pdo->query("
SELECT id, task_id, title, waveform_data, metadata
FROM music_tracks
WHERE status = 'complete'
ORDER BY id DESC
");
$tracks = $stmt->fetchAll(PDO::FETCH_ASSOC);
$total_tracks = count($tracks);
echo "<div class='stats'>";
echo "<h2>đ Statistics</h2>";
echo "<p><strong>Total tracks to process:</strong> $total_tracks</p>";
echo "</div>";
$updated_count = 0;
$skipped_count = 0;
$error_count = 0;
$from_json_count = 0;
$from_api_count = 0;
echo "<h2>đ Processing Tracks...</h2>";
foreach ($tracks as $track) {
$track_id = $track['id'];
$task_id = $track['task_id'];
$title = $track['title'];
echo "<div class='track-card'>";
echo "<h3>đĩ Track #$track_id: " . htmlspecialchars($title) . "</h3>";
echo "<p><strong>Task ID:</strong> $task_id</p>";
// Check if waveform_data already exists
$existing_waveform = json_decode($track['waveform_data'] ?? '{}', true);
if (!empty($existing_waveform) && (isset($existing_waveform['samples']) || isset($existing_waveform['data']))) {
$samples_count = count($existing_waveform['samples'] ?? $existing_waveform['data'] ?? []);
if ($samples_count > 0) {
echo "<p class='info'>âšī¸ Waveform data already exists ($samples_count samples). Skipping.</p>";
$skipped_count++;
echo "</div>";
continue;
}
}
// Try to extract from task_results JSON file first
$waveform_data = null;
$task_results_file = "task_results/{$task_id}.json";
if (file_exists($task_results_file)) {
echo "<p class='info'>đ Found task_results file, extracting waveform data...</p>";
$json_content = file_get_contents($task_results_file);
$json_data = json_decode($json_content, true);
if ($json_data) {
// Try multiple paths for waveform data
$waveform_data =
$json_data['waveform'] ??
$json_data['waveform_data'] ??
$json_data['data']['waveform'] ??
$json_data['data']['waveform_data'] ??
$json_data['metadata']['waveform_data'] ??
null;
// Also check if it's nested in metadata
if (!$waveform_data && isset($json_data['metadata'])) {
$metadata = is_string($json_data['metadata']) ? json_decode($json_data['metadata'], true) : $json_data['metadata'];
if ($metadata) {
$waveform_data = $metadata['waveform_data'] ?? $metadata['waveform'] ?? null;
}
}
if ($waveform_data) {
echo "<p class='success'>â
Found waveform data in task_results file</p>";
$from_json_count++;
} else {
echo "<p class='warning'>â ī¸ No waveform data found in task_results file</p>";
}
} else {
echo "<p class='error'>â Could not parse task_results JSON</p>";
}
} else {
echo "<p class='warning'>â ī¸ task_results file not found: $task_results_file</p>";
}
// If not found in JSON, try to fetch from API.Box
if (!$waveform_data && $task_id) {
echo "<p class='info'>đ Attempting to fetch from API.Box...</p>";
$api_url = "https://api.api.box/api/v1/status/$task_id";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $api_key,
'Content-Type: application/json',
'User-Agent: SoundStudioPro-Waveform-Backfill/1.0'
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curl_error = curl_error($ch);
curl_close($ch);
if (!$curl_error && $http_code === 200) {
$api_data = json_decode($response, true);
if ($api_data) {
$waveform_data =
$api_data['waveform'] ??
$api_data['waveform_data'] ??
$api_data['data']['waveform'] ??
$api_data['data']['waveform_data'] ??
null;
if ($waveform_data) {
echo "<p class='success'>â
Found waveform data from API.Box</p>";
$from_api_count++;
} else {
echo "<p class='warning'>â ī¸ No waveform data in API.Box response</p>";
}
} else {
echo "<p class='error'>â Could not parse API.Box response</p>";
}
} else {
echo "<p class='warning'>â ī¸ API.Box request failed: HTTP $http_code" . ($curl_error ? " - $curl_error" : "") . "</p>";
}
}
// Update database if waveform data was found
if ($waveform_data) {
// Ensure waveform_data is in the correct format
$formatted_waveform = null;
if (is_array($waveform_data)) {
// If it's already an array with samples/data, use it directly
if (isset($waveform_data['samples']) || isset($waveform_data['data'])) {
$formatted_waveform = $waveform_data;
} else {
// If it's a flat array, wrap it in a structure
$formatted_waveform = [
'samples' => $waveform_data,
'data' => $waveform_data
];
}
} else if (is_string($waveform_data)) {
// Try to decode if it's a JSON string
$decoded = json_decode($waveform_data, true);
if ($decoded) {
$formatted_waveform = $decoded;
} else {
// If it's a comma-separated string, convert to array
$formatted_waveform = [
'samples' => array_map('intval', explode(',', $waveform_data)),
'data' => array_map('intval', explode(',', $waveform_data))
];
}
}
if ($formatted_waveform) {
// Update waveform_data column
$update_stmt = $pdo->prepare("
UPDATE music_tracks
SET waveform_data = ?
WHERE id = ?
");
$result = $update_stmt->execute([
json_encode($formatted_waveform),
$track_id
]);
if ($result) {
$samples_count = count($formatted_waveform['samples'] ?? $formatted_waveform['data'] ?? []);
echo "<p class='success'>â
Updated waveform_data in database ($samples_count samples)</p>";
$updated_count++;
} else {
echo "<p class='error'>â Failed to update database</p>";
$error_count++;
}
} else {
echo "<p class='error'>â Could not format waveform data</p>";
$error_count++;
}
} else {
echo "<p class='warning'>â ī¸ No waveform data available for this track</p>";
$skipped_count++;
}
echo "</div>";
// Small delay to avoid overwhelming the server
usleep(100000); // 0.1 second
}
// Final statistics
echo "<div class='stats'>";
echo "<h2>đ Final Statistics</h2>";
echo "<p><strong>Total tracks processed:</strong> $total_tracks</p>";
echo "<p class='success'><strong>â
Updated:</strong> $updated_count</p>";
echo "<p class='info'><strong>đ From task_results JSON:</strong> $from_json_count</p>";
echo "<p class='info'><strong>đ From API.Box:</strong> $from_api_count</p>";
echo "<p class='warning'><strong>âī¸ Skipped (already had data):</strong> $skipped_count</p>";
echo "<p class='error'><strong>â Errors:</strong> $error_count</p>";
echo "</div>";
echo "<p><strong>â
Backfill complete!</strong></p>";
} catch (PDOException $e) {
echo "<p class='error'>â Database Error: " . htmlspecialchars($e->getMessage()) . "</p>";
} catch (Exception $e) {
echo "<p class='error'>â Error: " . htmlspecialchars($e->getMessage()) . "</p>";
}
echo "</body></html>";
?>