=h($meta['title'] ?: ($info['title'] ?? basename($d)))?>
=nl2br(h($meta['description']), false)?>
=h(basename($d))?> ยท =h($info['file_count'] ?? count(list_files($d)))?> files
'','date'=>'','place'=>'','description'=>'','captions'=>[],'video_audio'=>[]]; $file = $dir . '/data.txt'; if (!is_file($file)) return $data; $lines = file($file, FILE_IGNORE_NEW_LINES); for ($i = 0; $i < count($lines); $i++) { $line = trim($lines[$i]); if ($line === '' || str_starts_with($line, '#') || !str_contains($line, ':')) continue; [$key, $value] = array_map('trim', explode(':', $line, 2)); $low = strtolower($key); if ($value === '|' && in_array($low, ['description','desc','synopsis'], true)) { $block = []; while ($i + 1 < count($lines)) { $next = $lines[$i + 1]; if (trim($next) !== '' && $next[0] !== ' ' && $next[0] !== "\t") break; $i++; if (str_starts_with($next, ' ')) $next = substr($next, 2); elseif (str_starts_with($next, ' ') || str_starts_with($next, "\t")) $next = substr($next, 1); $block[] = rtrim($next); } $data['description'] = trim(implode("\n", $block)); } elseif (in_array($low, ['title','name'], true)) $data['title'] = $value; elseif (in_array($low, ['date','dates','when'], true)) $data['date'] = $value; elseif (in_array($low, ['place','location','where'], true)) $data['place'] = $value; elseif (in_array($low, ['description','desc','synopsis'], true)) $data['description'] = $value; elseif (str_ends_with($low, ' audio')) { $fileKey = trim(substr($key, 0, -6)); if ($fileKey !== '' && in_array(strtolower($value), ['1','yes','true','on'], true)) $data['video_audio'][$fileKey] = true; } elseif ($key !== '') $data['captions'][$key] = $value; } return $data; } function write_data($dir, $post){ $title = trim($post['title'] ?? ''); $place = trim($post['place'] ?? ''); $date = trim($post['date'] ?? ''); $description = trim($post['description'] ?? ''); if (function_exists('mb_substr')) $description = mb_substr($description, 0, 5000, 'UTF-8'); else $description = substr($description, 0, 5000); $lines = []; if ($title !== '') $lines[] = 'Title: ' . $title; if ($place !== '') $lines[] = 'Location: ' . $place; if ($date !== '') $lines[] = 'Date: ' . $date; if ($description !== '') { $lines[] = 'Description: |'; $description = str_replace(["\r\n", "\r"], "\n", $description); foreach (explode("\n", $description) as $descLine) $lines[] = ' ' . rtrim($descLine); } $useAudioFiles = $post['use_audio_files'] ?? []; $useAudio = is_array($useAudioFiles) ? array_flip(array_map('basename', array_map('strval', $useAudioFiles))) : []; foreach (array_keys($useAudio) as $fileName) if ($fileName !== '') $lines[] = $fileName . ' audio: yes'; $captionFiles = $post['caption_files'] ?? []; $captions = $post['captions'] ?? []; if (is_array($captionFiles) && is_array($captions)) { foreach ($captionFiles as $i => $fileName) { $fileName = basename((string)$fileName); $caption = trim((string)($captions[$i] ?? '')); if ($fileName !== '' && $caption !== '') $lines[] = $fileName . ': ' . $caption; } } file_put_contents($dir . '/data.txt', implode("\n", $lines) . "\n"); } function editable_file($f){ $ext = strtolower(pathinfo((string)$f, PATHINFO_EXTENSION)); return $f !== '' && $f[0] !== '.' && $f !== 'data.txt' && in_array($ext, ['jpg','jpeg','png','webp','gif','mp4','mov','m4v','avi','mkv','webm','mp3','wav','m4a','aac','ogg','flac'], true); } function natural_file_compare($a, $b){ return strnatcasecmp((string)$a, (string)$b); } function is_visual_media_file($name){ $ext = strtolower(pathinfo((string)$name, PATHINFO_EXTENSION)); return in_array($ext, ['jpg','jpeg','png','webp','gif','mp4','mov','m4v','avi','mkv','webm'], true); } function parse_date_from_filename($path) { $filename = basename($path); if (!preg_match('/(?:^|[^0-9])(\\d{8})[_-]?(\\d{6})(?:[^0-9]|$)/', $filename, $m)) { return null; } $y = (int)substr($m[1], 0, 4); $mo = (int)substr($m[1], 4, 2); $d = (int)substr($m[1], 6, 2); $hh = (int)substr($m[2], 0, 2); $mi = (int)substr($m[2], 2, 2); $ss = (int)substr($m[2], 4, 2); if (!checkdate($mo, $d, $y) || $hh > 23 || $mi > 59 || $ss > 59) { return null; } return $m[1] . $m[2]; } function mvlog_local_timezone(){ static $tz = null; if ($tz instanceof DateTimeZone) return $tz; $name = trim((string)@file_get_contents('/etc/timezone')); if ($name === '') $name = (string)date_default_timezone_get(); if ($name === '') $name = 'UTC'; try { $tz = new DateTimeZone($name); } catch (Throwable $e) { $tz = new DateTimeZone('UTC'); } return $tz; } function parse_embedded_datetime_to_local($value){ $value = trim((string)$value); if ($value === '') return null; try { $dt = new DateTimeImmutable($value); return $dt->setTimezone(mvlog_local_timezone())->format('YmdHis'); } catch (Throwable $e) { return null; } } function media_file_date($path){ $ext = strtolower(pathinfo((string)$path, PATHINFO_EXTENSION)); // Priority 2: Exif/embedded metadata if (in_array($ext, ['jpg','jpeg','tif','tiff'], true) && function_exists('exif_read_data')) { $exif = @exif_read_data($path); foreach (['DateTimeOriginal','DateTimeDigitized','DateTime'] as $k) { $value = (string)($exif[$k] ?? ''); if ($value !== '' && preg_match('/^(\\d{4}):(\\d{2}):(\\d{2})(?:\\s+(\\d{2}):(\\d{2}):(\\d{2}))?/', $value, $m)) { $date = $m[1] . $m[2] . $m[3]; $time = isset($m[4]) ? ($m[4] . $m[5] . $m[6]) : '000000'; return $date . $time; } } } if (in_array($ext, ['mp4','mov','m4v','avi','mkv','webm'], true)) { $ffprobe = trim((string)shell_exec('command -v ffprobe 2>/dev/null')); if ($ffprobe !== '') { $json = shell_exec(escapeshellarg($ffprobe).' -v quiet -print_format json -show_entries format_tags=creation_time:stream_tags=creation_time '.escapeshellarg($path).' 2>/dev/null'); $data = json_decode((string)$json, true); $candidates = []; if (is_array($data['format']['tags'] ?? null)) $candidates[] = (string)($data['format']['tags']['creation_time'] ?? ''); foreach (($data['streams'] ?? []) as $stream) if (is_array($stream['tags'] ?? null)) $candidates[] = (string)($stream['tags']['creation_time'] ?? ''); foreach ($candidates as $value) { $normalized = parse_embedded_datetime_to_local($value); if ($normalized !== null) { return $normalized; } if (preg_match('/^(\\d{4})-(\\d{2})-(\\d{2})/', $value, $m)) { return $m[1].$m[2].$m[3].'000000'; } } } } // Priority 3: filename parsing $from_filename = parse_date_from_filename($path); if ($from_filename) { return $from_filename; } return null; } function media_sort_datetime($path){ return media_file_date($path) ?? date('YmdHis', filemtime($path)); } function list_files($dir){ $files = array_values(array_filter(scandir($dir), fn($f)=>editable_file($f) && is_file($dir.'/'.$f))); $orderFile = $dir . '/order.json'; $media = []; $other = []; foreach ($files as $name) { if (is_visual_media_file($name)) $media[] = $name; else $other[] = $name; } // Priority 1: explicit order file, but regenerate if file set changed. if (is_file($orderFile)) { $order = json_decode((string)file_get_contents($orderFile), true); if (is_array($order)) { $mediaSet = array_fill_keys($media, true); $orderSet = []; $sortedMedia = []; foreach ($order as $name) { if (!is_string($name) || !is_visual_media_file($name)) continue; if (isset($mediaSet[$name]) && !isset($orderSet[$name])) { $orderSet[$name] = true; $sortedMedia[] = $name; } } $sameSet = (count($orderSet) === count($mediaSet)); if ($sameSet) { foreach ($mediaSet as $name => $_) { if (!isset($orderSet[$name])) { $sameSet = false; break; } } } if ($sameSet) { return array_merge($sortedMedia, $other); } } } // Missing/invalid/stale order.json: sort by datetime and recreate order.json usort($media, function($a, $b) use ($dir) { $da = media_sort_datetime($dir . '/' . $a); $db = media_sort_datetime($dir . '/' . $b); if ($da === $db) return 0; // never fallback to filename ordering return strcmp($da, $db); }); file_put_contents($orderFile, json_encode(array_values($media), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)); @chmod($orderFile, 0664); return array_merge($media, $other); } function media_sort_files($dir, $files){ return list_files($dir); } function input_dir_date_from_media($dir){ $dates = []; foreach (list_files($dir) as $file) { $date = media_file_date($dir . '/' . $file); if ($date !== null) $dates[] = $date; } if ($dates) { sort($dates, SORT_STRING); return $dates[0]; } return date('Ymd'); } function input_dir_date_from_text($text){ $text = trim((string)$text); if ($text === '') return null; if (preg_match('/^(\d{4})[.-]?(\d{2})[.-]?(\d{2})$/', $text, $m) && checkdate((int)$m[2], (int)$m[3], (int)$m[1])) return "$m[1]$m[2]$m[3]"; if (preg_match('/^(\d{1,2})[\/. -](\d{1,2})[\/. -](\d{4})$/', $text, $m) && checkdate((int)$m[2], (int)$m[1], (int)$m[3])) return sprintf('%04d%02d%02d', $m[3], $m[2], $m[1]); $ts = strtotime($text); if ($ts !== false) return date('Ymd', $ts); if (preg_match('/^(jan|january|feb|february|mar|march|apr|april|may|jun|june|jul|july|aug|august|sep|sept|september|oct|october|nov|november|dec|december)\s+(\d{4})$/i', $text, $m)) { $months = ['jan'=>1,'january'=>1,'feb'=>2,'february'=>2,'mar'=>3,'march'=>3,'apr'=>4,'april'=>4,'may'=>5,'jun'=>6,'june'=>6,'jul'=>7,'july'=>7,'aug'=>8,'august'=>8,'sep'=>9,'sept'=>9,'september'=>9,'oct'=>10,'october'=>10,'nov'=>11,'november'=>11,'dec'=>12,'december'=>12]; return sprintf('%04d%02d01', (int)$m[2], $months[strtolower($m[1])]); } return null; } function unique_input_dir($base, $slug){ $dir = $base . '/' . $slug; if (!file_exists($dir)) return $dir; for ($i = 2; ; $i++) { $candidate = $base . '/' . $slug . '-' . $i; if (!file_exists($candidate)) return $candidate; } } function input_dir_signature($dir){ $script = <<<'PY' import hashlib, json, os, sys root = sys.argv[1] entries = [] for dirpath, dirnames, filenames in os.walk(root): dirnames[:] = [d for d in dirnames if d != '.movmaker-lock'] for name in filenames: if name in {'.movmaker-state.json', '.movmaker-enabled', '.movmaker-preview', '.mvlog-hidden'}: continue path = os.path.join(dirpath, name) rel = os.path.relpath(path, root) st = os.stat(path) mtime_ns = getattr(st, 'st_mtime_ns', int(st.st_mtime * 1_000_000_000)) entries.append([rel, st.st_size, mtime_ns]) entries.sort() print(hashlib.sha256(json.dumps(entries, ensure_ascii=False, separators=(',', ':')).encode('utf-8')).hexdigest()) PY; $cmd = 'python3 -c ' . escapeshellarg($script) . ' ' . escapeshellarg($dir); $output = trim(shell_exec($cmd) ?? ''); return $output !== '' ? $output : ''; } function input_dir_info($dir){ $data = read_data($dir); $files = list_files($dir); return [ 'name' => basename($dir), 'title' => $data['title'] ?: basename($dir), 'sort_date' => input_dir_date_from_text($data['date'] ?? '') ?? input_dir_date_from_media($dir), 'file_count' => count($files), 'signature' => input_dir_signature($dir), ]; } function load_input_dir_cache($dirs){ $cacheFile = __DIR__ . '/cache/input-dirs.json'; $cache = is_file($cacheFile) ? json_decode((string)file_get_contents($cacheFile), true) : []; if (!is_array($cache) || ($cache['version'] ?? 0) !== 1) $cache = ['version'=>1,'items'=>[]]; $oldItems = is_array($cache['items'] ?? null) ? $cache['items'] : []; $items = []; $changed = false; foreach ($dirs as $dir) { $name = basename($dir); $sig = input_dir_signature($dir); $cached = $oldItems[$name] ?? null; if (is_array($cached) && ($cached['signature'] ?? '') === $sig) { $items[$name] = $cached; } else { $items[$name] = input_dir_info($dir); $changed = true; } } if (array_diff(array_keys($oldItems), array_keys($items))) $changed = true; if ($changed) file_put_contents($cacheFile, json_encode(['version'=>1,'generated_at'=>date('c'),'items'=>$items], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT), LOCK_EX); return $items; } function sort_input_dirs_by_metadata_date(&$dirs, $dirInfo){ usort($dirs, function($a, $b) use ($dirInfo) { $ad = $dirInfo[basename($a)]['sort_date'] ?? '00000000'; $bd = $dirInfo[basename($b)]['sort_date'] ?? '00000000'; $cmp = strcmp($bd, $ad); return $cmp !== 0 ? $cmp : strnatcasecmp(basename($b), basename($a)); }); } function orphan_videos($config){ $referenced = []; foreach (input_dirs($config['uploads_dir']) as $dir) { $stateFile = $dir . '/.movmaker-state.json'; $state = is_file($stateFile) ? json_decode((string)file_get_contents($stateFile), true) : []; if (is_array($state) && !empty($state['output'])) $referenced[basename((string)$state['output'])] = true; } $videos = glob($config['videos_dir'].'/*.{mp4,webm,mov,m4v}', GLOB_BRACE) ?: []; return array_values(array_filter($videos, fn($v)=>empty($referenced[basename($v)]))); } function input_dir_status($dir){ $stateFile = $dir . '/.movmaker-state.json'; $lockDir = $dir . '/.movmaker-lock'; $state = is_file($stateFile) ? json_decode((string)file_get_contents($stateFile), true) : []; $status = is_array($state) ? (string)($state['status'] ?? '') : ''; if (is_dir($lockDir)) return $status !== '' ? $status : 'processing'; return in_array($status, ['processing', 'stale', 'error'], true) ? $status : ''; } function input_dir_running($dir){ $stateFile = $dir . '/.movmaker-state.json'; $lockDir = $dir . '/.movmaker-lock'; $state = is_file($stateFile) ? json_decode((string)file_get_contents($stateFile), true) : []; $status = is_array($state) ? (string)($state['status'] ?? '') : ''; return is_dir($lockDir) || $status === 'processing'; } function input_dir_enabled($dir){ return is_file($dir . '/.movmaker-enabled'); } function set_input_dir_enabled($dir, $enabled){ $path = $dir . '/.movmaker-enabled'; if ($enabled) { file_put_contents($path, "enabled\n"); chmod($path, 0664); } elseif (is_file($path)) unlink($path); } function input_dir_preview($dir){ if (is_file($dir . '/.movmaker-preview')) return true; $stateFile = $dir . '/.movmaker-state.json'; if (!is_file($stateFile)) return false; $state = json_decode((string)file_get_contents($stateFile), true); return is_array($state) && !empty($state['preview']); } function set_input_dir_preview($dir, $preview){ $path = $dir . '/.movmaker-preview'; if ($preview) { file_put_contents($path, "preview\n"); chmod($path, 0664); } elseif (is_file($path)) unlink($path); $stateFile = $dir . '/.movmaker-state.json'; $state = is_file($stateFile) ? json_decode((string)file_get_contents($stateFile), true) : []; if (!is_array($state)) $state = []; $state['preview'] = (bool)$preview; $state['preview_updated_at'] = gmdate('c'); file_put_contents($stateFile, json_encode($state, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . " "); chmod($stateFile, 0664); } function input_dir_visible($dir){ return !is_file($dir . '/.mvlog-hidden'); } function set_input_dir_visible($dir, $visible){ $path = $dir . '/.mvlog-hidden'; if ($visible) { if (is_file($path)) unlink($path); } else { file_put_contents($path, "hidden\n"); chmod($path, 0664); } } function input_dir_state($dir){ $stateFile = $dir . '/.movmaker-state.json'; $state = is_file($stateFile) ? json_decode((string)file_get_contents($stateFile), true) : []; return is_array($state) ? $state : []; } function active_worker_jobs($config){ $jobs = []; foreach (input_dirs($config['uploads_dir']) as $dir) { $name = basename($dir); $state = input_dir_state($dir); $status = (string)($state['status'] ?? ''); $locked = is_dir($dir . '/.movmaker-lock'); if ($locked || $status === 'processing') { $started = ''; if (is_file($dir . '/.movmaker-lock/started_at')) $started = trim((string)file_get_contents($dir . '/.movmaker-lock/started_at')); if ($started === '' && !empty($state)) $started = (string)($state['started_at'] ?? $state['updated_at'] ?? ''); $jobs[] = ['name'=>$name,'status'=>$status ?: ($locked ? 'processing' : 'unknown'),'started_at'=>$started]; } } return $jobs; } function format_job_age($startedAt){ $ts = strtotime((string)$startedAt); if (!$ts) return ''; $seconds = max(0, time() - $ts); $h = intdiv($seconds, 3600); $m = intdiv($seconds % 3600, 60); $s = $seconds % 60; if ($h > 0) return $h . 'h ' . $m . 'm'; if ($m > 0) return $m . 'm ' . $s . 's'; return $s . 's'; } function input_dir_output($dir){ $state = input_dir_state($dir); return basename((string)($state['output'] ?? '')); } function regenerate_input_dir_maps($dir, $config){ $python = trim((string)shell_exec('command -v python3 2>/dev/null')); $script = __DIR__ . '/bin/gpsmap.py'; if ($python === '' || !is_file($script)) return; $state = input_dir_state($dir); $targets = []; foreach (['output', 'preview_output'] as $key) { $name = basename((string)($state[$key] ?? '')); if ($name === '') continue; $targets[] = $config['videos_dir'] . '/' . pathinfo($name, PATHINFO_FILENAME) . '.png'; } // Fallback target if no output filename is known yet. if (!$targets) { $targets[] = $config['videos_dir'] . '/' . basename((string)$dir) . '.png'; } $targets = array_values(array_unique($targets)); foreach ($targets as $outPng) { $cmd = escapeshellarg($python) . ' ' . escapeshellarg($script) . ' ' . escapeshellarg($dir) . ' ' . escapeshellarg($outPng) . ' 2>&1'; $output = (string)shell_exec($cmd); if (is_file($outPng)) { @chmod($outPng, 0664); } else { error_log('[mvlog] map regenerate failed for '.basename((string)$dir).' target='.basename((string)$outPng).' output='.trim($output)."\n", 3, '/var/log/mvlog_map.log'); } } } function cached_video_metadata($output, $fallback){ $meta = ['title'=>$fallback['title'] ?? '', 'date'=>$fallback['date'] ?? '', 'location'=>$fallback['place'] ?? '', 'description'=>$fallback['description'] ?? '']; $cacheFile = __DIR__ . '/cache/videos.json'; $cache = is_file($cacheFile) ? json_decode((string)file_get_contents($cacheFile), true) : []; $cached = $cache['items'][$output]['metadata'] ?? null; if (is_array($cached)) { foreach (['title','date','location','description'] as $k) if (!empty($cached[$k])) $meta[$k] = $cached[$k]; } return $meta; } function paginate($items, $page, $perPage = 8){ $total = count($items); $pages = max(1, (int)ceil($total / $perPage)); $page = max(1, min((int)$page, $pages)); return [$page, $pages, array_slice($items, ($page - 1) * $perPage, $perPage), $total]; } function page_links($tab, $page, $pages){ if ($pages <= 1) return ''; $extra = ''; if ($tab === 'edit') { $q = trim((string)($_GET['q'] ?? '')); if ($q !== '') $extra .= '&q=' . rawurlencode($q); } $html = '
No orphan videos.
=page_links('videos',$videoPage,$videoPages)?>No input directories yet.
=nl2br(h($meta['description']), false)?>
=h(basename($d))?> ยท =h($info['file_count'] ?? count(list_files($d)))?> files
in-dir/=h(basename($editDir))?>
in-dir/=h(basename($editDir))?>