Skip to content

Commit

Permalink
Merge pull request #5582 from avalonmediasystem/waveform_tempfile_revert
Browse files Browse the repository at this point in the history
Revert waveform tempfile commits
  • Loading branch information
cjcolvar authored Jan 17, 2024
2 parents 645368e + 7ce363d commit 5d56c18
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 25 deletions.
18 changes: 3 additions & 15 deletions app/services/waveform_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,27 +82,15 @@ def get_normalized_peaks(uri)
factor = max_peak.zero? ? 1 : res / max_peak.to_f
peaks.map { |peak| peak.collect { |num| (num * factor).to_i } }
ensure
if wave_io.is_a? Tempfile
wave_io.close
wave_io.unlink
else
Process.wait(wave_io.pid) if wave_io&.pid
end
Process.wait(wave_io.pid) if wave_io&.pid
end

def get_wave_io(uri)
headers = "-headers $'Referer: #{Rails.application.routes.url_helpers.root_url}\r\n'" if uri.starts_with? "http"
normalized_uri = uri.starts_with?("file") ? Addressable::URI.unencode(uri) : uri
timeout = 60000000 # Must be in microseconds. Current value = 1 minute.
tmpfile = Tempfile.new if uri.starts_with?("http")
cmd = "#{Settings.ffmpeg.path} #{headers} -rw_timeout #{timeout} -i '#{normalized_uri}' -f wav -ar 44100 -y #{tmpfile&.path || "-"} 2> /dev/null"
Rails.logger.debug("Getting wav file for waveform generation using ffmpeg command: #{cmd}")
if tmpfile
Kernel.system(cmd)
tmpfile
else
IO.popen(cmd)
end
cmd = "#{Settings.ffmpeg.path} #{headers} -rw_timeout #{timeout} -i '#{normalized_uri}' -f wav -ar 44100 - 2> /dev/null"
IO.popen(cmd)
end

def gather_peaks(wav_file)
Expand Down
15 changes: 5 additions & 10 deletions spec/services/waveform_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,17 @@

context "http file" do
let(:uri) { "http://domain/to/video.mp4" }
let(:cmd) {"#{Settings.ffmpeg.path} -headers $'Referer: http://test.host/\r\n' -rw_timeout 60000000 -i '#{uri}' -f wav -ar 44100 -y /tmp"}

before do
allow(Kernel).to receive(:system).and_return(nil)
end
let(:cmd) {"#{Settings.ffmpeg.path} -headers $'Referer: http://test.host/\r\n' -rw_timeout 60000000 -i '#{uri}' -f wav -ar 44100 - 2> /dev/null"}

it "should call ffmpeg with headers" do
io = service.send(:get_wave_io, uri)
expect(Kernel).to have_received(:system).with(start_with(cmd))
expect(io).to be_a Tempfile
service.send(:get_wave_io, uri)
expect(IO).to have_received(:popen).with(cmd)
end
end

context "local file" do
let(:uri) { "file:///path/to/video.mp4" }
let(:cmd) {"#{Settings.ffmpeg.path} -rw_timeout 60000000 -i '#{uri}' -f wav -ar 44100 -y - 2> /dev/null"}
let(:cmd) {"#{Settings.ffmpeg.path} -rw_timeout 60000000 -i '#{uri}' -f wav -ar 44100 - 2> /dev/null"}

it "should call ffmpeg without headers" do
service.send(:get_wave_io, uri)
Expand All @@ -92,7 +87,7 @@
context 'with spaces in filename' do
let(:uri) { 'file:///path/to/special%20video%20file.mp4' }
let(:unencoded_uri) { 'file:///path/to/special video file.mp4' }
let(:cmd) {"#{Settings.ffmpeg.path} -rw_timeout 60000000 -i '#{unencoded_uri}' -f wav -ar 44100 -y - 2> /dev/null"}
let(:cmd) {"#{Settings.ffmpeg.path} -rw_timeout 60000000 -i '#{unencoded_uri}' -f wav -ar 44100 - 2> /dev/null"}

it "should call ffmpeg without url encoding" do
service.send(:get_wave_io, uri)
Expand Down

0 comments on commit 5d56c18

Please sign in to comment.