Class: AudioExtractionService
- Inherits:
-
Object
- Object
- AudioExtractionService
- Defined in:
- app/services/audio_extraction_service.rb
Overview
Service object: audio extraction service.
Instance Method Summary collapse
- #cleanup ⇒ Object
- #extract ⇒ Object
- #extract_to_file(output_path) ⇒ Object
-
#initialize(video_file_path, ffmpeg_location = nil) ⇒ AudioExtractionService
constructor
A new instance of AudioExtractionService.
Constructor Details
#initialize(video_file_path, ffmpeg_location = nil) ⇒ AudioExtractionService
Returns a new instance of AudioExtractionService.
4 5 6 7 8 |
# File 'app/services/audio_extraction_service.rb', line 4 def initialize(video_file_path, ffmpeg_location = nil) @video_file_path = video_file_path @ffmpeg_location = ffmpeg_location || find_ffmpeg_location @audio_file = nil end |
Instance Method Details
#cleanup ⇒ Object
76 77 78 79 80 81 82 |
# File 'app/services/audio_extraction_service.rb', line 76 def cleanup return unless @audio_file @audio_file.close unless @audio_file.closed? @audio_file.unlink if File.exist?(@audio_file.path) @audio_file = nil end |
#extract ⇒ Object
10 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 37 38 39 40 41 42 |
# File 'app/services/audio_extraction_service.rb', line 10 def extract Rails.logger.info "Extracting audio from video: #{@video_file_path}" # Verify input file exists and is valid raise "Input video file is missing or empty: #{@video_file_path}" unless File.exist?(@video_file_path) && File.size(@video_file_path) > 0 @audio_file = Tempfile.new(['audio', '.mp3'], binmode: true) # Extract audio using FFmpeg with high quality settings for better transcription accuracy command = [ @ffmpeg_location, '-i', @video_file_path, '-vn', # No video '-acodec', 'mp3', # MP3 codec '-ar', '44100', # 44.1kHz sample rate (higher quality for speech recognition) '-ac', '1', # Mono (good for speech) '-q:a', '2', # High quality (lower number = higher quality, range 0-9) '-af', 'highpass=f=200,lowpass=f=3000', # Filter to focus on speech frequencies '-y', # Overwrite output file @audio_file.path ] Rails.logger.info "Running FFmpeg command: #{command.join(' ')}" result = system(*command) raise "FFmpeg audio extraction failed for file: #{@video_file_path}" unless result # Verify the output file was created and has content raise "FFmpeg failed to create valid audio file at: #{@audio_file.path}" unless File.exist?(@audio_file.path) && File.size(@audio_file.path) > 0 Rails.logger.info "Audio extraction completed: #{@audio_file.path} (size: #{File.size(@audio_file.path)} bytes)" @audio_file.path end |
#extract_to_file(output_path) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'app/services/audio_extraction_service.rb', line 44 def extract_to_file(output_path) Rails.logger.info "Extracting audio from video: #{@video_file_path} to: #{output_path}" # Verify input file exists and is valid raise "Input video file is missing or empty: #{@video_file_path}" unless File.exist?(@video_file_path) && File.size(@video_file_path) > 0 # Extract audio using FFmpeg with high quality settings for better transcription accuracy command = [ @ffmpeg_location, '-i', @video_file_path, '-vn', # No video '-acodec', 'mp3', # MP3 codec '-ar', '44100', # 44.1kHz sample rate (higher quality for speech recognition) '-ac', '1', # Mono (good for speech) '-q:a', '2', # High quality (lower number = higher quality, range 0-9) '-af', 'highpass=f=200,lowpass=f=3000', # Filter to focus on speech frequencies '-y', # Overwrite output file output_path ] Rails.logger.info "Running FFmpeg command: #{command.join(' ')}" result = system(*command) raise "FFmpeg audio extraction failed for file: #{@video_file_path}" unless result # Verify the output file was created and has content raise "FFmpeg failed to create valid audio file at: #{output_path}" unless File.exist?(output_path) && File.size(output_path) > 0 Rails.logger.info "Audio extraction completed: #{output_path} (size: #{File.size(output_path)} bytes)" output_path end |