A self-hosted media server and file management system
Vue ffmpegfile-serverjellyfinlaravelmediamedia-playermedia-servermediabrowsermediaservermusicmusic-librarymusic-playermusicplayerself-hostedvuejs
<?php
namespace App\Jobs;
use App\Data\Subtitles\SubtitleScanTarget;
use App\Enums\ImageType;
use App\Enums\MediaType;
use App\Enums\TaskStatus;
use App\Jobs\Metadata\ExtractFonts;
use App\Jobs\Metadata\GenerateStoryboard;
use App\Jobs\Utility\Subtitles\ScanSubtitles;
use App\Models\Metadata;
use App\Models\Record;
use App\Models\SubTask;
use App\Models\Task;
use App\Models\Video;
use App\Services\Images\ImageService;
use App\Services\Server\ServerConfigService;
use App\Services\TaskService;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Ramsey\Uuid\Uuid;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
class VerifyFiles extends ManagedSubTask {
protected $subtitleScanChain = [];
protected $embedChain = [];
protected $storyboardChain = [];
protected $fontChain = [];
protected $scannedDirectories = []; // this could go in the indexer but realistically it is only scanning each directory once and caching the "result" to later batch actual subtitle indexing
protected $fileMetaData = [];
/**
* Create a new job instance.
*
* @param Collection<int, Video> $videos
*/
public function __construct(public Collection $videos, int $taskId, public bool $generateImageTasks = false) {
$subTask = SubTask::create(['task_id' => $taskId, 'status' => TaskStatus::PENDING, 'name' => 'Verify ' . count($videos) . ' Files']);
$this->taskId = $taskId;
$this->subTaskId = $subTask->id;
}
public function handle(TaskService $taskService, ServerConfigService $config, ImageService $imageService): void {
if (! $this->beginSubTask($taskService)) {
return;
}
try {
$summary = $this->verifyFiles($taskService, $config, $imageService);
foreach ($this->scannedDirectories as $folderPath => $dirData) {
if (empty($dirData['targets'])) {
continue;
}
$this->subtitleScanChain[] = new ScanSubtitles(
taskId: $this->taskId,
targets: $dirData['targets'],
folderPath: $folderPath,
scanExternal: $dirData['dir_updated'],
);
}
$additionalTaskChain = array_merge($this->embedChain, $this->subtitleScanChain, $this->storyboardChain, $this->fontChain);
$additionalTaskCount = count($additionalTaskChain);
$taskCountUpdates = [
'sub_tasks_complete' => '++',
'sub_tasks_total' => $additionalTaskCount,
'sub_tasks_pending' => $additionalTaskCount,
'sub_tasks_current' => $additionalTaskCount,
];
if (count($this->subtitleScanChain)) {
$summary .= ' and queued ' . count($this->subtitleScanChain) . ' subtitle scans';
}
$this->completeSubTask($taskService, $summary, $taskCountUpdates);
// Starts other subtasks after updating current subtask and parent subtask states
// The parent task "ends" after the batch empties so in theory, this should delay that anyways and does not need to run before the task update
foreach ($additionalTaskChain as $additionalTask) {
$this->batch()->add($additionalTask);
}
} catch (\Throwable $th) {
$this->failSubTask($taskService, $th);
throw $th;
}
}
private function verifyFiles(TaskService $taskService, ServerConfigService $config, ImageService $imageService) {
$metadataTransactions = [];
$error = false;
$index = 0;
if (count($this->videos) == 0) {
throw new \Exception('Video Data Lost');
}
$taskUserId = Task::find($this->taskId)->user_id;
$generatedPosterCount = 0;
try {
foreach ($this->videos as $video) {
// Clear job cache on every loop
$this->fileMetaData = [];
$baseName = basename($video->path);
$compositeId = $video->folder->path . '/' . $baseName;
// absolute file path in storage
$filePath = $this->getAbsoluteMediaPath($video);
$folderPath = dirname($filePath);
/**
* @disregard P1013 Undefined method but it actually exists
*/
if (! Storage::disk('public')->fileExists("media/{$video->folder->path}/{$baseName}")) {
throw new \Exception("File media/{$video->folder->path}/{$baseName} no longer exists. Index your videos before running this task again.");
}
$uuid = $video->uuid;
// handle missing or invalid Uuid
if (! Uuid::isValid($uuid ?? '')) {
$uuid = $this->resolveMediaUuid($config, $video, $filePath); // Calls confirm metadata and will always lead to an ffprobe call
}
/**
* metadata should be defined by uuid alone but when I tried to do this, there were duplicates and issues so this needs investigation and fixing
* $metadata = Metadata::firstOrCreate(['uuid' => $uuid],['video_id' => $video->id, 'composite_id' => $compositeId]);
*/
$metadata = Metadata::where('uuid', $uuid)->orWhere('composite_id', $compositeId)->first();
if (! $metadata) {
$metadata = Metadata::create(['uuid' => $uuid, 'composite_id' => $compositeId, 'video_id' => $video->id]);
$metadata->refresh();
}
/**
* force replace uuid on video with one present on metadata
* for example after a match on composite id
*
* writes the uuid to the video (setting the one on the video table to null and then running an embed job if enabled)
* uses the metadata's uuid for the rest of the job
*/
if ($metadata->uuid !== $uuid) {
Log::warning('Uuid missmatch found!', ['video_id' => $video->id, 'path' => $filePath, 'uuid' => $uuid, 'metadata_uuid' => $metadata->uuid]);
$uuid = $metadata->uuid;
$this->embedMediaUuid($config, $video, $uuid, $filePath);
}
$stored = $metadata->toArray(); // Metadata from db
$changes = []; // Changes -> stored + changes . length has to be the same for every video so must generate defaults
if (isset($stored['raw_metadata']) && is_array($stored['raw_metadata'])) {
$stored['raw_metadata'] = json_encode($stored['raw_metadata']); // must re-encode stored metadata cache
}
$fileUpdated = $metadata->file_scanned_at && filemtime($filePath) > $metadata->file_scanned_at->timestamp;
$this->fileMetaData = $metadata->raw_metadata; // populate with potential cached metadata as a base value. If the base is insufficient, confirmMetadata should handle extraction on its own
if ($fileUpdated) { // if file was updated, force load raw data and save to db
$this->fileMetaData = $this->getFileMetadata($filePath, 'file updated');
$changes['raw_metadata'] = json_encode($this->fileMetaData);
}
if (is_null($metadata->uuid) || $fileUpdated) {
$changes['uuid'] = $uuid;
}
if (is_null($metadata->composite_id)) {
$changes['composite_id'] = $compositeId;
}
if (is_null($metadata->file_size) || $fileUpdated) {
$changes['file_size'] = filesize($filePath);
}
if (is_null($metadata->mime_type) || $metadata->mime_type === 'application/octet-stream' || $metadata->mime_type === 'application/x-genesis-rom') {
$changes['mime_type'] = $this->extractMimeType($filePath);
}
$file_modified_at = $metadata->file_modified_at;
if (is_null($metadata->file_modified_at) || $fileUpdated) {
$mtime = filemtime($filePath);
$ctime = filectime($filePath);
$file_modified_at = Carbon::createFromTimestampUTC(min($mtime, $ctime));
$changes['file_modified_at'] = $file_modified_at;
}
if (is_null($metadata->first_file_modified_at) && $file_modified_at !== null) {
$changes['first_file_modified_at'] = $file_modified_at;
}
$mime_type = $changes['mime_type'] ?? $metadata->mime_type;
$is_audio = str_starts_with($mime_type, 'audio');
$media_type = $is_audio ? MediaType::AUDIO : MediaType::VIDEO;
if ($stored['media_type'] !== $media_type) {
$changes['media_type'] = $media_type;
}
// if file is of type audio and one of the following is true: artist is null, album is null, codec is null => generate description from audio tags
$audioMetadata = ($fileUpdated || is_null($metadata->artist) || is_null($metadata->album) || is_null($metadata->codec)) && $is_audio ? $this->getAudioDescription($filePath) : [];
// region Poster
$primaryPoster = $metadata->primaryPoster; // current primary Image
$posterUrl = $metadata->poster_url; // current url
$posterIsExternal = $posterUrl && filter_var($posterUrl, FILTER_VALIDATE_URL) && ! str_contains($posterUrl, config('app.url'));
$externalPosterDownloaded = $primaryPoster?->source_url === $posterUrl;
$isPosterUserOwned = $primaryPoster?->image_source?->isUserOwned() ?? ($posterUrl && ! $posterIsExternal);
$autoOutdated = $fileUpdated && $primaryPoster && ! $isPosterUserOwned;
$needsScan = ! $primaryPoster && is_null($metadata->poster_scanned_at); // if never scanned and does not exist, then needs a scan
$shouldGenerate = $needsScan || $autoOutdated; // Generate only if a scan is needed or an auto generated image exists that is outdated
$image = match (true) {
$posterIsExternal && ! $externalPosterDownloaded => $imageService->downloadFromUrl($posterUrl, $metadata, ImageType::POSTER, $metadata->editor_id ?? $taskUserId),
$shouldGenerate && $is_audio => $imageService->extractPoster($filePath, $metadata),
$shouldGenerate && ! $is_audio => $imageService->generateVideoPoster($filePath, $metadata),
default => null,
};
if ($image) {
if (! $isPosterUserOwned) {
$changes['primary_poster_id'] = $image->id;
$changes['poster_url'] = '';
}
$changes['poster_scanned_at'] = now();
$generatedPosterCount += 1;
}
// For reference
if ($metadata->poster_url && ! $metadata->primaryPoster && ! $fileUpdated) {
$relativePath = match (true) {
str_starts_with($metadata->poster_url, 'https://app.test:8080/storage') => substr($metadata->poster_url, strlen('https://app.test:8080/storage')),
str_starts_with($metadata->poster_url, config('app.url') . '/storage') => substr($metadata->poster_url, strlen(config('app.url') . '/storage')),
default => null,
};
if ($relativePath) {
// Candidate for migration
}
}
// endregion
// region Title and Episode/Season or Track/Disk
preg_match('![sS]\d+!', $video->name, $seasonRaw);
preg_match('![eE]\d+!', $video->name, $episodeRaw);
preg_match('!\d+!', $seasonRaw[0] ?? '', $season);
preg_match('!\d+!', $episodeRaw[0] ?? '', $episode);
if (is_null($metadata->duration) || $fileUpdated) {
$this->confirmMetadata($filePath, 'Duration is missing or file was updated');
$duration = $this->fileMetaData['format']['duration'] ?? $this->fileMetaData['streams'][0]['duration'] ?? $metadata->duration;
$changes['duration'] = is_numeric($duration) ? floor($duration) : null;
}
// endregion
// #region Subtitles
// check if directory subtitle scan is needed
if (! isset($this->scannedDirectories[$folderPath])) {
$this->scannedDirectories[$folderPath] = [
'last_modified' => filemtime($folderPath),
'targets' => [],
'dir_updated' => false,
];
}
// if directory updated, check directory for related subtitle files and make subtitle transactions for them with stream 0
$dirUpdated = $metadata->file_scanned_at && $this->scannedDirectories[$folderPath]['last_modified'] > $metadata->file_scanned_at->timestamp;
$subtitleScanNeeded = ! $is_audio && (is_null($metadata->subtitles_scanned_at) || $fileUpdated || $dirUpdated);
if ($subtitleScanNeeded) {
$this->scannedDirectories[$folderPath]['targets'][] = new SubtitleScanTarget(
uuid: $uuid,
fileUpdated: $fileUpdated || is_null($metadata->subtitles_scanned_at), // If I dont check embedded by this null flag, "reset subtitles" no longer works so it needs to be authoritative?
);
$changes['subtitles_scanned_at'] = null;
if ($dirUpdated) {
$this->scannedDirectories[$folderPath]['dir_updated'] = true;
}
}
// #endregion
if (! $is_audio && (is_null($metadata->resolution_height) || is_null($metadata->codec) || $fileUpdated)) {
$this->confirmMetadata($filePath, 'Not audio and missing resolution or codec or because fileUpdated was {' . ($fileUpdated ? 'true' : 'false') . '}');
foreach ($this->fileMetaData['streams'] as $stream) {
if (! isset($stream['codec_type']) || $stream['codec_type'] !== 'video' || ! isset($stream['width'])) {
continue;
}
$changes['resolution_width'] = floor($stream['width']);
if (isset($stream['height'])) {
$changes['resolution_height'] = floor($stream['height']);
}
if (isset($stream['codec_name'])) {
$changes['codec'] = $stream['codec_name'];
}
if (isset($stream['avg_frame_rate'])) {
[$numerator, $denominator] = explode('/', $stream['avg_frame_rate']);
$changes['frame_rate'] = floor($numerator / max($denominator, 1));
}
break;
}
}
// Update episode and season (track and disc) if not set or file is audio and has embedded info and was updated
if (is_null($metadata->episode) || ($fileUpdated && $is_audio && isset($audioMetadata['episode']))) {
$changes['episode'] = count($episode) == 1 ? (int) $episode[0] : $audioMetadata['episode'] ?? null;
}
if (is_null($metadata->season) || ($fileUpdated && $is_audio && isset($audioMetadata['season']))) {
$changes['season'] = count($season) == 1 ? (int) $season[0] : $audioMetadata['season'] ?? null;
}
if (is_null($metadata->title) && ! $is_audio) {
$seasonStr = count($season) == 1 ? 'S' . (int) $season[0] : '';
$episodeStr = count($episode) == 1 ? 'E' . sprintf('%02d', $episode[0]) : '';
$changes['title'] = $episodeStr != '' ? $seasonStr . $episodeStr : $video->name;
}
// Only update title from audioMetadata if not set or file is of type audio with an embedded title and was updated
if ((is_null($metadata->title) || $fileUpdated) && $is_audio && isset($audioMetadata['title'])) {
$changes['title'] = substr($audioMetadata['title'], 0, 255) ?? $metadata->title;
}
if (is_null($metadata->description)) {
$changes['description'] = $audioMetadata['description'] ?? $video->description ?? null;
}
if (is_null($metadata->lyrics) || $fileUpdated) {
$changes['lyrics'] = $audioMetadata['lyrics'] ?? $metadata->lyrics; // Default to existing
}
if (is_null($metadata->artist) || $fileUpdated) {
$changes['artist'] = substr($audioMetadata['artist'] ?? $metadata->artist, 0, 255);
}
if (is_null($metadata->album) || $fileUpdated) {
$changes['album'] = substr($audioMetadata['album'] ?? $metadata->album, 0, 255);
}
if (is_null($metadata->codec) && ! isset($changes['codec'])) {
$changes['codec'] = substr($audioMetadata['codec'] ?? $metadata->codec, 0, 255);
}
if ((is_null($metadata->bitrate) || $fileUpdated) && ! isset($changes['bitrate']) && ! $is_audio) {
$this->confirmMetadata($filePath, 'Bitrate is missing on video');
$changes['bitrate'] = $this->fileMetaData['format']['bit_rate'] ?? $metadata->bitrate;
}
if (is_null($metadata->view_count)) {
$changes['view_count'] = ($metadata->id ? Record::where('metadata_id', $metadata->id)->count() : 0);
}
if (! $fileUpdated && ! empty($this->fileMetaData) && empty($metadata->raw_metadata)) { // if file wasnt updated but the db cached value was empty and a local cache was loaded, save to db
$changes['raw_metadata'] = json_encode($this->fileMetaData);
}
// If no storyboard and storyboard_scanned_at is null OR storyboard was scanned before file was last modified or file was just updated
$needsStoryboard = $fileUpdated || (! $metadata->storyboard_scanned_at && ! $metadata->storyboard) || $metadata->storyboard_scanned_at?->lt($metadata->file_modified_at);
if ($this->generateImageTasks && ! $is_audio && $video->folder->category->storyboard_enabled && $needsStoryboard) {
$this->storyboardChain[] = new GenerateStoryboard(
filePath: $filePath,
uuid: $uuid,
taskId: $this->taskId,
);
}
$needsFonts = $fileUpdated || (! $metadata->fonts_scanned_at && count($metadata->fonts) === 0) || $metadata->fonts_scanned_at?->lt($metadata->file_modified_at);
if (! $is_audio && $video->folder->category->fonts_enabled && $needsFonts) {
$this->fontChain[] = new ExtractFonts(
filePath: $filePath,
uuid: $uuid,
taskId: $this->taskId,
);
}
if (! empty($changes)) {
$changes['file_scanned_at'] = now();
unset(
$stored['created_at'],
$stored['updated_at'],
$stored['logical_composite_id']
);
$metadataTransactions[] = [...$stored, ...$changes];
/**
* DEBUG
* dump(count([...$stored, ...$changes]));
* if ($new) dump([...$stored, ...$changes]);
* dump([...$stored, ...$changes]);
* dump($changes);
* dump($video->name);
*/
}
$index += 1;
$taskService->updateSubTask($this->subTaskId, ['progress' => (int) (($index / count($this->videos)) * 100)]);
}
} catch (\Throwable $th) {
$ids = array_column($metadataTransactions, 'id');
$error = true;
$this->handleError('Cannot verify file metadata', $th, $ids, count($metadataTransactions), count($this->videos));
}
try {
if (empty($metadataTransactions) || $error) {
return 'No Changes Found';
}
Metadata::upsert(
$metadataTransactions,
'uuid',
[
'video_id',
'title',
'description',
'lyrics',
'artist',
'album',
'duration',
'season',
'episode',
'view_count',
'uuid',
'file_size',
'mime_type',
'codec',
'bitrate',
'resolution_width',
'resolution_height',
'frame_rate',
'file_scanned_at',
'file_modified_at',
'first_file_modified_at',
'media_type',
'subtitles_scanned_at',
'raw_metadata',
// legacy poster
'poster_url',
'raw_thumbnail_url',
// poster
'primary_poster_id',
'poster_scanned_at',
]
);
$generatedPosterSummary = $generatedPosterCount > 0 ? ", generated {$generatedPosterCount} posters" : '';
return 'Updated ' . count($metadataTransactions) . ' videos from id ' . ($metadataTransactions[0]['video_id']) . ' to ' . ($metadataTransactions[count($metadataTransactions) - 1]['video_id']) . $generatedPosterSummary;
} catch (\Throwable $th) {
$ids = array_column($metadataTransactions, 'id');
$this->handleError('Error inserting verified file metadata', $th, $ids, count($metadataTransactions));
}
}
public static function getFileMetadata(string $filePath, $reason = 'und') {
try {
// ? FFMPEG module with 6 test folders takes 35+ seconds but running the commands through shell takes 18 seconds
$ext = pathinfo($filePath, PATHINFO_EXTENSION);
if (config('app.env') === 'local') {
dump('PULLING METADATA ' . $filePath . ' reason: ' . $reason);
}
$command = [
'ffprobe',
'-v',
'quiet',
'-print_format',
'json',
'-show_format',
'-show_streams',
$filePath,
];
$process = new Process($command);
$process->run();
// Check if the process was successful
if (! $process->isSuccessful()) {
throw new ProcessFailedException($process);
}
$output = $process->getOutput();
$metadata = json_decode($output, true);
if (! is_array($metadata)) {
throw new \RuntimeException('Invalid ffprobe JSON output');
}
if ($ext === 'ogg') {
$metadata['format'] = $metadata['streams'][0] ?? [];
}
$format = $metadata['format'] ?? [];
$tags = array_change_key_case($format['tags'] ?? [], CASE_LOWER);
$streams = $metadata['streams'] ?? [];
if (! isset($tags['uuid']) && isset($tags['uid'])) {
$tags['uuid'] = $tags['uid']; // Old uid tag, does not apply to any versions from 2025 and up
}
if (! isset($tags['uuid']) && isset($tags['encoder']) && uuid_is_valid($tags['encoder'])) {
$tags['uuid'] = $tags['encoder']; // ExifTool tag
}
return [
'format' => $format,
'tags' => $tags,
'streams' => $streams,
];
} catch (\Throwable $th) {
Log::error('Unable to get file metadata', ['path' => $filePath, 'error' => $th->getMessage(), 'trace' => $th->getTraceAsString()]);
return ['format' => [], 'tags' => [], 'streams' => []];
}
}
// Call get metadata when needed
private function confirmMetadata(string $filePath, string $reason = 'und') {
if (! empty($this->fileMetaData)) {
return;
}
$this->fileMetaData = $this->getFileMetadata($filePath, $reason);
}
private function getAudioDescription($filePath) {
$this->confirmMetadata($filePath, 'Get audio description');
$tags = $this->fileMetaData['tags'] ?? [];
$streams = $this->fileMetaData['streams'] ?? [];
$artist = $tags['artist'] ?? $tags['ARTIST'] ?? '';
$album = $tags['album'] ?? $tags['ALBUM'] ?? '';
$description = implode(' - ', array_filter([
$album,
$artist,
]));
$results = [
'title' => $tags['title'] ?? $tags['TITLE'] ?? null,
'description' => $description ?: '',
'season' => isset($tags['disc']) ? (int) explode('/', $tags['disc'])[0] : 0,
'episode' => isset($tags['track']) ? (int) explode('/', $tags['track'])[0] : 0,
'lyrics' => $tags['lyrics- '] ?? $tags['UNSYNCEDLYRICS'] ?? null,
'album' => $album ?? null,
'artist' => $artist ?? null,
];
foreach ($streams as $stream) {
if ($stream['codec_type'] != 'audio') {
continue;
}
$results = array_merge($results, array_filter([
'codec' => $stream['codec_name'] ?? null,
'bitrate' => $stream['bit_rate'] ?? null,
], fn ($value) => ! is_null($value)));
break;
}
if (isset($this->fileMetaData['format']['bit_rate']) && (! isset($results['bitrate']) || is_null($results['bitrate']))) {
// This is inaccurate $results['bitrate'] = $this->fileMetaData['format']['bit_rate'];
}
return $results;
}
public static function getPathUrl($path) {
/**
* @disregard P1013 Undefined method but it actually exists
*/
return Storage::disk('public')->url($path);
}
protected function extractMimeType(string $filePath) {
$finfo = new \finfo(FILEINFO_MIME_TYPE);
$mimeType = $finfo->file($filePath);
if ($mimeType === 'application/octet-stream' || $mimeType === 'application/x-genesis-rom') {
$extension = pathinfo($filePath, PATHINFO_EXTENSION);
$mimeMap = [
'mp3' => 'audio/mpeg',
'wav' => 'audio/wav',
'ogg' => 'audio/ogg',
'aac' => 'audio/aac',
'flac' => 'audio/flac',
];
return $mimeMap[strtolower($extension)] ?? $mimeType;
}
return $mimeType;
}
public static function getAbsoluteMediaPath(Video $media) {
$path = str_starts_with($media->path, 'storage/')
? substr($media->path, 8)
: $media->path;
return str_replace('\\', '/', Storage::disk('public')->path($path));
}
/**
* Resolves uuid for media file
*
* Loads ffprobe data into job-scoped memory
*
* First check is for the value already embedded in the file
* Second check is a new uuid and subsequent embed
*/
protected function resolveMediaUuid(ServerConfigService $config, Video $media, string $filePath): string {
// if the media in db or file does not have a valid uuid, it will add it in both the db and on the file.
$this->confirmMetadata($filePath, 'resolve uuid');
if (isset($this->fileMetaData['tags']['uuid'])) {
$uuid = $this->fileMetaData['tags']['uuid'];
// If embedding, uuid is added to the media row in the job, but if found on the file, it is manually added here
// This means it was already embedded at some point but was not in the Videos table
$media->update(['uuid' => $uuid]);
dump("Found UUID on file {$uuid}");
return $uuid;
}
$uuid = Str::uuid()->toString();
$this->fileMetaData['tags']['uuid'] = $uuid;
$this->embedMediaUuid($config, $media, $uuid, $filePath);
return $uuid;
}
/**
* Reset uuid on video and queue an embed job
*/
protected function embedMediaUuid(ServerConfigService $config, Video $media, string $uuid, string $filePath): void {
$media->update(['uuid' => null]);
if ($config->get('uuid_embed', true)) {
$this->embedChain[] = new EmbedUidInMetadata($filePath, $uuid, $this->taskId, $media->id);
}
}
}