<%= @work.title %>
Duration: <%= format_duration(@video.duration) %>
```
### Video Player Stimulus Controller
```javascript
// app/javascript/controllers/video_player_controller.js
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
static values = { videoId: Number, startPosition: Number }
connect() {
this.player = videojs(this.element, {
controls: true,
responsive: true,
fluid: true,
playbackRates: [0.5, 1, 1.25, 1.5, 2]
})
this.player.ready(() => {
if (this.startPositionValue > 0) {
this.player.currentTime(this.startPositionValue)
}
})
// Save position every 10 seconds
this.interval = setInterval(() => {
this.savePosition()
}, 10000)
// Save on pause
this.player.on("pause", () => this.savePosition())
}
disconnect() {
clearInterval(this.interval)
this.player.dispose()
}
savePosition() {
const position = Math.floor(this.player.currentTime())
fetch(`/videos/${this.videoIdValue}/playback-position`, {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ position })
})
}
}
```
## Service Objects
### File Scanner Service
```ruby
class FileScannerService
def initialize(storage_location)
@storage_location = storage_location
end
def scan
return failure_result("Storage location not accessible") unless @storage_location.accessible?
video_files = find_video_files
new_videos = process_files(video_files)
success_result(new_videos)
rescue => e
failure_result(e.message)
end
private
def find_video_files
Dir.glob(File.join(@storage_location.path, "**", "*.{mp4,avi,mkv,mov,wmv,flv,webm,m4v}"))
end
def process_files(file_paths)
new_videos = []
file_paths.each do |file_path|
filename = File.basename(file_path)
next if Video.exists?(filename: filename, storage_location: @storage_location)
video = Video.create!(
filename: filename,
storage_location: @storage_location,
work: Work.find_or_create_by(title: extract_title(filename))
)
new_videos << video
VideoProcessorJob.perform_later(video.id)
end
new_videos
end
def extract_title(filename)
# Simple title extraction - can be enhanced
File.basename(filename, ".*").gsub(/[\[\(].*?[\]\)]/, "").strip
end
def success_result(videos = [])
{ success: true, videos: videos, message: "Found #{videos.length} new videos" }
end
def failure_result(message)
{ success: false, message: message }
end
end
```
### Video Transcoder Service
```ruby
class VideoTranscoder
def transcode_for_web(input_path:, output_path:, on_progress: nil)
movie = FFMPEG::Movie.new(input_path)
# Calculate progress callback
progress_callback = ->(progress) {
on_progress&.call(progress, 100)
}
movie.transcode(output_path, {
video_codec: "libx264",
audio_codec: "aac",
custom: %w[
-pix_fmt yuv420p
-preset medium
-crf 23
-movflags +faststart
-tune fastdecode
]
}, &progress_callback)
end
def extract_frame(input_path, seconds)
movie = FFMPEG::Movie.new(input_path)
output_path = "#{Rails.root}/tmp/thumbnail_#{SecureRandom.hex(8)}.jpg"
movie.screenshot(output_path, seek_time: seconds, resolution: "320x240")
output_path
end
end
```
## Routes
```ruby
Rails.application.routes.draw do
root "storage_locations#index"
resources :storage_locations, only: [:index, :show, :create, :destroy] do
member do
post :scan
end
end
resources :works, only: [:index, :show] do
resources :videos, only: [:show]
end
resources :videos, only: [] do
member do
get :stream
patch :playback_position
post :retry_processing
end
resources :playback_sessions, only: [:create]
end
# Real-time job progress
resources :jobs, only: [:show] do
member do
get :progress
end
end
end
```
## Implementation Sub-Phases
### Phase 1A: Core Foundation (Week 1-2)
1. Generate models with migrations
2. Implement model validations and associations
3. Create storage adapter pattern
4. Create FileScannerService
5. Basic UI for storage locations and video listing
### Phase 1B: Video Playback (Week 3)
1. Video streaming controller with byte-range support
2. Video.js integration with Stimulus controller
3. Playback session tracking
4. Resume functionality
### Phase 1C: Processing Pipeline (Week 4)
1. Video metadata extraction with FFmpeg
2. Background job processing with progress tracking
3. Thumbnail generation and storage
4. Processing UI with status indicators
### Phase 1D: Works & Grouping (Week 5)
1. Duplicate detection by file hash
2. Manual grouping interface
3. Works display with version selection
4. Search, filtering, and pagination
## Configuration
### Environment Variables
```bash
# Required
RAILS_ENV=development
RAILS_MASTER_KEY=your_master_key
# Video processing
FFMPEG_PATH=/usr/bin/ffmpeg
FFPROBE_PATH=/usr/bin/ffprobe
# Storage
VIDEOS_PATH=./velour_data/videos
MAX_TRANSCODE_SIZE_GB=50
# Background jobs (SolidQueue runs with defaults)
SOLID_QUEUE_PROCESSES="*:2" # 2 workers for all queues
```
### Docker Configuration
```dockerfile
FROM ruby:3.3-alpine
# Install FFmpeg and other dependencies
RUN apk add --no-cache \
ffmpeg \
imagemagick \
sqlite-dev \
nodejs \
npm
# Install Rails and other gems
COPY Gemfile* /app/
RUN bundle install
# Copy application code
COPY . /app/
# Create directories
RUN mkdir -p /app/velour_data/{assets,tmp}
EXPOSE 3000
CMD ["rails", "server", "-b", "0.0.0.0"]
```
## Testing Strategy
### Model Tests
- Model validations and associations
- Scopes and class methods
- JSON store accessor functionality
### Service Tests
- FileScannerService with sample directory
- VideoTranscoder service methods
- Background job processing
### System Tests
- Video playback functionality
- File scanning workflow
- Work grouping interface
This Phase 1 delivers a complete, useful video library application that provides immediate value while establishing the foundation for future enhancements.