Class: VideoProcessing::AudioExtractionService

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

Instance Method Summary collapse

Constructor Details

#initialize(video) ⇒ AudioExtractionService

Returns a new instance of AudioExtractionService.



7
8
9
# File 'app/services/video_processing/audio_extraction_service.rb', line 7

def initialize(video)
  @video = video
end

Instance Method Details

#cleanupObject



68
69
70
71
72
# File 'app/services/video_processing/audio_extraction_service.rb', line 68

def cleanup
  # No need to clean up temp files manually since Upload.temp_location handles persistence
  # The Upload model will manage the file lifecycle
  Rails.logger.info 'Audio extraction service cleanup completed'
end

#extract_and_storeObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/services/video_processing/audio_extraction_service.rb', line 11

def extract_and_store
  Rails.logger.info "Extracting and storing audio for video: #{@video.title}"

  # Check if we already have an audio extraction upload
  if @video.audio_extraction_upload.present?
    Rails.logger.info 'Audio extraction already exists, returning existing upload'
    return @video.audio_extraction_upload
  end

  # Get video file path
  video_file_path = get_video_file_path

  # Extract audio using the dedicated service
  extraction_service = ::AudioExtractionService.new(video_file_path, @video.ffmpeg_location)
  audio_file_path = extraction_service.extract

  # Store the extracted audio as an upload
  Rails.logger.info 'Storing audio extraction as upload'
  upload = @video.create_audio_extraction_upload(audio_file_path)

  # Cleanup the extraction service
  extraction_service.cleanup

  Rails.logger.info "Audio extraction and storage completed: #{upload.attachment_name}"
  upload
end

#extract_onlyObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/services/video_processing/audio_extraction_service.rb', line 49

def extract_only
  Rails.logger.info "Extracting audio for video: #{@video.title} to temporary file"

  # Use Upload.temp_location to get a persistent file location
  # This ensures the file persists until the Upload model manages it
  filename = "audio#{Time.current.strftime('%Y%m%d')}-#{Process.pid}-#{SecureRandom.hex(4)}.mp3"
  temp_audio_path = Upload.temp_location(filename).to_s

  # Get video file path
  video_file_path = get_video_file_path

  # Extract audio using the dedicated service to the persistent temp location
  extraction_service = ::AudioExtractionService.new(video_file_path, @video.ffmpeg_location)
  extraction_service.extract_to_file(temp_audio_path)

  Rails.logger.info "Audio extraction completed: #{temp_audio_path}"
  temp_audio_path
end

#extract_to_file(output_path) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'app/services/video_processing/audio_extraction_service.rb', line 38

def extract_to_file(output_path)
  Rails.logger.info "Extracting audio for video: #{@video.title} to: #{output_path}"

  # Get video file path
  video_file_path = get_video_file_path

  # Extract audio using the dedicated service to the specified output path
  extraction_service = ::AudioExtractionService.new(video_file_path, @video.ffmpeg_location)
  extraction_service.extract_to_file(output_path)
end