Skip to content

A self-hosted media server and file management system

chore(logging): temporary logging to find out what part of extracting a subtitle takes so long

aminnausinb7dc24e5696a
diff --git a/app/Services/Subtitles/SubtitleExtractor.php b/app/Services/Subtitles/SubtitleExtractor.php
index e74af93e..d2bbef19 100644
--- a/app/Services/Subtitles/SubtitleExtractor.php
+++ b/app/Services/Subtitles/SubtitleExtractor.php
@@ -17,12 +17,18 @@ class SubtitleExtractor {
* @param Subtitle $subtitle Information about the subtitle track
*/
public function extractStream(Metadata $metadata, Subtitle $subtitle) {
+ $extractionStart = microtime(true);
+ $timings = [];
+
try {
$mediaPath = $metadata->video->path;
$ext = $this->getExtensionFromCodec($subtitle['codec']);
+ $pathStart = microtime(true);
$outputPath = $this->getOutputPath($subtitle, $ext);
+ $timings['get_output_path'] = microtime(true) - $pathStart;
+ $commandStart = microtime(true);
$command = [
'ffmpeg',
'-y',
@@ -32,21 +38,42 @@ class SubtitleExtractor {
"0:$subtitle->track_id",
Storage::disk('local')->path($outputPath),
];
+ $timings['build_command'] = microtime(true) - $commandStart;
+ $ffmpegStart = microtime(true);
$process = new Process($command);
$process->mustRun();
+ $timings['ffmpeg_execution'] = microtime(true) - $ffmpegStart;
+ $verifyStart = microtime(true);
if (! Storage::disk('local')->exists($outputPath)) {
throw new \RuntimeException("Output file $outputPath does not exist after extraction");
}
+ $timings['verify_output'] = microtime(true) - $verifyStart;
+ $updateStart = microtime(true);
$subtitle->update([
'path' => $outputPath,
'format' => $ext,
]);
+ $timings['db_update'] = microtime(true) - $updateStart;
+
+ $timings['total_extraction'] = microtime(true) - $extractionStart;
+
+ Log::info('Subtitle extraction completed', [
+ 'subtitle_id' => $subtitle->id,
+ 'track_id' => $subtitle->track_id,
+ 'metadata_uuid' => $subtitle->metadata_uuid,
+ 'codec' => $subtitle->codec,
+ 'output_format' => $ext,
+ 'media_path' => $mediaPath,
+ 'output_path' => $outputPath,
+ 'timings_ms' => array_map(fn($t) => round($t * 1000, 2), $timings),
+ ]);
return $outputPath;
} catch (ProcessFailedException $e) {
+ $timings['total_extraction'] = microtime(true) - $extractionStart;
Log::error('Subtitle extraction failed (process)', [
'subtitle_id' => $subtitle->id,
'track_id' => $subtitle->track_id,
@@ -54,14 +81,17 @@ class SubtitleExtractor {
'command' => $e->getProcess()->getCommandLine(),
'exit_code' => $e->getProcess()->getExitCode(),
'error' => $e->getProcess()->getErrorOutput(),
+ 'timings_ms' => array_map(fn($t) => round($t * 1000, 2), $timings),
]);
throw $e;
} catch (\Throwable $th) {
+ $timings['total_extraction'] = microtime(true) - $extractionStart;
Log::error('Subtitle extraction failed (general)', [
'subtitle_id' => $subtitle->id,
'track_id' => $subtitle->track_id,
'metadata_uuid' => $subtitle->metadata_uuid,
'error' => $th->getMessage(),
+ 'timings_ms' => array_map(fn($t) => round($t * 1000, 2), $timings),
]);
throw $th;
}
diff --git a/app/Services/Subtitles/SubtitleFormatter.php b/app/Services/Subtitles/SubtitleFormatter.php
index 100d24bb..67b1b3b9 100644
--- a/app/Services/Subtitles/SubtitleFormatter.php
+++ b/app/Services/Subtitles/SubtitleFormatter.php
@@ -16,22 +16,58 @@ class SubtitleFormatter {
* @return string Converted file path
*/
public function convert(string $input, string $output, string $outputFormat): string {
+ $conversionStart = microtime(true);
+ $timings = [];
+
try {
+ $inputCheckStart = microtime(true);
if (! Storage::disk('local')->exists($input)) {
throw new \RuntimeException("Input file not found: {$input}");
}
+ $timings['input_exists_check'] = microtime(true) - $inputCheckStart;
+
+ // if ($input === $output) {
+ // return Storage::disk('local')->path($input);
+ // }
+
+ $sameFileStart = microtime(true);
+ $isSameFile = $input === $output;
+ $timings['same_file_check'] = microtime(true) - $sameFileStart;
+
+ if ($isSameFile) {
+ $timings['total_conversion'] = microtime(true) - $conversionStart;
+
+ Log::info('Subtitle conversion skipped (same file)', [
+ 'input' => $input,
+ 'output' => $output,
+ 'format' => $outputFormat,
+ 'timings_ms' => array_map(fn($t) => round($t * 1000, 2), $timings),
+ ]);
- if ($input === $output) {
return Storage::disk('local')->path($input);
}
$strategy = $this->matchStrategy($outputFormat);
+ $actualConversionStart = microtime(true);
$strategy->convert($input, $output);
+ $timings['actual_conversion'] = microtime(true) - $actualConversionStart;
+ $outputCheckStart = microtime(true);
if (! Storage::disk('local')->exists($output)) {
throw new \RuntimeException("Conversion failed: {$output} not created");
}
+ $timings['output_verification'] = microtime(true) - $outputCheckStart;
+
+ $timings['total_conversion'] = microtime(true) - $conversionStart;
+
+ Log::info('Subtitle conversion completed', [
+ 'input' => $input,
+ 'output' => $output,
+ 'format' => $outputFormat,
+ 'strategy' => get_class($strategy),
+ 'timings_ms' => array_map(fn($t) => round($t * 1000, 2), $timings),
+ ]);
return Storage::disk('local')->path($output);
} catch (\Throwable $th) {
diff --git a/app/Services/Subtitles/SubtitleResolver.php b/app/Services/Subtitles/SubtitleResolver.php
index de35f465..e8be951d 100644
--- a/app/Services/Subtitles/SubtitleResolver.php
+++ b/app/Services/Subtitles/SubtitleResolver.php
@@ -12,10 +12,15 @@ use InvalidArgumentException;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
class SubtitleResolver {
- public function __construct(protected SubtitleExtractor $extractor, protected SubtitleFormatter $formatter) {}
+ public function __construct(protected SubtitleExtractor $extractor, protected SubtitleFormatter $formatter) {
+ }
public function resolveSubtitles(Metadata $metadata, int $track, string $format, ?string $language = null): BinaryFileResponse {
+ $startTime = microtime(true);
+ $timings = [];
+
try {
+ $queryStart = microtime(true);
if ($track === 0 && $language) {
// find external track by language
$subtitle = $metadata->subtitles()
@@ -27,32 +32,91 @@ class SubtitleResolver {
->where('track_id', $track)
->firstOrFail();
}
+ $timings['db_query'] = microtime(true) - $queryStart;
$format = ltrim(strtolower($format), '.');
+ $pathStart = microtime(true);
$requestedPath = $subtitle->getFilePath($format, $language);
- if ($this->fileExists($requestedPath)) {
+ $timings['get_file_path'] = microtime(true) - $pathStart;
+
+ $existsStart = microtime(true);
+ $fileExists = $this->fileExists($requestedPath);
+
+ $timings['file_exists_check'] = microtime(true) - $existsStart;
+
+ if ($fileExists) {
+ $timings['total'] = microtime(true) - $startTime;
+ $timings['cache_hit'] = true;
+
+ Log::info('Subtitle resolved (cached)', [
+ 'metadata_uuid' => $metadata->uuid,
+ 'track' => $track,
+ 'format' => $format,
+ 'timings_ms' => array_map(fn($t) => round($t * 1000, 2), $timings),
+ ]);
+
return Response::file(Storage::disk('local')->path($requestedPath));
}
+ $timings['cache_hit'] = false;
+
+ // if ($this->fileExists($requestedPath)) {
+ // return Response::file(Storage::disk('local')->path($requestedPath));
+ // }
+
if (! $metadata->video_id) {
abort(404);
}
if ($subtitle->track_id === 0 && $subtitle->external_path && ! $subtitle->path) {
+ $externalStart = microtime(true);
$this->resolveExternalSubtitle($subtitle);
+ $timings['external_subtitle_copy'] = microtime(true) - $externalStart;
}
- if (! $subtitle->path || ! $this->fileExists($subtitle->path)) {
+ $extractionCheckStart = microtime(true);
+ $needsExtraction = ! $subtitle->path || ! $this->fileExists($subtitle->path);
+ $timings['extraction_check'] = microtime(true) - $extractionCheckStart;
+
+ if ($needsExtraction) {
+ $extractionStart = microtime(true);
$this->extractor->extractStream($metadata, $subtitle);
+ $timings['extraction'] = microtime(true) - $extractionStart;
}
+ $refreshStart = microtime(true);
$subtitle->refresh();
+ $timings['subtitle_refresh'] = microtime(true) - $refreshStart;
+
+ $conversionStart = microtime(true);
+ $convertedPath = $this->formatter->convert($subtitle->path, $requestedPath, $format);
+ $timings['format_conversion'] = microtime(true) - $conversionStart;
+
+ $timings['total'] = microtime(true) - $startTime;
+
+ Log::info('Subtitle resolved (generated)', [
+ 'metadata_uuid' => $metadata->uuid,
+ 'track' => $track,
+ 'format' => $format,
+ 'language' => $language,
+ 'needed_extraction' => $needsExtraction,
+ 'timings_ms' => array_map(fn($t) => round($t * 1000, 2), $timings),
+ ]);
+
+ return Response::file($convertedPath);
- return Response::file($this->formatter->convert($subtitle->path, $requestedPath, $format));
+ // return Response::file($this->formatter->convert($subtitle->path, $requestedPath, $format));
} catch (ModelNotFoundException $e) {
throw $e;
} catch (\Throwable $th) {
- Log::error('Subtitle Resolver Failed', ['error' => $th->getMessage()]);
+ // Log::error('Subtitle Resolver Failed', ['error' => $th->getMessage()]);
+ $timings['total'] = microtime(true) - $startTime;
+ Log::error('Subtitle Resolver Failed', [
+ 'error' => $th->getMessage(),
+ 'metadata_uuid' => $metadata->uuid ?? null,
+ 'track' => $track,
+ 'timings_ms' => array_map(fn($t) => round($t * 1000, 2), $timings),
+ ]);
throw $th;
}
}