yt-dlp via PHP

jemand anders

Erfahrenes Mitglied
Hallo,

ich versuche auf einem Linux-System mit PHP Videofiles mittels yt-dlp herunterzulagen, d. h. yt-dlp muss installiert sein.
Die möglichen Formate zeigt er auch an in einem JSON-Objekt, das ich in der Anwendung auswerten kann.
Das Herunterladen selbst klappt allerdings nicht, und sehe nicht, wo das Problem ist.
Vielleicht hat jemand eine Idee. Vielen Dank.

Freundl. Grüße

HTML:
Im Browser im URL-Feld eingeben (Localhost):

Formate: http://localhost/yt-dlp.php?url=https://www.youtube.com/watch?v=Sp1S267bM8Q
Download: http://localhost/yt-dlp.php?url=https://www.youtube.com/watch?v=Sp1S267bM8Q&format_id=230

PHP:
<?php

error_reporting(E_ALL);
ini_set('display_errors', 'On');

$DOWNLOAD_DIRECTORY = " --paths '.' ";

$url = htmlspecialchars($_GET["url"]);

$_format_id = "";
if(isset($_GET["format_id"])) {
    $_format_id = htmlspecialchars($_GET["format_id"]);
}

$format_id = ($_format_id!=="") ? ' -f ' . $_format_id . $DOWNLOAD_DIRECTORY : ' --dump-json ';

$shell_exec = 'yt-dlp --write-description --no-post-overwrite --rm-cache-dir --write-thumbnail --no-warnings --trim-filenames 200 ' .  $format_id . $url;

$output = shell_exec($shell_exec);

echo ($output);

?>

Gibt man $shell_exec in der Konsole ein, ist die Ausgabe wesentlich umfangreicher als das, was man über php erhält, wenn man es dort via echo ausgibt.

Code:
<!-- SOLL (via Konsole)

yt-dlp --write-description --no-post-overwrite --rm-cache-dir --write-thumbnail --no-warnings --trim-filenames 200 -f 230 --paths '.' https://www.youtube.com/watch?v=Sp1S267bM8Q
Removing cache dir /home/peter/.cache/yt-dlp ...

[youtube] Extracting URL: https://www.youtube.com/watch?v=Sp1S267bM8Q
[youtube] Sp1S267bM8Q: Downloading webpage
[youtube] Sp1S267bM8Q: Downloading tv player API JSON
[youtube] Sp1S267bM8Q: Downloading ios player API JSON
[youtube] Sp1S267bM8Q: Downloading player 20830619
[youtube] Sp1S267bM8Q: Downloading m3u8 information
[info] Sp1S267bM8Q: Downloading 1 format(s): 230
[info] Writing video description to: ./Unglaubliche Bauwerke auf Bergen: Wie Wurde Das Gebaut? [Sp1S267bM8Q].description

[info] Downloading video thumbnail 41 ...
[info] Writing video thumbnail 41 to: ./Unglaubliche Bauwerke auf Bergen: Wie Wurde Das Gebaut? [Sp1S267bM8Q].webp
[hlsnative] Downloading m3u8 manifest
[hlsnative] Total fragments: 273
[download] Destination: ./Unglaubliche Bauwerke auf Bergen: Wie Wurde Das Gebaut? [Sp1S267bM8Q].mp4
[download] 100% of   45.79MiB in 00:00:25 at 1.80MiB/s
[FixupM3u8] Fixing MPEG-TS in MP4 container of "./Unglaubliche Bauwerke auf Bergen: Wie Wurde Das Gebaut? [Sp1S267bM8Q].mp4"

-->

<!-- IST (via PHP)

Removing cache dir /var/lib/wwwrun/.cache/yt-dlp ...
[youtube] Extracting URL: https://www.youtube.com/watch?v=Sp1S267bM8Q
[youtube] Sp1S267bM8Q: Downloading webpage [youtube] Sp1S267bM8Q: Downloading tv player API JSON
[youtube] Sp1S267bM8Q: Downloading ios player API JSON
[youtube] Sp1S267bM8Q: Downloading player 20830619
[youtube] Sp1S267bM8Q: Downloading m3u8 information
[info] Sp1S267bM8Q: Downloading 1 format(s): 230
[info] Writing video description to: ./Unglaubliche Bauwerke auf Bergen: Wie Wurde Das Gebaut? [Sp1S267bM8Q].description

-->
 
Zuletzt bearbeitet:
Möglicherweise gibt es Berechtigungsproblem, denn mit dem PHP schreibt yt-dlp keine Files, wenn ich den Code jedoch kopiere und in der Konsole ausführe, werden die Files angelegt.
 
Ich habe zwischenzeitlich mal das Verzeichnis auf 777 gestellt, und dann klappte es. Und so sieht das Ergebnis aus:

Code:
Removing cache dir /var/lib/wwwrun/.cache/yt-dlp ... 
[youtube] Extracting URL: https://www.youtube.com/watch?v=Sp1S267bM8Q 
[youtube] Sp1S267bM8Q: Downloading webpage 
[youtube] Sp1S267bM8Q: Downloading tv player API JSON 
[youtube] Sp1S267bM8Q: Downloading ios player API JSON 
[youtube] Sp1S267bM8Q: Downloading player 20830619 
[youtube] Sp1S267bM8Q: Downloading m3u8 information 
[info] Sp1S267bM8Q: Downloading 1 format(s): 233 
[info] Writing video description to: ./Unglaubliche Bauwerke auf Bergen: Wie Wurde Das Gebaut? [Sp1S267bM8Q].description 
[info] Downloading video thumbnail 41 ... 
[info] Writing video thumbnail 41 to: ./Unglaubliche Bauwerke auf Bergen: Wie Wurde Das Gebaut? [Sp1S267bM8Q].webp [hlsnative] Downloading m3u8 manifest [hlsnative] Total fragments: 273 
[download] Destination: ./Unglaubliche Bauwerke auf Bergen: Wie Wurde Das Gebaut? [Sp1S267bM8Q].mp4 
[download] 100.0% of ~ 1.00KiB at 4.15KiB/s ETA Unknown (frag 0/273) 
[download] 1.1% of ~ 273.00KiB at 4.15KiB/s ETA Unknown (frag 0/273) 
[download] 0.9% of ~ 819.00KiB at 4.15KiB/s ETA Unknown (frag 0/273) 
[download] 0.8% of ~ 1.87MiB at 4.15KiB/s ETA Unknown (frag 0/273) 
[download] 0.7% of ~ 4.00MiB at 4.15KiB/s ETA Unknown (frag 0/273) 
... 
weitere ~1700 Zeilen
...
[download] 99.8% of ~ 7.85MiB at 424.16KiB/s ETA 00:00 (frag 271/273) 
[download] 99.1% of ~ 7.90MiB at 424.16KiB/s ETA 00:00 (frag 272/273) 
[download] 100.0% of ~ 7.83MiB at 426.67KiB/s ETA 00:00 (frag 272/273) 
[download] 100.0% of ~ 7.83MiB at 426.67KiB/s ETA 00:00 (frag 272/273) 
[download] 100.0% of ~ 7.83MiB at 426.67KiB/s ETA 00:00 (frag 272/273) 
[download] 100.0% of ~ 7.84MiB at 426.67KiB/s ETA 00:00 (frag 272/273) 
[download] 100.0% of ~ 7.85MiB at 426.67KiB/s ETA 00:00 (frag 272/273) 
[download] 99.7% of ~ 7.87MiB at 426.67KiB/s ETA 00:00 (frag 273/273) 
[download] 100% of 7.85MiB in 00:00:19 at 422.53KiB/s
Mir ist nicht wirklich klar, warum das nicht funkioniert hat, weil fopen und fwrite innerhalb PHP, also nicht über shell_exec bzw. exec, funktioniert.
Wie würde man denn PHP (in Verbindung mit yt-dlp?) eine Berechtigung für Verzeichnis erteilen? PHP und yt-dlp existieren beide nicht in der Benutzer- und Gruppenverwaltung.
 

Neue Beiträge

Zurück