Skip to content

Optimize video frame previews by generating thumbnails automatically (Closes #48) - #49

Merged
Ardnived merged 13 commits into
mainfrom
feature/issue-48-video-thumbnails
Sep 12, 2025
Merged

Ardnived merged 13 commits into
mainfrom
feature/issue-48-video-thumbnails

Conversation

@Ardnived

@Ardnived Ardnived commented Sep 9, 2025

Copy link
Copy Markdown
Contributor

Summary

This PR implements automatic video thumbnail generation for the Frames collection to significantly improve admin interface performance and user experience.

Key Changes

  • Added ffmpeg-static dependency for reliable video processing
  • Created videoThumbnailUtils.ts with thumbnail generation logic using FFmpeg + Sharp
  • Extended convertFile hook to generate 160x160 WebP thumbnails at 0.1 seconds from video start
  • Added thumbnail relationship field to Frames collection schema
  • Updated ThumbnailCell component to display generated thumbnails with play button overlays
  • Updated FrameItem component in MeditationFrameEditor to show video thumbnails
  • Comprehensive error handling with graceful fallbacks to video elements

Technical Implementation

  • Thumbnail Generation: Extracts frame at 0.1s using FFmpeg, processes with Sharp to 160x160 WebP
  • Storage Strategy: Thumbnails stored as separate Frames documents with relationship links
  • Component Integration: Both list views and frame editor now show fast-loading thumbnails
  • Performance: Eliminates slow video element loading in admin interface
  • Backward Compatibility: Graceful fallback for videos without thumbnails

Admin Interface Benefits

  • ✅ Fast Loading: Thumbnail images load significantly faster than video elements
  • ✅ Visual Identification: Video content easily identifiable in admin lists and frame editor
  • ✅ Performance Optimized: Reduces page load times when multiple video frames displayed
  • ✅ Consistent UX: Unified thumbnail display for both image and video content

Testing

  • Added comprehensive integration tests for utility functions
  • Validated schema changes and error handling
  • Tested admin interface functionality with browser automation
  • Ensured existing functionality remains intact

Documentation

  • Updated CLAUDE.md with complete video thumbnail architecture documentation
  • Detailed implementation, component integration, and performance benefits

🤖 Generated with Claude Code

Co-Authored-By: Claude [email protected]

Ardnived and others added 3 commits September 9, 2025 12:55
- Add ffmpeg-static dependency for reliable video processing
- Create videoThumbnailUtils.ts with thumbnail generation logic
- Extend convertFile hook to generate thumbnails at 0.1s from videos
- Add thumbnail relationship field to Frames collection
- Update ThumbnailCell to display generated thumbnails with play overlay
- Update FrameItem in MeditationFrameEditor to show video thumbnails
- Generate thumbnails as 160x160 WebP images matching existing sizes
- Implement graceful fallback to video elements when thumbnails unavailable

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
- Create videoThumbnails.int.spec.ts with utility function tests
- Add schema validation tests for thumbnail field
- Include error handling and graceful fallback tests
- Validate video detection functionality
- Ensure existing functionality remains intact
- Pass linting with no new errors introduced

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
- Document video thumbnail generation system architecture
- Explain FFmpeg integration and Sharp processing pipeline
- Detail component integration for ThumbnailCell and FrameItem
- Describe admin interface performance benefits
- Add comprehensive implementation details and error handling

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@Ardnived Ardnived self-assigned this Sep 9, 2025
- Remove separate thumbnail relationship field and afterChange hook
- Store thumbnail directly in sizes.small object like Payload's image resizing
- Write thumbnail file to disk alongside video file
- Update ThumbnailCell and FrameItem to use sizes.small.url
- Update tests to validate sizes object approach
- Maintain consistency with Payload's built-in image size handling

This approach keeps the thumbnail as part of the same document, avoiding
unnecessary separate Frame documents and maintaining data locality.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@Ardnived

Ardnived commented Sep 9, 2025

Copy link
Copy Markdown
Contributor Author

✅ Implementation Fixed

Thank you for catching that architectural issue! I've updated the implementation to store video thumbnails correctly:

Key Changes:

  • Removed separate thumbnail relationship field - No longer creates unnecessary Frame documents
  • Stores thumbnail in sizes.small object - Matches Payload's built-in image resizing approach
  • Writes thumbnail file alongside video - Maintains file locality in the same directory
  • Updated components - Both ThumbnailCell and FrameItem now use sizes.small.url

Technical Details:

The thumbnail is now stored as part of the same document in the sizes object, exactly like how Payload handles image resizing:

data.sizes = {
  small: {
    filename: 'video-small.webp',
    mimeType: 'image/webp',
    filesize: 12345,
    width: 160,
    height: 160,
    url: '/api/frames/file/video-small.webp'
  }
}

This approach:

  • ✅ Keeps everything on the same document
  • ✅ Follows Payload's established patterns
  • ✅ Avoids unnecessary database relationships
  • ✅ Maintains data locality

The implementation is now architecturally consistent with Payload CMS conventions.

Ardnived and others added 3 commits September 9, 2025 13:47
- Update test to handle different configurations between test and production
- Focus on validating the ability to store thumbnails in sizes object
- Handle cases where imageSizes might not be loaded in test environment

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
- Document that thumbnails are stored in sizes.small object
- Clarify that thumbnails are on the same document, not separate
- Update implementation details to match the fixed approach

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
…nails

- Use req.payloadUploadSizes.small to let Payload handle thumbnail storage
- Remove manual file system operations and direct disk writes
- Storage adapter now automatically handles thumbnail files (local, S3, etc.)
- Thumbnail cleanup happens automatically when Frame documents are deleted
- URL generation handled by Payload's built-in storage system
- Maintains compatibility with all configured storage adapters

This approach properly integrates with Payload's architecture instead of
bypassing the storage system, ensuring proper file management and cleanup.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@Ardnived

Ardnived commented Sep 9, 2025

Copy link
Copy Markdown
Contributor Author

🔧 Storage Integration Fixed

Thank you for pointing out the file storage issue! I've completely rewritten the thumbnail processing to properly integrate with Payload's storage architecture.

Key Changes:

❌ Previous (Incorrect) Approach:

  • Manually wrote files to disk using fs.writeFileSync
  • Bypassed Payload's storage adapter system
  • No automatic cleanup when documents deleted
  • Wouldn't work with S3/cloud storage setups

✅ New (Correct) Approach:

  • Uses req.payloadUploadSizes.small - The same system Payload uses for imageSizes
  • Automatic Storage - Storage adapter handles uploading (local, S3, Cloudflare R2, etc.)
  • Automatic Cleanup - Files deleted when Frame documents are deleted
  • URL Generation - Payload automatically generates proper URLs
  • Cloud Compatible - Works with any configured storage adapter

Technical Implementation:

// Generate thumbnail and add to Payload's upload system
const thumbnailBuffer = await generateVideoThumbnail(req.file.data)

// Let Payload's storage system handle it (just like imageSizes)
req.payloadUploadSizes.small = thumbnailBuffer

Benefits:

✅ Proper Integration - Uses Payload's established file handling patterns
✅ Storage Agnostic - Works with local storage, S3, Cloudflare R2, etc.
✅ Automatic Cleanup - Files cleaned up when documents are deleted
✅ URL Management - Payload handles URL generation and serving
✅ No Manual File Ops - No direct filesystem operations needed

This implementation now properly follows Payload CMS architecture and will work seamlessly with any storage adapter configuration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant