A self-hosted media server and file management system
Vue ffmpegfile-serverjellyfinlaravelmediamedia-playermedia-servermediabrowsermediaservermusicmusic-librarymusic-playermusicplayerself-hostedvuejs
fix: storyboard intervals are inconsistent at framerates greater than 0.1
aminnausindb25e0e76637
diff --git a/app/Jobs/Metadata/GenerateStoryboard.php b/app/Jobs/Metadata/GenerateStoryboard.phpindex 9a8548b5..c2229459 100644--- a/app/Jobs/Metadata/GenerateStoryboard.php+++ b/app/Jobs/Metadata/GenerateStoryboard.php@@ -99,7 +99,6 @@ class GenerateStoryboard extends ManagedSubTask {}$options = StoryboardOptions::fromMetadata($metadata);- $tile_count = ceil($metadata->duration * $options->fps);$command = $builder->storyboard(filePath: $filePath,@@ -135,13 +134,13 @@ class GenerateStoryboard extends ManagedSubTask {throw new FFmpegException('FFmpeg produced no output files');}- DB::transaction(function () use ($options, $tile_count, $timeElapsed, $rawCommand) {+ DB::transaction(function () use ($options, $timeElapsed, $rawCommand) {Storyboard::updateOrCreate(['metadata_uuid' => $this->uuid], ['tile_rows' => $options->rows,'tile_cols' => $options->cols,'tile_width' => $options->width,'tile_height' => $options->height,- 'tile_count' => $tile_count,+ 'tile_count' => $options->tileCount,'interval_seconds' => 1 / $options->fps,'modified_at' => now(),'generation_time' => $timeElapsed,diff --git a/app/Services/Ffmpeg/FFmpegCommandBuilder.php b/app/Services/Ffmpeg/FFmpegCommandBuilder.phpindex 0a3e6829..263125f6 100644--- a/app/Services/Ffmpeg/FFmpegCommandBuilder.php+++ b/app/Services/Ffmpeg/FFmpegCommandBuilder.php@@ -50,11 +50,11 @@ class FFmpegCommandBuilder {};$skipFrame = match (true) {- $options->duration < 10 => [],- default => [+ $options->skipFrameNokey => ['-skip_frame','nokey',- ]+ ],+ default => []};return [diff --git a/app/Services/Ffmpeg/FFprobeService.php b/app/Services/Ffmpeg/FFprobeService.phpnew file mode 100644index 00000000..4b2f3c44--- /dev/null+++ b/app/Services/Ffmpeg/FFprobeService.php@@ -0,0 +1,39 @@+<?php++namespace App\Services\Ffmpeg;++use Symfony\Component\Process\Process;++class FFprobeService {+ /**+ * Returns the number of detected keyframes in a video+ *+ * Uses -skip_frame nokey+ *+ * @param string $filePath Path to video+ */+ public static function countKeyframes(string $filePath): int {+ $process = new Process([+ 'ffprobe',+ '-select_streams',+ 'v',+ '-skip_frame',+ 'nokey',+ '-show_frames',+ '-show_entries',+ 'frame=pict_type',+ '-of',+ 'csv=p=0',+ $filePath,+ ]);++ $process->setTimeout(30);+ $process->run();++ if (! $process->isSuccessful()) {+ return 0;+ }++ return substr_count($process->getOutput(), 'I');+ }+}diff --git a/app/Services/Images/Storyboard/StoryboardOptions.php b/app/Services/Images/Storyboard/StoryboardOptions.phpindex 1fb11adc..408f4497 100644--- a/app/Services/Images/Storyboard/StoryboardOptions.php+++ b/app/Services/Images/Storyboard/StoryboardOptions.php@@ -3,7 +3,9 @@namespace App\Services\Images\Storyboard;use App\Exceptions\StoryboardNotSupportedException;+use App\Jobs\VerifyFiles;use App\Models\Metadata;+use App\Services\Ffmpeg\FFprobeService;class StoryboardOptions {public function __construct(@@ -16,6 +18,8 @@ class StoryboardOptions {public int $sourceWidth,public int $sourceHeight,public int $duration,+ public int $tileCount,+ public bool $skipFrameNokey,) {}public static function fromMetadata(Metadata $metadata): self {@@ -40,22 +44,34 @@ class StoryboardOptions {$intervalSeconds = max(1, config('media.storyboard.default_interval_seconds', 10)); // min configured value is every second$fps = match (true) {- $metadata->duration < 10 => 10, // every 100ms- $metadata->duration < 30 => 1.0, // every second+ $metadata->duration < 10 => 2, // every 500ms+ $metadata->duration < 30 => 1, // every second$metadata->duration < 120 => 0.5, // every other seconddefault => 1 / $intervalSeconds, // every (default=10) seconds};+ $expectedFrames = ceil($metadata->duration * $fps);+ $cols = min(10, $expectedFrames);+ $rows = max(1, ceil($expectedFrames / $cols));++ $skipFrameNokey = match (true) {+ $fps <= 0.1 => true,+ $metadata->duration > 120 => true,+ default => FFprobeService::countKeyframes(VerifyFiles::getAbsoluteMediaPath($metadata->video)) >= $expectedFrames,+ };+return new self(width: $cellWidth,height: $cellHeight,- cols: 10,- rows: 10,+ cols: $cols,+ rows: $rows,fps: $fps,tile: true,sourceWidth: $sourceW,sourceHeight: $sourceH,duration: $metadata->duration,+ skipFrameNokey: $skipFrameNokey,+ tileCount: $expectedFrames);}diff --git a/resources/js/service/__test__/generateStoryboardCues.test.ts b/resources/js/service/__test__/generateStoryboardCues.test.tsindex 9e81fab5..a23dd527 100644--- a/resources/js/service/__test__/generateStoryboardCues.test.ts+++ b/resources/js/service/__test__/generateStoryboardCues.test.ts@@ -25,9 +25,10 @@ describe('buildStoryboardCues', () => {expect(cues[0].start).toBe(0);});- it('last cue end does not exceed duration', () => {- const cues = buildStoryboardCues(UUID, 95, makeStoryboard());- expect(cues.at(-1)?.end).toBeLessThanOrEqual(95);+ it('last cue is padded by half of interval', () => {+ const storyboard = makeStoryboard();+ const cues = buildStoryboardCues(UUID, 95, storyboard);+ expect(cues.at(-1)?.end).toBeLessThanOrEqual(95 + storyboard.interval_seconds / 2);});it('first tile has x=0 y=0', () => {diff --git a/resources/js/service/storyboard/index.ts b/resources/js/service/storyboard/index.tsindex ffcfaf7c..1454a546 100644--- a/resources/js/service/storyboard/index.ts+++ b/resources/js/service/storyboard/index.ts@@ -14,9 +14,11 @@ export function buildStoryboardCues(uuid: string, durationSeconds: number, storyconst col = tileIndex % cols;const row = Math.floor(tileIndex / cols);+ const padding = durationSeconds < t + interval + interval ? interval / 2 : 0; // for last cue+cues.push({start: t,- end: Math.min(t + interval, durationSeconds),+ end: Math.min(t + interval, durationSeconds) + padding,image: `/storage/metadata/${uuid.slice(0, 2)}/${uuid}/storyboard/${sheet}.jpg`,x: col * tileWidth,y: row * tileHeight,diff --git a/tests/Unit/Services/FFmpeg/FFmpegCommandBuilderTest.php b/tests/Unit/Services/FFmpeg/FFmpegCommandBuilderTest.phpindex 21ba4f15..ea6a0a95 100644--- a/tests/Unit/Services/FFmpeg/FFmpegCommandBuilderTest.php+++ b/tests/Unit/Services/FFmpeg/FFmpegCommandBuilderTest.php@@ -38,6 +38,8 @@ class FFmpegCommandBuilderTest extends TestCase {sourceWidth: 1920,sourceHeight: 1080,duration: 600,+ tileCount: 10,+ skipFrameNokey: true,);}diff --git a/tests/Unit/Services/Images/StoryboardOptionsTest.php b/tests/Unit/Services/Images/StoryboardOptionsTest.phpindex c54ce049..1b065946 100644--- a/tests/Unit/Services/Images/StoryboardOptionsTest.php+++ b/tests/Unit/Services/Images/StoryboardOptionsTest.php@@ -4,6 +4,8 @@ namespace Tests\Unit\Services\Images;use App\Exceptions\StoryboardNotSupportedException;use App\Models\Metadata;+use App\Models\Video;+use App\Services\Ffmpeg\FFprobeService;use App\Services\Images\Storyboard\StoryboardOptions;use Tests\TestCase;@@ -48,14 +50,19 @@ class StoryboardOptionsTest extends TestCase {}public function test_short_video_gets_higher_fps(): void {- $metadata = Metadata::factory()->make([+ $video = Video::factory()->create();+ $metadata = Metadata::factory()->create(['codec' => 'h264','duration' => 16,'resolution_width' => 1920,'resolution_height' => 1080,'mime_type' => self::VIDEO_MIME_TYPE,+ 'video_id' => $video->id,]);+ $ffprobeMock = $this->createMock(FFprobeService::class);+ $ffprobeMock->method('countKeyframes')->willReturn(5);+$options = StoryboardOptions::fromMetadata($metadata);$this->assertEquals(1, $options->fps);