PHP: Wie kann ich die Videoladezeit verbessern?
Hallo,
ich habe folgendes Problem:
Ich habe diese PHP-Datei erstellt und bei mir auf meinem iPhone lädt es die Videodatei sehr langsam bis gar nicht. Wenn ich aber auf PC im gleichen Netflix schaue, lädt es sehr schnell.
Ich habe es bei anderen Freunden testen lassen, die auch ein iPhone haben. Bei der einen Person lädt es auch sehr schnell, bei der anderen auch nicht, so wie bei mir. Bei mir lädt es immer nur schnell, wenn ich mobile Daten anhabe.
Die Videodateien sind 2-7 GB groß.
Vielleicht kann jemand eine Lösung für mich finden, das zu beheben. Wichtig ist, dass ich die Größe der Videodateien nicht verkleinern kann. Das heißt, es muss trotzdem schnell die Videodatei laden.
Ich sage schon einmal danke an die Person, die sich die Zeit und Mühe nimmt, mir zu helfen.
Bei einer Verbindung mit dem normalen Heimnetz sieht es auf dem iPhone so aus:
Wenn ich über LTE (mobile Daten) lade:
Der PHP-Code:
<?php
require 'db.php'; // Die Datenbankverbindung einbinden
require 'is_premium.php'; // Die Datenbankverbindung einbinden
// Überprüfen, ob eine ID übergeben wurde
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
die('Ungültige Film-ID.');
}
$movie_id = intval($_GET['id']);
// Film aus der Datenbank abfragen
$stmt = $db->prepare('SELECT * FROM movies WHERE id = ?');
$stmt->bind_param('i', $movie_id);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows === 0) {
die('Film nicht gefunden.');
}
$movie = $result->fetch_assoc();
$stmt->close();
$db->close();
// Erkennen, ob die URL ein lokales Video ist oder über HTTPS geladen werden soll
$is_local_video = strpos($movie['video'], 'uploads/videos/') === 0;
$is_https = strpos($movie['video'], 'https://') === 0;
$thumbnail = htmlspecialchars($movie['thumbnail']); // Thumbnail aus der Datenbank
?>
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo htmlspecialchars($movie['name']); ?> - MovieVel</title>
<style>
#css code
</style>
</head>
<body>
<div class="container">
<div class="movie-detail">
<h1><?php echo htmlspecialchars($movie['name']); ?></h1>
<!-- Movie Video -->
<div class="<?php echo $is_local_video ? 'video-container' : 'iframe-container'; ?>">
<div class="play-btn">
▶ <!-- Play-Symbol -->
</div>
<?php if ($is_local_video): ?>
<!-- Video für progressive Web-Optimierung mit Bild-in-Bild-Unterstützung -->
<video id="video-player" controls autoplay preload="auto" poster="<?php echo $thumbnail; ?>"
onclick="this.requestPictureInPicture()" muted playsinline>
<source src="<?php echo htmlspecialchars($movie['video']); ?>" type="video/mp4">
Ihr Browser unterstützt dieses Videoformat nicht.
</video>
<?php elseif ($is_https): ?>
<iframe
src="<?php echo htmlspecialchars($movie['video']); ?>"
allowfullscreen>
</iframe>
<?php else: ?>
<p>Video konnte nicht geladen werden. Überprüfen Sie die URL oder die Serverkonfiguration.</p>
<?php endif; ?>
</div>
<!-- Movie Description -->
<div class="description-box">
<h2>Über den Film</h2>
<p><?php echo htmlspecialchars($movie['description']); ?></p>
</div>
<!-- Back Button -->
<a href="index.php" class="back-btn">Zurück zur Übersicht</a>
</div>
</div>
<script>
const video = document.getElementById('video-player');
// Überprüfen, ob Picture-in-Picture unterstützt wird
if ('pictureInPictureEnabled' in document) {
video.addEventListener('click', async () => {
try {
// Versuchen, Bild-in-Bild zu aktivieren
if (document.pictureInPictureElement !== video) {
await video.requestPictureInPicture();
} else {
// Wenn das Video schon im Bild-in-Bild-Modus ist, es wieder schließen
await document.exitPictureInPicture();
}
} catch (err) {
console.error('Fehler beim Wechseln in den Bild-in-Bild-Modus:', err);
}
});
}
</script>
</body>
</html>
Hi,
that has something to do with PHP indirectly. It depends on the charging technology and what format you use. As has already been written, video stream services such as Netflix and Co. use the technology to load blessings of the video instead of the entire video material at once. The advantage is that videos can be loaded quickly and you don’t have to wait 2 minutes, 3 minutes or longer until a video is loaded, but you often work ad-hoc.
This technique is also called Adaptive Bitrate Streaming (ABR) and Time intervals name Chunks.
Btw: This technique has been used intensively from the outset by adult providers and has contributed to the essential further development, as many adults particularly men often looked at and watch certain video material.
You won’t get much in PHP to download 2-7GB of material before you can watch it is just mentally ill xD
Rather, you should think about how to reduce or optimize the amount of data.
The greatest advantage is if you convert the video into a m3u8 with (WICHTIG) H.264 format.
At m3u8 not first the complete video is downloaded, but the video consists of very many small video segments. A video segment is for example 15 seconds. Then you only have to download the first segment, which is only 15 seconds long and therefore is very small. During these 15 seconds, the browser has time to download the next segments. This allows the video to load quickly and it runs like a normal mp4 video, so the user does not notice any difference.
If you spin at the player on minute 5 the corresponding video segment is queried, downloaded and played, then automatically download the next video segments.
This gives you fast and liquid videos. You can change the whole thing with “ffmpeg”.
Alternatively compress the mp4 video (e.g. with handbrake), then it is much smaller with the same quality, then you can use webm instead of mp4 (but not supported by all devices & browsers).
That’s a professional good answer, thank you!