Skip to content

A self-hosted media server and file management system

feat: load external subtitle files

aminnausin2b9e056bbd4e
diff --git a/app/Http/Controllers/Api/V1/Metadata/SubtitleController.php b/app/Http/Controllers/Api/V1/Metadata/SubtitleController.php
index c41b7945..74085d46 100644
--- a/app/Http/Controllers/Api/V1/Metadata/SubtitleController.php
+++ b/app/Http/Controllers/Api/V1/Metadata/SubtitleController.php
@@ -31,5 +31,14 @@ class SubtitleController extends Controller {
return $this->subtitleResolver->resolveSubtitles($metadata, $track, $format);
}
+ public function showExternalTrack(
+ Request $request,
+ Metadata $metadata,
+ string $language,
+ string $format = 'vtt'
+ ) {
+ return $this->subtitleResolver->resolveSubtitles($metadata, 0, $format, $language);
+ }
+
// TODO: Add an API route that gets subtitles for a specific metadata or video row and cache the results. These are only used to determine the filepath used in the <track/>
}
diff --git a/app/Services/Subtitles/SubtitleFormatter.php b/app/Services/Subtitles/SubtitleFormatter.php
index 6c235db0..c8e6d5a3 100644
--- a/app/Services/Subtitles/SubtitleFormatter.php
+++ b/app/Services/Subtitles/SubtitleFormatter.php
@@ -12,6 +12,7 @@ class SubtitleFormatter {
* @param string $input Relative local disk path of the input file
* @param string $output Relative local disk path of the output file
* @param string $outputFormat Output file format
+ * @return string Converted file path
*/
public function convert(string $input, string $output, string $outputFormat): string {
if (! Storage::disk('local')->exists($input)) {
@@ -26,7 +27,7 @@ class SubtitleFormatter {
throw new \RuntimeException("Conversion failed: {$output} not created");
}
- return $output;
+ return Storage::disk('local')->path($output);
}
/**
diff --git a/app/Services/Subtitles/SubtitleResolver.php b/app/Services/Subtitles/SubtitleResolver.php
index 8b5e2d86..7ed17a7b 100644
--- a/app/Services/Subtitles/SubtitleResolver.php
+++ b/app/Services/Subtitles/SubtitleResolver.php
@@ -14,48 +14,41 @@ use Symfony\Component\HttpFoundation\BinaryFileResponse;
class SubtitleResolver {
public function __construct(protected SubtitleExtractor $extractor, protected SubtitleFormatter $formatter) {}
- public function resolveSubtitles(Metadata $metadata, int $track, string $format): BinaryFileResponse {
+ public function resolveSubtitles(Metadata $metadata, int $track, string $format, ?string $language = null): BinaryFileResponse {
try {
- $subtitle = $metadata->subtitles()->where('track_id', $track)->firstOrFail(); // 404 on no result
+ if ($track === 0 && $language) {
+ // find external track by language
+ $subtitle = $metadata->subtitles()
+ ->where('track_id', 0)
+ ->where('language', $language)
+ ->firstOrFail();
+ } else {
+ $subtitle = $metadata->subtitles()
+ ->where('track_id', $track)
+ ->firstOrFail();
+ }
$format = ltrim(strtolower($format), '.');
- $requestedPath = $subtitle->getFilePath($format);
-
+ $requestedPath = $subtitle->getFilePath($format, $language);
if ($this->fileExists($requestedPath)) {
- return Response::file(
- Storage::disk('local')->path($requestedPath)
- );
+ return Response::file(Storage::disk('local')->path($requestedPath));
}
- if (! $metadata->video()) {
+ if (! $metadata->video_id) {
abort(404);
}
- if ($subtitle->stream === 0 && $subtitle->external_source_path && ! $subtitle->path) {
+ if ($subtitle->track_id === 0 && $subtitle->external_path && ! $subtitle->path) {
$this->resolveExternalSubtitle($subtitle);
- $subtitle->refresh();
}
if (! $subtitle->path || ! $this->fileExists($subtitle->path)) {
$this->extractor->extractStream($metadata, $subtitle);
- $subtitle->refresh();
- }
-
- $nativePath = $subtitle->path;
- $convertedPath = $this->formatter->convert(
- $nativePath,
- $requestedPath,
- $format
- );
-
- if (! $this->fileExists($convertedPath)) {
- abort(500, 'Subtitle file does not exist after conversion.');
}
+ $subtitle->refresh();
- return Response::file(
- Storage::disk('local')->path($convertedPath)
- );
+ return Response::file($this->formatter->convert($subtitle->path, $requestedPath, $format));
} catch (ModelNotFoundException $th) {
throw $th;
} catch (\Throwable $th) {
@@ -68,17 +61,14 @@ class SubtitleResolver {
* Copies an external subtitle file to the internal folder following the track.language.ext pattern
*/
private function resolveExternalSubtitle(Subtitle $subtitle): void {
- $sourcePath = $subtitle->external_source_path;
+ $sourcePath = $subtitle->external_path;
if (! $sourcePath) {
- throw new InvalidArgumentException;
+ throw new InvalidArgumentException('Subtitle has no external source path');
}
$ext = pathinfo($sourcePath, PATHINFO_EXTENSION);
$outputPath = $subtitle->getFilePath($ext, $subtitle->language);
- Storage::disk('local')->put(
- $outputPath,
- Storage::disk('public')->get("$sourcePath")
- );
+ Storage::disk('local')->put($outputPath, Storage::disk('public')->get("$sourcePath"));
$subtitle->update([
'path' => $outputPath,
diff --git a/resources/js/components/video/VideoPlayer.vue b/resources/js/components/video/VideoPlayer.vue
index c0f19aed..d0c48c2b 100755
--- a/resources/js/components/video/VideoPlayer.vue
+++ b/resources/js/components/video/VideoPlayer.vue
@@ -1174,7 +1174,7 @@ defineExpose({
kind="captions"
:label="track.language"
:srclang="track.language"
- :src="`/data/subtitles/${track.metadata_uuid}/${track.track_id}`"
+ :src="`/data/subtitles/${track.metadata_uuid}/${track.track_id}${track.track_id === 0 ? `.${track.language}` : ''}`"
/>
Your browser does not support the video tag.
</video>
diff --git a/routes/web.php b/routes/web.php
index ce121280..b2efc6f9 100755
--- a/routes/web.php
+++ b/routes/web.php
@@ -45,6 +45,7 @@ Route::get('/metadata/{path}', function (string $path) {
);
})->where('path', '.*');
+Route::get('/data/subtitles/{metadata:uuid}/0.{language}.{format?}', [SubtitleController::class, 'showExternalTrack'])->whereUuid('metadata')->where('format', 'vtt|srt|ass|json');
Route::get('/data/subtitles/{metadata:uuid}/{track}.{format?}', [SubtitleController::class, 'show'])->whereUuid('metadata')->where('format', 'vtt|srt|ass|json');
Route::get('/data/{path}', function (string $path) {