Skip to content

Commit 61cfd98

Browse files
authored
Merge pull request #32 from Vidyard/CRT-2193-S3-TMP-PROCESSING
[CRT-2193] Create tmp dir for interim file to process
2 parents 20e2f37 + 7bf19d7 commit 61cfd98

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

lib/ffmpeg/transcoder.rb

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
FIXED_LOWER_TO_UPPER_RATIO = 16.0/9.0
77
FIXED_UPPER_TO_LOWER_RATIO = 9.0/16.0
8+
TEMP_DIR = "/tmp/rlovelett".freeze
89

910
module FFMPEG
1011
class Transcoder
@@ -36,16 +37,18 @@ def initialize(movie, output_file, options = EncodingOptions.new, transcoder_opt
3637
if requires_pre_encode
3738
@movie.paths.each do |path|
3839
# Make the interim path folder if it doesn't exist
39-
dirname = "#{File.dirname(path)}/interim"
40+
41+
dirname = "#{TEMP_DIR}/interim#{File.dirname(path)}/"
4042
unless File.directory?(dirname)
4143
FileUtils.mkdir_p(dirname)
4244
end
43-
44-
interim_path = "#{File.dirname(path)}/interim/#{File.basename(path, File.extname(path))}_#{SecureRandom.urlsafe_base64}.mp4"
45+
# Example output: /tmp/rlovelett/interim/test_gv6Hw86ryqklKNiYCu9a8w.mp4
46+
interim_path = "#{dirname}#{File.basename(path, File.extname(path))}_#{SecureRandom.urlsafe_base64}.mp4"
4547
@movie.interim_paths << interim_path
4648
end
4749
else
4850
@movie.interim_paths << @movie.paths
51+
@movie.interim_paths.flatten!
4952
end
5053

5154
if options.is_a?(String)
@@ -171,7 +174,14 @@ def delete_files(destination)
171174
def transcode_movie
172175
pre_encode_if_necessary
173176

174-
@command = "#{@movie.ffmpeg_command} -y #{@raw_options} #{Shellwords.escape(@output_file)}"
177+
temp_output_dir = "#{TEMP_DIR}/output/"
178+
FileUtils.mkdir_p(temp_output_dir) unless File.directory?(temp_output_dir)
179+
180+
# We use a temporary output file to avoid issues with manipulating the output file directly in ffmpeg, which can occur due to s3 mountpoint restrictions.
181+
# Example output: /tmp/rlovelett/output/test_gv6Hw86ryqklKNiYCu9a8w.mp4
182+
temp_output_file = "#{temp_output_dir}#{File.basename(@output_file, File.extname(@output_file))}_#{SecureRandom.urlsafe_base64}#{File.extname(@output_file)}"
183+
184+
@command = "#{@movie.ffmpeg_command} -y #{@raw_options} #{Shellwords.escape(temp_output_file)}"
175185

176186
FFMPEG.logger.info("Running transcoding...\n#{@command}\n")
177187
@output = ""
@@ -204,6 +214,16 @@ def transcode_movie
204214
raise Error, "Process hung. Full output: #{@output}"
205215
end
206216
end
217+
218+
# Copy temp_output_file to output_file and cleanup interim paths
219+
if File.exist?(temp_output_file)
220+
FileUtils.cp(temp_output_file, @output_file)
221+
FileUtils.rm_rf(temp_output_file)
222+
end
223+
ensure
224+
@movie.interim_paths.each do |path|
225+
FileUtils.rm_rf(path) if path.start_with?(TEMP_DIR)
226+
end
207227
end
208228

209229
def validate_output_file(&block)

spec/ffmpeg/transcoder_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'spec_helper.rb'
2+
require 'fileutils'
23

34
module FFMPEG
45
describe Transcoder do
@@ -427,5 +428,28 @@ module FFMPEG
427428
expect(transcoder.requires_pre_encode).to be false
428429
end
429430
end
431+
432+
describe "#handle_temp_files" do
433+
let(:temp_output_file) { %r{#{TEMP_DIR}/output/output_.*\.mp4} }
434+
let(:output_file) { "#{tmp_path}/output.mp4" }
435+
let(:interim_path) { "#{TEMP_DIR}/interim/interim_path.mp4" }
436+
let(:transcoder) { Transcoder.new(movie, output_file, EncodingOptions.new) }
437+
438+
it 'copies the temp output file to the final output file' do
439+
expect(FileUtils).to receive(:cp).with(temp_output_file, output_file)
440+
transcoder.send(:transcode_movie)
441+
end
442+
443+
it 'removes the temp output file' do
444+
expect(FileUtils).to receive(:rm_rf).with(temp_output_file)
445+
transcoder.send(:transcode_movie)
446+
end
447+
448+
it 'removes interim paths' do
449+
movie.interim_paths << interim_path
450+
expect(FileUtils).to receive(:rm_rf).with(interim_path)
451+
transcoder.send(:transcode_movie)
452+
end
453+
end
430454
end
431455
end

0 commit comments

Comments
 (0)