pub / yt2rss

Transforms a youtube channel into a podcast RSS feed to insert into a podcatcher
git clone https://src.jayvii.de/pub/yt2rss.git
Home | Log | Files | Exports | Refs | Submodules | README | LICENSE | RSS

commit 37a07d9654795939ece5cda0eb01b0a4d82ccad3
parent b1fcd85d89996c66832f79522fb11cc67b6db25f
Author: JayVii <jayvii[AT]posteo[DOT]de>
Date:   Sat, 20 Apr 2024 14:23:04 +0200

More robust file checking

Diffstat:
Mindex.php | 35++++++++++++++++++++++++-----------
1 file changed, 24 insertions(+), 11 deletions(-)

diff --git a/index.php b/index.php @@ -4,6 +4,13 @@ * SPDX-License-Identifier: AGPL-3.0-or-later */ +function analyze_video($video_file) { + include_once("3rdparty/getid3/getid3/getid3.php"); + $getID3 = new getID3; + $video_info = $getID3->analyze($video_file); + return $video_info +} + if (!is_null($_GET["channel"])) { // Fetch Youtube XML Feed @@ -82,24 +89,24 @@ if (!is_null($_GET["channel"])) { if (!$included) continue; } $video_id = str_replace(array("yt_video:"), "", $entry["id"]); - // Get Video Length + // Get Video Length, size and type if (file_exists($video_id . ".opus")) { - $video_size = filesize($video_id . ".opus"); - include_once("3rdparty/getid3/getid3/getid3.php"); - $getID3 = new getID3; - $video_duration = $getID3->analyze( - $video_id . ".opus" - )["playtime_string"]; + $video_info = analyze_video($video_id . ".opus"); + $video_size = $video_info["filesize"]; + $video_duration = $video_info["playtime_string"]; + $video_ftype = $video_info["mime_type"]; } else { - $video_duration = "00:00:00"; $video_size = 0; + $video_duration = "00:00:00"; + $video_ftype = ""; } $rss_xml = $rss_xml . "<item>\n"; $rss_xml = $rss_xml . "<title>" . str_replace(array("&"), "&amp;", $entry["title"]) . "</title>\n"; // FIXME: fetch true description! $rss_xml = $rss_xml . "<description>" . - $entry["link"]["@attributes"]["href"] . PHP_EOL . + "Video-Link:" . PHP_EOL . + $entry["link"]["@attributes"]["href"] . PHP_EOL . PHP_EOL . str_replace( array("&"), "&amp;", @@ -118,7 +125,7 @@ if (!is_null($_GET["channel"])) { $video_id . "/hqdefault.jpg\"/>\n"; $rss_xml = $rss_xml . "<enclosure url=\"https://" . $_SERVER["SERVER_NAME"] . "/?video=" . $video_id . "\"". - " type=\"audio/opus\" length=\"" . $video_size . "\"/>\n"; + " type=\"" . $video_ftype . "\" length=\"" . $video_size . "\"/>\n"; $rss_xml = $rss_xml . "<itunes:duration>" . $video_duration . "</itunes:duration>\n"; $rss_xml = $rss_xml . "</item>\n"; @@ -142,8 +149,14 @@ if (!is_null($_GET["channel"])) { "-o '%(id)s.%(ext)s' " . "https://www.youtube.com/watch?v=" . basename($_GET["video"]) ); +) + } + // If file has been downloaded properly, check whether the file is valid + if (!(analyze_video(basename($_GET["video"]) . ".opus")["playtime_seconds"] > 0)) { + // Remove if unvalid + unlink(basename($_GET["video"]) . ".opus"); } - // If file has been downloaded properly, return to use + // If file still exists, return to user if (file_exists(basename($_GET["video"]) . ".opus")) { header("content-type: audio/ogg; codec=opus"); header(