271 lines
11 KiB
PHP
271 lines
11 KiB
PHP
<?php
|
||
/**
|
||
* Converti Video in Formato Web-Compatible
|
||
*
|
||
* Converte automaticamente i video caricati in formato MP4 con codec H.264 e AAC
|
||
* per garantire la massima compatibilità con tutti i browser
|
||
*/
|
||
|
||
require_once 'includes/config.php';
|
||
require_once 'includes/functions.php';
|
||
|
||
session_start();
|
||
check_session_timeout();
|
||
require_admin();
|
||
|
||
$message = '';
|
||
$error = '';
|
||
$videos = [];
|
||
|
||
// Trova tutti i video che potrebbero aver bisogno di conversione
|
||
$pdo = get_db_connection();
|
||
$stmt = $pdo->query("
|
||
SELECT id, title, video_url, video_platform
|
||
FROM lessons
|
||
WHERE type = 'video'
|
||
AND video_platform = 'local'
|
||
AND video_url IS NOT NULL
|
||
AND deleted_at IS NULL
|
||
ORDER BY created_at DESC
|
||
");
|
||
$videos = $stmt->fetchAll();
|
||
|
||
// Verifica se FFmpeg è disponibile
|
||
function check_ffmpeg() {
|
||
$output = [];
|
||
$return_var = 0;
|
||
exec('ffmpeg -version 2>&1', $output, $return_var);
|
||
return $return_var === 0;
|
||
}
|
||
|
||
$ffmpeg_available = check_ffmpeg();
|
||
|
||
// Funzione per convertire un video
|
||
function convert_video($input_path, $output_path) {
|
||
$command = sprintf(
|
||
'ffmpeg -i %s -c:v libx264 -preset medium -crf 23 -c:a aac -b:a 128k -movflags +faststart -y %s 2>&1',
|
||
escapeshellarg($input_path),
|
||
escapeshellarg($output_path)
|
||
);
|
||
|
||
$output = [];
|
||
$return_var = 0;
|
||
exec($command, $output, $return_var);
|
||
|
||
return [
|
||
'success' => $return_var === 0,
|
||
'output' => implode("\n", $output)
|
||
];
|
||
}
|
||
|
||
// Processa conversione se richiesta
|
||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['convert_video'])) {
|
||
$lesson_id = intval($_POST['lesson_id'] ?? 0);
|
||
|
||
if ($lesson_id > 0) {
|
||
$stmt = $pdo->prepare("SELECT video_url FROM lessons WHERE id = ?");
|
||
$stmt->execute([$lesson_id]);
|
||
$lesson = $stmt->fetch();
|
||
|
||
if ($lesson && $lesson['video_url']) {
|
||
$video_path = ltrim($lesson['video_url'], '/');
|
||
$input_file = __DIR__ . '/' . $video_path;
|
||
|
||
if (file_exists($input_file)) {
|
||
// Crea nome file di output
|
||
$path_info = pathinfo($input_file);
|
||
$output_file = $path_info['dirname'] . '/' . $path_info['filename'] . '_converted.mp4';
|
||
$backup_file = $input_file . '.backup';
|
||
|
||
// Backup del file originale
|
||
copy($input_file, $backup_file);
|
||
|
||
$result = convert_video($input_file, $output_file);
|
||
|
||
if ($result['success'] && file_exists($output_file)) {
|
||
// Sostituisci il file originale con quello convertito
|
||
unlink($input_file);
|
||
rename($output_file, $input_file);
|
||
|
||
// Aggiorna il database se l'estensione è cambiata
|
||
$new_extension = 'mp4';
|
||
$old_extension = strtolower($path_info['extension']);
|
||
|
||
if ($old_extension !== $new_extension) {
|
||
$new_video_url = str_replace('.' . $old_extension, '.' . $new_extension, $lesson['video_url']);
|
||
$new_file_path = str_replace('.' . $old_extension, '.' . $new_extension, $input_file);
|
||
rename($input_file, $new_file_path);
|
||
|
||
$stmt = $pdo->prepare("UPDATE lessons SET video_url = ? WHERE id = ?");
|
||
$stmt->execute([$new_video_url, $lesson_id]);
|
||
}
|
||
|
||
$message = "✅ Video convertito con successo! Il file originale è stato salvato come backup.";
|
||
|
||
// Ricarica la lista
|
||
$stmt = $pdo->query("
|
||
SELECT id, title, video_url, video_platform
|
||
FROM lessons
|
||
WHERE type = 'video'
|
||
AND video_platform = 'local'
|
||
AND video_url IS NOT NULL
|
||
AND deleted_at IS NULL
|
||
ORDER BY created_at DESC
|
||
");
|
||
$videos = $stmt->fetchAll();
|
||
} else {
|
||
$error = "❌ Errore durante la conversione: " . htmlspecialchars($result['output']);
|
||
// Ripristina il backup
|
||
if (file_exists($backup_file)) {
|
||
copy($backup_file, $input_file);
|
||
unlink($backup_file);
|
||
}
|
||
}
|
||
} else {
|
||
$error = "❌ File video non trovato: " . htmlspecialchars($video_path);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
?>
|
||
<!DOCTYPE html>
|
||
<html lang="it">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
<title>Converti Video - Pilates Platform</title>
|
||
<link rel="stylesheet" href="assets/css/style.css">
|
||
</head>
|
||
<body>
|
||
<header class="header">
|
||
<div class="container">
|
||
<div class="header-content">
|
||
<h1 class="logo">Pilates Studio - Conversione Video</h1>
|
||
<nav class="nav">
|
||
<a href="admin/dashboard.php" class="btn btn-outline">Torna alla Dashboard</a>
|
||
</nav>
|
||
</div>
|
||
</div>
|
||
</header>
|
||
|
||
<div class="container" style="padding: 2rem 0;">
|
||
<div class="card">
|
||
<h2>🎬 Conversione Video in Formato Web-Compatible</h2>
|
||
|
||
<?php if ($message): ?>
|
||
<div class="alert alert-success"><?php echo $message; ?></div>
|
||
<?php endif; ?>
|
||
|
||
<?php if ($error): ?>
|
||
<div class="alert alert-error"><?php echo $error; ?></div>
|
||
<?php endif; ?>
|
||
|
||
<div class="alert alert-info">
|
||
<h3>ℹ️ Informazioni</h3>
|
||
<p>Questo strumento converte i video in formato MP4 con codec H.264 e AAC per garantire la massima compatibilità con tutti i browser.</p>
|
||
<p><strong>Parametri di conversione:</strong></p>
|
||
<ul>
|
||
<li>Codec Video: H.264 (libx264)</li>
|
||
<li>Codec Audio: AAC</li>
|
||
<li>Qualità: CRF 23 (bilanciamento qualità/dimensione)</li>
|
||
<li>Ottimizzazione: Fast start per streaming web</li>
|
||
</ul>
|
||
</div>
|
||
|
||
<?php if (!$ffmpeg_available): ?>
|
||
<div class="alert alert-error">
|
||
<h3>⚠️ FFmpeg Non Disponibile</h3>
|
||
<p>FFmpeg non è installato sul server. Per utilizzare questa funzionalità, installa FFmpeg:</p>
|
||
<ul>
|
||
<li><strong>Ubuntu/Debian:</strong> <code>sudo apt-get install ffmpeg</code></li>
|
||
<li><strong>CentOS/RHEL:</strong> <code>sudo yum install ffmpeg</code></li>
|
||
<li><strong>Windows:</strong> Scarica da <a href="https://ffmpeg.org/download.html" target="_blank">ffmpeg.org</a></li>
|
||
</ul>
|
||
</div>
|
||
<?php else: ?>
|
||
<div class="alert alert-success">
|
||
✅ FFmpeg è installato e disponibile
|
||
</div>
|
||
<?php endif; ?>
|
||
|
||
<h3>Video Disponibili per Conversione</h3>
|
||
|
||
<?php if (empty($videos)): ?>
|
||
<p>Nessun video locale trovato.</p>
|
||
<?php else: ?>
|
||
<table class="table">
|
||
<thead>
|
||
<tr>
|
||
<th>ID</th>
|
||
<th>Titolo Lezione</th>
|
||
<th>Percorso Video</th>
|
||
<th>Formato</th>
|
||
<th>Dimensione</th>
|
||
<th>Azione</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<?php foreach ($videos as $video): ?>
|
||
<?php
|
||
$video_path = ltrim($video['video_url'], '/');
|
||
$full_path = __DIR__ . '/' . $video_path;
|
||
$exists = file_exists($full_path);
|
||
$extension = strtolower(pathinfo($video['video_url'], PATHINFO_EXTENSION));
|
||
$file_size = $exists ? filesize($full_path) : 0;
|
||
$file_size_mb = round($file_size / (1024 * 1024), 2);
|
||
?>
|
||
<tr>
|
||
<td><?php echo $video['id']; ?></td>
|
||
<td><?php echo htmlspecialchars($video['title']); ?></td>
|
||
<td style="font-size: 0.85em; color: #666;">
|
||
<?php echo htmlspecialchars($video['video_url']); ?>
|
||
<?php if (!$exists): ?>
|
||
<br><span style="color: red;">⚠️ File non trovato</span>
|
||
<?php endif; ?>
|
||
</td>
|
||
<td>
|
||
<span class="badge" style="background: <?php echo $extension === 'mp4' ? '#2ecc71' : '#e74c3c'; ?>;">
|
||
<?php echo strtoupper($extension); ?>
|
||
</span>
|
||
</td>
|
||
<td><?php echo $file_size_mb; ?> MB</td>
|
||
<td>
|
||
<?php if ($exists && $ffmpeg_available): ?>
|
||
<form method="POST" style="display: inline;"
|
||
onsubmit="return confirm('Vuoi convertire questo video? Il processo potrebbe richiedere alcuni minuti.');">
|
||
<input type="hidden" name="lesson_id" value="<?php echo $video['id']; ?>">
|
||
<button type="submit" name="convert_video" class="btn btn-primary btn-sm">
|
||
🔄 Converti
|
||
</button>
|
||
</form>
|
||
<?php else: ?>
|
||
<span style="color: #999;">Non disponibile</span>
|
||
<?php endif; ?>
|
||
</td>
|
||
</tr>
|
||
<?php endforeach; ?>
|
||
</tbody>
|
||
</table>
|
||
<?php endif; ?>
|
||
|
||
<div style="margin-top: 2rem; padding: 1rem; background: #f5f5f5; border-radius: 8px;">
|
||
<h4>💡 Suggerimenti</h4>
|
||
<ul>
|
||
<li>La conversione può richiedere diversi minuti per video di grandi dimensioni</li>
|
||
<li>Il file originale viene salvato come backup (.backup)</li>
|
||
<li>I video MP4 esistenti possono comunque essere riconvertiti per ottimizzazione</li>
|
||
<li>Per migliori risultati, carica video già in formato MP4 H.264</li>
|
||
</ul>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<footer class="footer">
|
||
<div class="container">
|
||
<p>© <?php echo date('Y'); ?> Pilates Platform. Tutti i diritti riservati.</p>
|
||
</div>
|
||
</footer>
|
||
</body>
|
||
</html>
|