<?php
set_time_limit(0);
include("connectSQL_Component.php");
require '../../vendor/autoload.php';

$ffmpeg = FFMpeg\FFMpeg::create([
    'ffmpeg.binaries' => 'C:\FFmpeg\bin\ffmpeg.exe',
    'ffprobe.binaries' => 'C:\FFmpeg\bin\ffprobe.exe',
]);

$GLOBALS["ffmpeg"] = $ffmpeg;
$GLOBALS["conn"] = $conn;
$root = '../../assets';
$folder = 'videos';
FindPath($root, $folder, $ffmpeg);
sqlsrv_close($conn);

function FindPath($root, $folder, $ffmpeg)
{

    $rootPath = $root . '/' . $folder;
    $paths = array_diff(scandir($root . '/' . $folder), array('.', '..', 'Thumbs.db'));
    natsort($paths);
    echo ("<ul>");
    foreach ($paths as $path) {

        if (str_contains($path, '.')) {
            if (str_contains(strtolower($path), '.mp4')) {
                echo ("<li data-jstree='{ " . '"type" : "file"' . " }' >");
                $title = explode(".", $path)[0];
                $file_path = substr($rootPath, 4) . "/" . $path;
                $path = "title:" . $title . "path:" . $file_path . "  dir_id:" . $folder;
                echo ($path);
                echo ("</li>");
            }
        } else {
            echo ("<li>");
            echo ("dir_name: " . $path . "parent:  " . $folder);

            FindPath($rootPath, $path, $ffmpeg);
            echo ("</li>");
        }
    }
    echo ("</ul>");
}
