diff --git a/app/Http/Controllers/VideoController.php b/app/Http/Controllers/VideoController.php index 378a829..7642420 100644 --- a/app/Http/Controllers/VideoController.php +++ b/app/Http/Controllers/VideoController.php @@ -479,8 +479,29 @@ class VideoController extends Controller } if ($nasUploadSucceeded) { - // For non-audio: HLS generation still runs (downloads from NAS, keeps HLS local) + // For non-audio: HLS generation still runs (downloads from NAS, keeps HLS local). + // GenerateHlsJob no-ops unless status === 'ready', and on the NAS branch the + // upload IS the "done" state (there's no local CompressVideoJob to flip it). if (! $isAudioUpload) { + $duration = 0; + try { + $ffprobeBin = config('ffmpeg.ffprobe.binaries', '/usr/bin/ffprobe'); + $localCopy = app(\App\Services\NasSyncService::class)->ensureLocalCopy($video->fresh()); + if ($localCopy && file_exists($localCopy)) { + $out = []; + exec("{$ffprobeBin} -v error -show_entries format=duration -of csv=p=0 " . escapeshellarg($localCopy), $out); + $duration = (int) round((float) ($out[0] ?? 0)); + } + } catch (\Throwable $e) { + \Log::warning('store: ffprobe duration failed: ' . $e->getMessage()); + } + + $video->update([ + 'status' => 'ready', + 'duration' => $duration ?: $video->duration, + 'is_shorts' => ($duration ?: $video->duration) <= 60 && $orientation === 'portrait', + ]); + \App\Jobs\GenerateHlsJob::dispatch($video->fresh()) ->onQueue('video-processing') ->onConnection('database'); @@ -831,7 +852,7 @@ class VideoController extends Controller 'key' => $video->getRouteKey(), 'type' => $video->type, 'has_hls' => (bool) $video->has_hls, - 'hls_url' => $video->has_hls ? route('videos.hls', ['video' => $video, 'file' => 'master.m3u8']) : null, + 'hls_url' => $video->has_hls ? route('videos.hls', ['video' => $video, 'file' => 'playlist.m3u8']) : null, 'stream_url' => route('videos.stream', $video) . '?v=' . $video->updated_at->timestamp, // When a specific language track is requested (playlist row pin, // shared link with ?track=), expose it separately. The player uses diff --git a/app/Jobs/CompressVideoJob.php b/app/Jobs/CompressVideoJob.php index f7c7d5a..da61dd0 100644 --- a/app/Jobs/CompressVideoJob.php +++ b/app/Jobs/CompressVideoJob.php @@ -4,8 +4,6 @@ namespace App\Jobs; use App\Models\Setting; use App\Models\Video; -use FFMpeg\FFMpeg; -use FFMpeg\Format\Video\X264; use Illuminate\Support\Facades\Config; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; @@ -13,7 +11,6 @@ use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Facades\Log; -use Illuminate\Support\Facades\Storage; class CompressVideoJob implements ShouldQueue { @@ -30,7 +27,6 @@ class CompressVideoJob implements ShouldQueue { $video = $this->video; - // Get original file path $originalPath = storage_path('app/' . $video->path); if (!file_exists($originalPath)) { @@ -38,98 +34,114 @@ class CompressVideoJob implements ShouldQueue return; } - // Create compressed file alongside the original $compressedFilename = 'compressed_' . $video->filename; $compressedPath = dirname($originalPath) . '/' . $compressedFilename; try { $ffmpegConfig = Config::get('ffmpeg'); - $ffmpeg = FFMpeg::create([ - 'ffmpeg.binaries' => $ffmpegConfig['ffmpeg'] ?? '/usr/bin/ffmpeg', - 'ffprobe.binaries' => $ffmpegConfig['ffprobe'] ?? '/usr/bin/ffprobe', - 'timeout' => $ffmpegConfig['timeout'] ?? 3600, - ]); - $ffmpegVideo = $ffmpeg->open($originalPath); + $ffmpegBin = $ffmpegConfig['ffmpeg'] ?? '/usr/bin/ffmpeg'; + $ffprobeBin = $ffmpegConfig['ffprobe'] ?? '/usr/bin/ffprobe'; + + // Detect the source video codec. Browsers on Chrome/Firefox/Android can't + // decode HEVC (H.265/hev1/hvc1) — those sources must be replaced with H.264 + // even if the transcode is larger, so downloads and MP4 fallback stay playable. + $srcCodec = ''; + try { + $probeOut = []; + exec(escapeshellcmd($ffprobeBin) . ' -v error -select_streams v:0 -show_entries stream=codec_name -of csv=p=0 ' . escapeshellarg($originalPath), $probeOut); + $srcCodec = strtolower(trim($probeOut[0] ?? '')); + } catch (\Throwable $e) {} + $forceReplace = in_array($srcCodec, ['hevc', 'h265', 'hev1', 'hvc1'], true); - // Verify the GPU is actually reachable and able to encode before sending - // the file to it; otherwise fall back to CPU so the job never hangs. $gpuEnabled = Setting::gpuUsable(); - $encoder = Setting::gpuEncoder(); - $preset = Setting::gpuPreset(); + $encoder = Setting::gpuEncoder(); // h264_nvenc / libx264 + $preset = Setting::gpuPreset(); // p1–p7 for NVENC, fast/medium/slow for x264 $device = Setting::gpuDevice(); + $hwaccel = Setting::gpuHwaccel(); - if ($gpuEnabled) { - $videoPasses = [ - "-c:v {$encoder}", - "-preset {$preset}", - '-rc vbr', - '-cq 23', - '-profile:v high', - '-pix_fmt yuv420p', - "-gpu {$device}", - ]; + // Build ffmpeg command directly — php-ffmpeg's X264 format validator + // only accepts 'libx264' and throws on 'h264_nvenc'. + $cmd = [escapeshellcmd($ffmpegBin), '-y']; + + if ($gpuEnabled && $hwaccel !== 'none') { + $cmd[] = "-hwaccel {$hwaccel}"; + $cmd[] = "-hwaccel_device {$device}"; + } + + $cmd[] = '-i ' . escapeshellarg($originalPath); + $cmd[] = "-c:v {$encoder}"; + + if ($gpuEnabled && str_contains($encoder, 'nvenc')) { + $cmd[] = "-preset {$preset}"; + $cmd[] = '-rc vbr'; + $cmd[] = '-cq 23'; + $cmd[] = "-gpu {$device}"; } else { - $videoPasses = [ - '-c:v libx264', - '-preset fast', - '-crf 23', - '-profile:v high', - '-pix_fmt yuv420p', - ]; + $cmd[] = "-preset {$preset}"; + $cmd[] = '-crf 23'; } - $audioPasses = ['-c:a aac', '-b:a 192k']; - $format = new X264('aac', $encoder); - foreach ($videoPasses as $pass) { - $format->addLegacyOption($pass); - } - foreach ($audioPasses as $pass) { - $format->addLegacyOption($pass); - } - $ffmpegVideo->save($format, $compressedPath); + $cmd[] = '-profile:v high'; + $cmd[] = '-pix_fmt yuv420p'; + $cmd[] = '-c:a aac'; + $cmd[] = '-b:a 192k'; + $cmd[] = '-movflags +faststart'; + $cmd[] = escapeshellarg($compressedPath); + + $fullCmd = implode(' ', $cmd) . ' 2>&1'; + + Log::info('CompressVideoJob: Starting', [ + 'video_id' => $video->id, + 'src_codec' => $srcCodec, + 'encoder' => $encoder, + 'gpu' => $gpuEnabled, + ]); + + $output = []; + exec($fullCmd, $output, $exitCode); + + if ($exitCode !== 0) { + $tail = implode("\n", array_slice($output, -30)); + throw new \RuntimeException("FFmpeg exited {$exitCode}:\n{$tail}"); + } - // Check if compressed file was created and is smaller if (file_exists($compressedPath)) { - $originalSize = filesize($originalPath); + $originalSize = filesize($originalPath); $compressedSize = filesize($compressedPath); - // Only use compressed file if it's smaller - if ($compressedSize < $originalSize) { - // Delete original and rename compressed + if ($compressedSize < $originalSize || $forceReplace) { unlink($originalPath); rename($compressedPath, $originalPath); - // Update video record $video->update([ - 'size' => $compressedSize, - 'filename' => $video->filename, // Keep same filename + 'size' => $compressedSize, 'mime_type' => 'video/mp4', ]); - Log::info('CompressVideoJob: Video compressed', [ - 'video_id' => $video->id, - 'original_size' => $originalSize, + Log::info('CompressVideoJob: Video compressed', [ + 'video_id' => $video->id, + 'original_size' => $originalSize, 'compressed_size' => $compressedSize, - 'saved' => round(($originalSize - $compressedSize) / $originalSize * 100) . '%', - 'encoder' => $encoder, - 'gpu' => $gpuEnabled, + 'saved' => round(($originalSize - $compressedSize) / max(1, $originalSize) * 100) . '%', + 'encoder' => $encoder, + 'gpu' => $gpuEnabled, + 'src_codec' => $srcCodec, + 'forced' => $forceReplace && $compressedSize >= $originalSize, ]); } else { - // Compressed file is larger, delete it unlink($compressedPath); - Log::info('CompressVideoJob: Compression made file larger, keeping original'); + Log::info('CompressVideoJob: Compression made file larger, keeping original', ['src_codec' => $srcCodec]); } } $video->update(['status' => 'ready']); - // Chain to HLS generation for GPU-accelerated adaptive playback \App\Jobs\GenerateHlsJob::dispatch($video); } catch (\Exception $e) { Log::error('CompressVideoJob failed: ' . $e->getMessage()); - $video->update(['status' => 'ready']); // Mark as ready anyway + if (file_exists($compressedPath)) @unlink($compressedPath); + $video->update(['status' => 'ready']); } } } - diff --git a/resources/views/components/video-player.blade.php b/resources/views/components/video-player.blade.php index fcf1b59..f87e8e2 100644 --- a/resources/views/components/video-player.blade.php +++ b/resources/views/components/video-player.blade.php @@ -9,7 +9,7 @@ @php // Force 16:9 for every video — orientation classes intentionally disabled $orientationClass = ''; - $hlsUrl = $video->has_hls ? route('videos.hls', ['video' => $video, 'file' => 'master.m3u8']) : null; + $hlsUrl = $video->has_hls ? route('videos.hls', ['video' => $video, 'file' => 'playlist.m3u8']) : null; $mp4Url = route('videos.stream', $video) . '?v=' . $video->updated_at->timestamp; $nextUrl = $nextVideo && $playlist ? route('videos.show', $nextVideo) .'?playlist='.$playlist->share_token : null; $prevUrl = $previousVideo && $playlist ? route('videos.show', $previousVideo).'?playlist='.$playlist->share_token : null; diff --git a/resources/views/layouts/partials/upload-modal.blade.php b/resources/views/layouts/partials/upload-modal.blade.php index 6f9fab8..cfcf83b 100644 --- a/resources/views/layouts/partials/upload-modal.blade.php +++ b/resources/views/layouts/partials/upload-modal.blade.php @@ -711,6 +711,55 @@ } .utc-card:hover .utc-card-arr { opacity: 1; transform: translateX(0); } +/* ── Mobile: render the chooser as a bottom sheet ───────────── */ +@media (max-width: 768px) { + .utc-overlay { + align-items: flex-end; justify-content: stretch; + padding: 0; background: rgba(0,0,0,.55); + backdrop-filter: blur(4px); + } + .utc-box { + max-width: none; width: 100%; + border-radius: 20px 20px 0 0; + border-left: none; border-right: none; border-bottom: none; + padding: 10px 18px calc(22px + env(safe-area-inset-bottom)); + transform: translateY(100%); transition: transform .28s cubic-bezier(.22,1,.36,1); + box-shadow: 0 -12px 40px rgba(0,0,0,.6); + max-height: 88vh; overflow-y: auto; + } + .utc-overlay.show .utc-box { transform: translateY(0); } + + /* Grabber handle at the top */ + .utc-box::before { + content: ''; display: block; width: 40px; height: 4px; + background: #333; border-radius: 2px; margin: 4px auto 14px; + } + + .utc-close { top: 12px; right: 12px; } + .utc-head { margin-bottom: 16px; } + .utc-head-icon { width: 44px; height: 44px; font-size: 20px; margin-bottom: 10px; } + .utc-title { font-size: 17px; } + .utc-sub { font-size: 12px; } + + /* Row of compact tiles instead of full stacked cards */ + .utc-grid { grid-template-columns: repeat(3, 1fr); gap: 10px; } + .utc-card { + padding: 16px 8px 14px; border-radius: 14px; border-width: 1px; + } + .utc-card-ico { + width: 44px; height: 44px; margin-bottom: 8px; + font-size: 20px; border-radius: 12px; + } + .utc-card-title { font-size: 13px; } + .utc-card-desc { display: none; } + .utc-card-arr { display: none; } + .utc-card:active { + transform: scale(.96); + background: color-mix(in srgb, var(--accent) 10%, #111); + border-color: var(--accent); + } +} + /* ════════════════════════════════════════════════════════════════ Unified control system — one consistent look for every field, dropdown and picker in the upload modal. Scoped to #uploadModal @@ -829,19 +878,28 @@ const _UTC_META = { }; function openUploadChooser() { - // Mobile keeps the existing full-page create flow (with its own type picker) - if (window.innerWidth < 992) { - window.location.href = '{{ route("videos.create") }}'; - return; - } + // Chooser is shown on every viewport — on mobile chooseUploadType() routes + // straight to the standalone create page with the picked ?type= preserved. document.getElementById('upload-type-chooser').classList.add('show'); } +// Called from the desktop chooser cards on mobile — carries the picked type +// through to the standalone create page via ?type=. +function chooseUploadTypeMobile(type) { + const t = ['generic', 'music', 'match'].includes(type) ? type : 'generic'; + window.location.href = '{{ route("videos.create") }}?type=' + encodeURIComponent(t); +} + function closeUploadChooser() { document.getElementById('upload-type-chooser').classList.remove('show'); } function chooseUploadType(type) { + // Mobile has no modal — route straight to the create page with the type preserved. + if (window.innerWidth < 992) { + chooseUploadTypeMobile(type); + return; + } const meta = _UTC_META[type] || _UTC_META.generic; closeUploadChooser(); openUploadModal(); diff --git a/resources/views/videos/create.blade.php b/resources/views/videos/create.blade.php index 6025938..9b7c572 100644 --- a/resources/views/videos/create.blade.php +++ b/resources/views/videos/create.blade.php @@ -784,6 +784,17 @@ setAudioMode(this.dataset.type === 'music'); }); }); + + // Honor ?type=generic|music|match forwarded from the desktop chooser + // when routed here on mobile — pre-select the corresponding option. + (function preselectTypeFromQuery() { + try { + const t = new URLSearchParams(window.location.search).get('type'); + if (!t) return; + const opt = document.querySelector('#type-options .option-item[data-type="' + t + '"]'); + if (opt) opt.click(); + } catch (_) {} + }()); document.querySelectorAll('#visibility-options .option-item').forEach(item => { item.querySelector('input').addEventListener('change', () => { document.querySelectorAll('#visibility-options .option-item').forEach(o => o.classList.remove('active'));