Class: VideoPosterExtractionService

Inherits:
Object
  • Object
show all
Defined in:
app/services/video_poster_extraction_service.rb

Defined Under Namespace

Classes: ExtractionError

Constant Summary collapse

JPEG_MAGIC_BYTES =
"\xFF\xD8\xFF".b.freeze
PNG_MAGIC_BYTES =
"\x89PNG".b.freeze
MIN_IMAGE_SIZE =

1 KB — anything smaller is likely an error page

1024

Instance Method Summary collapse

Constructor Details

#initialize(video, timestamp_seconds = 10.0) ⇒ VideoPosterExtractionService

Returns a new instance of VideoPosterExtractionService.



14
15
16
17
18
19
20
21
22
23
# File 'app/services/video_poster_extraction_service.rb', line 14

def initialize(video, timestamp_seconds = 10.0)
  @video = video
  @timestamp_seconds = if timestamp_seconds
                         timestamp_seconds
                       elsif video.poster_offset.present?
                         video.poster_offset.to_i / 1000.0
                       else
                         10.0
                       end
end

Instance Method Details

#extract_posterObject

Raises ExtractionError with a descriptive message on failure so callers
(and AppSignal) see the actual root cause instead of a bare false.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/services/video_poster_extraction_service.rb', line 27

def extract_poster
  Rails.logger.info "Starting poster extraction for video #{@video.id} (#{@video.title}) at timestamp #{@timestamp_seconds} seconds"

  if @video.cloudflare_uid.present?
    extract_cloudflare_poster
  elsif @video.attachment_uid.present?
    extract_self_hosted_poster
  else
    raise ExtractionError, "Video #{@video.id} has no valid source (neither Cloudflare UID nor attachment UID)"
  end

  Rails.logger.info "Successfully extracted poster for video #{@video.id}"
  true
end